]> 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 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 \f
36 ;;; Highlighting for autodoc messages:
37
38 (defvar fuel-autodoc--font-lock-buffer
39   (let ((buffer (get-buffer-create " *fuel help minibuffer messages*")))
40     (set-buffer buffer)
41     (set-syntax-table fuel-syntax--syntax-table)
42     (fuel-font-lock--font-lock-setup)
43     buffer))
44
45 (defun fuel-autodoc--font-lock-str (str)
46   (set-buffer fuel-autodoc--font-lock-buffer)
47   (erase-buffer)
48   (insert str)
49   (let ((font-lock-verbose nil)) (font-lock-fontify-buffer))
50   (buffer-string))
51
52 \f
53 ;;; Eldoc function:
54
55 (defvar fuel-autodoc--timeout 200)
56
57 (defun fuel-autodoc--word-synopsis (&optional word)
58   (let ((word (or word (fuel-syntax-symbol-at-point)))
59         (fuel-log--inhibit-p t))
60     (when word
61       (let* ((cmd (if (fuel-syntax--in-using)
62                       `(:fuel* (,word fuel-vocab-summary) :in t)
63                     `(:fuel* (((:quote ,word) synopsis :get)) :in)))
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-autodoc--font-lock-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       (fuel-autodoc--word-synopsis)))
78
79 \f
80 ;;; Autodoc mode:
81
82 (make-variable-buffer-local
83  (defvar fuel-autodoc-mode-string " A"
84    "Modeline indicator for fuel-autodoc-mode"))
85
86 (define-minor-mode fuel-autodoc-mode
87   "Toggle Fuel's Autodoc mode.
88 With no argument, this command toggles the mode.
89 Non-null prefix argument turns on the mode.
90 Null prefix argument turns off the mode.
91
92 When Autodoc mode is enabled, a synopsis of the word at point is
93 displayed in the minibuffer."
94   :init-value nil
95   :lighter fuel-autodoc-mode-string
96   :group 'fuel-autodoc
97
98   (set (make-local-variable 'eldoc-documentation-function)
99        (when fuel-autodoc-mode 'fuel-autodoc--eldoc-function))
100   (set (make-local-variable 'eldoc-minor-mode-string) nil)
101   (eldoc-mode fuel-autodoc-mode)
102   (message "Fuel Autodoc %s" (if fuel-autodoc-mode "enabled" "disabled")))
103
104 \f
105 (provide 'fuel-autodoc)
106 ;;; fuel-autodoc.el ends here