]> code.delx.au - gnu-emacs/blob - lisp/info-look.el
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
[gnu-emacs] / lisp / info-look.el
1 ;;; info-look.el --- major-mode-sensitive Info index lookup facility.
2 ;; An older version of this was known as libc.el.
3
4 ;; Copyright (C) 1995, 1996, 1997 Ralph Schleicher.
5
6 ;; Author: Ralph Schleicher <rs@purple.UL.BaWue.DE>
7 ;; Keywords: help languages
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Code:
27
28 (require 'info)
29
30 (defvar info-lookup-mode nil
31 "*Symbol of the current buffer's help mode.
32 Provide help according to the buffer's major mode if value is nil.
33 Automatically becomes buffer local when set in any fashion.")
34 (make-variable-buffer-local 'info-lookup-mode)
35
36 (defvar info-lookup-other-window-flag t
37 "*Non-nil means pop up the Info buffer in another window.")
38
39 (defvar info-lookup-highlight-face 'highlight
40 "*Face for highlighting looked up help items.
41 Setting this variable to nil disables highlighting.")
42
43 (defvar info-lookup-highlight-overlay nil
44 "Overlay object used for highlighting.")
45
46 (defvar info-lookup-history nil
47 "History of previous input lines.")
48
49 (defvar info-lookup-alist '((symbol . info-lookup-symbol-alist)
50 (file . info-lookup-file-alist))
51 "*Alist of known help topics.
52 Cons cells are of the form
53
54 (HELP-TOPIC . VARIABLE)
55
56 HELP-TOPIC is the symbol of a help topic.
57 VARIABLE is a variable storing HELP-TOPIC's public data.
58 Value is an alist with elements of the form
59
60 (HELP-MODE REGEXP IGNORE-CASE DOC-SPEC PARSE-RULE OTHER-MODES)
61
62 HELP-MODE is a mode's symbol.
63 REGEXP is a regular expression matching those help items whose
64 documentation can be looked up via DOC-SPEC.
65 IGNORE-CASE is non-nil if help items are case insensitive.
66 DOC-SPEC is a list of documentation specifications of the form
67
68 (INFO-NODE TRANS-FUNC PREFIX SUFFIX)
69
70 INFO-NODE is the name (including file name part) of an Info index.
71 TRANS-FUNC is a function translating index entries into help items;
72 nil means add only those index entries matching REGEXP, a string
73 means prepend string to the first word of all index entries.
74 PREFIX and SUFFIX are parts of a regular expression. If one of
75 them is non-nil then search the help item's Info node for the
76 first occurrence of the regular expression `PREFIX ITEM SUFFIX'.
77 ITEM will be highlighted with `info-lookup-highlight-face' if this
78 variable is not nil.
79 PARSE-RULE is either the symbol name of a function or a regular
80 expression for guessing the default help item at point. Fuzzy
81 regular expressions like \"[_a-zA-Z0-9]+\" do a better job if
82 there are no clear delimiters; do not try to write too complex
83 expressions. PARSE-RULE defaults to REGEXP.
84 OTHER-MODES is a list of cross references to other help modes.")
85
86 (defsubst info-lookup->topic-value (topic)
87 (symbol-value (cdr (assoc topic info-lookup-alist))))
88
89 (defsubst info-lookup->mode-value (topic mode)
90 (assoc mode (info-lookup->topic-value topic)))
91
92 (defsubst info-lookup->regexp (topic mode)
93 (nth 1 (info-lookup->mode-value topic mode)))
94
95 (defsubst info-lookup->ignore-case (topic mode)
96 (nth 2 (info-lookup->mode-value topic mode)))
97
98 (defsubst info-lookup->doc-spec (topic mode)
99 (nth 3 (info-lookup->mode-value topic mode)))
100
101 (defsubst info-lookup->parse-rule (topic mode)
102 (nth 4 (info-lookup->mode-value topic mode)))
103
104 (defsubst info-lookup->other-modes (topic mode)
105 (nth 5 (info-lookup->mode-value topic mode)))
106
107 (defvar info-lookup-cache nil
108 "Cache storing data maintained automatically by the program.
109 Value is an alist with cons cell of the form
110
111 (HELP-TOPIC . ((HELP-MODE INITIALIZED COMPLETIONS REFER-MODES) ...))
112
113 HELP-TOPIC is the symbol of a help topic.
114 HELP-MODE is a mode's symbol.
115 INITIALIZED is nil if HELP-MODE is uninitialized, t if
116 HELP-MODE is initialized, and `0' means HELP-MODE is
117 initialized but void.
118 COMPLETIONS is an alist of documented help items.
119 REFER-MODES is a list of other help modes to use.")
120
121 (defsubst info-lookup->cache (topic)
122 (or (assoc topic info-lookup-cache)
123 (car (setq info-lookup-cache
124 (cons (cons topic nil)
125 info-lookup-cache)))))
126
127 (defsubst info-lookup->topic-cache (topic)
128 (cdr (info-lookup->cache topic)))
129
130 (defsubst info-lookup->mode-cache (topic mode)
131 (assoc mode (info-lookup->topic-cache topic)))
132
133 (defsubst info-lookup->initialized (topic mode)
134 (nth 1 (info-lookup->mode-cache topic mode)))
135
136 (defsubst info-lookup->completions (topic mode)
137 (or (info-lookup->initialized topic mode)
138 (info-lookup-setup-mode topic mode))
139 (nth 2 (info-lookup->mode-cache topic mode)))
140
141 (defsubst info-lookup->refer-modes (topic mode)
142 (or (info-lookup->initialized topic mode)
143 (info-lookup-setup-mode topic mode))
144 (nth 3 (info-lookup->mode-cache topic mode)))
145
146 (defsubst info-lookup->all-modes (topic mode)
147 (cons mode (info-lookup->refer-modes topic mode)))
148
149 (defvar info-lookup-symbol-alist
150 '((autoconf-mode
151 "A[CM]_[_A-Z0-9]+" nil
152 (("(autoconf)Macro Index" "AC_"
153 "^[ \t]+- \\(Macro\\|Variable\\): .*\\<" "\\>")
154 ("(automake)Index" nil
155 "^[ \t]*`" "'"))
156 ;; Autoconf symbols are M4 macros. Thus use M4's parser.
157 ignore
158 (m4-mode))
159 (bison-mode
160 "[:;|]\\|%\\([%{}]\\|[_a-z]+\\)\\|YY[_A-Z]+\\|yy[_a-z]+" nil
161 (("(bison)Index" nil
162 "`" "'"))
163 "[:;|]\\|%\\([%{}]\\|[_a-zA-Z][_a-zA-Z0-9]*\\)"
164 (c-mode))
165 (c-mode
166 "\\(struct \\|union \\|enum \\)?[_a-zA-Z][_a-zA-Z0-9]*" nil
167 (("(libc)Function Index" nil
168 "^[ \t]+- \\(Function\\|Macro\\): .*\\<" "\\>")
169 ("(libc)Variable Index" nil
170 "^[ \t]+- \\(Variable\\|Macro\\): .*\\<" "\\>")
171 ("(libc)Type Index" nil
172 "^[ \t]+- Data Type: \\<" "\\>")
173 ("(termcap)Var Index" nil
174 "^[ \t]*`" "'"))
175 info-lookup-guess-c-symbol)
176 (emacs-lisp-mode
177 "[-_a-zA-Z+=*:&%$#@!^~][-_a-zA-Z0-9+=*:&%$#@!^~]*" nil
178 ("(elisp)Index" nil
179 "^[ \t]+- \\(Function\\|Macro\\|User Option\\|Variable\\): .*\\<"
180 "\\>"))
181 (m4-mode
182 "[_a-zA-Z][_a-zA-Z0-9]*" nil
183 (("(m4)Macro index"))
184 "[_a-zA-Z0-9]+")
185 (makefile-mode
186 "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z][_a-zA-Z0-9-]*" nil
187 (("(make)Name Index" nil
188 "^[ \t]*`" "'"))
189 "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z0-9-]+")
190 (texinfo-mode
191 "@\\([a-zA-Z]+\\|[^a-zA-Z]\\)" nil
192 (("(texinfo)Command and Variable Index"
193 ;; Ignore Emacs commands and prepend a `@'.
194 (lambda (item)
195 (if (string-match "^\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\( .*\\)?$" item)
196 (concat "@" (match-string 1 item))))
197 "`" "'"))))
198 "*Alist of help specifications for symbol names.
199 See the documentation of the variable `info-lookup-alist' for more details.")
200
201 (defvar info-lookup-file-alist
202 '((c-mode
203 "[_a-zA-Z0-9./+-]+" nil
204 (("(libc)File Index"))))
205 "*Alist of help specifications for file names.
206 See the documentation of the variable `info-lookup-alist' for more details.")
207
208 ;;;###autoload
209 (defun info-lookup-reset ()
210 "Throw away all cached data.
211 This command is useful if the user wants to start at the beginning without
212 quitting Emacs, for example, after some Info documents were updated on the
213 system."
214 (interactive)
215 (setq info-lookup-cache nil))
216
217 ;;;###autoload
218 (defun info-lookup-symbol (symbol &optional mode)
219 "Display the documentation of a symbol.
220 If called interactively, SYMBOL will be read from the mini-buffer.
221 Prefix argument means unconditionally insert the default symbol name
222 into the mini-buffer so that it can be edited.
223 The default symbol is the one found at point."
224 (interactive
225 (info-lookup-interactive-arguments 'symbol))
226 (info-lookup 'symbol symbol mode))
227
228 ;;;###autoload
229 (defun info-lookup-file (file &optional mode)
230 "Display the documentation of a file.
231 If called interactively, FILE will be read from the mini-buffer.
232 Prefix argument means unconditionally insert the default file name
233 into the mini-buffer so that it can be edited.
234 The default file name is the one found at point."
235 (interactive
236 (info-lookup-interactive-arguments 'file))
237 (info-lookup 'file file mode))
238
239 (defun info-lookup-interactive-arguments (topic)
240 "Return default value and help mode for help topic TOPIC."
241 (let* ((mode (if (info-lookup->mode-value
242 topic (or info-lookup-mode major-mode))
243 (or info-lookup-mode major-mode)
244 (info-lookup-change-mode topic)))
245 (completions (info-lookup->completions topic mode))
246 (default (info-lookup-guess-default topic mode))
247 (input (if (or current-prefix-arg (not (assoc default completions)))
248 default))
249 (completion-ignore-case (info-lookup->ignore-case topic mode))
250 (enable-recursive-minibuffers t)
251 (value (completing-read
252 (if (and default (not input))
253 (format "Describe %s (default %s): " topic default)
254 (format "Describe %s: " topic))
255 completions nil nil input 'info-lookup-history)))
256 (list (if (equal value "") default value) mode)))
257
258 (defun info-lookup-change-mode (topic)
259 (let* ((completions (mapcar (lambda (arg)
260 (cons (symbol-name (car arg)) (car arg)))
261 (info-lookup->topic-value topic)))
262 (mode (completing-read
263 (format "Use %s help mode: " topic)
264 completions nil t nil 'info-lookup-history)))
265 (or (setq mode (cdr (assoc mode completions)))
266 (error "No %s help available" topic))
267 (or (info-lookup->mode-value topic mode)
268 (error "No %s help available for `%s'" topic mode))
269 (setq info-lookup-mode mode)))
270
271 (defun info-lookup (topic item mode)
272 "Display the documentation of a help item."
273 (if (not mode)
274 (setq mode (or info-lookup-mode major-mode)))
275 (or (info-lookup->mode-value topic mode)
276 (error "No %s help available for `%s'" topic mode))
277 (let ((entry (or (assoc (if (info-lookup->ignore-case topic mode)
278 (downcase item) item)
279 (info-lookup->completions topic mode))
280 (error "Not documented as a %s: %s" topic (or item ""))))
281 (modes (info-lookup->all-modes topic mode))
282 (window (selected-window))
283 found doc-spec node prefix suffix)
284 (if (not info-lookup-other-window-flag)
285 (info)
286 (save-window-excursion (info))
287 (switch-to-buffer-other-window "*info*"))
288 (while (and (not found) modes)
289 (setq doc-spec (info-lookup->doc-spec topic (car modes)))
290 (while (and (not found) doc-spec)
291 (setq node (nth 0 (car doc-spec))
292 prefix (nth 2 (car doc-spec))
293 suffix (nth 3 (car doc-spec)))
294 (condition-case nil
295 (progn
296 (Info-goto-node node)
297 (Info-menu (or (cdr entry) item))
298 (setq found t)
299 (if (or prefix suffix)
300 (let ((case-fold-search
301 (info-lookup->ignore-case topic (car modes)))
302 (buffer-read-only nil))
303 (goto-char (point-min))
304 (re-search-forward
305 (concat prefix (regexp-quote item) suffix))
306 (goto-char (match-beginning 0))
307 (and window-system info-lookup-highlight-face
308 ;; Search again for ITEM so that the first
309 ;; occurence of ITEM will be highlighted.
310 (re-search-forward (regexp-quote item))
311 (let ((start (match-beginning 0))
312 (end (match-end 0)))
313 (if (overlayp info-lookup-highlight-overlay)
314 (move-overlay info-lookup-highlight-overlay
315 start end (current-buffer))
316 (setq info-lookup-highlight-overlay
317 (make-overlay start end))))
318 (overlay-put info-lookup-highlight-overlay
319 'face info-lookup-highlight-face)))))
320 (error nil))
321 (setq doc-spec (cdr doc-spec)))
322 (setq modes (cdr modes)))
323 ;; Don't leave the Info buffer if the help item couldn't be looked up.
324 (if (and info-lookup-other-window-flag found)
325 (select-window window))))
326
327 (defun info-lookup-setup-mode (topic mode)
328 "Initialize the internal data structure."
329 (or (info-lookup->initialized topic mode)
330 (let (cell data (initialized 0) completions refer-modes)
331 (if (not (info-lookup->mode-value topic mode))
332 (message "No %s help available for `%s'" topic mode)
333 ;; Recursively setup cross references.
334 ;; But refer only to non-void modes.
335 (mapcar (lambda (arg)
336 (or (info-lookup->initialized topic arg)
337 (info-lookup-setup-mode topic arg))
338 (and (eq (info-lookup->initialized topic arg) t)
339 (setq refer-modes (cons arg refer-modes))))
340 (info-lookup->other-modes topic mode))
341 (setq refer-modes (nreverse refer-modes))
342 ;; Build the full completion alist.
343 (setq completions
344 (nconc (info-lookup-make-completions topic mode)
345 (apply 'append
346 (mapcar (lambda (arg)
347 (info-lookup->completions topic arg))
348 refer-modes))))
349 (setq initialized t))
350 ;; Update `info-lookup-cache'.
351 (setq cell (info-lookup->mode-cache topic mode)
352 data (list initialized completions refer-modes))
353 (if (not cell)
354 (setcdr (info-lookup->cache topic)
355 (cons (cons mode data) (info-lookup->topic-cache topic)))
356 (setcdr cell data))
357 initialized)))
358
359 (defun info-lookup-make-completions (topic mode)
360 "Create a unique alist from all index entries."
361 (let ((doc-spec (info-lookup->doc-spec topic mode))
362 (regexp (concat "^\\(" (info-lookup->regexp topic mode)
363 "\\)\\([ \t].*\\)?$"))
364 node trans entry item prefix result
365 (buffer (get-buffer-create " temp-info-look")))
366 (with-current-buffer buffer
367 (Info-mode))
368 (while doc-spec
369 (setq node (nth 0 (car doc-spec))
370 trans (cond ((eq (nth 1 (car doc-spec)) nil)
371 (lambda (arg)
372 (if (string-match regexp arg)
373 (match-string 1 arg))))
374 ((stringp (nth 1 (car doc-spec)))
375 (setq prefix (nth 1 (car doc-spec)))
376 (lambda (arg)
377 (if (string-match "^\\([^: \t\n]+\\)" arg)
378 (concat prefix (match-string 1 arg)))))
379 (t (nth 1 (car doc-spec)))))
380 (condition-case nil
381 (with-current-buffer buffer
382 (message "Processing Info node `%s'..." node)
383 (Info-goto-node node)
384 (goto-char (point-min))
385 (and (search-forward "\n* Menu:" nil t)
386 (while (re-search-forward "\n\\* \\([^:\t\n]*\\):" nil t)
387 (setq entry (match-string 1)
388 item (funcall trans entry))
389 (and (info-lookup->ignore-case topic mode)
390 (setq item (downcase item)))
391 (and (string-equal entry item)
392 (setq entry nil))
393 (or (assoc item result)
394 (setq result (cons (cons item entry) result))))))
395 (error nil))
396 (message "Processing Info node `%s'...done" node)
397 (setq doc-spec (cdr doc-spec)))
398 result))
399
400 (defun info-lookup-guess-default (topic mode)
401 "Pick up default item at point (with favor to look back).
402 Return nil if there is nothing appropriate."
403 (let ((modes (info-lookup->all-modes topic mode))
404 (start (point)) guess whitespace)
405 (while (and (not guess) modes)
406 (setq guess (info-lookup-guess-default* topic (car modes))
407 modes (cdr modes))
408 (goto-char start))
409 ;; Collapse whitespace characters.
410 (and guess (concat (delete nil (mapcar (lambda (ch)
411 (if (or (char-equal ch ? )
412 (char-equal ch ?\t)
413 (char-equal ch ?\n))
414 (if (not whitespace)
415 (setq whitespace ? ))
416 (setq whitespace nil) ch))
417 guess))))))
418
419 (defun info-lookup-guess-default* (topic mode)
420 (let ((case-fold-search (info-lookup->ignore-case topic mode))
421 (rule (or (info-lookup->parse-rule topic mode)
422 (info-lookup->regexp topic mode)))
423 (start (point)) end regexp subexp result)
424 (if (symbolp rule)
425 (setq result (funcall rule))
426 (if (consp rule)
427 (setq regexp (car rule)
428 subexp (cdr rule))
429 (setq regexp rule
430 subexp 0))
431 (skip-chars-backward " \t\n") (setq end (point))
432 (while (and (re-search-backward regexp nil t)
433 (looking-at regexp)
434 (>= (match-end 0) end))
435 (setq result (match-string subexp)))
436 (if (not result)
437 (progn
438 (goto-char start)
439 (skip-chars-forward " \t\n")
440 (and (looking-at regexp)
441 (setq result (match-string subexp))))))
442 result))
443
444 (defun info-lookup-guess-c-symbol ()
445 "Get the C symbol at point."
446 (condition-case nil
447 (progn
448 (backward-sexp)
449 (let ((start (point)) prefix name)
450 ;; Test for a leading `struct', `union', or `enum' keyword
451 ;; but ignore names like `foo_struct'.
452 (setq prefix (and (< (skip-chars-backward " \t\n") 0)
453 (< (skip-chars-backward "_a-zA-Z0-9") 0)
454 (looking-at "\\(struct\\|union\\|enum\\)\\s ")
455 (concat (match-string 1) " ")))
456 (goto-char start)
457 (and (looking-at "[_a-zA-Z][_a-zA-Z0-9]*")
458 (setq name (match-string 0)))
459 ;; Caveat! Look forward if point is at `struct' etc.
460 (and (not prefix)
461 (or (string-equal name "struct")
462 (string-equal name "union")
463 (string-equal name "enum"))
464 (looking-at "[a-z]+\\s +\\([_a-zA-Z][_a-zA-Z0-9]*\\)")
465 (setq prefix (concat name " ")
466 name (match-string 1)))
467 (and (or prefix name)
468 (concat prefix name))))
469 (error nil)))
470
471 ;;;###autoload
472 (defun info-complete-symbol (&optional mode)
473 "Perform completion on symbol preceding point."
474 (interactive)
475 (info-complete 'symbol
476 (or mode
477 (if (info-lookup->mode-value
478 'symbol (or info-lookup-mode major-mode))
479 (or info-lookup-mode major-mode)
480 (info-lookup-change-mode 'symbol)))))
481
482 ;;;###autoload
483 (defun info-complete-file (&optional mode)
484 "Perform completion on file preceding point."
485 (interactive
486 (list (if (info-lookup->mode-value
487 'file (or info-lookup-mode major-mode))
488 (or info-lookup-mode major-mode)
489 (info-lookup-change-mode 'file))))
490 (info-complete 'file mode))
491
492 (defun info-complete (topic mode)
493 "Try to complete a help item."
494 (barf-if-buffer-read-only)
495 (if (not mode)
496 (setq mode (or info-lookup-mode major-mode)))
497 (or (info-lookup->mode-value topic mode)
498 (error "No %s completion available for `%s'" topic mode))
499 (let ((modes (info-lookup->all-modes topic mode))
500 (start (point)) try completion)
501 (while (and (not try) modes)
502 (setq mode (car modes)
503 modes (cdr modes)
504 try (info-lookup-guess-default* topic mode))
505 (goto-char start))
506 (and (not try)
507 (error "Found no %s to complete" topic))
508 (setq completion (try-completion
509 try (info-lookup->completions topic mode)))
510 (cond ((not completion)
511 (ding))
512 ((stringp completion)
513 (delete-region (- start (length try)) start)
514 (insert completion)))))
515
516 (provide 'info-look)
517
518 ;;; info-look.el ends here