-
Notifications
You must be signed in to change notification settings - Fork 25
/
j-help.el
205 lines (179 loc) · 7.99 KB
/
j-help.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
;; -*- lexical-binding:t -*-
;;; j-help.el --- Documentation extention for j-mode
;; Copyright (C) 2012 Zachary Elliott
;; Copyright (C) 2023, 2024 LdBeth
;;
;; Authors: Zachary Elliott <[email protected]>
;; URL: http://github.com/zellio/j-mode
;; Version: 2.0.1
;; Keywords: J, Languages
;; This file is not part of GNU Emacs.
;;; Commentary:
;; j-help provides access to the J software vocabulary via two functions
;; j-help-lookup-symbol and j-help-lookup-symbol-at-point. j-help-look-symbol
;; takes one string argument ( generally via the mini-buffer ) which it then
;; looks up. j-help-lookup-symbol-at-point attempts to determine which symbol
;; is under your cursor and then passes that to j-help-lookup-symbol.
;;
;; The module provides the following key bindings for convenience
;;
;; * <kbd>C-c h</kbd> runs j-help-lookup-symbol
;; * <kbd>C-c C-h</kbd> j-help-lookup-symbol-at-point
;;; License:
;; 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; see the file COPYING. If not, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
;; USA.
;;; Code:
(defun j-help--process-voc-list (alist)
(let ((table (make-hash-table))
res)
(dolist (x alist)
(let ((len (length (car x))))
(puthash len
(cons x (gethash len table))
table)))
(maphash (lambda (key l) (push
(list key
(regexp-opt (mapcar #'car l))
l)
res))
table)
res))
(defgroup j-help nil
"Documentation extention for j-mode"
:group 'applications
:prefix "j-help-")
(defcustom j-help-local-dictionary-url ""
"Path to the local instance of the j-dictionary"
:type 'string
:group 'j-help)
(defcustom j-help-remote-dictionary-url "http://www.jsoftware.com/help/dictionary"
"Path to the remote instance of the j-dictionary"
:type 'string
:group 'j-help)
(defcustom j-help-symbol-search-branch-limit 5
"Distance from initial point they system can search for a valid symbol."
:type 'integer
:group 'j-help)
(defconst j-help-voc-alist
'(("~" . "d220v") ("}" . "d530n") ("|" . "d230") ("#" . "d400")
("{" . "d520") ("`" . "d610") ("_" . "d030") ("^" . "d200")
("]" . "d500") ("\\" . "d430") ("\\:" . "d432") ("\\." . "d431")
("\"" . "d600n") ("[" . "d500") ("@" . "d620") ("?" . "d640")
("=" . "d000") (";" . "d330") (":" . "d310n") ("/" . "d420")
("." . "d300") ("-" . "d120") ("," . "d320") ("+" . "d100")
("*" . "d110") ("<" . "d010") (">" . "d020") ("&" . "d630n")
("%" . "d130") ("$" . "d210") ("~:" . "d222") ("~." . "d221")
("}:" . "d532") ("}." . "d531") ("|:" . "d232") ("|." . "d231")
("{:" . "d522") ("{." . "d521") ("x:" . "dxco") ("u:" . "duco")
("t:" . "dtco") ("t." . "dtdotu") ("s:" . "dsco") ("r." . "drdot")
("q:" . "dqco") ("p:" . "dpco") ("p." . "dpdot") ("o." . "dodot")
("j." . "djdot") ("i:" . "dico") ("i." . "didot") ("f." . "dfdot")
("e." . "dedot") ("d." . "dddot") ("b." . "dbdotn") ("a:" . "dadot")
("a." . "dadot") ("`:" . "d612") ("_:" . "d032") ("_." . "d031")
("^:" . "d202n") ("^." . "d201") ("\":" . "d602") ("\"." . "d601")
("[:" . "d502") ("T." . "dtcapdot") ("@." . "d621") ("?." . "d641")
("=:" . "d001") ("=." . "d001") (";:" . "d332") (";." . "d331")
("::" . "d312") (":." . "d311") ("/:" . "d422") ("/." . "d421")
(".:" . "d301") (".." . "d301") ("-:" . "d122") ("-." . "d121")
(",:" . "d322") (",." . "d321") ("+:" . "d102") ("+." . "d101")
("*:" . "d112") ("*." . "d111") ("<:" . "d012") ("<." . "d011")
(">:" . "d022") (">." . "d021") ("&:" . "d632") ("&." . "d631") ("&.:" . "d631c")
("%:" . "d132") ("%." . "d131") ("$:" . "d212") ("$." . "d211")
("#:" . "d402") ("#." . "d401") ("S:" . "dscapco") ("M." . "dmcapdot")
("L:" . "dlcapco") ("L." . "dlcapdot") ("I." . "dicapdot") ("H." . "dhcapdot")
("E." . "decapdot") ("D:" . "ddcapco") ("D." . "ddcapdot") ("C." . "dccapdot")
("A." . "dacapdot") ("@:" . "d622") ("!" . "d410") ("!." . "d411") ("!:" . "d412") ("{::" . "d523")
("p.." . "dpdotdot") ("_9:" . "dconsf") ("&.:" . "d631") ("NB." . "dnb"))
"(string * string) alist")
(defconst j-help-dictionary-data-block
(j-help--process-voc-list j-help-voc-alist)
"(int * string * (string * string) alist) list")
(defun j-help-valid-dictionary ()
"Return best defined dictionary"
(replace-regexp-in-string
"/$" ""
(cond ((not (string= "" j-help-local-dictionary-url))
j-help-local-dictionary-url)
((not (string= "" j-help-remote-dictionary-url))
j-help-remote-dictionary-url))))
(defun j-help-symbol-pair-to-doc-url ( alist-data )
(let ((dic (j-help-valid-dictionary)))
(if (or (not alist-data) (string= dic ""))
(error "%s" "No dictionary found. Please specify a dictionary.")
(let ((_name (car alist-data))
(doc-name (cdr alist-data)))
(format "%s/%s.%s" dic doc-name "htm")))))
(defun j-help-symbol-to-doc-url ( j-symbol )
"Convert J-SYMBOL into localtion URL"
(j-help-symbol-pair-to-doc-url (assoc j-symbol j-help-voc-alist)))
(defun j-help--determine-symbol ( s point )
"Internal function to determine j symbols. Should not be called directly
string * int -> (string * string) list"
(unless (or (< point 0) (< (length s) point))
(let ((list j-help-dictionary-data-block)
val)
(while (and list (not val))
(setq val (let* ((x (car list))
(check-size (car x)))
(and
(<= (+ check-size point) (length s))
(string-match (cadr x) (substring s point (+ point check-size)))
(let* ((m (match-data))
(ss (substring s (+ point (car m)) (+ point (cadr m)))))
(assoc ss (caddr x)))))
list (cdr list)))
val)))
(defun j-help-determine-symbol-at-point ( point )
"int -> (string * string) list"
(save-excursion
(goto-char point)
(let* ((bol (pos-bol))
(eol (pos-eol))
(s (buffer-substring-no-properties bol eol)))
(j-help--determine-symbol s (- point bol)))))
(defun j-help-branch-determine-symbol-at-point*
( string current-index target-index resolved-symbol )
(if (> current-index target-index) resolved-symbol
(let ((next-symbol (j-help--determine-symbol string current-index)))
(j-help-branch-determine-symbol-at-point*
string
(+ current-index (length (or (car next-symbol) " ")))
target-index
next-symbol))))
(defun j-help-branch-determine-symbol-at-point ( point )
(save-excursion
(goto-char point)
(j-help-branch-determine-symbol-at-point*
(buffer-substring-no-properties (pos-bol) (pos-eol))
(- (max (- point j-help-symbol-search-branch-limit) (pos-bol)) (pos-bol))
(- point (pos-bol))
nil)))
;;;###autoload
(defun j-help-lookup-symbol ( symbol )
"Lookup symbol in dictionary"
(interactive "sJ Symbol: ")
(let ((url (j-help-symbol-to-doc-url symbol)))
(message "Loading %s ..." url)
(browse-url url)))
;;;###autoload
(defun j-help-lookup-symbol-at-point ( point )
"Determine the symbol nearest to POINT and look it up in the dictionary"
(interactive "d")
(let ((symbol (j-help-branch-determine-symbol-at-point point)))
(if symbol
(j-help-lookup-symbol (car symbol))
(error "No symbol could be determined for point %d" point))))
(provide 'j-help)
;;; j-help.el ends here