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.
4 ;; Copyright (C) 1995-1999, 2001-2013 Free Software Foundation, Inc.
6 ;; Author: Ralph Schleicher <rs@nunatak.allgaeu.org>
7 ;; (did not show signs of life (Nov 2001) -stef)
8 ;; Keywords: help languages
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
27 ;; Really cool code to lookup info indexes.
28 ;; Try especially info-lookup-symbol (aka C-h S).
30 ;; Some additional sources of (Tex)info files for non-GNU packages:
32 ;; Scheme: <URL:http://groups.csail.mit.edu/mac/ftpdir/scm/r5rs.info.tar.gz>
34 ;; <URL:ftp://ctan.tug.org/tex-archive/info/latex2e-help-texinfo/latex2e.texi>
36 ;; Perl: <URL:ftp://ftp.cpan.org/pub/CPAN/doc/manual/texinfo/> (or CPAN mirrors)
42 (defgroup info-lookup nil
43 "Major mode sensitive help agent."
44 :group
'help
:group
'languages
)
46 (defvar info-lookup-mode nil
47 "Symbol of the current buffer's help mode.
48 Help is provided according to the buffer's major mode if value is nil.
49 Automatically becomes buffer local when set in any fashion.")
50 (make-variable-buffer-local 'info-lookup-mode
)
52 (defcustom info-lookup-other-window-flag t
53 "Non-nil means pop up the Info buffer in another window."
54 :group
'info-lookup
:type
'boolean
)
56 (defcustom info-lookup-highlight-face
'match
57 "Face for highlighting looked up help items.
58 Setting this variable to nil disables highlighting."
59 :group
'info-lookup
:type
'face
)
61 (defvar info-lookup-highlight-overlay nil
62 "Overlay object used for highlighting.")
64 (defcustom info-lookup-file-name-alist
65 '(("\\`ac\\(local\\|site\\|include\\)\\.m4\\'" . autoconf-mode
))
66 "Alist of file names handled specially.
67 List elements are cons cells of the form
71 If a file name matches REGEXP, then use help mode MODE instead of the
73 :group
'info-lookup
:type
'(repeat (cons (string :tag
"Regexp")
74 (symbol :tag
"Mode"))))
76 (defvar info-lookup-history nil
77 "History of previous input lines.")
79 (defvar info-lookup-alist nil
80 "Alist of known help topics.
81 Cons cells are of the form
83 (HELP-TOPIC . HELP-DATA)
85 HELP-TOPIC is the symbol of a help topic.
86 HELP-DATA is a HELP-TOPIC's public data set.
87 Value is an alist with elements of the form
89 (HELP-MODE REGEXP IGNORE-CASE DOC-SPEC PARSE-RULE OTHER-MODES)
91 HELP-MODE is a mode's symbol.
92 REGEXP is a regular expression matching those help items whose
93 documentation can be looked up via DOC-SPEC.
94 IGNORE-CASE is non-nil if help items are case insensitive.
95 DOC-SPEC is a list of documentation specifications of the form
97 (INFO-NODE TRANS-FUNC PREFIX SUFFIX)
99 INFO-NODE is the name (including file name part) of an Info index.
100 TRANS-FUNC is a function translating index entries into help items;
101 nil means add only those index entries matching REGEXP, a string
102 means prepend string to the first word of all index entries.
103 PREFIX and SUFFIX are parts of a regular expression. If one of
104 them is non-nil then search the help item's Info node for the
105 first occurrence of the regular expression `PREFIX ITEM SUFFIX'.
106 ITEM will be highlighted with `info-lookup-highlight-face' if this
108 PARSE-RULE is either the symbol name of a function or a regular
109 expression for guessing the default help item at point. Fuzzy
110 regular expressions like \"[_a-zA-Z0-9]+\" do a better job if
111 there are no clear delimiters; do not try to write too complex
112 expressions. PARSE-RULE defaults to REGEXP.
113 OTHER-MODES is a list of cross references to other help modes.")
115 (defsubst info-lookup-
>topic-value
(topic)
116 (cdr (assoc topic info-lookup-alist
)))
118 (defsubst info-lookup-
>mode-value
(topic mode
)
119 (assoc mode
(info-lookup->topic-value topic
)))
121 (defsubst info-lookup-
>regexp
(topic mode
)
122 (nth 1 (info-lookup->mode-value topic mode
)))
124 (defsubst info-lookup-
>ignore-case
(topic mode
)
125 (nth 2 (info-lookup->mode-value topic mode
)))
127 (defsubst info-lookup-
>doc-spec
(topic mode
)
128 (nth 3 (info-lookup->mode-value topic mode
)))
130 (defsubst info-lookup-
>parse-rule
(topic mode
)
131 (nth 4 (info-lookup->mode-value topic mode
)))
133 (defsubst info-lookup-
>other-modes
(topic mode
)
134 (nth 5 (info-lookup->mode-value topic mode
)))
136 (defun info-lookup-add-help (&rest arg
)
137 "Add or update a help specification.
138 Function arguments are specified as keyword/argument pairs:
140 \(KEYWORD . ARGUMENT)
142 KEYWORD is either `:topic', `:mode', `:regexp', `:ignore-case',
143 `:doc-spec', `:parse-rule', or `:other-modes'.
144 ARGUMENT has a value as explained in the documentation of the
145 variable `info-lookup-alist'.
147 If no topic or mode option has been specified, then the help topic defaults
148 to `symbol', and the help mode defaults to the current major mode."
149 (apply 'info-lookup-add-help
* nil arg
))
151 (defun info-lookup-maybe-add-help (&rest arg
)
152 "Add a help specification if none is defined.
153 See the documentation of the function `info-lookup-add-help'
155 (apply 'info-lookup-add-help
* t arg
))
157 (defun info-lookup-add-help* (maybe &rest arg
)
158 (let (topic mode regexp ignore-case doc-spec
159 parse-rule other-modes keyword value
)
164 (setq keyword
(car arg
))
165 (or (symbolp keyword
)
166 (error "Junk in argument list \"%S\"" arg
))
169 (error "Keyword \"%S\" is missing an argument" keyword
))
170 (setq value
(car arg
)
172 (cond ((eq keyword
:topic
)
176 ((eq keyword
:regexp
)
178 ((eq keyword
:ignore-case
)
179 (setq ignore-case value
))
180 ((eq keyword
:doc-spec
)
181 (setq doc-spec value
))
182 ((eq keyword
:parse-rule
)
183 (setq parse-rule value
))
184 ((eq keyword
:other-modes
)
185 (setq other-modes value
))
187 (error "Unknown keyword \"%S\"" keyword
))))
188 (or (and maybe
(info-lookup->mode-value topic mode
))
189 (let* ((data (list regexp ignore-case doc-spec parse-rule other-modes
))
190 (topic-cell (or (assoc topic info-lookup-alist
)
191 (car (setq info-lookup-alist
192 (cons (cons topic nil
)
193 info-lookup-alist
)))))
194 (mode-cell (assoc mode topic-cell
)))
196 (setcdr topic-cell
(cons (cons mode data
) (cdr topic-cell
)))
197 (setcdr mode-cell data
))))
200 (defvar info-lookup-cache nil
201 "Cache storing data maintained automatically by the program.
202 Value is an alist with cons cell of the form
204 (HELP-TOPIC . ((HELP-MODE INITIALIZED COMPLETIONS REFER-MODES) ...))
206 HELP-TOPIC is the symbol of a help topic.
207 HELP-MODE is a mode's symbol.
208 INITIALIZED is nil if HELP-MODE is uninitialized, t if
209 HELP-MODE is initialized, and `0' means HELP-MODE is
210 initialized but void.
211 COMPLETIONS is an alist of documented help items.
212 REFER-MODES is a list of other help modes to use.")
214 (defsubst info-lookup-
>cache
(topic)
215 (or (assoc topic info-lookup-cache
)
216 (car (setq info-lookup-cache
217 (cons (cons topic nil
)
218 info-lookup-cache
)))))
220 (defun info-lookup->topic-cache
(topic)
221 (cdr (info-lookup->cache topic
)))
223 (defun info-lookup->mode-cache
(topic mode
)
224 (assoc mode
(info-lookup->topic-cache topic
)))
226 (defun info-lookup->initialized
(topic mode
)
227 (nth 1 (info-lookup->mode-cache topic mode
)))
229 (defun info-lookup->completions
(topic mode
)
230 (or (info-lookup->initialized topic mode
)
231 (info-lookup-setup-mode topic mode
))
232 (nth 2 (info-lookup->mode-cache topic mode
)))
234 (defun info-lookup->refer-modes
(topic mode
)
235 (or (info-lookup->initialized topic mode
)
236 (info-lookup-setup-mode topic mode
))
237 (nth 3 (info-lookup->mode-cache topic mode
)))
239 (defun info-lookup->all-modes
(topic mode
)
240 (cons mode
(info-lookup->refer-modes topic mode
)))
242 (defun info-lookup-quick-all-modes (topic mode
)
243 (cons mode
(info-lookup->other-modes topic mode
)))
246 (defun info-lookup-reset ()
247 "Throw away all cached data.
248 This command is useful if the user wants to start at the beginning without
249 quitting Emacs, for example, after some Info documents were updated on the
252 (setq info-lookup-cache nil
))
254 ;;;###autoload (put 'info-lookup-symbol 'info-file "emacs")
256 (defun info-lookup-symbol (symbol &optional mode
)
257 "Display the definition of SYMBOL, as found in the relevant manual.
258 When this command is called interactively, it reads SYMBOL from the
259 minibuffer. In the minibuffer, use M-n to yank the default argument
260 value into the minibuffer so you can edit it. The default symbol is the
263 With prefix arg a query for the symbol help mode is offered."
265 (info-lookup-interactive-arguments 'symbol current-prefix-arg
))
266 (info-lookup 'symbol symbol mode
))
268 ;;;###autoload (put 'info-lookup-file 'info-file "emacs")
270 (defun info-lookup-file (file &optional mode
)
271 "Display the documentation of a file.
272 When this command is called interactively, it reads FILE from the minibuffer.
273 In the minibuffer, use M-n to yank the default file name
274 into the minibuffer so you can edit it.
275 The default file name is the one found at point.
277 With prefix arg a query for the file help mode is offered."
279 (info-lookup-interactive-arguments 'file current-prefix-arg
))
280 (info-lookup 'file file mode
))
282 (defun info-lookup-interactive-arguments (topic &optional query
)
283 "Read and return argument value (and help mode) for help topic TOPIC.
284 If optional argument QUERY is non-nil, query for the help mode."
285 (let* ((mode (cond (query
286 (info-lookup-change-mode topic
))
287 ((info-lookup->mode-value topic
(info-lookup-select-mode))
289 ((info-lookup-change-mode topic
))))
290 (completions (info-lookup->completions topic mode
))
291 (default (info-lookup-guess-default topic mode
))
292 (completion-ignore-case (info-lookup->ignore-case topic mode
))
293 (enable-recursive-minibuffers t
)
294 (value (completing-read
296 (format "Describe %s (default %s): " topic default
)
297 (format "Describe %s: " topic
))
298 completions nil nil nil
'info-lookup-history default
)))
299 (list (if (equal value
"") default value
) mode
)))
301 (defun info-lookup-select-mode ()
302 (when (and (not info-lookup-mode
) (buffer-file-name))
303 (let ((file-name (file-name-nondirectory (buffer-file-name)))
304 (file-name-alist info-lookup-file-name-alist
))
305 (while (and (not info-lookup-mode
) file-name-alist
)
306 (when (string-match (caar file-name-alist
) file-name
)
307 (setq info-lookup-mode
(cdar file-name-alist
)))
308 (setq file-name-alist
(cdr file-name-alist
)))))
310 ;; If major-mode has no setups in info-lookup-alist, under any topic, then
311 ;; search up through derived-mode-parent to find a parent mode which does
312 ;; have some setups. This means that a `define-derived-mode' with no
313 ;; setups of its own will select its parent mode for lookups, if one of
314 ;; its parents has some setups. Good for example on `makefile-gmake-mode'
315 ;; and similar derivatives of `makefile-mode'.
317 (let ((mode major-mode
)) ;; Look for `mode' with some setups.
318 (while (and mode
(not info-lookup-mode
))
319 (dolist (topic-cell info-lookup-alist
) ;; Usually only two topics here.
320 (if (info-lookup->mode-value
(car topic-cell
) mode
)
321 (setq info-lookup-mode mode
)))
322 (setq mode
(get mode
'derived-mode-parent
))))
324 (or info-lookup-mode
(setq info-lookup-mode major-mode
)))
326 (defun info-lookup-change-mode (topic)
327 (let* ((completions (mapcar (lambda (arg)
328 (cons (symbol-name (car arg
)) (car arg
)))
329 (info-lookup->topic-value topic
)))
330 (mode (completing-read
331 (format "Use %s help mode: " topic
)
332 completions nil t nil
'info-lookup-history
)))
333 (or (setq mode
(cdr (assoc mode completions
)))
334 (error "No %s help available" topic
))
335 (or (info-lookup->mode-value topic mode
)
336 (error "No %s help available for `%s'" topic mode
))
337 (setq info-lookup-mode mode
)))
339 (defun info-lookup (topic item mode
)
340 "Display the documentation of a help item."
341 (or mode
(setq mode
(info-lookup-select-mode)))
342 (or (info-lookup->mode-value topic mode
)
343 (error "No %s help available for `%s'" topic mode
))
344 (let* ((completions (info-lookup->completions topic mode
))
345 (ignore-case (info-lookup->ignore-case topic mode
))
346 (entry (or (assoc (if ignore-case
(downcase item
) item
) completions
)
347 (assoc-string item completions t
)
348 (error "Not documented as a %s: %s" topic
(or item
""))))
349 (modes (info-lookup->all-modes topic mode
))
350 (window (selected-window))
352 ;; Avoid clobbering Info-history with nodes searched during
353 ;; lookup. If lookup succeeds set `Info-history' to
354 ;; `new-Info-history'.
355 (when (get-buffer "*info*")
356 (with-current-buffer "*info*"
357 (cons (list Info-current-file Info-current-node
(point))
359 found doc-spec node prefix suffix doc-found
)
360 (unless (eq major-mode
'Info-mode
)
361 (if (not info-lookup-other-window-flag
)
363 (save-window-excursion (info))
364 (let* ((info-window (get-buffer-window "*info*" t
))
365 (info-frame (and info-window
(window-frame info-window
))))
367 (not (eq info-frame
(selected-frame)))
368 (display-multi-frame-p)
369 (memq info-frame
(frames-on-display-list)))
370 ;; *info* is visible in another frame on same display.
371 ;; Raise that frame and select the window.
373 (select-window info-window
)
374 (raise-frame info-frame
))
375 ;; In any other case, switch to *info* in another window.
376 (switch-to-buffer-other-window "*info*")))))
377 (while (and (not found
) modes
)
378 (setq doc-spec
(info-lookup->doc-spec topic
(car modes
)))
379 (while (and (not found
) doc-spec
)
380 (setq node
(nth 0 (car doc-spec
))
381 prefix
(nth 2 (car doc-spec
))
382 suffix
(nth 3 (car doc-spec
)))
383 (when (condition-case nil
385 ;; Don't need Index menu fontifications here, and
386 ;; they slow down the lookup.
387 (let (Info-fontify-maximum-menu-size
389 (Info-goto-node node
)
392 (message "Cannot access Info node %s" node
)
397 ;; Don't use Info-menu, it forces case-fold-search to t
398 (let ((case-fold-search nil
))
400 (concat "^\\* " (regexp-quote (or (cdr entry
) (car entry
)))
402 (Info-follow-nearest-node)
404 (if (or prefix suffix
)
405 (let ((case-fold-search
406 (info-lookup->ignore-case topic
(car modes
)))
407 (buffer-read-only nil
))
408 (goto-char (point-min))
410 (concat prefix
(regexp-quote (car entry
)) suffix
))
411 (goto-char (match-beginning 0))
412 (and (display-color-p) info-lookup-highlight-face
413 ;; Search again for ITEM so that the first
414 ;; occurrence of ITEM will be highlighted.
415 (re-search-forward (regexp-quote (car entry
)))
416 (let ((start (match-beginning 0))
418 (if (overlayp info-lookup-highlight-overlay
)
419 (move-overlay info-lookup-highlight-overlay
420 start end
(current-buffer))
421 (setq info-lookup-highlight-overlay
422 (make-overlay start end
))))
423 (overlay-put info-lookup-highlight-overlay
424 'face info-lookup-highlight-face
)))))
426 (setq doc-spec
(cdr doc-spec
)))
427 (setq modes
(cdr modes
)))
428 ;; Alert the user if case was munged, and do this after bringing up the
429 ;; info buffer since that can print messages
430 (unless (or ignore-case
431 (string-equal item
(car entry
)))
432 (message "Found in different case: %s" (car entry
)))
434 (setq Info-history new-Info-history
))
436 (error "Info documentation for lookup was not found"))
437 ;; Don't leave the Info buffer if the help item couldn't be looked up.
438 (if (and info-lookup-other-window-flag found
)
439 (select-window window
))))
441 (defun info-lookup-setup-mode (topic mode
)
442 "Initialize the internal data structure."
443 (or (info-lookup->initialized topic mode
)
444 (let ((initialized 0)
445 cell data completions refer-modes Info-history-list
)
446 (if (not (info-lookup->mode-value topic mode
))
447 (message "No %s help available for `%s'" topic mode
)
448 ;; Recursively setup cross references.
449 ;; But refer only to non-void modes.
450 (dolist (arg (info-lookup->other-modes topic mode
))
451 (or (info-lookup->initialized topic arg
)
452 (info-lookup-setup-mode topic arg
))
453 (and (eq (info-lookup->initialized topic arg
) t
)
454 (setq refer-modes
(cons arg refer-modes
))))
455 (setq refer-modes
(nreverse refer-modes
))
456 ;; Build the full completion alist.
458 (nconc (condition-case nil
459 (info-lookup-make-completions topic mode
)
462 (mapcar (lambda (arg)
463 (info-lookup->completions topic arg
))
465 (setq initialized t
))
466 ;; Update `info-lookup-cache'.
467 (setq cell
(info-lookup->mode-cache topic mode
)
468 data
(list initialized completions refer-modes
))
470 (setcdr (info-lookup->cache topic
)
471 (cons (cons mode data
) (info-lookup->topic-cache topic
)))
475 (defun info-lookup-make-completions (topic mode
)
476 "Create a unique alist from all index entries."
477 (let ((doc-spec (info-lookup->doc-spec topic mode
))
478 (regexp (concat "^\\(" (info-lookup->regexp topic mode
)
479 "\\)\\([ \t].*\\)?$"))
480 Info-history-list Info-fontify-maximum-menu-size
481 node trans entry item prefix result doc-found
482 (buffer (get-buffer-create " temp-info-look")))
483 (with-current-buffer buffer
486 (setq node
(nth 0 (car doc-spec
))
487 trans
(cond ((eq (nth 1 (car doc-spec
)) nil
)
489 (if (string-match regexp arg
)
490 (match-string 1 arg
))))
491 ((stringp (nth 1 (car doc-spec
)))
492 (setq prefix
(nth 1 (car doc-spec
)))
494 (if (string-match "^\\([^: \t\n]+\\)" arg
)
495 (concat prefix
(match-string 1 arg
)))))
496 (t (nth 1 (car doc-spec
)))))
497 (with-current-buffer buffer
498 (message "Processing Info node `%s'..." node
)
499 (when (condition-case nil
501 (Info-goto-node node
)
504 (message "Cannot access Info node `%s'" node
)
509 (goto-char (point-min))
510 (and (search-forward "\n* Menu:" nil t
)
511 (while (re-search-forward "\n\\* \\(.*\\): " nil t
)
512 (setq entry
(match-string 1)
513 item
(funcall trans entry
))
514 ;; `trans' can return nil if the regexp doesn't match.
516 ;; Sometimes there's more than one Menu:
517 (not (string= entry
"Menu")))
518 (and (info-lookup->ignore-case topic mode
)
519 (setq item
(downcase item
)))
520 (and (string-equal entry item
)
522 (and (or (assoc item result
)
523 (setq result
(cons (cons item entry
)
526 (message "Processing Info node `%s'...done" node
)
527 (setq doc-spec
(cdr doc-spec
)))
529 (error "Info documentation for lookup was not found"))
532 (defun info-lookup-guess-default (topic mode
)
533 "Return a guess for a symbol to look up, based on text around point.
534 Try all related modes applicable to TOPIC and MODE.
535 Return nil if there is nothing appropriate in the buffer near point."
536 (let ((modes (info-lookup->all-modes topic mode
))
538 (while (and (not guess
) modes
)
539 (setq guess
(info-lookup-guess-default* topic
(car modes
))
541 ;; Collapse whitespace characters.
544 (while (string-match "[ \t\n]+" guess pos
)
545 (setq pos
(1+ (match-beginning 0)))
546 (setq guess
(replace-match " " t t guess
)))))
549 (defun info-lookup-guess-default* (topic mode
)
550 (let ((case-fold-search (info-lookup->ignore-case topic mode
))
551 (rule (or (info-lookup->parse-rule topic mode
)
552 (info-lookup->regexp topic mode
)))
553 (start (point)) end regexp subexp result
)
556 (setq result
(funcall rule
))
558 (setq regexp
(car rule
)
562 ;; If at start of symbol, don't go back to end of previous one.
564 (looking-at "[ \t\n]"))
565 (skip-chars-backward " \t\n"))
567 (while (and (re-search-backward regexp nil t
)
569 (>= (match-end 0) end
))
570 (setq result
(match-string subexp
)))
574 (skip-chars-forward " \t\n")
575 (and (looking-at regexp
)
576 (setq result
(match-string subexp
)))))))
579 (defun info-lookup-guess-c-symbol ()
580 "Get the C symbol at point."
583 (skip-syntax-backward "w_")
584 (let ((start (point)) prefix name
)
585 ;; Test for a leading `struct', `union', or `enum' keyword
586 ;; but ignore names like `foo_struct'.
587 (setq prefix
(and (< (skip-chars-backward " \t\n") 0)
588 (< (skip-chars-backward "_a-zA-Z0-9") 0)
589 (looking-at "\\(struct\\|union\\|enum\\)\\s ")
590 (concat (match-string 1) " ")))
592 (and (looking-at "[_a-zA-Z][_a-zA-Z0-9]*")
593 (setq name
(match-string 0)))
594 ;; Caveat! Look forward if point is at `struct' etc.
596 (or (string-equal name
"struct")
597 (string-equal name
"union")
598 (string-equal name
"enum"))
599 (looking-at "[a-z]+\\s +\\([_a-zA-Z][_a-zA-Z0-9]*\\)")
600 (setq prefix
(concat name
" ")
601 name
(match-string 1)))
602 (and (or prefix name
)
603 (concat prefix name
))))
606 (defun info-lookup-guess-custom-symbol ()
607 "Get symbol at point in custom buffers."
610 (let ((case-fold-search t
)
611 (ignored-chars "][()`',:.\" \t\n")
612 (significant-chars "^][()`',:.\" \t\n")
615 ((and (memq (get-char-property (point) 'face
)
616 '(custom-variable-tag custom-variable-tag-face
))
617 (setq beg
(previous-single-char-property-change
618 (point) 'face nil
(line-beginning-position)))
619 (setq end
(next-single-char-property-change
620 (point) 'face nil
(line-end-position)))
622 (subst-char-in-string
623 ?\s ?\-
(buffer-substring-no-properties beg end
)))
624 ((or (and (looking-at (concat "[" significant-chars
"]"))
626 (skip-chars-backward significant-chars
)
628 (skip-chars-forward significant-chars
)
631 (and (looking-at "[ \t\n]")
632 (looking-back (concat "[" significant-chars
"]"))
634 (skip-chars-backward significant-chars
)
637 (and (skip-chars-forward ignored-chars
)
639 (skip-chars-forward significant-chars
)
642 (buffer-substring-no-properties beg end
)))))
646 (defun info-complete-symbol (&optional mode
)
647 "Perform completion on symbol preceding point."
649 (info-complete 'symbol
651 (if (info-lookup->mode-value
652 'symbol
(info-lookup-select-mode))
654 (info-lookup-change-mode 'symbol
)))))
657 (defun info-complete-file (&optional mode
)
658 "Perform completion on file preceding point."
662 (if (info-lookup->mode-value
663 'file
(info-lookup-select-mode))
665 (info-lookup-change-mode 'file
)))))
667 (defun info-lookup-completions-at-point (topic mode
)
668 "Try to complete a help item."
669 (or mode
(setq mode
(info-lookup-select-mode)))
670 (when (info-lookup->mode-value topic mode
)
671 (let ((modes (info-lookup-quick-all-modes topic mode
))
674 (while (and (not try
) modes
)
675 (setq mode
(car modes
)
677 try
(info-lookup-guess-default* topic mode
))
680 (let ((completions (info-lookup->completions topic mode
)))
682 (when (info-lookup->ignore-case topic mode
)
684 (lambda (string pred action
)
685 (let ((completion-ignore-case t
))
686 (complete-with-action
687 action completions string pred
)))))
689 ;; Find the original symbol and zap it.
691 (while (and (search-backward try nil t
)
693 (list (match-beginning 0) (match-end 0) completions
694 :exclusive
'no
))))))))
696 (defun info-complete (topic mode
)
697 "Try to complete a help item."
698 (barf-if-buffer-read-only)
699 (let ((data (info-lookup-completions-at-point topic mode
)))
701 (error "No %s completion available for `%s' at point" topic mode
)
702 (completion-in-region (nth 0 data
) (nth 1 data
) (nth 2 data
)))))
705 ;;; Initialize some common modes.
707 (info-lookup-maybe-add-help
708 :mode
'c-mode
:topic
'symbol
709 :regexp
"\\(struct \\|union \\|enum \\)?[_a-zA-Z][_a-zA-Z0-9]*"
710 :doc-spec
'(("(libc)Function Index" nil
711 "^[ \t]+-+ \\(Function\\|Macro\\): .*\\<" "\\>")
712 ;; prefix/suffix has to match things like
713 ;; " -- Macro: int F_DUPFD"
714 ;; " -- Variable: char * tzname [2]"
715 ;; "`DBL_MAX'" (texinfo @table)
716 ;; suffix "\\>" is not used because that sends DBL_MAX to
717 ;; DBL_MAX_EXP ("_" is a non-word char)
718 ("(libc)Variable Index" nil
719 "^\\([ \t]+-+ \\(Variable\\|Macro\\): .*\\<\\|`\\)"
721 ("(libc)Type Index" nil
722 "^[ \t]+-+ Data Type: \\<" "\\>")
723 ("(termcap)Var Index" nil
725 :parse-rule
'info-lookup-guess-c-symbol
)
727 (info-lookup-maybe-add-help
728 :mode
'c-mode
:topic
'file
729 :regexp
"[_a-zA-Z0-9./+-]+"
730 :doc-spec
'(("(libc)File Index")))
732 (info-lookup-maybe-add-help
734 :regexp
"[:;|]\\|%\\([%{}]\\|[_a-z]+\\)\\|YY[_A-Z]+\\|yy[_a-z]+"
735 :doc-spec
'(("(bison)Index" nil
737 :parse-rule
"[:;|]\\|%\\([%{}]\\|[_a-zA-Z][_a-zA-Z0-9]*\\)"
738 :other-modes
'(c-mode))
740 (info-lookup-maybe-add-help
742 :regexp
"\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z][_a-zA-Z0-9-]*"
743 :doc-spec
'(("(make)Name Index" nil
745 :parse-rule
"\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z0-9-]+")
747 (info-lookup-maybe-add-help
749 :mode
'makefile-automake-mode
750 ;; similar regexp/parse-rule as makefile-mode, but also the following
751 ;; (which have index entries),
752 ;; "##" special automake comment
753 ;; "+=" append operator, separate from the GNU make one
754 :regexp
"\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z][_a-zA-Z0-9-]*\\|##\\|\\+="
755 :parse-rule
"\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z0-9-]+\\|##\\|\\+="
757 ;; "(automake)Macro Index" is autoconf macros used in
758 ;; configure.ac, not Makefile.am, so don't have that here.
759 ("(automake)Variable Index" nil
"^[ \t]*`" "'")
760 ;; In automake 1.4 macros and variables were a combined node.
761 ("(automake)Macro and Variable Index" nil
"^[ \t]*`" "'")
762 ;; Directives like "if" are in the "General Index".
763 ;; Prefix "`" since the text for say `+=' isn't always an
764 ;; @item etc and so not always at the start of a line.
765 ("(automake)General Index" nil
"`" "'")
766 ;; In automake 1.3 there was just a single "Index" node.
767 ("(automake)Index" nil
"`" "'"))
768 :other-modes
'(makefile-mode))
770 (info-lookup-maybe-add-help
772 :regexp
"@\\([a-zA-Z]+\\|[^a-zA-Z]\\)"
773 :doc-spec
'(("(texinfo)Command and Variable Index"
774 ;; Ignore Emacs commands and prepend a `@'.
776 (if (string-match "^\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\( .*\\)?$" item
)
777 (concat "@" (match-string 1 item
))))
780 (info-lookup-maybe-add-help
782 :regexp
"[_a-zA-Z][_a-zA-Z0-9]*"
783 :doc-spec
'(("(m4)Macro index"))
784 :parse-rule
"[_a-zA-Z0-9]+")
786 (info-lookup-maybe-add-help
788 :regexp
"A[CM]_[_A-Z0-9]+"
789 :doc-spec
'(;; Autoconf Macro Index entries are without an "AC_" prefix,
790 ;; but with "AH_" or "AU_" for those. So add "AC_" if there
791 ;; isn't already an "A._".
792 ("(autoconf)Autoconf Macro Index"
794 (if (string-match "^A._" item
) item
(concat "AC_" item
)))
795 "^[ \t]+-+ \\(Macro\\|Variable\\): .*\\<" "\\>")
796 ;; M4 Macro Index entries are without "AS_" prefixes, and
797 ;; mostly without "m4_" prefixes. "dnl" is an exception, not
798 ;; wanting any prefix. So AS_ is added back to upper-case
799 ;; names (if needed), m4_ to others which don't already an m4_.
800 ("(autoconf)M4 Macro Index"
802 (let ((case-fold-search nil
))
803 (cond ((or (string-equal item
"dnl")
804 (string-match "^m4_" item
)
805 ;; Autoconf 2.62 index includes some macros
806 ;; (e.g., AS_HELP_STRING), so avoid prefixing.
807 (string-match "^AS_" item
))
809 ((string-match "^[A-Z0-9_]+$" item
)
812 (concat "m4_" item
)))))
813 "^[ \t]+-+ Macro: .*\\<" "\\>")
814 ;; Autotest Macro Index entries are without "AT_".
815 ("(autoconf)Autotest Macro Index" "AT_"
816 "^[ \t]+-+ Macro: .*\\<" "\\>")
817 ;; This is for older versions (probably pre autoconf 2.5x):
818 ("(autoconf)Macro Index" "AC_"
819 "^[ \t]+-+ \\(Macro\\|Variable\\): .*\\<" "\\>")
820 ;; Automake has index entries for its notes on various autoconf
821 ;; macros (eg. AC_PROG_CC). Ensure this is after the autoconf
822 ;; index, so as to prefer the autoconf docs.
823 ("(automake)Macro and Variable Index" nil
825 ;; Autoconf symbols are M4 macros. Thus use M4's parser.
827 :other-modes
'(m4-mode))
829 (info-lookup-maybe-add-help
832 :doc-spec
'(("(gawk)Index"
834 (let ((case-fold-search nil
))
836 ;; `BEGIN' and `END'.
837 ((string-match "^\\([A-Z]+\\) special pattern\\b" item
)
838 (match-string 1 item
))
839 ;; `if', `while', `do', ...
840 ((string-match "^\\([a-z]+\\) statement\\b" item
)
841 (if (not (string-equal (match-string 1 item
) "control"))
842 (match-string 1 item
)))
844 ((string-match "^[A-Z]+$" item
)
846 ;; Built-in functions (matches to many entries).
847 ((string-match "^[a-z]+$" item
)
849 "`" "\\([ \t]*([^)]*)\\)?'")))
851 (info-lookup-maybe-add-help
853 :regexp
"[$@%][^a-zA-Z]\\|\\$\\^[A-Z]\\|[$@%]?[a-zA-Z][_a-zA-Z0-9]*"
854 :doc-spec
'(("(perl5)Function Index"
856 (if (string-match "^\\([a-zA-Z0-9]+\\)" item
)
857 (match-string 1 item
)))
859 ("(perl5)Variable Index"
861 ;; Work around bad formatted array variables.
862 (let ((sym (cond ((or (string-match "^\\$\\(.\\|@@\\)$" item
)
863 (string-match "^\\$\\^[A-Z]$" item
))
866 "^\\([$%@]\\|@@\\)?[_a-zA-Z0-9]+" item
)
867 (match-string 0 item
))
869 (if (string-match "@@" sym
)
870 (setq sym
(concat (substring sym
0 (match-beginning 0))
871 (substring sym
(1- (match-end 0))))))
872 (if (string-equal sym
"") nil sym
)))
874 :parse-rule
"[$@%]?\\([_a-zA-Z0-9]+\\|[^a-zA-Z]\\)")
876 (info-lookup-maybe-add-help
878 :regexp
"[$@%][^a-zA-Z]\\|\\$\\^[A-Z]\\|[$@%]?[a-zA-Z][_a-zA-Z0-9]*"
879 :other-modes
'(perl-mode))
881 (info-lookup-maybe-add-help
883 :regexp
"\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)"
884 :doc-spec
'(("(latex)Command Index" nil
885 "`" "\\({[^}]*}\\)?'")))
887 (info-lookup-maybe-add-help
888 :mode
'emacs-lisp-mode
889 :regexp
"[^][()`',\" \t\n]+"
890 :doc-spec
'(;; Commands with key sequences appear in nodes as `foo' and
891 ;; those without as `M-x foo'.
892 ("(emacs)Command Index" nil
"`\\(M-x[ \t\n]+\\)?" "'")
893 ;; Variables normally appear in nodes as just `foo'.
894 ("(emacs)Variable Index" nil
"`" "'")
895 ;; Almost all functions, variables, etc appear in nodes as
896 ;; " -- Function: foo" etc. A small number of aliases and
897 ;; symbols appear only as `foo', and will miss out on exact
898 ;; positions. Allowing `foo' would hit too many false matches
899 ;; for things that should go to Function: etc, and those latter
900 ;; are much more important. Perhaps this could change if some
901 ;; sort of fallback match scheme existed.
902 ("(elisp)Index" nil
"^ -+ .*: " "\\( \\|$\\)")))
904 ;; docstrings talk about elisp, so have apropos-mode follow emacs-lisp-mode
905 (info-lookup-maybe-add-help
907 :regexp
"[^][()`',\" \t\n]+" ;; same as emacs-lisp-mode above
908 :other-modes
'(emacs-lisp-mode))
910 (info-lookup-maybe-add-help
911 :mode
'lisp-interaction-mode
912 :regexp
"[^][()`',\" \t\n]+"
914 :other-modes
'(emacs-lisp-mode))
916 (info-lookup-maybe-add-help
918 :regexp
"[^()`',\" \t\n]+"
920 :other-modes
'(emacs-lisp-mode))
922 (info-lookup-maybe-add-help
924 :regexp
"[^()`',\" \t\n]+"
926 ;; Aubrey Jaffer's rendition from <URL:ftp://ftp-swiss.ai.mit.edu/pub/scm>
927 :doc-spec
'(("(r5rs)Index" nil
928 "^[ \t]+-+ [^:]+:[ \t]*" "\\b")))
930 (info-lookup-maybe-add-help
932 :regexp
"[_a-zA-Z0-9]+\\|\\s.+\\|[-!=^|*/.\\,><~&+]\\{1,3\\}\\|[][();,\"']"
933 :doc-spec
'(("(octave)Function Index" nil
934 "^ -+ [^:]+:[ ]+\\(\\[[^=]*=[ ]+\\)?" nil
)
935 ("(octave)Variable Index" nil
"^ -+ [^:]+:[ ]+" nil
)
936 ("(octave)Operator Index" nil nil nil
)
937 ;; Catch lines of the form "xyz statement"
938 ("(octave)Concept Index"
941 ((string-match "^\\([A-Z]+\\) statement\\b" item
)
942 (match-string 1 item
))
944 nil
; "^ -+ [^:]+:[ ]+" don't think this prefix is useful here.
947 (info-lookup-maybe-add-help
950 :regexp
"[a-zA-Z0-9_%]+"
951 :doc-spec
'( ("(maxima)Function and Variable Index" nil
952 "^ -+ [^:]+:[ ]+\\(\\[[^=]*=[ ]+\\)?" nil
)))
954 (info-lookup-maybe-add-help
955 :mode
'inferior-maxima-mode
956 :regexp
"[a-zA-Z0-9_%]+"
957 :other-modes
'(maxima-mode))
959 ;; coreutils and bash builtins overlap in places, eg. printf, so there's a
960 ;; question which should come first. Some of the coreutils descriptions are
961 ;; more detailed, but if bash is usually /bin/sh on a GNU system then the
962 ;; builtins will be what's normally run.
964 ;; Maybe special variables like $? should be matched as $?, not just ?.
965 ;; This would avoid a clash between variable $! and negation !, or variable
966 ;; $# and comment # (though comment # is not currently indexed in bash).
967 ;; Unfortunately if $? etc is the symbol, then we wouldn't be taken to the
968 ;; exact spot in the relevant node, since the bash manual has just `?' etc
969 ;; there. Maybe an extension to the prefix/suffix scheme could help this.
971 (info-lookup-maybe-add-help
972 :mode
'sh-mode
:topic
'symbol
973 ;; bash has "." and ":" in its index, but those chars will probably never
974 ;; work in info, so don't bother matching them in the regexp.
975 :regexp
"\\([a-zA-Z0-9_-]+\\|[!{}@*#?$]\\|\\[\\[?\\|]]?\\)"
976 :doc-spec
'(("(bash)Builtin Index" nil
"^`" "[ .']")
977 ("(bash)Reserved Word Index" nil
"^`" "[ .']")
978 ("(bash)Variable Index" nil
"^`" "[ .']")
980 ;; coreutils (version 4.5.10) doesn't have a separate program
981 ;; index, so exclude extraneous stuff (most of it) by demanding
982 ;; "[a-z]+" in the trans-func.
983 ;; coreutils version 8.1 has node "Concept Index" and past
984 ;; versions have node "Index", look for both, whichever is
985 ;; absent is quietly ignored
987 (lambda (item) (if (string-match "\\`[a-z]+\\'" item
) item
)))
988 ("(coreutils)Concept Index"
989 (lambda (item) (if (string-match "\\`[a-z]+\\'" item
) item
)))
991 ;; diff (version 2.8.1) has only a few programs, index entries
992 ;; are things like "foo invocation".
995 (if (string-match "\\`\\([a-z]+\\) invocation\\'" item
)
996 (match-string 1 item
))))
997 ;; there's no plain "sed" index entry as such, mung another
998 ;; hopefully unique one to get to the invocation section
999 ("(sed)Concept Index"
1001 (if (string-equal item
"Standard input, processing as input")
1003 ;; there's no plain "awk" or "gawk" index entries, mung other
1004 ;; hopefully unique ones to get to the command line options
1007 (cond ((string-equal item
"gawk, extensions, disabling")
1009 ((string-equal item
"gawk, versions of, information about, printing")
1012 ;; This misses some things which occur as node names but not in the
1013 ;; index. Unfortunately it also picks up the wrong one of multiple
1014 ;; entries for the same term in some cases. --fx
1015 (info-lookup-maybe-add-help
1016 :mode
'cfengine-mode
1017 :regexp
"[[:alnum:]_]+\\(?:()\\)?"
1018 :doc-spec
'(("(cfengine-Reference)Variable Index"
1020 ;; Index entries may be like `IsPlain()'
1021 (if (string-match "\\([[:alnum:]_]+\\)()" item
)
1022 (match-string 1 item
)
1024 ;; This gets functions in evaluated classes. Other
1025 ;; possible patterns don't seem to work too well.
1028 (info-lookup-maybe-add-help
1031 :regexp
"[^][()`',:\" \t\n]+"
1032 :parse-rule
'info-lookup-guess-custom-symbol
1033 :other-modes
'(emacs-lisp-mode))
1035 (info-lookup-maybe-add-help
1037 :regexp
"[^][()`',:\" \t\n]+"
1038 :other-modes
'(emacs-lisp-mode))
1040 (provide 'info-look
)
1042 ;;; info-look.el ends here