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