-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmi-elisp.el
361 lines (317 loc) · 12.4 KB
/
mi-elisp.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
357
358
359
360
361
;;; mi-elisp.el --- Mode-info backend for Emacs Lisp Reference Manual -*- coding: iso-2022-7bit; -*-
;; Copyright (C) 1998-2006 TSUCHIYA Masatoshi <[email protected]>
;; Author: TSUCHIYA Masatoshi <[email protected]>
;; Keywords: emacs-lisp info
;; This file is a part of mode-info.
;; 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 2, 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, you can either send email to this
;; program's maintainer or write to: The Free Software Foundation,
;; Inc.; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
;;; Commentary:
;; This file provides mode-info backend stuffs to see the GNU Emacs
;; Lisp Reference Manual, which can be downloaded from:
;;
;; ftp://ftp.gnu.org/pub/gnu/emacs/elisp-manual-21-2.7a.tar.gz
;;
;; The Japanese version of its edition 2.5 can be downloaded from:
;;
;; ftp://ftp.ascii.co.jp/pub/GNU/elisp-manual-20-2.5-jp.tgz
;;
;; The Japanese version of itsh edition 2.4 can be downloaded from:
;;
;; ftp://etlport.etl.go.jp/pub/mule/elisp-manual-19-2.4-jp2.0.tar.gz
;;; Code:
(require 'mode-info)
(eval-when-compile
(require 'cl)
(require 'mi-config) ; For mode-info-with-help-buffer().
(require 'mi-index))
(condition-case nil
(require 'help-mode) ; For `help-xref' button of Emacs-21.3.50.
(error nil))
(eval-and-compile
(autoload 'find-function-search-for-symbol "find-func")
(autoload 'find-variable-noselect "find-func"))
(defgroup mode-info-elisp nil
"Various sorts of improved help system for emacs-lisp-mode."
:group 'mode-info)
(defcustom mode-info-elisp-index-file
(expand-file-name "mi-elisp.idx" mode-info-index-directory)
"*Index file of functions and variables described in Info about emacs-lisp."
:group 'mode-info-elisp
:type 'file)
(defcustom mode-info-elisp-titles
(delq nil
(list '("elisp-ja" "elisp-jp" "elisp")
(and (boundp 'MULE) "mule-jp")))
"*Info titles about emacs-lisp."
:group 'mode-info-elisp
:type mode-info-titles-type)
(defconst mode-info-elisp-entry-regexp "\
^[ \t]+-+[ \t]+\\(\\(Prefix[ \t]+\\)?Command\\|\\($B%W%l%U%#%C%/%9(B\\)?$B%3%^%s%I(B\\|\
Function\\|Special[ \t]+Form\\|Macro\\|\\(\\(Glob\\|Loc\\)al[ \t]+\\)?Variable\\|\
\\(User[ \t]+\\)?Option\\):[ \t]+\\([^ \t\n]+\\)[ \t\n]")
(defconst mode-info-elisp-entry-pos 7)
(defconst mode-info-elisp-packages
'(calender diary-lib dired edebug facemenu font-lock view))
(mode-info-defclass elisp)
(mode-info-defmethod function-at-point ((class elisp))
(mode-info-static-if (featurep 'xemacs)
(function-at-point)
(function-called-at-point)))
(mode-info-defmethod read-function ((class elisp) &optional prompt
default predicate require-match)
(unless default
(setq default (mode-info-function-at-point class)))
(let* ((enable-recursive-minibuffers t)
(x (completing-read
(or prompt
(if default
(format "[%s] Describe function (default %s): "
(mode-info-class-name class) default)
(format "[%s] Describe function: "
(mode-info-class-name class))))
obarray
(if predicate
`(lambda (x) (and (fboundp x) (,predicate x)))
(function fboundp))
require-match)))
(if (string= x "") default (intern x))))
(mode-info-defmethod function-described-p ((class elisp) function)
(mode-info-load-index class)
(and (assq function (mode-info-function-alist class)) t))
(defsubst mode-info-elisp-function-document-1 (function)
(goto-char (point-max))
(forward-line -1)
(or (when (search-forward "not documented" nil t)
(let ((x (ignore-errors
(unless (subrp (symbol-function function))
;; Don't use find-function-noselect because it
;; follows aliases (which fails for built-in
;; functions).
(find-function-search-for-symbol
function nil (symbol-file function))))))
(when x
(message "%s is not documented" function)
x)))
(cons (current-buffer) (point-min))))
(mode-info-defmethod function-document ((class elisp) function)
(mode-info-load-index class)
(let ((entry (assq function (mode-info-function-alist class))))
(if entry
(mode-info-goto-info-entry class entry)
(describe-function function)
(mode-info-with-help-buffer
(mode-info-elisp-function-document-1 function)))))
(mode-info-defmethod variable-at-point ((class elisp))
(let ((v (variable-at-point)))
(if (eq v 0) nil v)))
(mode-info-defmethod read-variable ((class elisp) &optional prompt
default predicate require-match)
(unless default
(setq default (mode-info-variable-at-point class)))
(let* ((enable-recursive-minibuffers t)
(x (completing-read
(or prompt
(if default
(format "[%s] Describe variable (default %s): "
(mode-info-class-name class) default)
(format "[%s] Describe variable: "
(mode-info-class-name class))))
obarray
(if predicate
`(lambda (x) (and (boundp x) (,predicate x)))
(function boundp))
require-match)))
(if (string= x "") default (intern x))))
(mode-info-defmethod variable-described-p ((class elisp) variable)
(mode-info-load-index class)
(and (assq variable (mode-info-variable-alist class)) t))
(mode-info-defmethod variable-document ((class elisp) variable)
(mode-info-load-index class)
(let ((entry (assq variable (mode-info-variable-alist class))))
(if entry
(mode-info-goto-info-entry class entry)
(describe-variable variable)
(mode-info-with-help-buffer
(cons (current-buffer) (point-min))))))
(mode-info-defmethod describe-variable-internal ((class elisp) variable
&optional keep-window)
(let ((value (symbol-value variable))
(buffer (when (local-variable-p variable) (buffer-name))))
(mode-info-method-next)
(when (mode-info-variable-described-p class variable)
(if buffer
(message "%s's local value is %s in buffer %s; %s"
variable (prin1-to-string value) buffer
(if (default-boundp variable)
(format "global value is %s"
(prin1-to-string (default-value variable)))
"globally void"))
(message "%s's value is %s" variable (prin1-to-string value))))))
(mode-info-defmethod read-tag ((class elisp))
(let* ((default (or (mode-info-variable-at-point class)
(mode-info-function-at-point class)))
(x (completing-read (if default
(format "[%s] Find tag (default %s): "
(mode-info-class-name class)
default)
(format "[%s] Find tag: "
(mode-info-class-name class)))
obarray
(lambda (s) (or (boundp s) (fboundp s)))
t)))
(if (string= x "") default (intern x))))
(mode-info-defmethod find-tag-noselect ((class elisp) tag)
(cond
((fboundp tag)
(if (subrp (symbol-function tag))
(prog1 (mode-info-function-document class tag)
(message "%s is a primitive function" tag))
;; Don't use find-function-noselect because it follows aliases
;; (which fails for built-in functions).
(find-function-search-for-symbol tag nil (symbol-file tag))))
((boundp tag)
(condition-case err
(find-variable-noselect tag)
(error
(let ((msg (error-message-string err)))
(if (string= msg
(format "Don't know where `%s' is defined" tag))
(prog1 (mode-info-variable-document class tag)
(message msg))
(signal (car err) (cdr err)))))))))
(eval-and-compile
(if (fboundp 'define-button-type)
(defalias 'mode-info-define-button-type 'define-button-type)
(defun mode-info-define-button-type (type &rest properties)
"Define a `button type' called NAME (dummy function)."
(put type 'mode-info-button properties))
(put 'mode-info-define-button-type 'lisp-indent-function 1)))
(eval-and-compile
(if (fboundp 'button-type-get)
(defalias 'mode-info-button-type-get 'button-type-get)
(defun mode-info-button-type-get (type prop)
"Get the property of button-type TYPE named PROP (dummy function)."
(plist-get (get type 'mode-info-button) prop))))
(mode-info-static-if (fboundp 'help-insert-xref-button)
(eval-and-compile
(if (fboundp 'define-button-type)
(defalias 'mode-info-insert-button 'help-insert-xref-button)
(defun mode-info-insert-button (string type &rest args)
"Insert STRING and make a hyperlink."
(help-insert-xref-button string
(mode-info-button-type-get type
'help-function)
args
(mode-info-button-type-get type
'help-echo)))))
(mode-info-static-if (fboundp 'help-xref-button)
;; The function is designed for Emacs-20.x, based on
;; `help-insert-xref-button' defined in help.el of Emacs-21.2.
(defun mode-info-insert-button (string type &rest args)
"Insert STRING and make a hyperlink."
(let ((pos (point)))
(insert string)
(goto-char pos)
(search-forward string)
(help-xref-button 0
(mode-info-button-type-get type 'help-function)
args)))
(defun mode-info-insert-button (string type &rest args)
"Insert STRING (dummy function)."
(insert string))))
(mode-info-define-button-type 'mode-info-describe-function
':supertype 'help-xref
'help-function 'mode-info-describe-function
'help-echo "mouse-2, RET: go to Info.")
(defun mode-info-elisp-add-function-button (function)
(let ((buffer-read-only)
(class (mode-info-find-class 'elisp)))
(when (mode-info-function-described-p class function)
(save-excursion
(save-match-data
(goto-char (point-max))
(if (re-search-backward "\\[[-a-z]+\\]"
(line-beginning-position) t)
(progn
(end-of-line)
(insert " "))
(insert (if (bolp) "\n" "\n\n")))
(mode-info-insert-button "[info]"
'mode-info-describe-function
function class t))))))
(mode-info-define-button-type 'mode-info-describe-variable
':supertype 'help-xref
'help-function 'mode-info-describe-variable
'help-echo "mouse-2, RET: go to Info.")
(defun mode-info-elisp-add-variable-button (variable)
(let ((buffer-read-only)
(class (mode-info-find-class 'elisp)))
(when (mode-info-variable-described-p class variable)
(save-excursion
(save-match-data
(goto-char (point-max))
(if (re-search-backward "\\[[-a-z]+\\]"
(line-beginning-position) t)
(progn
(end-of-line)
(insert " "))
(insert (if (bolp) "\n" "\n\n")))
(mode-info-insert-button "[info]"
'mode-info-describe-variable
variable class t))))))
(defun mode-info-elisp-info-ref (function)
"Look up an Emacs Lisp function in the Elisp manual in the Info system."
(let ((class (mode-info-find-class 'elisp)))
(when (mode-info-function-described-p class function)
(mode-info-describe-function-internal
class function (memq major-mode '(Info-mode help-mode))))))
(defun mode-info-elisp-make-index ()
"Make index of Info files listed in `mode-info-elisp-titles'."
(interactive)
(dolist (package mode-info-elisp-packages)
(ignore-errors (require package)))
(mode-info-make-index 'elisp
mode-info-elisp-titles
mode-info-elisp-entry-regexp
mode-info-elisp-entry-pos))
(mode-info-defmethod write-index-file ((class elisp) functions variables
&optional regexp)
(with-temp-buffer
(dolist (sym (list functions variables))
(set sym (mapcar (lambda (x)
(cons (intern (car x)) (cdr x)))
(symbol-value sym))))
(mode-info-dump-symbols functions variables)
(write-region (point-min) (point-max)
(mode-info-index-file-name class))))
(mode-info-defmethod process-index-node ((class elisp) title nodename
functions variables)
(when (string= "Index" nodename)
(let (x)
(while (re-search-forward mode-info-index-entry-regexp nil t)
(and (setq x (intern-soft (match-string 1)))
(fboundp x)
(mode-info-process-index-node-1 title
functions
(match-string 1)
(match-string 2)
(match-string 3)))
(and (setq x (intern-soft (match-string 1)))
(boundp x)
(mode-info-process-index-node-1 title
variables
(match-string 1)
(match-string 2)
(match-string 3)))))))
(provide 'mi-elisp)
;;; mi-elisp.el ends here