forked from larsbrinkhoff/forth-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforth-spec.el
171 lines (144 loc) · 5.39 KB
/
forth-spec.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
;;; forth-spec.el --- Browse words in Forth standard -*-lexical-binding:t-*-
;;
;; Copyright (C) 2016 Helmut Eller <eller.helmut@gmail.com>
;;
;; 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; This package makes it convenient to browse documentation for
;; standard Forth words from within Emacs. The command
;; `forth-spec-lookup' asks for the word name and invokes the HTML
;; browser with the right URL.
;;; Code:
(eval-and-compile
(or (require 'cl-lib nil t)
;; Emacs 23
(progn
(require 'cl)
(defmacro cl-ecase (&rest x) `(ecase . ,x)))))
(defgroup forth-spec
nil
"Browsing Forth standards."
:group 'forth)
(defcustom forth-spec-url-2012 "http://www.forth200x.org/documents/html/"
"The URL which contains the HTML version of the standard.
If you have a local copy set this variable to
something like \"file://home/joe/docs/ANS-Forth/\".
Note: the string should have a trailing backslash."
:type 'file
:group 'forth-spec)
(defcustom forth-spec-url-1994 "http://lars.nocrew.org/dpans/"
"URL for 1994 version of standard."
:type 'file
:group 'forth-spec)
(defcustom forth-spec-browse-url #'browse-url
"Just in case you want to use a special browser."
:type 'function
:group 'forth-spec)
(defun forth-spec-lookup-2012 (name)
"View the documentation on NAME from the Forth 2012 Standard."
(interactive (list (forth-spec--read-name 2012)))
(forth-spec--lookup name 2012))
(defun forth-spec-lookup-1994 (name)
"View the documentation on NAME from the ANS'94 Forth Standard."
(interactive (list (forth-spec--read-name 1994)))
(forth-spec--lookup name 1994))
(defun forth-spec--lookup (name version)
(funcall forth-spec-browse-url (forth-spec--build-url name version)))
(defvar forth-spec--lookup-history '())
(defun forth-spec--read-name (version)
"Read a word-name in the minibuffer, with completion."
(let ((completion-ignore-case t))
(completing-read "Word: " (forth-spec--index version)
nil t (thing-at-point 'symbol)
'forth-spec--lookup-history)))
(eval-and-compile
(defvar forth-spec--versioned-info
'((2012 forth-spec-url-2012 "alpha.html" #'forth-spec--parse-2012)
(1994 forth-spec-url-1994 "dpansf.htm" #'forth-spec--parse-1994))))
(defmacro forth-spec--versioned (name version)
(let ((index (cl-ecase name
(url 1)
(index 2)
(parse-index 3))))
`(cl-ecase ,version
(2012 ,(elt (assoc 2012 forth-spec--versioned-info) index))
(1994 ,(elt (assoc 1994 forth-spec--versioned-info) index)))))
(defun forth-spec--root (version)
(forth-spec--versioned url version))
(defun forth-spec--build-url (name version)
"Return the URL for the word NAME."
(concat (forth-spec--root version)
(elt (or (assoc name (forth-spec--index version))
(error "Name not found in index: %s" name))
1)))
(defvar forth-spec--index-cache nil)
(defun forth-spec--index (version)
"Return a list ((NAME HREF PRONUNCIATION) ...)."
(let ((entry (assoc version forth-spec--index-cache)))
(cond (entry (cdr entry))
(t
(let ((index (forth-spec--parse-index version)))
(push (cons version index) forth-spec--index-cache)
index)))))
(defun forth-spec--index-url (version)
(concat (forth-spec--root version) (forth-spec--versioned index version)))
(defun forth-spec--parse-index (version)
(forth-spec--call/url-buffer (forth-spec--index-url version)
(forth-spec--versioned parse-index version)))
(defun forth-spec--call/url-buffer (url fun)
(let ((buffer (url-retrieve-synchronously url)))
(unwind-protect
(with-current-buffer buffer
(funcall fun))
(kill-buffer buffer))))
(defun forth-spec--parse-2012 ()
(let ((index '())
(case-fold-search nil)
(rx "<td>\
<a href=\"\\([^\"]+\\)\">\\([^<]+\\)</a>\
</td><td>\\(?:\"\\([^\"]+\\)\"\\)?</td>"))
(search-forward "<table")
(while (re-search-forward rx nil t)
(push (list (forth-spec--decode-entities (match-string 2))
(match-string 1)
(match-string 3))
index))
(reverse index)))
;; (forth-spec--parse-index 1994)
(defun forth-spec--parse-1994 ()
(let ((index '())
(case-fold-search nil)
(rx "<A href=\\(dpans[^>]+\\)>[^<]+</A>[ ]*\\([^ ]+\\)[ ]*\
\\(?:<B>\\([^\<]+\\)</B>\\)?"))
(search-forward "<PRE>")
(while (re-search-forward rx nil t)
(push (list (forth-spec--decode-entities (match-string 2))
(match-string 1)
(match-string 3))
index))
(reverse index)))
(declare-function mm-url-decode-entities "gnus/mm-url")
(autoload 'mm-url-decode-entities "gnus/mm-url")
;; For annoying reasons, we need to declare this here.
(autoload 'mm-disable-multibyte "gnus/mm-util")
(defun forth-spec--decode-entities (string)
(with-temp-buffer
(insert string)
(goto-char (point-min))
(save-match-data
(mm-url-decode-entities))
(buffer-string)))
(provide 'forth-spec)
;;; forth-spec.el ends here