1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
;;; geiser-autodoc.el -- autodoc mode -*- lexical-binding: t; -*-
;; Copyright (C) 2009, 2010, 2011, 2012, 2015, 2016, 2021, 2022 Jose Antonio Ortega Ruiz
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the Modified BSD License. You should
;; have received a copy of the license along with this program. If
;; not, see <http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5>.
;; Start date: Sun Feb 08, 2009 19:44
;;; Code:
(require 'geiser-eval)
(require 'geiser-syntax)
(require 'geiser-custom)
(require 'geiser-base)
(require 'eldoc)
;;; Customization:
(defgroup geiser-autodoc nil
"Options for displaying autodoc strings in the echo area."
:group 'geiser)
(geiser-custom--defface autodoc-current-arg
'font-lock-variable-name-face
geiser-autodoc "highlighting current argument in autodoc messages")
(geiser-custom--defface autodoc-identifier
'font-lock-function-name-face
geiser-autodoc "highlighting procedure name in autodoc messages")
(geiser-custom--defcustom geiser-autodoc-delay 0.3
"Delay before autodoc messages are fetched and displayed, in seconds."
:type 'number)
(geiser-custom--defcustom geiser-autodoc-display-module t
"Whether to display procedure module in autodoc strings."
:type 'boolean)
(geiser-custom--defcustom geiser-autodoc-identifier-format "%s:%s"
"Format for displaying module and procedure or variable name, in that order,
when `geiser-autodoc-display-module' is on."
:type 'string)
(geiser-custom--defcustom geiser-autodoc-use-docsig t
"Provide signature docstrings for systems like company or corfu.
With this flag set, the signature of selected completions using
packages like company, corfu or completion-in-region functions
will be displayed in the echo area. For the case of a
completion-in-region function (e.g. consult's), which collects
all the docstrings at once, this might have a performace impact:
you can set this variable to nil to avoid them."
:type 'boolean)
;;; Procedure arguments:
(defvar-local geiser-autodoc--cached-signatures nil)
(defsubst geiser-autodoc--clean-cache ()
(setq geiser-autodoc--cached-signatures nil))
(defun geiser-autodoc--update-signatures (ret callback)
(let ((res (geiser-eval--retort-result ret))
(signs))
(when res
(dolist (item res)
(push (cons (format "%s" (car item)) (cdr item)) signs))
(when (functionp callback)
(let* ((path (geiser-syntax--scan-sexps))
(str (geiser-autodoc--autodoc path nil signs)))
(funcall callback str)))
(setq geiser-autodoc--cached-signatures signs))))
(defun geiser-autodoc--get-signatures (funs callback)
(when funs
(let* ((m (format "'(%s)" (mapconcat 'identity funs " ")))
(str (geiser-eval--scheme-str `(:eval (:ge autodoc (:scm ,m))))))
(if callback
(geiser-eval--send str
(lambda (r)
(geiser-autodoc--update-signatures r callback)))
(geiser-autodoc--update-signatures (geiser-eval--send/wait str) nil))))
(and (or (assoc (car funs) geiser-autodoc--cached-signatures)
(assoc (cadr funs) geiser-autodoc--cached-signatures))
geiser-autodoc--cached-signatures))
(defun geiser-autodoc--sanitize-args (args)
(cond ((null args) nil)
((listp args)
(cons (car args) (geiser-autodoc--sanitize-args (cdr args))))
(t '("..."))))
(defun geiser-autodoc--format-arg (a)
(cond ((and (listp a) (geiser-syntax--keywordp (car a)))
(if (and (cdr a) (listp (cdr a)))
(format "(#%s %s)" (car a) (geiser-syntax--display (cadr a)))
(format "(#%s)" (car a))))
(t (geiser-syntax--display a))))
(defvar geiser-autodoc--arg-face 'geiser-font-lock-autodoc-current-arg)
(defun geiser-autodoc--insert-arg-group (args current &optional pos)
(when args (insert " "))
(dolist (a (geiser-autodoc--sanitize-args args))
(let ((p (point)))
(insert (geiser-autodoc--format-arg a))
(when (or (and (numberp pos)
(numberp current)
(setq current (1+ current))
(= (1+ pos) current))
(and (geiser-syntax--keywordp current)
(listp a)
(geiser-syntax--symbol-eq current (car a))))
(put-text-property p (point) 'face geiser-autodoc--arg-face)
(setq pos nil current nil)))
(insert " "))
(when args (backward-char))
current)
(defun geiser-autodoc--insert-args (args pos prev)
(let ((cpos 1)
(reqs (cdr (assoc "required" args)))
(opts (mapcar (lambda (a)
(if (and (symbolp a)
(not (equal (symbol-name a) "...")))
(list a)
a))
(cdr (assoc "optional" args))))
(keys (cdr (assoc "key" args))))
(setq cpos
(geiser-autodoc--insert-arg-group reqs
cpos
(and (not (zerop pos)) pos)))
(setq cpos (geiser-autodoc--insert-arg-group opts cpos pos))
(geiser-autodoc--insert-arg-group keys prev nil)))
(defsubst geiser-autodoc--id-name (proc module)
(let ((str (if (and module geiser-autodoc-display-module)
(format geiser-autodoc-identifier-format module proc)
(format "%s" proc))))
(propertize str 'face 'geiser-font-lock-autodoc-identifier)))
(defun geiser-autodoc--str* (full-signature)
(let ((geiser-autodoc--arg-face 'default)
(sign (if (listp full-signature) full-signature (list full-signature))))
(geiser-autodoc--str (list (car sign)) sign)))
(defsubst geiser-autodoc--value-str (proc module value)
(let ((name (geiser-autodoc--id-name proc module)))
(if value (format "%s => %s" name value) name)))
(defun geiser-autodoc--str (desc signature)
(let ((proc (car desc))
(args (cdr (assoc "args" signature)))
(module (cdr (assoc "module" signature))))
(if (not args)
(geiser-autodoc--value-str proc module (cdr (assoc "value" signature)))
(save-current-buffer
(set-buffer (geiser-syntax--font-lock-buffer))
(erase-buffer)
(insert (format "(%s" (geiser-autodoc--id-name proc module)))
(let ((pos (or (cadr desc) 0))
(prev (car (cddr desc))))
(dolist (a args)
(when (not (member a (cdr (member a args))))
(geiser-autodoc--insert-args a pos prev)
(insert " |"))))
(delete-char -2)
(insert ")")
(buffer-substring (point-min) (point))))))
(defun geiser-autodoc--autodoc (path callback &optional signs)
(let ((signs (or signs
(geiser-autodoc--get-signatures (mapcar 'car path) callback)))
(p (car path))
(s))
(if callback
t
(while (and p (not s))
(unless (setq s (cdr (assoc (car p) signs)))
(setq p (car path))
(setq path (cdr path))))
(when s (geiser-autodoc--str p s)))))
;;; Autodoc functions:
(defvar-local geiser-autodoc--inhibit-function nil)
(defsubst geiser-autodoc--inhibit ()
(and geiser-autodoc--inhibit-function
(funcall geiser-autodoc--inhibit-function)))
(defsubst geiser-autodoc--inhibit-autodoc ()
(setq geiser-autodoc--inhibit-function (lambda () t)))
(defsubst geiser-autodoc--disinhibit-autodoc ()
(setq geiser-autodoc--inhibit-function nil))
(defsubst geiser-autodoc--autodoc-at-point (callback)
(geiser-autodoc--autodoc (geiser-syntax--scan-sexps) callback))
(defun geiser-autodoc--eldoc-function (&optional callback)
(ignore-errors
(when (not (geiser-autodoc--inhibit))
(geiser-autodoc--autodoc-at-point (or callback 'eldoc-message)))))
(defun geiser-autodoc-show ()
"Show the signature or value of the symbol at point in the echo area."
(interactive)
(message (geiser-autodoc--autodoc-at-point nil)))
;;; Autodoc mode:
(defvar-local geiser-autodoc-mode-string " A"
"Modeline indicator for geiser-autodoc-mode")
(define-minor-mode geiser-autodoc-mode
"Toggle Geiser's Autodoc mode.
With no argument, this command toggles the mode.
Non-null prefix argument turns on the mode.
Null prefix argument turns off the mode.
When Autodoc mode is enabled, a synopsis of the word at point is
displayed in the minibuffer."
:init-value nil
:lighter geiser-autodoc-mode-string
:group 'geiser-autodoc
(if (boundp 'eldoc-documentation-functions)
(if geiser-autodoc-mode
(add-hook 'eldoc-documentation-functions
#'geiser-autodoc--eldoc-function nil t)
(remove-hook 'eldoc-documentation-functions
#'geiser-autodoc--eldoc-function t))
(set (make-local-variable 'eldoc-documentation-function)
(when geiser-autodoc-mode 'geiser-autodoc--eldoc-function)))
(set (make-local-variable 'eldoc-minor-mode-string) nil)
(set (make-local-variable 'eldoc-idle-delay) geiser-autodoc-delay)
(eldoc-mode (if geiser-autodoc-mode 1 -1))
(when (called-interactively-p nil)
(message "Geiser Autodoc %s"
(if geiser-autodoc-mode "enabled" "disabled"))))
(provide 'geiser-autodoc)
|