]> code.delx.au - gnu-emacs/blob - lisp/info-look.el
Merge from trunk; up to 2013-02-18T01:30:27Z!monnier@iro.umontreal.ca.
[gnu-emacs] / lisp / info-look.el
1 ;;; info-look.el --- major-mode-sensitive Info index lookup facility -*- lexical-binding: t -*-
2 ;; An older version of this was known as libc.el.
3
4 ;; Copyright (C) 1995-1999, 2001-2013 Free Software Foundation, Inc.
5
6 ;; Author: Ralph Schleicher <rs@nunatak.allgaeu.org>
7 ;; (did not show signs of life (Nov 2001) -stef)
8 ;; Keywords: help languages
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; Really cool code to lookup info indexes.
28 ;; Try especially info-lookup-symbol (aka C-h S).
29
30 ;;; Code:
31
32 (require 'info)
33
34 (defgroup info-lookup nil
35 "Major mode sensitive help agent."
36 :group 'help :group 'languages)
37
38 (defvar info-lookup-mode nil
39 "Symbol of the current buffer's help mode.
40 Help is provided according to the buffer's major mode if value is nil.
41 Automatically becomes buffer local when set in any fashion.")
42 (make-variable-buffer-local 'info-lookup-mode)
43
44 (defcustom info-lookup-other-window-flag t
45 "Non-nil means pop up the Info buffer in another window."
46 :group 'info-lookup :type 'boolean)
47
48 (defcustom info-lookup-highlight-face 'match
49 "Face for highlighting looked up help items.
50 Setting this variable to nil disables highlighting."
51 :group 'info-lookup :type 'face)
52
53 (defvar info-lookup-highlight-overlay nil
54 "Overlay object used for highlighting.")
55
56 (defcustom info-lookup-file-name-alist
57 '(("\\`ac\\(local\\|site\\|include\\)\\.m4\\'" . autoconf-mode))
58 "Alist of file names handled specially.
59 List elements are cons cells of the form
60
61 (REGEXP . MODE)
62
63 If a file name matches REGEXP, then use help mode MODE instead of the
64 buffer's major mode."
65 :group 'info-lookup :type '(repeat (cons (string :tag "Regexp")
66 (symbol :tag "Mode"))))
67
68 (defvar info-lookup-history nil
69 "History of previous input lines.")
70
71 (defvar info-lookup-alist nil
72 "Alist of known help topics.
73 Cons cells are of the form
74
75 (HELP-TOPIC . HELP-DATA)
76
77 HELP-TOPIC is the symbol of a help topic.
78 HELP-DATA is a HELP-TOPIC's public data set.
79 Value is an alist with elements of the form
80
81 (HELP-MODE REGEXP IGNORE-CASE DOC-SPEC PARSE-RULE OTHER-MODES)
82
83 HELP-MODE is a mode's symbol.
84 REGEXP is a regular expression matching those help items whose
85 documentation can be looked up via DOC-SPEC.
86 IGNORE-CASE is non-nil if help items are case insensitive.
87 DOC-SPEC is a list of documentation specifications of the form
88
89 (INFO-NODE TRANS-FUNC PREFIX SUFFIX)
90
91 INFO-NODE is the name (including file name part) of an Info index.
92 TRANS-FUNC is a function translating index entries into help items;
93 nil means add only those index entries matching REGEXP, a string
94 means prepend string to the first word of all index entries.
95 PREFIX and SUFFIX are parts of a regular expression. If one of
96 them is non-nil then search the help item's Info node for the
97 first occurrence of the regular expression `PREFIX ITEM SUFFIX'.
98 ITEM will be highlighted with `info-lookup-highlight-face' if this
99 variable is not nil.
100 PARSE-RULE is either the symbol name of a function or a regular
101 expression for guessing the default help item at point. Fuzzy
102 regular expressions like \"[_a-zA-Z0-9]+\" do a better job if
103 there are no clear delimiters; do not try to write too complex
104 expressions. PARSE-RULE defaults to REGEXP.
105 OTHER-MODES is a list of cross references to other help modes.")
106
107 (defsubst info-lookup->topic-value (topic)
108 (cdr (assoc topic info-lookup-alist)))
109
110 (defsubst info-lookup->mode-value (topic mode)
111 (assoc mode (info-lookup->topic-value topic)))
112
113 (defsubst info-lookup->regexp (topic mode)
114 (nth 1 (info-lookup->mode-value topic mode)))
115
116 (defsubst info-lookup->ignore-case (topic mode)
117 (nth 2 (info-lookup->mode-value topic mode)))
118
119 (defsubst info-lookup->doc-spec (topic mode)
120 (nth 3 (info-lookup->mode-value topic mode)))
121
122 (defsubst info-lookup->parse-rule (topic mode)
123 (nth 4 (info-lookup->mode-value topic mode)))
124
125 (defsubst info-lookup->other-modes (topic mode)
126 (nth 5 (info-lookup->mode-value topic mode)))
127
128 (defun info-lookup-add-help (&rest arg)
129 "Add or update a help specification.
130 Function arguments are specified as keyword/argument pairs:
131
132 \(KEYWORD . ARGUMENT)
133
134 KEYWORD is either `:topic', `:mode', `:regexp', `:ignore-case',
135 `:doc-spec', `:parse-rule', or `:other-modes'.
136 ARGUMENT has a value as explained in the documentation of the
137 variable `info-lookup-alist'.
138
139 If no topic or mode option has been specified, then the help topic defaults
140 to `symbol', and the help mode defaults to the current major mode."
141 (apply 'info-lookup-add-help* nil arg))
142
143 (defun info-lookup-maybe-add-help (&rest arg)
144 "Add a help specification if none is defined.
145 See the documentation of the function `info-lookup-add-help'
146 for more details."
147 (apply 'info-lookup-add-help* t arg))
148
149 (defun info-lookup-add-help* (maybe &rest arg)
150 (let (topic mode regexp ignore-case doc-spec
151 parse-rule other-modes keyword value)
152 (setq topic 'symbol
153 mode major-mode
154 regexp "\\w+")
155 (while arg
156 (setq keyword (car arg))
157 (or (symbolp keyword)
158 (error "Junk in argument list \"%S\"" arg))
159 (setq arg (cdr arg))
160 (and (null arg)
161 (error "Keyword \"%S\" is missing an argument" keyword))
162 (setq value (car arg)
163 arg (cdr arg))
164 (cond ((eq keyword :topic)
165 (setq topic value))
166 ((eq keyword :mode)
167 (setq mode value))
168 ((eq keyword :regexp)
169 (setq regexp value))
170 ((eq keyword :ignore-case)
171 (setq ignore-case value))
172 ((eq keyword :doc-spec)
173 (setq doc-spec value))
174 ((eq keyword :parse-rule)
175 (setq parse-rule value))
176 ((eq keyword :other-modes)
177 (setq other-modes value))
178 (t
179 (error "Unknown keyword \"%S\"" keyword))))
180 (or (and maybe (info-lookup->mode-value topic mode))
181 (let* ((data (list regexp ignore-case doc-spec parse-rule other-modes))
182 (topic-cell (or (assoc topic info-lookup-alist)
183 (car (setq info-lookup-alist
184 (cons (cons topic nil)
185 info-lookup-alist)))))
186 (mode-cell (assoc mode topic-cell)))
187 (if (null mode-cell)
188 (setcdr topic-cell (cons (cons mode data) (cdr topic-cell)))
189 (setcdr mode-cell data))))
190 nil))
191
192 (defvar info-lookup-cache nil
193 "Cache storing data maintained automatically by the program.
194 Value is an alist with cons cell of the form
195
196 (HELP-TOPIC . ((HELP-MODE INITIALIZED COMPLETIONS REFER-MODES) ...))
197
198 HELP-TOPIC is the symbol of a help topic.
199 HELP-MODE is a mode's symbol.
200 INITIALIZED is nil if HELP-MODE is uninitialized, t if
201 HELP-MODE is initialized, and `0' means HELP-MODE is
202 initialized but void.
203 COMPLETIONS is an alist of documented help items.
204 REFER-MODES is a list of other help modes to use.")
205
206 (defsubst info-lookup->cache (topic)
207 (or (assoc topic info-lookup-cache)
208 (car (setq info-lookup-cache
209 (cons (cons topic nil)
210 info-lookup-cache)))))
211
212 (defun info-lookup->topic-cache (topic)
213 (cdr (info-lookup->cache topic)))
214
215 (defun info-lookup->mode-cache (topic mode)
216 (assoc mode (info-lookup->topic-cache topic)))
217
218 (defun info-lookup->initialized (topic mode)
219 (nth 1 (info-lookup->mode-cache topic mode)))
220
221 (defun info-lookup->completions (topic mode)
222 (or (info-lookup->initialized topic mode)
223 (info-lookup-setup-mode topic mode))
224 (nth 2 (info-lookup->mode-cache topic mode)))
225
226 (defun info-lookup->refer-modes (topic mode)
227 (or (info-lookup->initialized topic mode)
228 (info-lookup-setup-mode topic mode))
229 (nth 3 (info-lookup->mode-cache topic mode)))
230
231 (defun info-lookup->all-modes (topic mode)
232 (cons mode (info-lookup->refer-modes topic mode)))
233
234 (defun info-lookup-quick-all-modes (topic mode)
235 (cons mode (info-lookup->other-modes topic mode)))
236
237 ;;;###autoload
238 (defun info-lookup-reset ()
239 "Throw away all cached data.
240 This command is useful if the user wants to start at the beginning without
241 quitting Emacs, for example, after some Info documents were updated on the
242 system."
243 (interactive)
244 (setq info-lookup-cache nil))
245
246 ;;;###autoload (put 'info-lookup-symbol 'info-file "emacs")
247 ;;;###autoload
248 (defun info-lookup-symbol (symbol &optional mode)
249 "Display the definition of SYMBOL, as found in the relevant manual.
250 When this command is called interactively, it reads SYMBOL from the
251 minibuffer. In the minibuffer, use M-n to yank the default argument
252 value into the minibuffer so you can edit it. The default symbol is the
253 one found at point.
254
255 With prefix arg a query for the symbol help mode is offered."
256 (interactive
257 (info-lookup-interactive-arguments 'symbol current-prefix-arg))
258 (info-lookup 'symbol symbol mode))
259
260 ;;;###autoload (put 'info-lookup-file 'info-file "emacs")
261 ;;;###autoload
262 (defun info-lookup-file (file &optional mode)
263 "Display the documentation of a file.
264 When this command is called interactively, it reads FILE from the minibuffer.
265 In the minibuffer, use M-n to yank the default file name
266 into the minibuffer so you can edit it.
267 The default file name is the one found at point.
268
269 With prefix arg a query for the file help mode is offered."
270 (interactive
271 (info-lookup-interactive-arguments 'file current-prefix-arg))
272 (info-lookup 'file file mode))
273
274 (defun info-lookup-interactive-arguments (topic &optional query)
275 "Read and return argument value (and help mode) for help topic TOPIC.
276 If optional argument QUERY is non-nil, query for the help mode."
277 (let* ((mode (cond (query
278 (info-lookup-change-mode topic))
279 ((info-lookup->mode-value topic (info-lookup-select-mode))
280 info-lookup-mode)
281 ((info-lookup-change-mode topic))))
282 (completions (info-lookup->completions topic mode))
283 (default (info-lookup-guess-default topic mode))
284 (completion-ignore-case (info-lookup->ignore-case topic mode))
285 (enable-recursive-minibuffers t)
286 (value (completing-read
287 (if default
288 (format "Describe %s (default %s): " topic default)
289 (format "Describe %s: " topic))
290 completions nil nil nil 'info-lookup-history default)))
291 (list (if (equal value "") default value) mode)))
292
293 (defun info-lookup-select-mode ()
294 (when (and (not info-lookup-mode) (buffer-file-name))
295 (let ((file-name (file-name-nondirectory (buffer-file-name)))
296 (file-name-alist info-lookup-file-name-alist))
297 (while (and (not info-lookup-mode) file-name-alist)
298 (when (string-match (caar file-name-alist) file-name)
299 (setq info-lookup-mode (cdar file-name-alist)))
300 (setq file-name-alist (cdr file-name-alist)))))
301
302 ;; If major-mode has no setups in info-lookup-alist, under any topic, then
303 ;; search up through derived-mode-parent to find a parent mode which does
304 ;; have some setups. This means that a `define-derived-mode' with no
305 ;; setups of its own will select its parent mode for lookups, if one of
306 ;; its parents has some setups. Good for example on `makefile-gmake-mode'
307 ;; and similar derivatives of `makefile-mode'.
308 ;;
309 (let ((mode major-mode)) ;; Look for `mode' with some setups.
310 (while (and mode (not info-lookup-mode))
311 (dolist (topic-cell info-lookup-alist) ;; Usually only two topics here.
312 (if (info-lookup->mode-value (car topic-cell) mode)
313 (setq info-lookup-mode mode)))
314 (setq mode (get mode 'derived-mode-parent))))
315
316 (or info-lookup-mode (setq info-lookup-mode major-mode)))
317
318 (defun info-lookup-change-mode (topic)
319 (let* ((completions (mapcar (lambda (arg)
320 (cons (symbol-name (car arg)) (car arg)))
321 (info-lookup->topic-value topic)))
322 (mode (completing-read
323 (format "Use %s help mode: " topic)
324 completions nil t nil 'info-lookup-history)))
325 (or (setq mode (cdr (assoc mode completions)))
326 (error "No %s help available" topic))
327 (or (info-lookup->mode-value topic mode)
328 (error "No %s help available for `%s'" topic mode))
329 (setq info-lookup-mode mode)))
330
331 (defun info-lookup (topic item mode)
332 "Display the documentation of a help item."
333 (or mode (setq mode (info-lookup-select-mode)))
334 (or (info-lookup->mode-value topic mode)
335 (error "No %s help available for `%s'" topic mode))
336 (let* ((completions (info-lookup->completions topic mode))
337 (ignore-case (info-lookup->ignore-case topic mode))
338 (entry (or (assoc (if ignore-case (downcase item) item) completions)
339 (assoc-string item completions t)
340 (error "Not documented as a %s: %s" topic (or item ""))))
341 (modes (info-lookup->all-modes topic mode))
342 (window (selected-window))
343 (new-Info-history
344 ;; Avoid clobbering Info-history with nodes searched during
345 ;; lookup. If lookup succeeds set `Info-history' to
346 ;; `new-Info-history'.
347 (when (get-buffer "*info*")
348 (with-current-buffer "*info*"
349 (cons (list Info-current-file Info-current-node (point))
350 Info-history))))
351 found doc-spec node prefix suffix doc-found)
352 (unless (eq major-mode 'Info-mode)
353 (if (not info-lookup-other-window-flag)
354 (info)
355 (save-window-excursion (info))
356 (let* ((info-window (get-buffer-window "*info*" t))
357 (info-frame (and info-window (window-frame info-window))))
358 (if (and info-frame
359 (not (eq info-frame (selected-frame)))
360 (display-multi-frame-p)
361 (memq info-frame (frames-on-display-list)))
362 ;; *info* is visible in another frame on same display.
363 ;; Raise that frame and select the window.
364 (progn
365 (select-window info-window)
366 (raise-frame info-frame))
367 ;; In any other case, switch to *info* in another window.
368 (switch-to-buffer-other-window "*info*")))))
369 (while (and (not found) modes)
370 (setq doc-spec (info-lookup->doc-spec topic (car modes)))
371 (while (and (not found) doc-spec)
372 (setq node (nth 0 (car doc-spec))
373 prefix (nth 2 (car doc-spec))
374 suffix (nth 3 (car doc-spec)))
375 (when (condition-case nil
376 (progn
377 ;; Don't need Index menu fontifications here, and
378 ;; they slow down the lookup.
379 (let (Info-fontify-maximum-menu-size
380 Info-history-list)
381 (Info-goto-node node)
382 (setq doc-found t)))
383 (error
384 (message "Cannot access Info node %s" node)
385 (sit-for 1)
386 nil))
387 (condition-case nil
388 (progn
389 ;; Don't use Info-menu, it forces case-fold-search to t
390 (let ((case-fold-search nil))
391 (re-search-forward
392 (concat "^\\* " (regexp-quote (or (cdr entry) (car entry)))
393 ":")))
394 (Info-follow-nearest-node)
395 (setq found t)
396 (if (or prefix suffix)
397 (let ((case-fold-search
398 (info-lookup->ignore-case topic (car modes)))
399 (buffer-read-only nil))
400 (goto-char (point-min))
401 (re-search-forward
402 (concat prefix (regexp-quote (car entry)) suffix))
403 (goto-char (match-beginning 0))
404 (and (display-color-p) info-lookup-highlight-face
405 ;; Search again for ITEM so that the first
406 ;; occurrence of ITEM will be highlighted.
407 (re-search-forward (regexp-quote (car entry)))
408 (let ((start (match-beginning 0))
409 (end (match-end 0)))
410 (if (overlayp info-lookup-highlight-overlay)
411 (move-overlay info-lookup-highlight-overlay
412 start end (current-buffer))
413 (setq info-lookup-highlight-overlay
414 (make-overlay start end))))
415 (overlay-put info-lookup-highlight-overlay
416 'face info-lookup-highlight-face)))))
417 (error nil)))
418 (setq doc-spec (cdr doc-spec)))
419 (setq modes (cdr modes)))
420 ;; Alert the user if case was munged, and do this after bringing up the
421 ;; info buffer since that can print messages
422 (unless (or ignore-case
423 (string-equal item (car entry)))
424 (message "Found in different case: %s" (car entry)))
425 (when found
426 (setq Info-history new-Info-history))
427 (or doc-found
428 (error "Info documentation for lookup was not found"))
429 ;; Don't leave the Info buffer if the help item couldn't be looked up.
430 (if (and info-lookup-other-window-flag found)
431 (select-window window))))
432
433 (defun info-lookup-setup-mode (topic mode)
434 "Initialize the internal data structure."
435 (or (info-lookup->initialized topic mode)
436 (let ((initialized 0)
437 cell data completions refer-modes Info-history-list)
438 (if (not (info-lookup->mode-value topic mode))
439 (message "No %s help available for `%s'" topic mode)
440 ;; Recursively setup cross references.
441 ;; But refer only to non-void modes.
442 (dolist (arg (info-lookup->other-modes topic mode))
443 (or (info-lookup->initialized topic arg)
444 (info-lookup-setup-mode topic arg))
445 (and (eq (info-lookup->initialized topic arg) t)
446 (setq refer-modes (cons arg refer-modes))))
447 (setq refer-modes (nreverse refer-modes))
448 ;; Build the full completion alist.
449 (setq completions
450 (nconc (condition-case nil
451 (info-lookup-make-completions topic mode)
452 (error nil))
453 (apply 'append
454 (mapcar (lambda (arg)
455 (info-lookup->completions topic arg))
456 refer-modes))))
457 (setq initialized t))
458 ;; Update `info-lookup-cache'.
459 (setq cell (info-lookup->mode-cache topic mode)
460 data (list initialized completions refer-modes))
461 (if (not cell)
462 (setcdr (info-lookup->cache topic)
463 (cons (cons mode data) (info-lookup->topic-cache topic)))
464 (setcdr cell data))
465 initialized)))
466
467 (defun info-lookup-make-completions (topic mode)
468 "Create a unique alist from all index entries."
469 (let ((doc-spec (info-lookup->doc-spec topic mode))
470 (regexp (concat "^\\(" (info-lookup->regexp topic mode)
471 "\\)\\([ \t].*\\)?$"))
472 Info-history-list Info-fontify-maximum-menu-size
473 node trans entry item prefix result doc-found
474 (buffer (get-buffer-create " temp-info-look")))
475 (with-current-buffer buffer
476 (Info-mode))
477 (while doc-spec
478 (setq node (nth 0 (car doc-spec))
479 trans (cond ((eq (nth 1 (car doc-spec)) nil)
480 (lambda (arg)
481 (if (string-match regexp arg)
482 (match-string 1 arg))))
483 ((stringp (nth 1 (car doc-spec)))
484 (setq prefix (nth 1 (car doc-spec)))
485 (lambda (arg)
486 (if (string-match "^\\([^: \t\n]+\\)" arg)
487 (concat prefix (match-string 1 arg)))))
488 (t (nth 1 (car doc-spec)))))
489 (with-current-buffer buffer
490 (message "Processing Info node `%s'..." node)
491 (when (condition-case nil
492 (progn
493 (Info-goto-node node)
494 (setq doc-found t))
495 (error
496 (message "Cannot access Info node `%s'" node)
497 (sit-for 1)
498 nil))
499 (condition-case nil
500 (progn
501 (goto-char (point-min))
502 (and (search-forward "\n* Menu:" nil t)
503 (while (re-search-forward "\n\\* \\(.*\\): " nil t)
504 (setq entry (match-string 1)
505 item (funcall trans entry))
506 ;; `trans' can return nil if the regexp doesn't match.
507 (when (and item
508 ;; Sometimes there's more than one Menu:
509 (not (string= entry "Menu")))
510 (and (info-lookup->ignore-case topic mode)
511 (setq item (downcase item)))
512 (and (string-equal entry item)
513 (setq entry nil))
514 (and (or (assoc item result)
515 (setq result (cons (cons item entry)
516 result))))))))
517 (error nil))))
518 (message "Processing Info node `%s'...done" node)
519 (setq doc-spec (cdr doc-spec)))
520 (or doc-found
521 (error "Info documentation for lookup was not found"))
522 result))
523
524 (defun info-lookup-guess-default (topic mode)
525 "Return a guess for a symbol to look up, based on text around point.
526 Try all related modes applicable to TOPIC and MODE.
527 Return nil if there is nothing appropriate in the buffer near point."
528 (let ((modes (info-lookup->all-modes topic mode))
529 guess)
530 (while (and (not guess) modes)
531 (setq guess (info-lookup-guess-default* topic (car modes))
532 modes (cdr modes)))
533 ;; Collapse whitespace characters.
534 (when guess
535 (let ((pos 0))
536 (while (string-match "[ \t\n]+" guess pos)
537 (setq pos (1+ (match-beginning 0)))
538 (setq guess (replace-match " " t t guess)))))
539 guess))
540
541 (defun info-lookup-guess-default* (topic mode)
542 (let ((case-fold-search (info-lookup->ignore-case topic mode))
543 (rule (or (info-lookup->parse-rule topic mode)
544 (info-lookup->regexp topic mode)))
545 (start (point)) end regexp subexp result)
546 (save-excursion
547 (if (symbolp rule)
548 (setq result (funcall rule))
549 (if (consp rule)
550 (setq regexp (car rule)
551 subexp (cdr rule))
552 (setq regexp rule
553 subexp 0))
554 ;; If at start of symbol, don't go back to end of previous one.
555 (if (save-match-data
556 (looking-at "[ \t\n]"))
557 (skip-chars-backward " \t\n"))
558 (setq end (point))
559 (while (and (re-search-backward regexp nil t)
560 (looking-at regexp)
561 (>= (match-end 0) end))
562 (setq result (match-string subexp)))
563 (if (not result)
564 (progn
565 (goto-char start)
566 (skip-chars-forward " \t\n")
567 (and (looking-at regexp)
568 (setq result (match-string subexp)))))))
569 result))
570
571 (defun info-lookup-guess-c-symbol ()
572 "Get the C symbol at point."
573 (condition-case nil
574 (progn
575 (skip-syntax-backward "w_")
576 (let ((start (point)) prefix name)
577 ;; Test for a leading `struct', `union', or `enum' keyword
578 ;; but ignore names like `foo_struct'.
579 (setq prefix (and (< (skip-chars-backward " \t\n") 0)
580 (< (skip-chars-backward "_a-zA-Z0-9") 0)
581 (looking-at "\\(struct\\|union\\|enum\\)\\s ")
582 (concat (match-string 1) " ")))
583 (goto-char start)
584 (and (looking-at "[_a-zA-Z][_a-zA-Z0-9]*")
585 (setq name (match-string 0)))
586 ;; Caveat! Look forward if point is at `struct' etc.
587 (and (not prefix)
588 (or (string-equal name "struct")
589 (string-equal name "union")
590 (string-equal name "enum"))
591 (looking-at "[a-z]+\\s +\\([_a-zA-Z][_a-zA-Z0-9]*\\)")
592 (setq prefix (concat name " ")
593 name (match-string 1)))
594 (and (or prefix name)
595 (concat prefix name))))
596 (error nil)))
597
598 (defun info-lookup-guess-custom-symbol ()
599 "Get symbol at point in custom buffers."
600 (condition-case nil
601 (save-excursion
602 (let ((case-fold-search t)
603 (ignored-chars "][()`',:.\" \t\n")
604 (significant-chars "^][()`',:.\" \t\n")
605 beg end)
606 (cond
607 ((and (memq (get-char-property (point) 'face)
608 '(custom-variable-tag custom-variable-tag-face))
609 (setq beg (previous-single-char-property-change
610 (point) 'face nil (line-beginning-position)))
611 (setq end (next-single-char-property-change
612 (point) 'face nil (line-end-position)))
613 (> end beg))
614 (subst-char-in-string
615 ?\s ?\- (buffer-substring-no-properties beg end)))
616 ((or (and (looking-at (concat "[" significant-chars "]"))
617 (save-excursion
618 (skip-chars-backward significant-chars)
619 (setq beg (point)))
620 (skip-chars-forward significant-chars)
621 (setq end (point))
622 (> end beg))
623 (and (looking-at "[ \t\n]")
624 (looking-back (concat "[" significant-chars "]"))
625 (setq end (point))
626 (skip-chars-backward significant-chars)
627 (setq beg (point))
628 (> end beg))
629 (and (skip-chars-forward ignored-chars)
630 (setq beg (point))
631 (skip-chars-forward significant-chars)
632 (setq end (point))
633 (> end beg)))
634 (buffer-substring-no-properties beg end)))))
635 (error nil)))
636
637 ;;;###autoload
638 (defun info-complete-symbol (&optional mode)
639 "Perform completion on symbol preceding point."
640 (interactive)
641 (info-complete 'symbol
642 (or mode
643 (if (info-lookup->mode-value
644 'symbol (info-lookup-select-mode))
645 info-lookup-mode
646 (info-lookup-change-mode 'symbol)))))
647
648 ;;;###autoload
649 (defun info-complete-file (&optional mode)
650 "Perform completion on file preceding point."
651 (interactive)
652 (info-complete 'file
653 (or mode
654 (if (info-lookup->mode-value
655 'file (info-lookup-select-mode))
656 info-lookup-mode
657 (info-lookup-change-mode 'file)))))
658
659 (defun info-lookup-completions-at-point (topic mode)
660 "Try to complete a help item."
661 (or mode (setq mode (info-lookup-select-mode)))
662 (when (info-lookup->mode-value topic mode)
663 (let ((modes (info-lookup-quick-all-modes topic mode))
664 (start (point))
665 try)
666 (while (and (not try) modes)
667 (setq mode (car modes)
668 modes (cdr modes)
669 try (info-lookup-guess-default* topic mode))
670 (goto-char start))
671 (when try
672 (let ((completions (info-lookup->completions topic mode)))
673 (when completions
674 (when (info-lookup->ignore-case topic mode)
675 (setq completions
676 (lambda (string pred action)
677 (let ((completion-ignore-case t))
678 (complete-with-action
679 action completions string pred)))))
680 (save-excursion
681 ;; Find the original symbol and zap it.
682 (end-of-line)
683 (while (and (search-backward try nil t)
684 (< start (point))))
685 (list (match-beginning 0) (match-end 0) completions
686 :exclusive 'no))))))))
687
688 (defun info-complete (topic mode)
689 "Try to complete a help item."
690 (barf-if-buffer-read-only)
691 (let ((data (info-lookup-completions-at-point topic mode)))
692 (if (null data)
693 (error "No %s completion available for `%s' at point" topic mode)
694 (completion-in-region (nth 0 data) (nth 1 data) (nth 2 data)))))
695
696 \f
697 ;;; Initialize some common modes.
698
699 (info-lookup-maybe-add-help
700 :mode 'c-mode :topic 'symbol
701 :regexp "\\(struct \\|union \\|enum \\)?[_a-zA-Z][_a-zA-Z0-9]*"
702 :doc-spec '(("(libc)Function Index" nil
703 "^[ \t]+-+ \\(Function\\|Macro\\): .*\\<" "\\>")
704 ;; prefix/suffix has to match things like
705 ;; " -- Macro: int F_DUPFD"
706 ;; " -- Variable: char * tzname [2]"
707 ;; "`DBL_MAX'" (texinfo @table)
708 ;; suffix "\\>" is not used because that sends DBL_MAX to
709 ;; DBL_MAX_EXP ("_" is a non-word char)
710 ("(libc)Variable Index" nil
711 "^\\([ \t]+-+ \\(Variable\\|Macro\\): .*\\<\\|`\\)"
712 "\\( \\|'?$\\)")
713 ("(libc)Type Index" nil
714 "^[ \t]+-+ Data Type: \\<" "\\>")
715 ("(termcap)Var Index" nil
716 "^[ \t]*`" "'"))
717 :parse-rule 'info-lookup-guess-c-symbol)
718
719 (info-lookup-maybe-add-help
720 :mode 'c-mode :topic 'file
721 :regexp "[_a-zA-Z0-9./+-]+"
722 :doc-spec '(("(libc)File Index")))
723
724 (info-lookup-maybe-add-help
725 :mode 'bison-mode
726 :regexp "[:;|]\\|%\\([%{}]\\|[_a-z]+\\)\\|YY[_A-Z]+\\|yy[_a-z]+"
727 :doc-spec '(("(bison)Index" nil
728 "`" "'"))
729 :parse-rule "[:;|]\\|%\\([%{}]\\|[_a-zA-Z][_a-zA-Z0-9]*\\)"
730 :other-modes '(c-mode))
731
732 (info-lookup-maybe-add-help
733 :mode 'makefile-mode
734 :regexp "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z][_a-zA-Z0-9-]*"
735 :doc-spec '(("(make)Name Index" nil
736 "^[ \t]*`" "'"))
737 :parse-rule "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z0-9-]+")
738
739 (info-lookup-maybe-add-help
740 :topic 'symbol
741 :mode 'makefile-automake-mode
742 ;; similar regexp/parse-rule as makefile-mode, but also the following
743 ;; (which have index entries),
744 ;; "##" special automake comment
745 ;; "+=" append operator, separate from the GNU make one
746 :regexp "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z][_a-zA-Z0-9-]*\\|##\\|\\+="
747 :parse-rule "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z0-9-]+\\|##\\|\\+="
748 :doc-spec '(
749 ;; "(automake)Macro Index" is autoconf macros used in
750 ;; configure.ac, not Makefile.am, so don't have that here.
751 ("(automake)Variable Index" nil "^[ \t]*`" "'")
752 ;; In automake 1.4 macros and variables were a combined node.
753 ("(automake)Macro and Variable Index" nil "^[ \t]*`" "'")
754 ;; Directives like "if" are in the "General Index".
755 ;; Prefix "`" since the text for say `+=' isn't always an
756 ;; @item etc and so not always at the start of a line.
757 ("(automake)General Index" nil "`" "'")
758 ;; In automake 1.3 there was just a single "Index" node.
759 ("(automake)Index" nil "`" "'"))
760 :other-modes '(makefile-mode))
761
762 (info-lookup-maybe-add-help
763 :mode 'texinfo-mode
764 :regexp "@\\([a-zA-Z]+\\|[^a-zA-Z]\\)"
765 :doc-spec '(("(texinfo)Command and Variable Index"
766 ;; Ignore Emacs commands and prepend a `@'.
767 (lambda (item)
768 (if (string-match "^\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\( .*\\)?$" item)
769 (concat "@" (match-string 1 item))))
770 "`" "[' ]")))
771
772 (info-lookup-maybe-add-help
773 :mode 'm4-mode
774 :regexp "[_a-zA-Z][_a-zA-Z0-9]*"
775 :doc-spec '(("(m4)Macro index"))
776 :parse-rule "[_a-zA-Z0-9]+")
777
778 (info-lookup-maybe-add-help
779 :mode 'autoconf-mode
780 :regexp "A[CM]_[_A-Z0-9]+"
781 :doc-spec '(;; Autoconf Macro Index entries are without an "AC_" prefix,
782 ;; but with "AH_" or "AU_" for those. So add "AC_" if there
783 ;; isn't already an "A._".
784 ("(autoconf)Autoconf Macro Index"
785 (lambda (item)
786 (if (string-match "^A._" item) item (concat "AC_" item)))
787 "^[ \t]+-+ \\(Macro\\|Variable\\): .*\\<" "\\>")
788 ;; M4 Macro Index entries are without "AS_" prefixes, and
789 ;; mostly without "m4_" prefixes. "dnl" is an exception, not
790 ;; wanting any prefix. So AS_ is added back to upper-case
791 ;; names (if needed), m4_ to others which don't already an m4_.
792 ("(autoconf)M4 Macro Index"
793 (lambda (item)
794 (let ((case-fold-search nil))
795 (cond ((or (string-equal item "dnl")
796 (string-match "^m4_" item)
797 ;; Autoconf 2.62 index includes some macros
798 ;; (e.g., AS_HELP_STRING), so avoid prefixing.
799 (string-match "^AS_" item))
800 item)
801 ((string-match "^[A-Z0-9_]+$" item)
802 (concat "AS_" item))
803 (t
804 (concat "m4_" item)))))
805 "^[ \t]+-+ Macro: .*\\<" "\\>")
806 ;; Autotest Macro Index entries are without "AT_".
807 ("(autoconf)Autotest Macro Index" "AT_"
808 "^[ \t]+-+ Macro: .*\\<" "\\>")
809 ;; This is for older versions (probably pre autoconf 2.5x):
810 ("(autoconf)Macro Index" "AC_"
811 "^[ \t]+-+ \\(Macro\\|Variable\\): .*\\<" "\\>")
812 ;; Automake has index entries for its notes on various autoconf
813 ;; macros (eg. AC_PROG_CC). Ensure this is after the autoconf
814 ;; index, so as to prefer the autoconf docs.
815 ("(automake)Macro and Variable Index" nil
816 "^[ \t]*`" "'"))
817 ;; Autoconf symbols are M4 macros. Thus use M4's parser.
818 :parse-rule 'ignore
819 :other-modes '(m4-mode))
820
821 (info-lookup-maybe-add-help
822 :mode 'awk-mode
823 :regexp "[_a-zA-Z]+"
824 :doc-spec '(("(gawk)Index"
825 (lambda (item)
826 (let ((case-fold-search nil))
827 (cond
828 ;; `BEGIN' and `END'.
829 ((string-match "^\\([A-Z]+\\) special pattern\\b" item)
830 (match-string 1 item))
831 ;; `if', `while', `do', ...
832 ((string-match "^\\([a-z]+\\) statement\\b" item)
833 (if (not (string-equal (match-string 1 item) "control"))
834 (match-string 1 item)))
835 ;; `NR', `NF', ...
836 ((string-match "^[A-Z]+$" item)
837 item)
838 ;; Built-in functions (matches to many entries).
839 ((string-match "^[a-z]+$" item)
840 item))))
841 "`" "\\([ \t]*([^)]*)\\)?'")))
842
843 (info-lookup-maybe-add-help
844 :mode 'perl-mode
845 :regexp "[$@%][^a-zA-Z]\\|\\$\\^[A-Z]\\|[$@%]?[a-zA-Z][_a-zA-Z0-9]*"
846 :doc-spec '(("(perl5)Function Index"
847 (lambda (item)
848 (if (string-match "^\\([a-zA-Z0-9]+\\)" item)
849 (match-string 1 item)))
850 "^" "\\b")
851 ("(perl5)Variable Index"
852 (lambda (item)
853 ;; Work around bad formatted array variables.
854 (let ((sym (cond ((or (string-match "^\\$\\(.\\|@@\\)$" item)
855 (string-match "^\\$\\^[A-Z]$" item))
856 item)
857 ((string-match
858 "^\\([$%@]\\|@@\\)?[_a-zA-Z0-9]+" item)
859 (match-string 0 item))
860 (t ""))))
861 (if (string-match "@@" sym)
862 (setq sym (concat (substring sym 0 (match-beginning 0))
863 (substring sym (1- (match-end 0))))))
864 (if (string-equal sym "") nil sym)))
865 "^" "\\b"))
866 :parse-rule "[$@%]?\\([_a-zA-Z0-9]+\\|[^a-zA-Z]\\)")
867
868 (info-lookup-maybe-add-help
869 :mode 'cperl-mode
870 :regexp "[$@%][^a-zA-Z]\\|\\$\\^[A-Z]\\|[$@%]?[a-zA-Z][_a-zA-Z0-9]*"
871 :other-modes '(perl-mode))
872
873 (info-lookup-maybe-add-help
874 :mode 'latex-mode
875 :regexp "\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)"
876 :doc-spec '(("(latex)Command Index" nil
877 "`" "\\({[^}]*}\\)?'")))
878
879 (info-lookup-maybe-add-help
880 :mode 'emacs-lisp-mode
881 :regexp "[^][()`',\" \t\n]+"
882 :doc-spec '(;; Commands with key sequences appear in nodes as `foo' and
883 ;; those without as `M-x foo'.
884 ("(emacs)Command Index" nil "`\\(M-x[ \t\n]+\\)?" "'")
885 ;; Variables normally appear in nodes as just `foo'.
886 ("(emacs)Variable Index" nil "`" "'")
887 ;; Almost all functions, variables, etc appear in nodes as
888 ;; " -- Function: foo" etc. A small number of aliases and
889 ;; symbols appear only as `foo', and will miss out on exact
890 ;; positions. Allowing `foo' would hit too many false matches
891 ;; for things that should go to Function: etc, and those latter
892 ;; are much more important. Perhaps this could change if some
893 ;; sort of fallback match scheme existed.
894 ("(elisp)Index" nil "^ -+ .*: " "\\( \\|$\\)")))
895
896 ;; docstrings talk about elisp, so have apropos-mode follow emacs-lisp-mode
897 (info-lookup-maybe-add-help
898 :mode 'apropos-mode
899 :regexp "[^][()`',\" \t\n]+" ;; same as emacs-lisp-mode above
900 :other-modes '(emacs-lisp-mode))
901
902 (info-lookup-maybe-add-help
903 :mode 'lisp-interaction-mode
904 :regexp "[^][()`',\" \t\n]+"
905 :parse-rule 'ignore
906 :other-modes '(emacs-lisp-mode))
907
908 (info-lookup-maybe-add-help
909 :mode 'lisp-mode
910 :regexp "[^()`',\" \t\n]+"
911 :parse-rule 'ignore
912 :other-modes '(emacs-lisp-mode))
913
914 (info-lookup-maybe-add-help
915 :mode 'scheme-mode
916 :regexp "[^()`',\" \t\n]+"
917 :ignore-case t
918 ;; Aubrey Jaffer's rendition from <URL:ftp://ftp-swiss.ai.mit.edu/pub/scm>
919 :doc-spec '(("(r5rs)Index" nil
920 "^[ \t]+-+ [^:]+:[ \t]*" "\\b")))
921
922 (info-lookup-maybe-add-help
923 :mode 'octave-mode
924 :regexp "[_a-zA-Z0-9]+\\|\\s.+\\|[-!=^|*/.\\,><~&+]\\{1,3\\}\\|[][();,\"']"
925 :doc-spec '(("(octave)Function Index" nil
926 "^ -+ [^:]+:[ ]+\\(\\[[^=]*=[ ]+\\)?" nil)
927 ("(octave)Variable Index" nil "^ -+ [^:]+:[ ]+" nil)
928 ("(octave)Operator Index" nil nil nil)
929 ;; Catch lines of the form "xyz statement"
930 ("(octave)Concept Index"
931 (lambda (item)
932 (cond
933 ((string-match "^\\([A-Z]+\\) statement\\b" item)
934 (match-string 1 item))
935 (t nil)))
936 nil; "^ -+ [^:]+:[ ]+" don't think this prefix is useful here.
937 nil)))
938
939 (info-lookup-maybe-add-help
940 :mode 'maxima-mode
941 :ignore-case t
942 :regexp "[a-zA-Z0-9_%]+"
943 :doc-spec '( ("(maxima)Function and Variable Index" nil
944 "^ -+ [^:]+:[ ]+\\(\\[[^=]*=[ ]+\\)?" nil)))
945
946 (info-lookup-maybe-add-help
947 :mode 'inferior-maxima-mode
948 :regexp "[a-zA-Z0-9_%]+"
949 :other-modes '(maxima-mode))
950
951 ;; coreutils and bash builtins overlap in places, eg. printf, so there's a
952 ;; question which should come first. Some of the coreutils descriptions are
953 ;; more detailed, but if bash is usually /bin/sh on a GNU system then the
954 ;; builtins will be what's normally run.
955 ;;
956 ;; Maybe special variables like $? should be matched as $?, not just ?.
957 ;; This would avoid a clash between variable $! and negation !, or variable
958 ;; $# and comment # (though comment # is not currently indexed in bash).
959 ;; Unfortunately if $? etc is the symbol, then we wouldn't be taken to the
960 ;; exact spot in the relevant node, since the bash manual has just `?' etc
961 ;; there. Maybe an extension to the prefix/suffix scheme could help this.
962
963 (info-lookup-maybe-add-help
964 :mode 'sh-mode :topic 'symbol
965 ;; bash has "." and ":" in its index, but those chars will probably never
966 ;; work in info, so don't bother matching them in the regexp.
967 :regexp "\\([a-zA-Z0-9_-]+\\|[!{}@*#?$]\\|\\[\\[?\\|]]?\\)"
968 :doc-spec '(("(bash)Builtin Index" nil "^`" "[ .']")
969 ("(bash)Reserved Word Index" nil "^`" "[ .']")
970 ("(bash)Variable Index" nil "^`" "[ .']")
971
972 ;; coreutils (version 4.5.10) doesn't have a separate program
973 ;; index, so exclude extraneous stuff (most of it) by demanding
974 ;; "[a-z]+" in the trans-func.
975 ;; coreutils version 8.1 has node "Concept Index" and past
976 ;; versions have node "Index", look for both, whichever is
977 ;; absent is quietly ignored
978 ("(coreutils)Index"
979 (lambda (item) (if (string-match "\\`[a-z]+\\'" item) item)))
980 ("(coreutils)Concept Index"
981 (lambda (item) (if (string-match "\\`[a-z]+\\'" item) item)))
982
983 ;; diff (version 2.8.1) has only a few programs, index entries
984 ;; are things like "foo invocation".
985 ("(diff)Index"
986 (lambda (item)
987 (if (string-match "\\`\\([a-z]+\\) invocation\\'" item)
988 (match-string 1 item))))
989 ;; there's no plain "sed" index entry as such, mung another
990 ;; hopefully unique one to get to the invocation section
991 ("(sed)Concept Index"
992 (lambda (item)
993 (if (string-equal item "Standard input, processing as input")
994 "sed")))
995 ;; there's no plain "awk" or "gawk" index entries, mung other
996 ;; hopefully unique ones to get to the command line options
997 ("(gawk)Index"
998 (lambda (item)
999 (cond ((string-equal item "gawk, extensions, disabling")
1000 "awk")
1001 ((string-equal item "gawk, versions of, information about, printing")
1002 "gawk"))))))
1003
1004 ;; This misses some things which occur as node names but not in the
1005 ;; index. Unfortunately it also picks up the wrong one of multiple
1006 ;; entries for the same term in some cases. --fx
1007 (info-lookup-maybe-add-help
1008 :mode 'cfengine-mode
1009 :regexp "[[:alnum:]_]+\\(?:()\\)?"
1010 :doc-spec '(("(cfengine-Reference)Variable Index"
1011 (lambda (item)
1012 ;; Index entries may be like `IsPlain()'
1013 (if (string-match "\\([[:alnum:]_]+\\)()" item)
1014 (match-string 1 item)
1015 item))
1016 ;; This gets functions in evaluated classes. Other
1017 ;; possible patterns don't seem to work too well.
1018 "`" "(")))
1019
1020 (info-lookup-maybe-add-help
1021 :mode 'Custom-mode
1022 :ignore-case t
1023 :regexp "[^][()`',:\" \t\n]+"
1024 :parse-rule 'info-lookup-guess-custom-symbol
1025 :other-modes '(emacs-lisp-mode))
1026
1027 (info-lookup-maybe-add-help
1028 :mode 'help-mode
1029 :regexp "[^][()`',:\" \t\n]+"
1030 :other-modes '(emacs-lisp-mode))
1031 \f
1032 (provide 'info-look)
1033
1034 ;;; info-look.el ends here