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