Mercurial > emacs
annotate lisp/info-look.el @ 20690:a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
(info-lookup-make-completions): Likewise.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Tue, 20 Jan 1998 20:09:39 +0000 |
parents | c7b3ef0ed1ad |
children | d1f6ac340403 |
rev | line source |
---|---|
18322 | 1 ;;; info-look.el --- major-mode-sensitive Info index lookup facility. |
2 ;; An older version of this was known as libc.el. | |
3 | |
20690
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
4 ;; Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. |
18322 | 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) | |
20519
c7b3ef0ed1ad
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
Richard M. Stallman <rms@gnu.org>
parents:
18323
diff
changeset
|
176 (emacs-lisp-mode |
c7b3ef0ed1ad
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
Richard M. Stallman <rms@gnu.org>
parents:
18323
diff
changeset
|
177 "[-_a-zA-Z+=*:&%$#@!^~][-_a-zA-Z0-9+=*:&%$#@!^~]*" nil |
c7b3ef0ed1ad
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
Richard M. Stallman <rms@gnu.org>
parents:
18323
diff
changeset
|
178 ("(elisp)Index" nil |
c7b3ef0ed1ad
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
Richard M. Stallman <rms@gnu.org>
parents:
18323
diff
changeset
|
179 "^[ \t]+- \\(Function\\|Macro\\|User Option\\|Variable\\): .*\\<" |
c7b3ef0ed1ad
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
Richard M. Stallman <rms@gnu.org>
parents:
18323
diff
changeset
|
180 "\\>")) |
18322 | 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)) | |
20690
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
283 found doc-spec node prefix suffix doc-found) |
18322 | 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))) | |
20690
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
294 (when (condition-case error-data |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
295 (progn |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
296 (Info-goto-node node) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
297 (setq doc-found t)) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
298 (error |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
299 (message "Cannot access Info node %s" node) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
300 (sit-for 1) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
301 nil)) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
302 (condition-case nil |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
303 (progn |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
304 (Info-menu (or (cdr entry) item)) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
305 (setq found t) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
306 (if (or prefix suffix) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
307 (let ((case-fold-search |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
308 (info-lookup->ignore-case topic (car modes))) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
309 (buffer-read-only nil)) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
310 (goto-char (point-min)) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
311 (re-search-forward |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
312 (concat prefix (regexp-quote item) suffix)) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
313 (goto-char (match-beginning 0)) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
314 (and window-system info-lookup-highlight-face |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
315 ;; Search again for ITEM so that the first |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
316 ;; occurence of ITEM will be highlighted. |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
317 (re-search-forward (regexp-quote item)) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
318 (let ((start (match-beginning 0)) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
319 (end (match-end 0))) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
320 (if (overlayp info-lookup-highlight-overlay) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
321 (move-overlay info-lookup-highlight-overlay |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
322 start end (current-buffer)) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
323 (setq info-lookup-highlight-overlay |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
324 (make-overlay start end)))) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
325 (overlay-put info-lookup-highlight-overlay |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
326 'face info-lookup-highlight-face))))) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
327 (error nil))) |
18322 | 328 (setq doc-spec (cdr doc-spec))) |
329 (setq modes (cdr modes))) | |
20690
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
330 (or doc-found |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
331 (error "Info documentation for lookup was not found")) |
18322 | 332 ;; Don't leave the Info buffer if the help item couldn't be looked up. |
333 (if (and info-lookup-other-window-flag found) | |
334 (select-window window)))) | |
335 | |
336 (defun info-lookup-setup-mode (topic mode) | |
337 "Initialize the internal data structure." | |
338 (or (info-lookup->initialized topic mode) | |
339 (let (cell data (initialized 0) completions refer-modes) | |
340 (if (not (info-lookup->mode-value topic mode)) | |
341 (message "No %s help available for `%s'" topic mode) | |
342 ;; Recursively setup cross references. | |
343 ;; But refer only to non-void modes. | |
344 (mapcar (lambda (arg) | |
345 (or (info-lookup->initialized topic arg) | |
346 (info-lookup-setup-mode topic arg)) | |
347 (and (eq (info-lookup->initialized topic arg) t) | |
348 (setq refer-modes (cons arg refer-modes)))) | |
349 (info-lookup->other-modes topic mode)) | |
350 (setq refer-modes (nreverse refer-modes)) | |
351 ;; Build the full completion alist. | |
352 (setq completions | |
353 (nconc (info-lookup-make-completions topic mode) | |
354 (apply 'append | |
355 (mapcar (lambda (arg) | |
356 (info-lookup->completions topic arg)) | |
357 refer-modes)))) | |
358 (setq initialized t)) | |
359 ;; Update `info-lookup-cache'. | |
360 (setq cell (info-lookup->mode-cache topic mode) | |
361 data (list initialized completions refer-modes)) | |
362 (if (not cell) | |
363 (setcdr (info-lookup->cache topic) | |
364 (cons (cons mode data) (info-lookup->topic-cache topic))) | |
365 (setcdr cell data)) | |
366 initialized))) | |
367 | |
368 (defun info-lookup-make-completions (topic mode) | |
369 "Create a unique alist from all index entries." | |
20519
c7b3ef0ed1ad
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
Richard M. Stallman <rms@gnu.org>
parents:
18323
diff
changeset
|
370 (let ((doc-spec (info-lookup->doc-spec topic mode)) |
c7b3ef0ed1ad
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
Richard M. Stallman <rms@gnu.org>
parents:
18323
diff
changeset
|
371 (regexp (concat "^\\(" (info-lookup->regexp topic mode) |
c7b3ef0ed1ad
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
Richard M. Stallman <rms@gnu.org>
parents:
18323
diff
changeset
|
372 "\\)\\([ \t].*\\)?$")) |
20690
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
373 node trans entry item prefix result doc-found |
20519
c7b3ef0ed1ad
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
Richard M. Stallman <rms@gnu.org>
parents:
18323
diff
changeset
|
374 (buffer (get-buffer-create " temp-info-look"))) |
c7b3ef0ed1ad
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
Richard M. Stallman <rms@gnu.org>
parents:
18323
diff
changeset
|
375 (with-current-buffer buffer |
c7b3ef0ed1ad
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
Richard M. Stallman <rms@gnu.org>
parents:
18323
diff
changeset
|
376 (Info-mode)) |
c7b3ef0ed1ad
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
Richard M. Stallman <rms@gnu.org>
parents:
18323
diff
changeset
|
377 (while doc-spec |
c7b3ef0ed1ad
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
Richard M. Stallman <rms@gnu.org>
parents:
18323
diff
changeset
|
378 (setq node (nth 0 (car doc-spec)) |
c7b3ef0ed1ad
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
Richard M. Stallman <rms@gnu.org>
parents:
18323
diff
changeset
|
379 trans (cond ((eq (nth 1 (car doc-spec)) nil) |
c7b3ef0ed1ad
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
Richard M. Stallman <rms@gnu.org>
parents:
18323
diff
changeset
|
380 (lambda (arg) |
c7b3ef0ed1ad
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
Richard M. Stallman <rms@gnu.org>
parents:
18323
diff
changeset
|
381 (if (string-match regexp arg) |
c7b3ef0ed1ad
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
Richard M. Stallman <rms@gnu.org>
parents:
18323
diff
changeset
|
382 (match-string 1 arg)))) |
c7b3ef0ed1ad
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
Richard M. Stallman <rms@gnu.org>
parents:
18323
diff
changeset
|
383 ((stringp (nth 1 (car doc-spec))) |
c7b3ef0ed1ad
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
Richard M. Stallman <rms@gnu.org>
parents:
18323
diff
changeset
|
384 (setq prefix (nth 1 (car doc-spec))) |
c7b3ef0ed1ad
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
Richard M. Stallman <rms@gnu.org>
parents:
18323
diff
changeset
|
385 (lambda (arg) |
c7b3ef0ed1ad
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
Richard M. Stallman <rms@gnu.org>
parents:
18323
diff
changeset
|
386 (if (string-match "^\\([^: \t\n]+\\)" arg) |
c7b3ef0ed1ad
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
Richard M. Stallman <rms@gnu.org>
parents:
18323
diff
changeset
|
387 (concat prefix (match-string 1 arg))))) |
c7b3ef0ed1ad
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
Richard M. Stallman <rms@gnu.org>
parents:
18323
diff
changeset
|
388 (t (nth 1 (car doc-spec))))) |
20690
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
389 (with-current-buffer buffer |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
390 (message "Processing Info node `%s'..." node) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
391 (when (condition-case error-data |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
392 (progn |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
393 (Info-goto-node node) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
394 (setq doc-found t)) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
395 (error |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
396 (message "Cannot access Info node `%s'" node) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
397 (sit-for 1) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
398 nil)) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
399 (condition-case nil |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
400 (progn |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
401 (goto-char (point-min)) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
402 (and (search-forward "\n* Menu:" nil t) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
403 (while (re-search-forward "\n\\* \\([^:\t\n]*\\):" nil t) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
404 (setq entry (match-string 1) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
405 item (funcall trans entry)) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
406 (and (info-lookup->ignore-case topic mode) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
407 (setq item (downcase item))) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
408 (and (string-equal entry item) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
409 (setq entry nil)) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
410 (or (assoc item result) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
411 (setq result (cons (cons item entry) result)))))) |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
412 (error nil)))) |
20519
c7b3ef0ed1ad
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
Richard M. Stallman <rms@gnu.org>
parents:
18323
diff
changeset
|
413 (message "Processing Info node `%s'...done" node) |
c7b3ef0ed1ad
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
Richard M. Stallman <rms@gnu.org>
parents:
18323
diff
changeset
|
414 (setq doc-spec (cdr doc-spec))) |
20690
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
415 (or doc-found |
a51c71dd5cda
(info-lookup): Report if Info-goto-node fails.
Richard M. Stallman <rms@gnu.org>
parents:
20519
diff
changeset
|
416 (error "Info documentation for lookup was not found")) |
20519
c7b3ef0ed1ad
(info-lookup-symbol-alist): Handle Emacs Lisp mode.
Richard M. Stallman <rms@gnu.org>
parents:
18323
diff
changeset
|
417 result)) |
18322 | 418 |
419 (defun info-lookup-guess-default (topic mode) | |
420 "Pick up default item at point (with favor to look back). | |
421 Return nil if there is nothing appropriate." | |
422 (let ((modes (info-lookup->all-modes topic mode)) | |
423 (start (point)) guess whitespace) | |
424 (while (and (not guess) modes) | |
425 (setq guess (info-lookup-guess-default* topic (car modes)) | |
426 modes (cdr modes)) | |
427 (goto-char start)) | |
428 ;; Collapse whitespace characters. | |
429 (and guess (concat (delete nil (mapcar (lambda (ch) | |
430 (if (or (char-equal ch ? ) | |
431 (char-equal ch ?\t) | |
432 (char-equal ch ?\n)) | |
433 (if (not whitespace) | |
434 (setq whitespace ? )) | |
435 (setq whitespace nil) ch)) | |
436 guess)))))) | |
437 | |
438 (defun info-lookup-guess-default* (topic mode) | |
439 (let ((case-fold-search (info-lookup->ignore-case topic mode)) | |
440 (rule (or (info-lookup->parse-rule topic mode) | |
441 (info-lookup->regexp topic mode))) | |
442 (start (point)) end regexp subexp result) | |
443 (if (symbolp rule) | |
444 (setq result (funcall rule)) | |
445 (if (consp rule) | |
446 (setq regexp (car rule) | |
447 subexp (cdr rule)) | |
448 (setq regexp rule | |
449 subexp 0)) | |
450 (skip-chars-backward " \t\n") (setq end (point)) | |
451 (while (and (re-search-backward regexp nil t) | |
452 (looking-at regexp) | |
453 (>= (match-end 0) end)) | |
454 (setq result (match-string subexp))) | |
455 (if (not result) | |
456 (progn | |
457 (goto-char start) | |
458 (skip-chars-forward " \t\n") | |
459 (and (looking-at regexp) | |
460 (setq result (match-string subexp)))))) | |
461 result)) | |
462 | |
463 (defun info-lookup-guess-c-symbol () | |
464 "Get the C symbol at point." | |
465 (condition-case nil | |
466 (progn | |
467 (backward-sexp) | |
468 (let ((start (point)) prefix name) | |
469 ;; Test for a leading `struct', `union', or `enum' keyword | |
470 ;; but ignore names like `foo_struct'. | |
471 (setq prefix (and (< (skip-chars-backward " \t\n") 0) | |
472 (< (skip-chars-backward "_a-zA-Z0-9") 0) | |
473 (looking-at "\\(struct\\|union\\|enum\\)\\s ") | |
474 (concat (match-string 1) " "))) | |
475 (goto-char start) | |
476 (and (looking-at "[_a-zA-Z][_a-zA-Z0-9]*") | |
477 (setq name (match-string 0))) | |
478 ;; Caveat! Look forward if point is at `struct' etc. | |
479 (and (not prefix) | |
480 (or (string-equal name "struct") | |
481 (string-equal name "union") | |
482 (string-equal name "enum")) | |
483 (looking-at "[a-z]+\\s +\\([_a-zA-Z][_a-zA-Z0-9]*\\)") | |
484 (setq prefix (concat name " ") | |
485 name (match-string 1))) | |
486 (and (or prefix name) | |
487 (concat prefix name)))) | |
488 (error nil))) | |
489 | |
490 ;;;###autoload | |
491 (defun info-complete-symbol (&optional mode) | |
492 "Perform completion on symbol preceding point." | |
18323
64d728fb396a
(info-complete-symbol): If MODE is nil, use the default value.
Richard M. Stallman <rms@gnu.org>
parents:
18322
diff
changeset
|
493 (interactive) |
64d728fb396a
(info-complete-symbol): If MODE is nil, use the default value.
Richard M. Stallman <rms@gnu.org>
parents:
18322
diff
changeset
|
494 (info-complete 'symbol |
64d728fb396a
(info-complete-symbol): If MODE is nil, use the default value.
Richard M. Stallman <rms@gnu.org>
parents:
18322
diff
changeset
|
495 (or mode |
64d728fb396a
(info-complete-symbol): If MODE is nil, use the default value.
Richard M. Stallman <rms@gnu.org>
parents:
18322
diff
changeset
|
496 (if (info-lookup->mode-value |
64d728fb396a
(info-complete-symbol): If MODE is nil, use the default value.
Richard M. Stallman <rms@gnu.org>
parents:
18322
diff
changeset
|
497 'symbol (or info-lookup-mode major-mode)) |
64d728fb396a
(info-complete-symbol): If MODE is nil, use the default value.
Richard M. Stallman <rms@gnu.org>
parents:
18322
diff
changeset
|
498 (or info-lookup-mode major-mode) |
64d728fb396a
(info-complete-symbol): If MODE is nil, use the default value.
Richard M. Stallman <rms@gnu.org>
parents:
18322
diff
changeset
|
499 (info-lookup-change-mode 'symbol))))) |
18322 | 500 |
501 ;;;###autoload | |
502 (defun info-complete-file (&optional mode) | |
503 "Perform completion on file preceding point." | |
504 (interactive | |
505 (list (if (info-lookup->mode-value | |
506 'file (or info-lookup-mode major-mode)) | |
507 (or info-lookup-mode major-mode) | |
508 (info-lookup-change-mode 'file)))) | |
509 (info-complete 'file mode)) | |
510 | |
511 (defun info-complete (topic mode) | |
512 "Try to complete a help item." | |
513 (barf-if-buffer-read-only) | |
514 (if (not mode) | |
515 (setq mode (or info-lookup-mode major-mode))) | |
516 (or (info-lookup->mode-value topic mode) | |
517 (error "No %s completion available for `%s'" topic mode)) | |
518 (let ((modes (info-lookup->all-modes topic mode)) | |
519 (start (point)) try completion) | |
520 (while (and (not try) modes) | |
521 (setq mode (car modes) | |
522 modes (cdr modes) | |
523 try (info-lookup-guess-default* topic mode)) | |
524 (goto-char start)) | |
525 (and (not try) | |
526 (error "Found no %s to complete" topic)) | |
527 (setq completion (try-completion | |
528 try (info-lookup->completions topic mode))) | |
529 (cond ((not completion) | |
530 (ding)) | |
531 ((stringp completion) | |
532 (delete-region (- start (length try)) start) | |
533 (insert completion))))) | |
534 | |
535 (provide 'info-look) | |
536 | |
537 ;;; info-look.el ends here |