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