]> gitweb.factorcode.org Git - factor.git/blob - misc/fuel/fuel-autodoc.el
Merge branch 'master' of git://factorcode.org/git/factor
[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-font-lock)
19 (require 'fuel-syntax)
20 (require 'fuel-base)
21
22 \f
23 ;;; Customization:
24
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 (fuel-syntax-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 (fuel-syntax--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               (fuel-font-lock--factor-str res)
69             res))))))
70
71 (make-variable-buffer-local
72  (defvar fuel-autodoc--fallback-function nil))
73
74 (defun fuel-autodoc--eldoc-function ()
75   (or (and fuel-autodoc--fallback-function
76            (funcall fuel-autodoc--fallback-function))
77       (condition-case e
78           (fuel-autodoc--word-synopsis)
79         (error (format "Autodoc not available (%s)"
80                        (error-message-string e))))))
81
82 \f
83 ;;; Autodoc mode:
84
85 (make-variable-buffer-local
86  (defvar fuel-autodoc-mode-string " A"
87    "Modeline indicator for fuel-autodoc-mode"))
88
89 (define-minor-mode fuel-autodoc-mode
90   "Toggle Fuel's Autodoc mode.
91 With no argument, this command toggles the mode.
92 Non-null prefix argument turns on the mode.
93 Null prefix argument turns off the mode.
94
95 When Autodoc mode is enabled, a synopsis of the word at point is
96 displayed in the minibuffer."
97   :init-value nil
98   :lighter fuel-autodoc-mode-string
99   :group 'fuel-autodoc
100
101   (set (make-local-variable 'eldoc-documentation-function)
102        (when fuel-autodoc-mode 'fuel-autodoc--eldoc-function))
103   (set (make-local-variable 'eldoc-minor-mode-string) nil)
104   (eldoc-mode fuel-autodoc-mode)
105   (message "Fuel Autodoc %s" (if fuel-autodoc-mode "enabled" "disabled")))
106
107 \f
108 (provide 'fuel-autodoc)
109 ;;; fuel-autodoc.el ends here