-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsrfi.el
356 lines (299 loc) · 12.3 KB
/
srfi.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
;;; srfi.el --- Scheme Requests for Implementation browser -*- lexical-binding: t -*-
;; Copyright 2019, 2020, 2021 Lassi Kortela
;; Copyright 2020 Arthur A. Gleckler
;; SPDX-License-Identifier: MIT
;; Author: Lassi Kortela <[email protected]>
;; URL: https://github.com/srfi-explorations/emacs-srfi
;; Version: 0.3.0
;; Package-Requires: ((emacs "25.1"))
;; Keywords: languages util
;; This file is not part of GNU Emacs.
;;; Commentary:
;; Provides quick access to Scheme Requests for Implementation (SRFI)
;; documents from within Emacs:
;; * `M-x srfi-list` brings up a *SRFI* buffer listing all SRFIs.
;; * `M-x srfi` does the same, but lets you live-narrow the list by
;; typing numbers or words into the minibuffer.
;; * The *SRFI* buffer provides one-key commands to visit the SRFI
;; document, its discussion (mailing list) archive, and its version
;; control repository. These commands open the right web pages in
;; your web browser using `browse-url'. The standard variable
;; `browse-url-browser-function' can be used to control which web
;; browser is used to open what pages.
;;; Code:
(require 'browse-url)
(require 'srfi-data)
(defconst srfi-mode-font-lock-keywords
`(("^SRFI +\\([0-9]+\\): \\(.*?\\) (draft)$"
(1 font-lock-keyword-face)
(2 font-lock-preprocessor-face))
("^SRFI +\\([0-9]+\\): \\(.*?\\) ([0-9]\\{4\\})$"
(1 font-lock-keyword-face)
(2 font-lock-function-name-face))
("^SRFI +\\([0-9]+\\): \\(.*?\\) ([0-9]\\{4\\}, withdrawn)$"
(1 font-lock-keyword-face)
(2 font-lock-comment-face))))
(defvar srfi-narrow-query ""
"The current narrowing text in effect in the *SRFI* buffer.
This is always a string, but if the string consists entirely of
decimal digits, it's used to filter SRFI numbers.")
(defvar srfi-narrow-keyword nil
"The current keyword being shown in the *SRFI* buffer.")
(defvar srfi-abstract-directory nil
"A directory containing all the SRFI abstracts.")
(defvar srfi-source-directory nil
"A directory containing all the SRFI repos.")
(defun srfi--parse-number (string)
"Internal function to parse a SRFI number from STRING.
Strict rules: base 10, no leading zeros, no whitespace."
(save-match-data
(if (string= string "0") 0
(and (string-match "^[1-9][0-9]*$" string)
(string-to-number string 10)))))
(defun srfi--number-title (number)
"Get the title corresponding to the given SRFI NUMBER as string."
(elt srfi-data (+ 2 (* 3 number))))
(defun srfi--number-on-line ()
"Get the number of the SRFI on the current line."
(or (get-text-property (point) 'srfi-number)
(error "No SRFI on this line")))
(defun srfi--goto-first-srfi ()
"Scroll window to top and go to line of first SRFI."
(goto-char (point-min))
(let ((recenter-redisplay nil))
(recenter 0))
(goto-char (next-single-property-change
(point-min) 'srfi-number nil (point-max))))
(defun srfi--goto-number (number)
"Go to line of SRFI NUMBER."
(let* ((buffer (get-buffer "*SRFI*"))
(window (and buffer (get-buffer-window buffer))))
(when window
(with-selected-window window
(let* ((prev (point-min)) (here prev) (number-here nil))
(while (and (not (equal number number-here))
(setq here (next-single-property-change
here 'srfi-number nil (point-max)))
(< prev here))
(goto-char here)
(setq prev here)
(setq number-here (get-text-property here 'srfi-number)))
(when (equal number number-here) number))))))
(defun srfi--repository-url (number)
"Get the web URL for the version control repository of SRFI NUMBER."
(format "https://github.com/scheme-requests-for-implementation/srfi-%d"
number))
(defun srfi--discussion-url (number)
"Get the web URL for the mailing list archive of SRFI NUMBER."
(format "https://srfi-email.schemers.org/srfi-%d/" number))
(defun srfi--discussion-email-address (number)
"Get the email address for the mailing list of SRFI NUMBER."
(format "srfi-%[email protected]" number))
(defun srfi--landing-page-url (number)
"Get the web URL for the landing page of SRFI NUMBER."
(format "https://srfi.schemers.org/srfi-%d/"
number))
(defun srfi--document-url (number)
"Get the web URL for the SRFI document NUMBER."
(format "https://srfi.schemers.org/srfi-%d/srfi-%d.html"
number number))
(defun srfi-browse-repository-url (number)
"Browse SRFI NUMBER version control repository."
(interactive (list (srfi--number-on-line)))
(browse-url (srfi--repository-url number)))
(defun srfi-browse-discussion-url (number)
"Browse SRFI NUMBER mailing list archive."
(interactive (list (srfi--number-on-line)))
(browse-url (srfi--discussion-url number)))
(defun srfi-browse-landing-page-url (number)
"Browse SRFI NUMBER landing page."
(interactive (list (srfi--number-on-line)))
(browse-url (srfi--landing-page-url number)))
(defun srfi-browse-document-url (number)
"Browse SRFI NUMBER document."
(interactive (list (srfi--number-on-line)))
(browse-url (srfi--document-url number)))
(defun srfi-compose-mail ()
"Write an email to the mailing list of the SRFI on the current line.
Use `mail-user-agent' for customization."
(interactive)
(let ((number (srfi--number-on-line)))
(compose-mail (srfi--discussion-email-address number)
(srfi--number-title number))))
(defun srfi-browse-website-url ()
"Browse the home page of the SRFI specification process."
(interactive)
(browse-url "https://srfi.schemers.org/"))
(defvar srfi-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map special-mode-map)
(define-key map (kbd "RET") 'srfi-browse-document-url)
(define-key map (kbd "a") 'srfi-abstract)
(define-key map (kbd "d") 'srfi-browse-discussion-url)
(define-key map (kbd "f") 'srfi-source)
(define-key map (kbd "j") 'srfi-dired)
(define-key map (kbd "k") 'srfi-keyword)
(define-key map (kbd "l") 'srfi-browse-landing-page-url)
(define-key map (kbd "m") 'srfi-compose-mail)
(define-key map (kbd "r") 'srfi-browse-repository-url)
(define-key map (kbd "s") 'srfi-search)
(define-key map (kbd "S") 'srfi-fresh-search)
(define-key map (kbd "w") 'srfi-browse-website-url)
map)
"Keymap for `srfi-mode'.")
(define-derived-mode srfi-mode special-mode "SRFI"
"Major mode for browsing Scheme Requests for Implementation.
https://srfi.schemers.org/
\\{srfi-mode-map}"
(setq-local revert-buffer-function 'srfi-revert)
(setq-local font-lock-defaults
'((srfi-mode-font-lock-keywords) nil nil nil nil))
(unless (equal (buffer-name) "*SRFI*")
(message (concat "Note: srfi-mode is only meant for the *SRFI* buffer. "
"Try M-x srfi."))))
(defun srfi--reverse-iota (count)
"Internal function to return the integers 0 below COUNT as a list."
(let ((n 0) (ns '()))
(while (< n count)
(push n ns)
(setq n (+ n 1)))
ns))
(defun srfi--narrow-to-regexp (regexp)
"Internal function to narrow the *SRFI* buffer based on REGEXP."
(save-match-data
(with-current-buffer (get-buffer "*SRFI*")
(with-selected-window (get-buffer-window (current-buffer))
(cl-assert (null (buffer-file-name)))
(let* ((inhibit-read-only t)
(case-fold-search t)
(srfi-count (truncate (length srfi-data) 3))
(numbers (if srfi-narrow-keyword
(reverse (cdr (assoc srfi-narrow-keyword
srfi-data-keywords)))
(srfi--reverse-iota srfi-count))))
(erase-buffer)
(srfi-mode)
(insert
"Scheme Requests for Implementation"
(if (not srfi-narrow-keyword) ""
(concat " (" srfi-narrow-keyword ")"))
"\n"
"\n"
"RET: browse SRFI document | "
"d: discussion | "
"r: repo | "
"s: search | "
"w: website\n"
"\n")
(dolist (number numbers)
(let* ((base (* number 3))
(year (elt srfi-data base))
(status (elt srfi-data (+ base 1)))
(title (elt srfi-data (+ base 2)))
(s-text (cl-case status
((final) year)
((withdrawn) (format "%S, withdrawn" year))
(t status)))
(line (format "SRFI %3d: %s (%s)\n"
number title s-text))
(beg (point)))
(when (or (= 0 (length regexp))
(string-match regexp line))
(insert line)
(let ((end (point)))
(put-text-property beg end 'srfi-number number)))))
(srfi--goto-first-srfi)
regexp)))))
(defun srfi--narrow-to-string (string)
"Internal function to narrow *SRFI* buffer to STRING."
(srfi--narrow-to-regexp (regexp-quote string)))
(defun srfi--narrow-to-number (number)
"Internal function to narrow *SRFI* buffer to full or partial SRFI NUMBER."
(srfi--narrow-to-regexp
(if (and (integerp number) (>= number 0))
(concat "^SRFI +" (regexp-quote (number-to-string number)) "[0-9]*:")
"")))
(defun srfi--narrow-to-number-or-string (query)
"Internal function to narrow *SRFI* buffer to QUERY (digits or title)."
(let ((number (srfi--parse-number query)))
(cond (number
(srfi--narrow-to-number number)
number)
(t
(srfi--narrow-to-string query)
query))))
(defun srfi--narrow-minibuffer (&rest _ignored)
"Internal function to narrow the *SRFI* buffer based on minibuffer."
(srfi--narrow-to-number-or-string (minibuffer-contents)))
(defun srfi-revert (&optional _arg _noconfirm)
"(Re-)initialize the *SRFI* buffer."
(let ((parsed-query (srfi--narrow-to-number-or-string srfi-narrow-query)))
(when (numberp parsed-query)
(srfi--goto-number parsed-query))))
;;;###autoload
(defun srfi-list ()
"Show the *SRFI* buffer."
(interactive)
(let ((old (get-buffer "*SRFI*")))
(unless (and old (eq old (current-buffer)))
(switch-to-buffer-other-window (get-buffer-create "*SRFI*")))
(unless old
(setq-local default-directory (expand-file-name "~"))))
(srfi-revert))
;;;###autoload
(defun srfi-search (query)
"Show the *SRFI* buffer and live-narrow it from the minibuffer.
When called from Emacs Lisp code, QUERY is the string or SRFI
number. The number can be passed as an integer or a string."
(interactive
(minibuffer-with-setup-hook
(lambda () (add-hook 'after-change-functions
#'srfi--narrow-minibuffer
nil 'local))
(srfi-list)
(list (read-string "SRFI: " srfi-narrow-query))))
(setq srfi-narrow-query query)
(srfi-list))
(defun srfi-fresh-search ()
"Show the *SRFI* buffer and live-narrow it from scratch."
(interactive)
(setq srfi-narrow-query "")
(call-interactively #'srfi-search))
(defun srfi-keyword (keyword)
"Show the *SRFI* buffer and narrow it to a paricular KEYWORD."
(interactive (list (completing-read
"Narrow SRFIs to keyword: " srfi-data-keywords
nil t nil nil (list nil))))
(setq srfi-narrow-keyword keyword)
(unless (= 0 (length srfi-narrow-query))
(message "NOTE: The SRFI search filter is still active."))
(srfi-list))
(defun srfi-dired ()
"Open directory containing SRFI document in Dired."
(interactive)
(unless srfi-source-directory
(error "You must set `srfi-source-directory' first"))
(dired
(substitute-in-file-name
(format "%s/srfi-%d/" srfi-source-directory (srfi--number-on-line)))))
(defun srfi-source ()
"Open SRFI document."
(interactive)
(unless srfi-source-directory
(error "You must set `srfi-source-directory' first"))
(let ((srfi (srfi--number-on-line)))
(find-file
(substitute-in-file-name
(format "%s/srfi-%d/srfi-%d.html" srfi-source-directory srfi srfi)))))
(defun srfi-abstract ()
"Open SRFI abstract document."
(interactive)
(unless srfi-abstract-directory
(error "You must set `srfi-abstract-directory' first"))
(find-file
(substitute-in-file-name
(format "%s/%d.html" srfi-abstract-directory (srfi--number-on-line)))))
;;;###autoload
(defalias 'srfi 'srfi-search)
(provide 'srfi)
;;; srfi.el ends here