1 ;; geiser-autodoc.el -- autodoc mode
3 ;; Copyright (C) 2009, 2010 Jose Antonio Ortega Ruiz
5 ;; This program is free software; you can redistribute it and/or
6 ;; modify it under the terms of the Modified BSD License. You should
7 ;; have received a copy of the license along with this program. If
8 ;; not, see <http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5>.
10 ;; Start date: Sun Feb 08, 2009 19:44
14 (require 'geiser-eval
)
15 (require 'geiser-syntax
)
16 (require 'geiser-custom
)
17 (require 'geiser-base
)
24 (defgroup geiser-autodoc nil
25 "Options for displaying autodoc strings in the echo area."
28 (geiser-custom--defface autodoc-current-arg
29 'bold geiser-autodoc
"highlighting current argument in autodoc messages")
31 (geiser-custom--defface autodoc-identifier
32 'font-lock-function-name-face
33 geiser-autodoc
"highlighting procedure name in autodoc messages")
35 (geiser-custom--defcustom geiser-autodoc-delay
0.3
36 "Delay before autodoc messages are fetched and displayed, in seconds."
38 :group
'geiser-autodoc
)
40 (geiser-custom--defcustom geiser-autodoc-display-module-p t
41 "Whether to display procedure module in autodoc strings."
43 :group
'geiser-autodoc
)
45 (geiser-custom--defcustom geiser-autodoc-identifier-format
"%s:%s"
46 "Format for displaying module and procedure or variable name, in that order,
47 when `geiser-autodoc-display-module-p' is on."
49 :group
'geiser-autodoc
)
52 ;;; Procedure arguments:
54 (make-variable-buffer-local
55 (defvar geiser-autodoc--cached-signatures nil
))
57 (defsubst geiser-autodoc--clean-cache
()
58 (setq geiser-autodoc--cached-signatures nil
))
60 (defun geiser-autodoc--get-signatures (funs &optional keep-cached
)
62 (let ((fs (assoc (car funs
) geiser-autodoc--cached-signatures
)))
64 (let ((missing) (cached))
65 (if (not geiser-autodoc--cached-signatures
)
68 (let ((cf (assoc f geiser-autodoc--cached-signatures
)))
69 (if cf
(push cf cached
)
71 (unless (or cached keep-cached
) (geiser-autodoc--clean-cache))
73 (let* ((missing (mapcar 'make-symbol missing
))
74 (res (geiser-eval--send/result
75 `(:eval
(:ge autodoc
(quote ,missing
))) 500)))
77 (setq geiser-autodoc--cached-signatures
78 (append (mapcar (lambda (s)
79 (cons (format "%s" (car s
)) (cdr s
)))
82 geiser-autodoc--cached-signatures
84 geiser-autodoc--cached-signatures
)))
86 (defun geiser-autodoc--sanitize-args (args)
87 (cond ((null args
) nil
)
89 (cons (car args
) (geiser-autodoc--sanitize-args (cdr args
))))
92 (defun geiser-autodoc--format-arg (a)
93 (cond ((and (listp a
) (geiser-syntax--keywordp (car a
)))
94 (if (and (cdr a
) (listp (cdr a
)))
95 (format "(#%s %s)" (car a
) (geiser-syntax--display (cadr a
)))
96 (format "(#%s)" (car a
))))
97 (t (geiser-syntax--display a
))))
99 (defun geiser-autodoc--insert-arg-group (args current
&optional pos
)
100 (when args
(insert " "))
101 (dolist (a (geiser-autodoc--sanitize-args args
))
103 (insert (geiser-autodoc--format-arg a
))
104 (when (or (and (numberp pos
)
106 (setq current
(1+ current
))
107 (= (1+ pos
) current
))
108 (and (geiser-syntax--keywordp current
)
110 (geiser-syntax--symbol-eq current
(car a
))))
111 (put-text-property p
(point)
112 'face
'geiser-font-lock-autodoc-current-arg
)
113 (setq pos nil current nil
)))
115 (when args
(backward-char))
118 (defun geiser-autodoc--insert-args (args pos prev
)
120 (reqs (cdr (assoc "required" args
)))
121 (opts (mapcar (lambda (a)
123 (not (equal (symbol-name a
) "...")))
126 (cdr (assoc "optional" args
))))
127 (keys (cdr (assoc "key" args
))))
129 (geiser-autodoc--insert-arg-group reqs
131 (and (not (zerop pos
)) pos
)))
132 (setq cpos
(geiser-autodoc--insert-arg-group opts cpos pos
))
133 (geiser-autodoc--insert-arg-group keys prev nil
)))
135 (defsubst geiser-autodoc--id-name
(proc module
)
136 (let ((str (if module
137 (format geiser-autodoc-identifier-format module proc
)
138 (format "%s" proc
))))
139 (propertize str
'face
'geiser-font-lock-autodoc-identifier
)))
141 (defun geiser-autodoc--str* (full-signature)
142 (let ((geiser-font-lock-autodoc-current-arg 'default
))
143 (geiser-autodoc--str (list (car full-signature
)) full-signature
)))
145 (defsubst geiser-autodoc--value-str
(proc module value
)
146 (let ((name (geiser-autodoc--id-name proc module
)))
147 (if value
(format "%s => %s" name value
) name
)))
149 (defun geiser-autodoc--str (desc signature
)
150 (let ((proc (car desc
))
151 (args (cdr (assoc "args" signature
)))
152 (module (cdr (assoc "module" signature
))))
154 (geiser-autodoc--value-str proc module
(cdr (assoc "value" signature
)))
156 (set-buffer (geiser-syntax--font-lock-buffer))
158 (insert (format "(%s" (geiser-autodoc--id-name proc module
)))
159 (let ((pos (or (cadr desc
) 0))
160 (prev (car (cddr desc
))))
162 (when (not (member a
(cdr (member a args
))))
163 (geiser-autodoc--insert-args a pos prev
)
167 (buffer-substring (point-min) (point))))))
169 (defun geiser-autodoc--autodoc (path &optional keep-cached
)
170 (let ((signs (geiser-autodoc--get-signatures (mapcar 'car path
)
174 (while (and p
(not s
))
175 (unless (setq s
(cdr (assoc (car p
) signs
)))
177 (setq path
(cdr path
))))
178 (when s
(geiser-autodoc--str p s
))))
181 ;;; Autodoc function:
183 (make-variable-buffer-local
184 (defvar geiser-autodoc--inhibit-function nil
))
186 (defsubst geiser-autodoc--inhibit
()
187 (and geiser-autodoc--inhibit-function
188 (funcall geiser-autodoc--inhibit-function
)))
190 (defun geiser-autodoc--eldoc-function ()
192 (and (not (geiser-autodoc--inhibit))
193 (geiser-autodoc--autodoc (geiser-syntax--scan-sexps)))
194 (error (format "Autodoc not available (%s)" (error-message-string e
)))))
199 (make-variable-buffer-local
200 (defvar geiser-autodoc-mode-string
" A"
201 "Modeline indicator for geiser-autodoc-mode"))
203 (define-minor-mode geiser-autodoc-mode
204 "Toggle Geiser's Autodoc mode.
205 With no argument, this command toggles the mode.
206 Non-null prefix argument turns on the mode.
207 Null prefix argument turns off the mode.
209 When Autodoc mode is enabled, a synopsis of the word at point is
210 displayed in the minibuffer."
212 :lighter geiser-autodoc-mode-string
213 :group
'geiser-autodoc
215 (set (make-local-variable 'eldoc-documentation-function
)
216 (when geiser-autodoc-mode
'geiser-autodoc--eldoc-function
))
217 (set (make-local-variable 'eldoc-minor-mode-string
) nil
)
218 (set (make-local-variable 'eldoc-idle-delay
) geiser-autodoc-delay
)
219 (eldoc-mode geiser-autodoc-mode
)
220 (message "Geiser Autodoc %s" (if geiser-autodoc-mode
"enabled" "disabled")))
223 (provide 'geiser-autodoc
)
224 ;;; geiser-autodoc.el ends here