Commit | Line | Data |
---|---|---|
5a79736d RS |
1 | ;;; info-look.el --- major-mode-sensitive Info index lookup facility. |
2 | ;; An older version of this was known as libc.el. | |
3 | ||
1d010333 | 4 | ;; Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. |
5a79736d RS |
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) | |
1eff0ba1 RS |
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 | "\\>")) | |
5a79736d RS |
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)))) | |
6bc7c7a2 RS |
197 | "`" "'"))) |
198 | (awk-mode ;;; Added by Peter Galbraith <galbraith@mixing.qc.dfo.ca> | |
199 | "[_a-zA-Z]+" | |
200 | nil ; Don't ignore-case | |
201 | (("(gawk)Index" ; info node | |
202 | (lambda (item) ; TRANS-FUNC index-entry-->help-item | |
203 | ;; In awk-mode, we want entries like: | |
204 | ;; * BEGIN special pattern: BEGIN/END. | |
205 | ;; * break statement: Break Statement. | |
206 | ;; * FS: Field Separators. | |
207 | ;; * gsub: String Functions. | |
208 | ;; * system: I/O Functions. | |
209 | ;; * systime: Time Functions. | |
210 | ;; | |
211 | ;; But not: | |
212 | ;; * time of day: Time Functions. | |
213 | ;; ^^^^^^^^^^^ More than one word. | |
214 | ;; | |
215 | ;; However, info-look's info-lookup-make-completions doesn't pass | |
216 | ;; the whole line to the TRANS-FUNC, but only up to the first | |
217 | ;; colon. So I can't use all the available info to decide what to | |
218 | ;; keep. Therefore, I have to `set-buffer' to the *info* buffer. | |
219 | ;; | |
220 | ;; Also, info-look offers no way to add non-indexed entries like | |
221 | ;; `cos' and other gawk Numeric Built-in Functions (or does it?) | |
222 | ;; as in ftp://ftp.phys.ocean.dal.ca/users/rhogee/elisp/func-doc.el | |
223 | ;; and http://www.ifi.uio.no/~jensthi/word-help.el (which adds a | |
224 | ;; heap of stuff for latex!) | |
225 | (let ((case-fold-search nil)) | |
226 | (cond | |
227 | ((string-match "\\([^ ]+\\) *\\(special pattern\\|statement\\)$" | |
228 | item) | |
229 | (match-string 1 item)) | |
230 | ((string-match "^[A-Z]+$" item) ;This will grab FS and the like. | |
231 | item) | |
232 | ((string-match "^[a-z]+$" item) | |
233 | (save-excursion | |
234 | (set-buffer "*info*") | |
235 | (if (looking-at " *\\(String\\|I/O\\|Time\\) Functions") | |
236 | item)))))) | |
237 | "`" "'"))) ;Append PREFIX and SUFFIX to finetune | |
238 | ; displayed location in Info node. | |
239 | ;; Perl -- Added by Peter Galbraith <galbraith@mixing.qc.dfo.ca> | |
240 | ;; Perl Version 5 Info files are available in CPAN sites, at: | |
241 | ;; http://www.perl.com/CPAN/doc/manual/info/ | |
242 | ;; | |
243 | ;; ftp://ftp.funet.fi/pub/languages/perl/CPAN/doc/manual/texinfo/ | |
244 | ;; ftp://ftp.pasteur.fr/pub/computing/unix/perl/CPAN/doc/manual/texinfo/ | |
245 | ;; ftp://mango.softwords.bc.ca/pub/perl/CPAN/doc/manual/texinfo/ | |
246 | ;; ftp://uiarchive.cso.uiuc.edu/pub/lang/perl/CPAN/doc/manual/texinfo/ | |
247 | (perl-mode | |
248 | "$?[^ \n\t{(]+" | |
249 | nil ; Don't ignore-case | |
250 | (("(perl5)Function Index" ; info node | |
251 | (lambda (item) ; TRANS-FUNC index-entry-->help-item | |
252 | (if (string-match "^\\([a-z0-9]+\\)" item) | |
253 | (match-string 1 item))) | |
254 | "^" nil) | |
255 | ("(perl5)Variable Index" ; info node | |
256 | (lambda (item) ; TRANS-FUNC index-entry-->help-item | |
257 | (if (string-match "^\\([^ \n\t{(]+\\)" item) ;First word | |
258 | (match-string 1 item))) | |
259 | "^" nil))) | |
260 | ;; LaTeX -- Added by Peter Galbraith <galbraith@mixing.qc.dfo.ca> | |
261 | ;; Info file available at: | |
262 | ;; ftp://ftp.dante.de:pub/tex/info/latex2e-help-texinfo/latex2e.texi | |
263 | (latex-mode "[\\a-zA-Z]+" nil (("(latex)Command Index" nil "`" nil))) | |
264 | (emacs-lisp-mode | |
265 | "[^][ ()\n\t.\"'#]+" nil | |
266 | (("(elisp)Index" | |
267 | (lambda (item) | |
268 | (if (string-match "[^ ]+" item) ;First word | |
269 | (match-string 0 item))) | |
270 | "" "") | |
271 | ("(emacs)Command Index" | |
272 | (lambda (item) | |
273 | (if (string-match "[^ ]+" item) ;First word | |
274 | (match-string 0 item))) | |
275 | "" "")))) | |
5a79736d RS |
276 | "*Alist of help specifications for symbol names. |
277 | See the documentation of the variable `info-lookup-alist' for more details.") | |
278 | ||
279 | (defvar info-lookup-file-alist | |
280 | '((c-mode | |
281 | "[_a-zA-Z0-9./+-]+" nil | |
282 | (("(libc)File Index")))) | |
283 | "*Alist of help specifications for file names. | |
284 | See the documentation of the variable `info-lookup-alist' for more details.") | |
285 | ||
286 | ;;;###autoload | |
287 | (defun info-lookup-reset () | |
288 | "Throw away all cached data. | |
289 | This command is useful if the user wants to start at the beginning without | |
290 | quitting Emacs, for example, after some Info documents were updated on the | |
291 | system." | |
292 | (interactive) | |
293 | (setq info-lookup-cache nil)) | |
294 | ||
295 | ;;;###autoload | |
296 | (defun info-lookup-symbol (symbol &optional mode) | |
297 | "Display the documentation of a symbol. | |
298 | If called interactively, SYMBOL will be read from the mini-buffer. | |
299 | Prefix argument means unconditionally insert the default symbol name | |
300 | into the mini-buffer so that it can be edited. | |
301 | The default symbol is the one found at point." | |
302 | (interactive | |
303 | (info-lookup-interactive-arguments 'symbol)) | |
304 | (info-lookup 'symbol symbol mode)) | |
305 | ||
306 | ;;;###autoload | |
307 | (defun info-lookup-file (file &optional mode) | |
308 | "Display the documentation of a file. | |
309 | If called interactively, FILE will be read from the mini-buffer. | |
310 | Prefix argument means unconditionally insert the default file name | |
311 | into the mini-buffer so that it can be edited. | |
312 | The default file name is the one found at point." | |
313 | (interactive | |
314 | (info-lookup-interactive-arguments 'file)) | |
315 | (info-lookup 'file file mode)) | |
316 | ||
317 | (defun info-lookup-interactive-arguments (topic) | |
318 | "Return default value and help mode for help topic TOPIC." | |
319 | (let* ((mode (if (info-lookup->mode-value | |
320 | topic (or info-lookup-mode major-mode)) | |
321 | (or info-lookup-mode major-mode) | |
322 | (info-lookup-change-mode topic))) | |
323 | (completions (info-lookup->completions topic mode)) | |
324 | (default (info-lookup-guess-default topic mode)) | |
325 | (input (if (or current-prefix-arg (not (assoc default completions))) | |
326 | default)) | |
327 | (completion-ignore-case (info-lookup->ignore-case topic mode)) | |
328 | (enable-recursive-minibuffers t) | |
329 | (value (completing-read | |
330 | (if (and default (not input)) | |
331 | (format "Describe %s (default %s): " topic default) | |
332 | (format "Describe %s: " topic)) | |
333 | completions nil nil input 'info-lookup-history))) | |
334 | (list (if (equal value "") default value) mode))) | |
335 | ||
336 | (defun info-lookup-change-mode (topic) | |
337 | (let* ((completions (mapcar (lambda (arg) | |
338 | (cons (symbol-name (car arg)) (car arg))) | |
339 | (info-lookup->topic-value topic))) | |
340 | (mode (completing-read | |
341 | (format "Use %s help mode: " topic) | |
342 | completions nil t nil 'info-lookup-history))) | |
343 | (or (setq mode (cdr (assoc mode completions))) | |
344 | (error "No %s help available" topic)) | |
345 | (or (info-lookup->mode-value topic mode) | |
346 | (error "No %s help available for `%s'" topic mode)) | |
347 | (setq info-lookup-mode mode))) | |
348 | ||
349 | (defun info-lookup (topic item mode) | |
350 | "Display the documentation of a help item." | |
351 | (if (not mode) | |
352 | (setq mode (or info-lookup-mode major-mode))) | |
353 | (or (info-lookup->mode-value topic mode) | |
354 | (error "No %s help available for `%s'" topic mode)) | |
355 | (let ((entry (or (assoc (if (info-lookup->ignore-case topic mode) | |
356 | (downcase item) item) | |
357 | (info-lookup->completions topic mode)) | |
358 | (error "Not documented as a %s: %s" topic (or item "")))) | |
359 | (modes (info-lookup->all-modes topic mode)) | |
360 | (window (selected-window)) | |
1d010333 | 361 | found doc-spec node prefix suffix doc-found) |
5a79736d RS |
362 | (if (not info-lookup-other-window-flag) |
363 | (info) | |
364 | (save-window-excursion (info)) | |
365 | (switch-to-buffer-other-window "*info*")) | |
366 | (while (and (not found) modes) | |
367 | (setq doc-spec (info-lookup->doc-spec topic (car modes))) | |
368 | (while (and (not found) doc-spec) | |
369 | (setq node (nth 0 (car doc-spec)) | |
370 | prefix (nth 2 (car doc-spec)) | |
371 | suffix (nth 3 (car doc-spec))) | |
1d010333 RS |
372 | (when (condition-case error-data |
373 | (progn | |
374 | (Info-goto-node node) | |
375 | (setq doc-found t)) | |
376 | (error | |
377 | (message "Cannot access Info node %s" node) | |
378 | (sit-for 1) | |
379 | nil)) | |
380 | (condition-case nil | |
381 | (progn | |
382 | (Info-menu (or (cdr entry) item)) | |
383 | (setq found t) | |
384 | (if (or prefix suffix) | |
385 | (let ((case-fold-search | |
386 | (info-lookup->ignore-case topic (car modes))) | |
387 | (buffer-read-only nil)) | |
388 | (goto-char (point-min)) | |
389 | (re-search-forward | |
390 | (concat prefix (regexp-quote item) suffix)) | |
391 | (goto-char (match-beginning 0)) | |
392 | (and window-system info-lookup-highlight-face | |
393 | ;; Search again for ITEM so that the first | |
394 | ;; occurence of ITEM will be highlighted. | |
395 | (re-search-forward (regexp-quote item)) | |
396 | (let ((start (match-beginning 0)) | |
397 | (end (match-end 0))) | |
398 | (if (overlayp info-lookup-highlight-overlay) | |
399 | (move-overlay info-lookup-highlight-overlay | |
400 | start end (current-buffer)) | |
401 | (setq info-lookup-highlight-overlay | |
402 | (make-overlay start end)))) | |
403 | (overlay-put info-lookup-highlight-overlay | |
404 | 'face info-lookup-highlight-face))))) | |
405 | (error nil))) | |
5a79736d RS |
406 | (setq doc-spec (cdr doc-spec))) |
407 | (setq modes (cdr modes))) | |
1d010333 RS |
408 | (or doc-found |
409 | (error "Info documentation for lookup was not found")) | |
5a79736d RS |
410 | ;; Don't leave the Info buffer if the help item couldn't be looked up. |
411 | (if (and info-lookup-other-window-flag found) | |
412 | (select-window window)))) | |
413 | ||
414 | (defun info-lookup-setup-mode (topic mode) | |
415 | "Initialize the internal data structure." | |
416 | (or (info-lookup->initialized topic mode) | |
417 | (let (cell data (initialized 0) completions refer-modes) | |
418 | (if (not (info-lookup->mode-value topic mode)) | |
419 | (message "No %s help available for `%s'" topic mode) | |
420 | ;; Recursively setup cross references. | |
421 | ;; But refer only to non-void modes. | |
422 | (mapcar (lambda (arg) | |
423 | (or (info-lookup->initialized topic arg) | |
424 | (info-lookup-setup-mode topic arg)) | |
425 | (and (eq (info-lookup->initialized topic arg) t) | |
426 | (setq refer-modes (cons arg refer-modes)))) | |
427 | (info-lookup->other-modes topic mode)) | |
428 | (setq refer-modes (nreverse refer-modes)) | |
429 | ;; Build the full completion alist. | |
430 | (setq completions | |
431 | (nconc (info-lookup-make-completions topic mode) | |
432 | (apply 'append | |
433 | (mapcar (lambda (arg) | |
434 | (info-lookup->completions topic arg)) | |
435 | refer-modes)))) | |
436 | (setq initialized t)) | |
437 | ;; Update `info-lookup-cache'. | |
438 | (setq cell (info-lookup->mode-cache topic mode) | |
439 | data (list initialized completions refer-modes)) | |
440 | (if (not cell) | |
441 | (setcdr (info-lookup->cache topic) | |
442 | (cons (cons mode data) (info-lookup->topic-cache topic))) | |
443 | (setcdr cell data)) | |
444 | initialized))) | |
445 | ||
446 | (defun info-lookup-make-completions (topic mode) | |
447 | "Create a unique alist from all index entries." | |
1eff0ba1 RS |
448 | (let ((doc-spec (info-lookup->doc-spec topic mode)) |
449 | (regexp (concat "^\\(" (info-lookup->regexp topic mode) | |
450 | "\\)\\([ \t].*\\)?$")) | |
1d010333 | 451 | node trans entry item prefix result doc-found |
1eff0ba1 RS |
452 | (buffer (get-buffer-create " temp-info-look"))) |
453 | (with-current-buffer buffer | |
454 | (Info-mode)) | |
455 | (while doc-spec | |
456 | (setq node (nth 0 (car doc-spec)) | |
457 | trans (cond ((eq (nth 1 (car doc-spec)) nil) | |
458 | (lambda (arg) | |
459 | (if (string-match regexp arg) | |
460 | (match-string 1 arg)))) | |
461 | ((stringp (nth 1 (car doc-spec))) | |
462 | (setq prefix (nth 1 (car doc-spec))) | |
463 | (lambda (arg) | |
464 | (if (string-match "^\\([^: \t\n]+\\)" arg) | |
465 | (concat prefix (match-string 1 arg))))) | |
466 | (t (nth 1 (car doc-spec))))) | |
1d010333 RS |
467 | (with-current-buffer buffer |
468 | (message "Processing Info node `%s'..." node) | |
469 | (when (condition-case error-data | |
470 | (progn | |
471 | (Info-goto-node node) | |
472 | (setq doc-found t)) | |
473 | (error | |
474 | (message "Cannot access Info node `%s'" node) | |
475 | (sit-for 1) | |
476 | nil)) | |
477 | (condition-case nil | |
478 | (progn | |
479 | (goto-char (point-min)) | |
480 | (and (search-forward "\n* Menu:" nil t) | |
481 | (while (re-search-forward "\n\\* \\([^:\t\n]*\\):" nil t) | |
482 | (setq entry (match-string 1) | |
483 | item (funcall trans entry)) | |
484 | (and (info-lookup->ignore-case topic mode) | |
485 | (setq item (downcase item))) | |
486 | (and (string-equal entry item) | |
487 | (setq entry nil)) | |
488 | (or (assoc item result) | |
489 | (setq result (cons (cons item entry) result)))))) | |
490 | (error nil)))) | |
1eff0ba1 RS |
491 | (message "Processing Info node `%s'...done" node) |
492 | (setq doc-spec (cdr doc-spec))) | |
1d010333 RS |
493 | (or doc-found |
494 | (error "Info documentation for lookup was not found")) | |
1eff0ba1 | 495 | result)) |
5a79736d RS |
496 | |
497 | (defun info-lookup-guess-default (topic mode) | |
498 | "Pick up default item at point (with favor to look back). | |
499 | Return nil if there is nothing appropriate." | |
500 | (let ((modes (info-lookup->all-modes topic mode)) | |
501 | (start (point)) guess whitespace) | |
502 | (while (and (not guess) modes) | |
503 | (setq guess (info-lookup-guess-default* topic (car modes)) | |
504 | modes (cdr modes)) | |
505 | (goto-char start)) | |
506 | ;; Collapse whitespace characters. | |
507 | (and guess (concat (delete nil (mapcar (lambda (ch) | |
508 | (if (or (char-equal ch ? ) | |
509 | (char-equal ch ?\t) | |
510 | (char-equal ch ?\n)) | |
511 | (if (not whitespace) | |
512 | (setq whitespace ? )) | |
513 | (setq whitespace nil) ch)) | |
514 | guess)))))) | |
515 | ||
516 | (defun info-lookup-guess-default* (topic mode) | |
517 | (let ((case-fold-search (info-lookup->ignore-case topic mode)) | |
518 | (rule (or (info-lookup->parse-rule topic mode) | |
519 | (info-lookup->regexp topic mode))) | |
520 | (start (point)) end regexp subexp result) | |
521 | (if (symbolp rule) | |
522 | (setq result (funcall rule)) | |
523 | (if (consp rule) | |
524 | (setq regexp (car rule) | |
525 | subexp (cdr rule)) | |
526 | (setq regexp rule | |
527 | subexp 0)) | |
528 | (skip-chars-backward " \t\n") (setq end (point)) | |
529 | (while (and (re-search-backward regexp nil t) | |
530 | (looking-at regexp) | |
531 | (>= (match-end 0) end)) | |
532 | (setq result (match-string subexp))) | |
533 | (if (not result) | |
534 | (progn | |
535 | (goto-char start) | |
536 | (skip-chars-forward " \t\n") | |
537 | (and (looking-at regexp) | |
538 | (setq result (match-string subexp)))))) | |
539 | result)) | |
540 | ||
541 | (defun info-lookup-guess-c-symbol () | |
542 | "Get the C symbol at point." | |
543 | (condition-case nil | |
544 | (progn | |
545 | (backward-sexp) | |
546 | (let ((start (point)) prefix name) | |
547 | ;; Test for a leading `struct', `union', or `enum' keyword | |
548 | ;; but ignore names like `foo_struct'. | |
549 | (setq prefix (and (< (skip-chars-backward " \t\n") 0) | |
550 | (< (skip-chars-backward "_a-zA-Z0-9") 0) | |
551 | (looking-at "\\(struct\\|union\\|enum\\)\\s ") | |
552 | (concat (match-string 1) " "))) | |
553 | (goto-char start) | |
554 | (and (looking-at "[_a-zA-Z][_a-zA-Z0-9]*") | |
555 | (setq name (match-string 0))) | |
556 | ;; Caveat! Look forward if point is at `struct' etc. | |
557 | (and (not prefix) | |
558 | (or (string-equal name "struct") | |
559 | (string-equal name "union") | |
560 | (string-equal name "enum")) | |
561 | (looking-at "[a-z]+\\s +\\([_a-zA-Z][_a-zA-Z0-9]*\\)") | |
562 | (setq prefix (concat name " ") | |
563 | name (match-string 1))) | |
564 | (and (or prefix name) | |
565 | (concat prefix name)))) | |
566 | (error nil))) | |
567 | ||
568 | ;;;###autoload | |
569 | (defun info-complete-symbol (&optional mode) | |
570 | "Perform completion on symbol preceding point." | |
82fb111c RS |
571 | (interactive) |
572 | (info-complete 'symbol | |
573 | (or mode | |
574 | (if (info-lookup->mode-value | |
575 | 'symbol (or info-lookup-mode major-mode)) | |
576 | (or info-lookup-mode major-mode) | |
577 | (info-lookup-change-mode 'symbol))))) | |
5a79736d RS |
578 | |
579 | ;;;###autoload | |
580 | (defun info-complete-file (&optional mode) | |
581 | "Perform completion on file preceding point." | |
582 | (interactive | |
583 | (list (if (info-lookup->mode-value | |
584 | 'file (or info-lookup-mode major-mode)) | |
585 | (or info-lookup-mode major-mode) | |
586 | (info-lookup-change-mode 'file)))) | |
587 | (info-complete 'file mode)) | |
588 | ||
589 | (defun info-complete (topic mode) | |
590 | "Try to complete a help item." | |
591 | (barf-if-buffer-read-only) | |
592 | (if (not mode) | |
593 | (setq mode (or info-lookup-mode major-mode))) | |
594 | (or (info-lookup->mode-value topic mode) | |
595 | (error "No %s completion available for `%s'" topic mode)) | |
596 | (let ((modes (info-lookup->all-modes topic mode)) | |
6bc7c7a2 RS |
597 | (start (point)) |
598 | (completion-list (info-lookup->completions topic mode)) | |
599 | try completion) | |
5a79736d RS |
600 | (while (and (not try) modes) |
601 | (setq mode (car modes) | |
602 | modes (cdr modes) | |
603 | try (info-lookup-guess-default* topic mode)) | |
604 | (goto-char start)) | |
605 | (and (not try) | |
606 | (error "Found no %s to complete" topic)) | |
6bc7c7a2 | 607 | (setq completion (try-completion try completion-list)) |
5a79736d | 608 | (cond ((not completion) |
6bc7c7a2 | 609 | (message "No %s match" topic) |
5a79736d RS |
610 | (ding)) |
611 | ((stringp completion) | |
612 | (delete-region (- start (length try)) start) | |
6bc7c7a2 RS |
613 | (insert completion) |
614 | (if (or (string-equal try completion) | |
615 | (and (boundp 'info-look-completion) | |
616 | info-look-completion | |
617 | (= (point) (car info-look-completion)) | |
618 | (equal completion (car (cdr info-look-completion))))) | |
619 | ;; Show completion list | |
620 | (let ((list (all-completions completion completion-list))) | |
621 | (with-output-to-temp-buffer "*Completions*" | |
622 | (display-completion-list list)) | |
623 | (if (member completion list) | |
624 | (message "Complete but not unique")))) | |
625 | (setq info-look-completion (list (point) completion))) | |
626 | (t | |
627 | (message "%s is complete" topic))))) | |
628 | ||
629 | (defvar info-look-completion nil | |
630 | "info-look cache for last completion and point to display completion or not") | |
631 | (make-variable-buffer-local 'info-look-completion) | |
5a79736d RS |
632 | |
633 | (provide 'info-look) | |
634 | ||
635 | ;;; info-look.el ends here |