-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathswank-help.lisp
264 lines (235 loc) · 11.3 KB
/
swank-help.lisp
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
255
256
257
258
259
260
261
262
263
264
(require :def-properties (merge-pathnames #p"cl-def-properties/module.lisp" (uiop/pathname:pathname-directory-pathname *load-pathname*)))
(ql:quickload "split-sequence")
(require :split-sequence)
(require :uiop)
(require :alexandria)
(defpackage :swank-help
(:use :cl :def-properties)
(:shadow #:apropos #:apropos-list #:describe)
(:export
:read-emacs-symbol-info
:read-emacs-package-info
:read-emacs-system-info
:read-emacs-packages-info
:read-emacs-systems-info
:apropos
:apropos-list
:describe)
(:documentation "Utilities for augmented help."))
(in-package :swank-help)
(defun aget (alist key)
(cdr (assoc key alist :test 'equalp)))
(defun sort-by-name (infos)
(sort infos #'string< :key (lambda (info) (aget info :name))))
(defun info-for-emacs (info)
(when (aget info :package)
(setf (cdr (assoc :package info))
(package-name (aget info :package))))
(when (aget info :arglist)
;; arglist is conflictive for slime protocol. do not use.
(setf (cdr (assoc :arglist info)) nil))
(when (aget info :documentation)
(push (cons :parsed-documentation
(parse-docstring (aget info :documentation)
(when (member (aget info :type) '(:function :generic-function :macro))
(list-lambda-list-args
(read-from-string (aget info :args))))
:package (find-package (aget info :package))))
info))
(push (cons :symbol (cdr (assoc :name info))) info)
(setf (cdr (assoc :name info)) (symbol-name (cdr (assoc :name info))))
info)
(defun read-emacs-symbol-info (symbol &optional kind shallow)
(let ((infos (symbol-properties symbol shallow)))
(if kind
(alexandria:when-let ((info (find kind infos
:key (lambda (info)
(aget info :type)))))
(info-for-emacs info))
(mapcar 'info-for-emacs infos))))
(defun read-emacs-package-info (package-name &optional shallow)
(let ((package (or (and (typep package-name 'package)
package-name)
;; we use SWANK::PARSE-PACKAGE instead of CL:FIND-PACKAGE here
;; to support alternative values of CL:*PRINT-CASE*
(swank::parse-package package-name)
(error "Package not found: ~a" package-name))))
(list (cons :type :package)
(cons :name (package-name package))
(cons :documentation (documentation package t))
(unless shallow
(let (infos)
(do-external-symbols (symbol package)
(dolist (info (read-emacs-symbol-info symbol nil t))
(push info infos)))
(cons :external-symbols (sort-by-name infos)))))))
(defun read-emacs-system-info (system-name &optional shallow)
(let ((system (asdf:find-system system-name)))
(list (cons :type :system)
(cons :name system-name)
(cons :documentation
(slot-value system 'asdf/system::description)
;;(asdf:system-description system)
)
(cons :dependencies (remove-if-not 'stringp
(asdf:system-depends-on system)))
(cons :loaded-p (asdf:component-loaded-p system-name))
(unless shallow
(cons :packages (sort (mapcar 'package-name
(asdf-system-packages system-name))
#'string<))))))
(defun read-emacs-packages-info ()
(sort-by-name
(mapcar (lambda (package)
(read-emacs-package-info package t))
(list-all-packages))))
(defun read-emacs-systems-info ()
(sort-by-name
(mapcar (lambda (system)
(read-emacs-system-info system t))
(asdf:registered-systems))))
(swank::defslimefun apropos-documentation-for-emacs
(pattern &optional external-only case-sensitive package)
"Make an apropos search in docstrings for Emacs.
The result is a list of property lists."
(let ((package (if package
(or (swank::parse-package package)
(error "No such package: ~S" package)))))
;; The MAPCAN will filter all uninteresting symbols, i.e. those
;; who cannot be meaningfully described.
(mapcan (swank::listify #'swank::briefly-describe-symbol-for-emacs)
(sort (remove-duplicates
(apropos-symbols-documentation pattern
:external-only external-only
:case-sensitive case-sensitive
:package package))
#'swank::present-symbol-before-p))))
(defun some-documentation (symbol)
;; Trick to disable warnings from DOCUMENTATION function
;; DOCUMENTATION function complains on some type of DOC-TYPE arguments signaling a warning.
;; I temporarily rebind *ERROR-OUTPUT* so those warnings don't appear on the REPL output.
(let ((*error-output* (make-string-output-stream)))
(some (lambda (type)
(documentation symbol type))
'(function variable type structure setf t))))
(defun make-apropos-documentation-matcher (pattern case-sensitive)
(let ((chr= (if case-sensitive #'char= #'char-equal)))
(lambda (docstring)
(every (lambda (word)
(search word docstring :test chr=))
(if (stringp pattern)
(list pattern)
pattern)))))
(defun apropos-symbols-documentation (pattern &key external-only case-sensitive package return-docs)
(let ((packages (or package (remove (find-package :keyword)
(list-all-packages))))
(matcher (make-apropos-documentation-matcher pattern case-sensitive))
(result))
(with-package-iterator (next packages :external :internal)
(loop (multiple-value-bind (morep symbol) (next)
(when (not morep) (return))
(let ((doc (some-documentation symbol)))
(when (or (not external-only) (swank::symbol-external-p symbol))
(let ((name-and-doc (if doc
(format nil "~a~%~a" symbol doc)
(symbol-name symbol))))
(when (funcall matcher name-and-doc)
(if return-docs
(push (cons symbol doc) result)
(push symbol result)))))))))
result))
;; Provide an apropos functions that matches on name and docstrings
(defun parse-apropos-pattern (string)
(split-sequence:split-sequence #\space string :remove-empty-subseqs t))
(defun docstring-summary (docstring)
(let ((first-line (with-input-from-string (s docstring)
(uiop/stream:slurp-stream-line s))))
(let ((dotpos (position #\. first-line)))
(if dotpos
(subseq first-line 0 (1+ dotpos))
first-line))))
(defun apropos (string-designator &optional package external-only (print-docstring t))
"Briefly describe all symbols which contain the specified STRING.
If PACKAGE is supplied then only describe symbols present in
that package. If EXTERNAL-ONLY then only describe
external symbols in the specified package.)
If PRINT-DOCSTRING the the results docstrings are made part of the output."
(let ((packages (or package (remove (find-package :keyword)
(list-all-packages))))
(matcher (make-apropos-documentation-matcher
(parse-apropos-pattern string-designator) nil)))
(with-package-iterator (next packages :external :internal)
(loop (multiple-value-bind (morep symbol) (next)
(when (not morep) (return))
(let ((doc (some-documentation symbol)))
(when (or (not external-only) (swank::symbol-external-p symbol))
(let ((name-and-doc (if doc
(format nil "~a~%~a" symbol doc)
(symbol-name symbol))))
(when (funcall matcher name-and-doc)
(if (and print-docstring doc)
(format t "~a : ~a" symbol (docstring-summary doc))
(format t "~a" symbol))
(terpri))))))))
(values)))
(defun apropos-list (string-designator &optional package external-only)
"Like SWANK-HELP:APROPOS, except that it returns a list of the symbols found instead of describing them."
(let ((packages (or package (remove (find-package :keyword)
(list-all-packages))))
(matcher (make-apropos-documentation-matcher
(parse-apropos-pattern string-designator) nil))
(result))
(with-package-iterator (next packages :external :internal)
(loop (multiple-value-bind (morep symbol) (next)
(when (not morep) (return))
(let ((doc (some-documentation symbol)))
(when (or (not external-only) (swank::symbol-external-p symbol))
(let ((name-and-doc (if doc
(format nil "~a~%~a" symbol doc)
(symbol-name symbol))))
(when (funcall matcher name-and-doc)
(pushnew symbol result))))))))
result))
(defgeneric describe (object &optional destination)
(:documentation "Print an augmented description of OBJECT to output DESTINATION."))
(defmethod describe (object &optional (destination *standard-output*))
(cl:describe object destination))
(defmethod describe ((package package) &optional (destination *standard-output*))
(uiop:with-output (out destination)
(cl:describe package out)
(format out "~%Exported definitions: ~%")
(do-external-symbols (symbol package)
(let* ((properties (car (symbol-properties symbol t 'cl:macro-function)))
(args (cdr (assoc :args properties))))
(when properties
(when (or (string= args "nil") ;; TODO: fix def-properties
(string= args "NIL")
(null args))
(setf args "()"))
;;(prin1 properties out)
(format out "[MACRO] ~a ~a: ~%" symbol args)
(terpri out)
(alexandria:if-let ((docs (cdr (assoc :documentation properties))))
(write-string docs out)
(write-string "Not documented." out))
(terpri out)
(terpri out))))
(terpri out)
(do-external-symbols (symbol package)
(let* ((properties (car (symbol-properties symbol t 'cl:function)))
(args (cdr (assoc :args properties))))
(when properties
(when (or (string= args "nil") ;; TODO: fix def-properties
(string= args "NIL")
(null args))
(setf args "()"))
;;(prin1 properties out)
(format out "[FUNCTION] ~a ~a: ~%" symbol args)
(terpri out)
(alexandria:if-let ((docs (cdr (assoc :documentation properties))))
(write-string docs out)
(write-string "Not documented." out))
(terpri out)
(terpri out))))))
;; (describe (find-package :swank-help))
(provide :swank-help)