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--show-signatures (ret)
61 (let ((res (geiser-eval--retort-result ret
))
65 (push (cons (format "%s" (car item
)) (cdr item
)) signs
))
66 (let ((str (geiser-autodoc--autodoc (geiser-syntax--scan-sexps) signs
)))
67 (when (not (string-equal str eldoc-last-message
))
69 (setq geiser-autodoc--cached-signatures signs
))))
71 (defun geiser-autodoc--get-signatures (funs)
73 (let ((m (format "'(%s)" (mapconcat 'identity funs
" "))))
74 (geiser-eval--send `(:eval
(:ge autodoc
(:scm
,m
)))
75 'geiser-autodoc--show-signatures
)))
76 (and (or (assoc (car funs
) geiser-autodoc--cached-signatures
)
77 (assoc (cadr funs
) geiser-autodoc--cached-signatures
))
78 geiser-autodoc--cached-signatures
))
80 (defun geiser-autodoc--sanitize-args (args)
81 (cond ((null args
) nil
)
83 (cons (car args
) (geiser-autodoc--sanitize-args (cdr args
))))
86 (defun geiser-autodoc--format-arg (a)
87 (cond ((and (listp a
) (geiser-syntax--keywordp (car a
)))
88 (if (and (cdr a
) (listp (cdr a
)))
89 (format "(#%s %s)" (car a
) (geiser-syntax--display (cadr a
)))
90 (format "(#%s)" (car a
))))
91 (t (geiser-syntax--display a
))))
93 (defun geiser-autodoc--insert-arg-group (args current
&optional pos
)
94 (when args
(insert " "))
95 (dolist (a (geiser-autodoc--sanitize-args args
))
97 (insert (geiser-autodoc--format-arg a
))
98 (when (or (and (numberp pos
)
100 (setq current
(1+ current
))
101 (= (1+ pos
) current
))
102 (and (geiser-syntax--keywordp current
)
104 (geiser-syntax--symbol-eq current
(car a
))))
105 (put-text-property p
(point)
106 'face
'geiser-font-lock-autodoc-current-arg
)
107 (setq pos nil current nil
)))
109 (when args
(backward-char))
112 (defun geiser-autodoc--insert-args (args pos prev
)
114 (reqs (cdr (assoc "required" args
)))
115 (opts (mapcar (lambda (a)
117 (not (equal (symbol-name a
) "...")))
120 (cdr (assoc "optional" args
))))
121 (keys (cdr (assoc "key" args
))))
123 (geiser-autodoc--insert-arg-group reqs
125 (and (not (zerop pos
)) pos
)))
126 (setq cpos
(geiser-autodoc--insert-arg-group opts cpos pos
))
127 (geiser-autodoc--insert-arg-group keys prev nil
)))
129 (defsubst geiser-autodoc--id-name
(proc module
)
130 (let ((str (if module
131 (format geiser-autodoc-identifier-format module proc
)
132 (format "%s" proc
))))
133 (propertize str
'face
'geiser-font-lock-autodoc-identifier
)))
135 (defun geiser-autodoc--str* (full-signature)
136 (let ((geiser-font-lock-autodoc-current-arg 'default
))
137 (geiser-autodoc--str (list (car full-signature
)) full-signature
)))
139 (defsubst geiser-autodoc--value-str
(proc module value
)
140 (let ((name (geiser-autodoc--id-name proc module
)))
141 (if value
(format "%s => %s" name value
) name
)))
143 (defun geiser-autodoc--str (desc signature
)
144 (let ((proc (car desc
))
145 (args (cdr (assoc "args" signature
)))
146 (module (cdr (assoc "module" signature
))))
148 (geiser-autodoc--value-str proc module
(cdr (assoc "value" signature
)))
150 (set-buffer (geiser-syntax--font-lock-buffer))
152 (insert (format "(%s" (geiser-autodoc--id-name proc module
)))
153 (let ((pos (or (cadr desc
) 0))
154 (prev (car (cddr desc
))))
156 (when (not (member a
(cdr (member a args
))))
157 (geiser-autodoc--insert-args a pos prev
)
161 (buffer-substring (point-min) (point))))))
163 (defun geiser-autodoc--autodoc (path &optional signs
)
164 (let ((signs (or signs
(geiser-autodoc--get-signatures (mapcar 'car path
))))
167 (while (and p
(not s
))
168 (unless (setq s
(cdr (assoc (car p
) signs
)))
170 (setq path
(cdr path
))))
171 (when s
(geiser-autodoc--str p s
))))
174 ;;; Autodoc functions:
176 (make-variable-buffer-local
177 (defvar geiser-autodoc--inhibit-function nil
))
179 (defsubst geiser-autodoc--inhibit
()
180 (and geiser-autodoc--inhibit-function
181 (funcall geiser-autodoc--inhibit-function
)))
183 (defsubst geiser-autodoc--autodoc-at-point
()
184 (geiser-autodoc--autodoc (geiser-syntax--scan-sexps)))
186 (defun geiser-autodoc--eldoc-function ()
188 (and (not (geiser-autodoc--inhibit)) (geiser-autodoc--autodoc-at-point))
189 (error (format "Autodoc not available (%s)" (error-message-string e
)))))
191 (defun geiser-autodoc-show ()
192 "Show the signature or value of the symbol at point in the echo area."
194 (message (geiser-autodoc--autodoc-at-point)))
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 (if geiser-autodoc-mode
1 -
1))
220 (when (interactive-p)
221 (message "Geiser Autodoc %s"
222 (if geiser-autodoc-mode
"enabled" "disabled"))))
224 (defadvice eldoc-display-message-no-interference-p
225 (after geiser-autodoc--message-ok-p
)
226 (when geiser-autodoc-mode
227 (setq ad-return-value
229 ;; Display arglist only when the minibuffer is
230 ;; inactive, e.g. not on `C-x C-f'. Lifted from slime.
231 (not (active-minibuffer-window)))))
236 (provide 'geiser-autodoc
)
237 ;;; geiser-autodoc.el ends here