]> gitweb.factorcode.org Git - factor.git/blob - misc/fuel/fuel-autodoc.el
Merge branch 'master' into new_ui
[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-syntax)
19 (require 'fuel-base)
20
21 \f
22 ;;; Customization:
23
24 (defgroup fuel-autodoc nil
25   "Options controlling FUEL's autodoc system."
26   :group 'fuel)
27
28 (defcustom fuel-autodoc-minibuffer-font-lock t
29   "Whether to use font lock for info messages in the minibuffer."
30   :group 'fuel-autodoc
31   :type 'boolean)
32
33 \f
34 ;;; Autodoc mode:
35
36 (defvar fuel-autodoc--font-lock-buffer
37   (let ((buffer (get-buffer-create " *fuel help minibuffer messages*")))
38     (set-buffer buffer)
39     (fuel-font-lock--font-lock-setup)
40     buffer))
41
42 (defun fuel-autodoc--font-lock-str (str)
43   (set-buffer fuel-autodoc--font-lock-buffer)
44   (erase-buffer)
45   (insert str)
46   (let ((font-lock-verbose nil)) (font-lock-fontify-buffer))
47   (buffer-string))
48
49 (defun fuel-autodoc--word-synopsis (&optional word)
50   (let ((word (or word (fuel-syntax-symbol-at-point)))
51         (fuel-log--inhibit-p t))
52     (when word
53       (let* ((cmd (if (fuel-syntax--in-using)
54                       `(:fuel* (,word fuel-vocab-summary) t t)
55                     `(:fuel* (((:quote ,word) synopsis :get)) t)))
56              (ret (fuel-eval--send/wait cmd 20))
57              (res (fuel-eval--retort-result ret)))
58         (when (and ret (not (fuel-eval--retort-error ret)) (stringp res))
59           (if fuel-autodoc-minibuffer-font-lock
60               (fuel-autodoc--font-lock-str res)
61             res))))))
62
63 (make-variable-buffer-local
64  (defvar fuel-autodoc--fallback-function nil))
65
66 (defun fuel-autodoc--eldoc-function ()
67   (or (and fuel-autodoc--fallback-function
68            (funcall fuel-autodoc--fallback-function))
69       (fuel-autodoc--word-synopsis)))
70
71 (make-variable-buffer-local
72  (defvar fuel-autodoc-mode-string " A"
73    "Modeline indicator for fuel-autodoc-mode"))
74
75 (define-minor-mode fuel-autodoc-mode
76   "Toggle Fuel's Autodoc mode.
77 With no argument, this command toggles the mode.
78 Non-null prefix argument turns on the mode.
79 Null prefix argument turns off the mode.
80
81 When Autodoc mode is enabled, a synopsis of the word at point is
82 displayed in the minibuffer."
83   :init-value nil
84   :lighter fuel-autodoc-mode-string
85   :group 'fuel-autodoc
86
87   (set (make-local-variable 'eldoc-documentation-function)
88        (when fuel-autodoc-mode 'fuel-autodoc--eldoc-function))
89   (set (make-local-variable 'eldoc-minor-mode-string) nil)
90   (eldoc-mode fuel-autodoc-mode)
91   (message "Fuel Autodoc %s" (if fuel-autodoc-mode "enabled" "disabled")))
92
93 \f
94 (provide 'fuel-autodoc)
95 ;;; fuel-autodoc.el ends here