-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathelisp-demos.el
288 lines (258 loc) · 10.3 KB
/
elisp-demos.el
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
;;; elisp-demos.el --- Elisp API Demos -*- lexical-binding: t; -*-
;; Copyright (C) 2018-2024 Xu Chunyang
;; Author: Xu Chunyang
;; Homepage: https://github.com/xuchunyang/elisp-demos
;; Keywords: lisp, docs
;; Version: 2024.01.16
;; Package-Requires: ((emacs "26.3"))
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Elisp API Demos
;;; Code:
(eval-when-compile (require 'cl-lib))
(require 'subr-x)
(defconst elisp-demos--load-dir (file-name-directory
(or load-file-name buffer-file-name)))
(defconst elisp-demos--elisp-demos.org (expand-file-name
"elisp-demos.org"
elisp-demos--load-dir))
(defcustom elisp-demos-user-files nil
"Files to search in addition to the one from the elisp-demos package.
If set, new notes are added to the first file in this list."
:group 'help
:type '(repeat file))
(defun elisp-demos--search (symbol)
(let (results)
(dolist (file (append elisp-demos-user-files (list elisp-demos--elisp-demos.org)))
(when (file-exists-p file)
(with-temp-buffer
(insert-file-contents file)
(goto-char (point-min))
(when (re-search-forward
(format "^\\* %s$" (regexp-quote (symbol-name symbol)))
nil t)
(let (beg end)
(forward-line 1)
(setq beg (point))
(if (re-search-forward "^\\*" nil t)
(setq end (line-beginning-position))
(setq end (point-max)))
(push (propertize
(string-trim (buffer-substring-no-properties beg end))
'file file
'pos beg)
results))))))
(when results
(string-join (nreverse results) "\n\n"))))
(defun elisp-demos--syntax-highlight (orgsrc)
(with-temp-buffer
(insert orgsrc)
(delay-mode-hooks (org-mode))
(if (fboundp 'font-lock-ensure)
(font-lock-ensure)
(with-no-warnings
(font-lock-fontify-buffer)))
(buffer-string)))
(defun elisp-demos--symbols ()
(let (symbols)
(dolist (file (append elisp-demos-user-files (list elisp-demos--elisp-demos.org)))
(with-temp-buffer
(insert-file-contents file)
(goto-char (point-min))
(delay-mode-hooks (org-mode))
(while (re-search-forward "^\\* +\\(.+\\)$" nil t)
(push (org-entry-get (point) "ITEM") symbols))))
(mapcar 'intern (sort (cl-delete-duplicates symbols :test #'eq) #'string<))))
(defun elisp-demos-find-demo (symbol)
"Find the demo of the SYMBOL."
(interactive
(let* ((sym-here (symbol-at-point))
(symbols (elisp-demos--symbols))
(default-val (and sym-here
(memq sym-here symbols)
(symbol-name sym-here)))
(prompt (if default-val
(format "Find demo (default: %s): " default-val)
"Find demo: ")))
(list (read (completing-read prompt
(mapcar #'symbol-name symbols)
nil t nil nil default-val)))))
(cl-assert symbol)
(catch 'found
(dolist (file (append elisp-demos-user-files (list elisp-demos--elisp-demos.org)))
(with-current-buffer (find-file-noselect file)
(let ((pos (org-find-exact-headline-in-buffer (symbol-name symbol))))
(when pos
(goto-char pos)
(funcall (if (fboundp 'org-fold-show-entry)
'org-fold-show-entry
'org-show-entry))
(if (fboundp 'pop-to-buffer-same-window)
(pop-to-buffer-same-window (current-buffer))
(pop-to-buffer (current-buffer)))
(throw 'found (point)))))))
t)
;; Borrowed from `helpful--read-symbol'
(defun elisp-demos--read-symbol (prompt predicate)
(let* ((sym-here (symbol-at-point))
(default-val
(when (funcall predicate sym-here)
(symbol-name sym-here))))
(when default-val
(setq prompt
(replace-regexp-in-string
(rx ": " eos)
(format " (default: %s): " default-val)
prompt)))
(intern (completing-read prompt obarray
predicate t nil nil
default-val))))
(defun elisp-demos-add-demo (symbol)
"Add demo for SYMBOL."
(interactive
(list (elisp-demos--read-symbol "Add demo: "
(lambda (sym)
(or (functionp sym)
(special-form-p sym)
(macrop sym))))))
;; Try to reuse existing window
(let* ((file (or (car elisp-demos-user-files) elisp-demos--elisp-demos\.org))
(buffer (get-file-buffer file))
(window (and buffer (get-buffer-window buffer))))
(if window
(select-window window)
(find-file file)))
(goto-char (point-min))
(or
(catch 'found
(while (re-search-forward "^\\* \\(.+\\)$" nil t)
(cond ((string= (org-entry-get (point) "ITEM") (symbol-name symbol))
(goto-char (line-beginning-position))
(user-error "%s already exists" symbol))
((string< (symbol-name symbol) (org-entry-get (point) "ITEM"))
(goto-char (line-beginning-position))
(throw 'found t)))))
(goto-char (point-max)))
(org-insert-heading)
(insert (symbol-name symbol) "\n"
"\n"
"#+BEGIN_SRC elisp\n"
(format " (%s )\n" (symbol-name symbol))
"#+END_SRC")
(search-backward ")\n#+END_SRC"))
;;; * C-h f (`describe-function')
(defun elisp-demos-help-find-demo-at-point ()
"Find the demo at point."
(interactive)
(let ((file (get-text-property (point) 'file))
(pos (get-text-property (point) 'pos)))
(find-file file)
(goto-char pos)))
(defvar elisp-demos-help-keymap
(let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") #'elisp-demos-help-find-demo-at-point)
map))
;;;###autoload
(defun elisp-demos-advice-describe-function-1 (function)
(let ((src (and (symbolp function)
(elisp-demos--search function)))
(buf (get-buffer "*Help*")))
(when (and src buf)
(with-current-buffer buf
(save-excursion
(let ((inhibit-read-only t))
(goto-char (point-max))
;; Ensure two final newlines
(if (not (eq (char-before) ?\n))
(insert "\n\n")
(if (not (eq (char-before (1- (point))) ?\n))
(insert "\n")))
(insert
(propertize (elisp-demos--syntax-highlight src)
'start (point)
'symbol function
'keymap elisp-demos-help-keymap)
"\n")
(unless (eobp) (insert "\n"))))))))
;;; * helpful.el - https://github.com/Wilfred/helpful
(defvar helpful--sym)
(declare-function helpful--heading "helpful")
;;;###autoload
(defun elisp-demos-advice-helpful-update ()
(let ((src (and (symbolp helpful--sym)
(elisp-demos--search helpful--sym))))
(save-excursion
(goto-char (point-min))
(when (re-search-forward "^References$")
(goto-char (line-beginning-position))
(let ((inhibit-read-only t))
(insert
(helpful--heading "Demos")
(if (and src (not (string= src "")))
(concat
(propertize (elisp-demos--syntax-highlight src)
'start (point)
'symbol helpful--sym
'keymap elisp-demos-help-keymap)
"\n\n")
"")
(if (fboundp 'buttonize)
(buttonize "[Add]" #'elisp-demos-add-demo helpful--sym)
(insert-text-button
"[Add]"
'face 'link
'action (lambda (_button)
(elisp-demos-add-demo helpful--sym))))
"\n\n"))))))
;;;###autoload
(defun elisp-demos-for-helpful ()
"Find a demo for the current `helpful' buffer."
(interactive)
(let ((file (get-text-property (point) 'file))
(pos (get-text-property (point) 'pos)))
(find-file file)
(goto-char pos)))
;;; * JSON
(declare-function json-encode-string "json" (string))
(defun elisp-demos--export-json-file (json-file)
"Export all demos as json to JSON-FILE."
(require 'json)
(let ((output-buffer (generate-new-buffer " *elisp-demos-json*"))
title body beg end)
(dolist (file (append elisp-demos-user-files (list elisp-demos--elisp-demos.org)))
(with-temp-buffer
(insert-file-contents file)
(goto-char (point-min))
(delay-mode-hooks (org-mode))
(while (re-search-forward "^\\* +\\(.+\\)$" nil t)
(setq title (org-entry-get (point) "ITEM"))
(setq beg (save-excursion
(forward-line 1)
(line-beginning-position)))
(setq end (save-excursion
(if (re-search-forward "^\\* " nil t)
(line-beginning-position)
(point-max))))
(setq body (buffer-substring-no-properties beg end))
(setq title (string-trim title))
(setq body (string-trim body))
(with-current-buffer output-buffer
(insert
(json-encode-string title) ": " (json-encode-string body) ",\n")))))
(with-current-buffer output-buffer
(delete-char -2)
(goto-char (point-min)) (insert "{\n")
(goto-char (point-max)) (insert "}\n")
(write-region (point-min) (point-max) json-file))
(kill-buffer output-buffer)))
(provide 'elisp-demos)
;;; elisp-demos.el ends here