]> gitweb.factorcode.org Git - factor.git/blob - misc/fuel/fuel-autodoc.el
Large reorg of FUEL codebase
[factor.git] / misc / fuel / fuel-autodoc.el
1 ;;; fuel-autodoc.el -- doc snippets in the echo area
2
3 ;; Copyright (C) 2008, 2009 Jose Antonio Ortega Ruiz
4 ;; See http://factorcode.org/license.txt for BSD license.
5
6 ;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
7 ;; Keywords: languages, fuel, factor
8 ;; Start date: Sat Dec 20, 2008 00:50
9
10 ;;; Comentary:
11
12 ;; Utilities for displaying information automatically in the echo
13 ;; area.
14
15 ;;; Code:
16
17 (require 'fuel-eval)
18 (require 'fuel-base)
19 (require 'factor-mode)
20
21 \f
22 ;;; Customization:
23
24 ;;;###autoload
25 (defgroup fuel-autodoc nil
26   "Options controlling FUEL's autodoc system."
27   :group 'fuel)
28
29 (defcustom fuel-autodoc-minibuffer-font-lock t
30   "Whether to use font lock for info messages in the minibuffer."
31   :group 'fuel-autodoc
32   :type 'boolean)
33
34
35 (defcustom fuel-autodoc-eval-using-form-p nil
36   "When enabled, automatically load vocabularies in USING: form
37 to display autodoc messages.
38
39 In order to show autodoc messages for words in a Factor buffer,
40 the used vocabularies must be loaded in the Factor image. Setting
41 this variable to `t' will do that automatically for you,
42 asynchronously. That means that you'll be able to move around
43 while the vocabs are being loaded, but no other FUEL
44 functionality will be available until loading finishes (and it
45 may take a while). Thus, this functionality is disabled by
46 default. You can force loading the vocabs in a Factor buffer
47 USING: form with \\[fuel-load-usings]."
48   :group 'fuel-autodoc
49   :type 'boolean)
50
51 \f
52 ;;; Eldoc function:
53
54 (defvar fuel-autodoc--timeout 200)
55
56 (defun fuel-autodoc--word-synopsis (&optional word)
57   (let ((word (or word (factor-symbol-at-point)))
58         (fuel-log--inhibit-p t))
59     (when word
60       (let* ((usings (if fuel-autodoc-eval-using-form-p :usings t))
61              (cmd (if (factor-in-using)
62                       `(:fuel* (,word fuel-vocab-summary) :in t)
63                     `(:fuel* ((,word :usings fuel-word-synopsis)) t ,usings)))
64              (ret (fuel-eval--send/wait cmd fuel-autodoc--timeout))
65              (res (fuel-eval--retort-result ret)))
66         (when (and ret (not (fuel-eval--retort-error ret)) (stringp res))
67           (if fuel-autodoc-minibuffer-font-lock
68               (factor-font-lock-string res)
69             res))))))
70
71 (defvar-local fuel-autodoc--fallback-function nil)
72
73 (defun fuel-autodoc--eldoc-function ()
74   (or (and fuel-autodoc--fallback-function
75            (funcall fuel-autodoc--fallback-function))
76       (condition-case e
77           (fuel-autodoc--word-synopsis)
78         (error (format "Autodoc not available (%s)"
79                        (error-message-string e))))))
80
81 \f
82 ;;; Autodoc mode:
83
84 (defvar-local fuel-autodoc-mode-string " A"
85   "Modeline indicator for fuel-autodoc-mode")
86
87 ;;;###autoload
88 (define-minor-mode fuel-autodoc-mode
89   "Toggle Fuel's Autodoc mode.
90 With no argument, this command toggles the mode.
91 Non-null prefix argument turns on the mode.
92 Null prefix argument turns off the mode.
93
94 When Autodoc mode is enabled, a synopsis of the word at point is
95 displayed in the minibuffer."
96   :init-value nil
97   :lighter fuel-autodoc-mode-string
98   :group 'fuel-autodoc
99
100   (setq-local eldoc-documentation-function
101        (when fuel-autodoc-mode 'fuel-autodoc--eldoc-function))
102   (setq-local eldoc-minor-mode-string nil)
103   (eldoc-mode fuel-autodoc-mode)
104   (message "Fuel Autodoc %s" (if fuel-autodoc-mode "enabled" "disabled")))
105
106 \f
107 (provide 'fuel-autodoc)
108
109 ;;; fuel-autodoc.el ends here