Merge from gnulib
[emacs.git] / lisp / info-look.el
blob9cf185edf573d70e0ed3e7635da28dd4639b1f32
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-2015 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 (setq end (point))
639 (skip-chars-backward significant-chars)
640 (setq beg (point))
641 (> end beg))
642 (and (skip-chars-forward ignored-chars)
643 (setq beg (point))
644 (skip-chars-forward significant-chars)
645 (setq end (point))
646 (> end beg)))
647 (buffer-substring-no-properties beg end)))))
648 (error nil)))
650 ;;;###autoload
651 (defun info-complete-symbol (&optional mode)
652 "Perform completion on symbol preceding point."
653 (interactive)
654 (info-complete 'symbol
655 (or mode
656 (if (info-lookup->mode-value
657 'symbol (info-lookup-select-mode))
658 info-lookup-mode
659 (info-lookup-change-mode 'symbol)))))
661 ;;;###autoload
662 (defun info-complete-file (&optional mode)
663 "Perform completion on file preceding point."
664 (interactive)
665 (info-complete 'file
666 (or mode
667 (if (info-lookup->mode-value
668 'file (info-lookup-select-mode))
669 info-lookup-mode
670 (info-lookup-change-mode 'file)))))
672 (defun info-lookup-completions-at-point (topic mode)
673 "Try to complete a help item."
674 (or mode (setq mode (info-lookup-select-mode)))
675 (when (info-lookup->mode-value topic mode)
676 (let ((modes (info-lookup-quick-all-modes topic mode))
677 (start (point))
678 try)
679 (while (and (not try) modes)
680 (setq mode (car modes)
681 modes (cdr modes)
682 try (info-lookup-guess-default* topic mode))
683 (goto-char start))
684 (when try
685 (let ((completions (info-lookup->completions topic mode)))
686 (when completions
687 (when (info-lookup->ignore-case topic mode)
688 (setq completions
689 (lambda (string pred action)
690 (let ((completion-ignore-case t))
691 (complete-with-action
692 action completions string pred)))))
693 (save-excursion
694 ;; Find the original symbol and zap it.
695 (end-of-line)
696 (while (and (search-backward try nil t)
697 (< start (point))))
698 (list (match-beginning 0) (match-end 0) completions
699 :exclusive 'no))))))))
701 (defun info-complete (topic mode)
702 "Try to complete a help item."
703 (barf-if-buffer-read-only)
704 (let ((data (info-lookup-completions-at-point topic mode)))
705 (if (null data)
706 (error "No %s completion available for `%s' at point" topic mode)
707 (completion-in-region (nth 0 data) (nth 1 data) (nth 2 data)))))
710 ;;; Initialize some common modes.
712 (info-lookup-maybe-add-help
713 :mode 'c-mode :topic 'symbol
714 :regexp "\\(struct \\|union \\|enum \\)?[_a-zA-Z][_a-zA-Z0-9]*"
715 :doc-spec '(("(libc)Function Index" nil
716 "^[ \t]+-+ \\(Function\\|Macro\\): .*\\<" "\\>")
717 ;; prefix/suffix has to match things like
718 ;; " -- Macro: int F_DUPFD"
719 ;; " -- Variable: char * tzname [2]"
720 ;; "`DBL_MAX'" (texinfo @table)
721 ;; suffix "\\>" is not used because that sends DBL_MAX to
722 ;; DBL_MAX_EXP ("_" is a non-word char)
723 ("(libc)Variable Index" nil
724 "^\\([ \t]+-+ \\(Variable\\|Macro\\): .*\\<\\|['`‘]\\)"
725 "\\( \\|['’]?$\\)")
726 ("(libc)Type Index" nil
727 "^[ \t]+-+ Data Type: \\<" "\\>")
728 ("(termcap)Var Index" nil
729 "^[ \t]*['`‘]" "['’]"))
730 :parse-rule 'info-lookup-guess-c-symbol)
732 (info-lookup-maybe-add-help
733 :mode 'c-mode :topic 'file
734 :regexp "[_a-zA-Z0-9./+-]+"
735 :doc-spec '(("(libc)File Index")))
737 (info-lookup-maybe-add-help
738 :mode 'bison-mode
739 :regexp "[:;|]\\|%\\([%{}]\\|[_a-z]+\\)\\|YY[_A-Z]+\\|yy[_a-z]+"
740 :doc-spec '(("(bison)Index" nil
741 "['`‘]" "['’]"))
742 :parse-rule "[:;|]\\|%\\([%{}]\\|[_a-zA-Z][_a-zA-Z0-9]*\\)"
743 :other-modes '(c-mode))
745 (info-lookup-maybe-add-help
746 :mode 'makefile-mode
747 :regexp "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z][_a-zA-Z0-9-]*"
748 :doc-spec '(("(make)Name Index" nil
749 "^[ \t]*['`‘]" "['’]"))
750 :parse-rule "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z0-9-]+")
752 (info-lookup-maybe-add-help
753 :topic 'symbol
754 :mode 'makefile-automake-mode
755 ;; similar regexp/parse-rule as makefile-mode, but also the following
756 ;; (which have index entries),
757 ;; "##" special automake comment
758 ;; "+=" append operator, separate from the GNU make one
759 :regexp "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z][_a-zA-Z0-9-]*\\|##\\|\\+="
760 :parse-rule "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z0-9-]+\\|##\\|\\+="
761 :doc-spec '(
762 ;; "(automake)Macro Index" is autoconf macros used in
763 ;; configure.ac, not Makefile.am, so don't have that here.
764 ("(automake)Variable Index" nil "^[ \t]*['`‘]" "['’]")
765 ;; In automake 1.4 macros and variables were a combined node.
766 ("(automake)Macro and Variable Index" nil "^[ \t]*['`‘]"
767 "['’]")
768 ;; Directives like "if" are in the "General Index".
769 ;; Prefix "`" since the text for say `+=' isn't always an
770 ;; @item etc and so not always at the start of a line.
771 ("(automake)General Index" nil "['`‘]" "['’]")
772 ;; In automake 1.3 there was just a single "Index" node.
773 ("(automake)Index" nil "['`‘]" "['’]"))
774 :other-modes '(makefile-mode))
776 (info-lookup-maybe-add-help
777 :mode 'texinfo-mode
778 :regexp "@\\([a-zA-Z]+\\|[^a-zA-Z]\\)"
779 :doc-spec '(("(texinfo)Command and Variable Index"
780 ;; Ignore Emacs commands and prepend a `@'.
781 (lambda (item)
782 (if (string-match "^\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\( .*\\)?$" item)
783 (concat "@" (match-string 1 item))))
784 "['`‘]" "['’ ]")))
786 (info-lookup-maybe-add-help
787 :mode 'm4-mode
788 :regexp "[_a-zA-Z][_a-zA-Z0-9]*"
789 :doc-spec '(("(m4)Macro index"))
790 :parse-rule "[_a-zA-Z0-9]+")
792 (info-lookup-maybe-add-help
793 :mode 'autoconf-mode
794 :regexp "A[CM]_[_A-Z0-9]+"
795 :doc-spec '(;; Autoconf Macro Index entries are without an "AC_" prefix,
796 ;; but with "AH_" or "AU_" for those. So add "AC_" if there
797 ;; isn't already an "A._".
798 ("(autoconf)Autoconf Macro Index"
799 (lambda (item)
800 (if (string-match "^A._" item) item (concat "AC_" item)))
801 "^[ \t]+-+ \\(Macro\\|Variable\\): .*\\<" "\\>")
802 ;; M4 Macro Index entries are without "AS_" prefixes, and
803 ;; mostly without "m4_" prefixes. "dnl" is an exception, not
804 ;; wanting any prefix. So AS_ is added back to upper-case
805 ;; names (if needed), m4_ to others which don't already an m4_.
806 ("(autoconf)M4 Macro Index"
807 (lambda (item)
808 (let ((case-fold-search nil))
809 (cond ((or (string-equal item "dnl")
810 (string-match "^m4_" item)
811 ;; Autoconf 2.62 index includes some macros
812 ;; (e.g., AS_HELP_STRING), so avoid prefixing.
813 (string-match "^AS_" item))
814 item)
815 ((string-match "^[A-Z0-9_]+$" item)
816 (concat "AS_" item))
818 (concat "m4_" item)))))
819 "^[ \t]+-+ Macro: .*\\<" "\\>")
820 ;; Autotest Macro Index entries are without "AT_".
821 ("(autoconf)Autotest Macro Index" "AT_"
822 "^[ \t]+-+ Macro: .*\\<" "\\>")
823 ;; This is for older versions (probably pre autoconf 2.5x):
824 ("(autoconf)Macro Index" "AC_"
825 "^[ \t]+-+ \\(Macro\\|Variable\\): .*\\<" "\\>")
826 ;; Automake has index entries for its notes on various autoconf
827 ;; macros (eg. AC_PROG_CC). Ensure this is after the autoconf
828 ;; index, so as to prefer the autoconf docs.
829 ("(automake)Macro and Variable Index" nil
830 "^[ \t]*['`‘]" "['’]"))
831 ;; Autoconf symbols are M4 macros. Thus use M4's parser.
832 :parse-rule 'ignore
833 :other-modes '(m4-mode))
835 (info-lookup-maybe-add-help
836 :mode 'awk-mode
837 :regexp "[_a-zA-Z]+"
838 :doc-spec '(("(gawk)Index"
839 (lambda (item)
840 (let ((case-fold-search nil))
841 (cond
842 ;; `BEGIN' and `END'.
843 ((string-match "^\\([A-Z]+\\) special pattern\\b" item)
844 (match-string 1 item))
845 ;; `if', `while', `do', ...
846 ((string-match "^\\([a-z]+\\) statement\\b" item)
847 (if (not (string-equal (match-string 1 item) "control"))
848 (match-string 1 item)))
849 ;; `NR', `NF', ...
850 ((string-match "^[A-Z]+$" item)
851 item)
852 ;; Built-in functions (matches to many entries).
853 ((string-match "^[a-z]+$" item)
854 item))))
855 "['`‘]" "\\([ \t]*([^)]*)\\)?['’]")))
857 (info-lookup-maybe-add-help
858 :mode 'perl-mode
859 :regexp "[$@%][^a-zA-Z]\\|\\$\\^[A-Z]\\|[$@%]?[a-zA-Z][_a-zA-Z0-9]*"
860 :doc-spec '(("(perl5)Function Index"
861 (lambda (item)
862 (if (string-match "^\\([a-zA-Z0-9]+\\)" item)
863 (match-string 1 item)))
864 "^" "\\b")
865 ("(perl5)Variable Index"
866 (lambda (item)
867 ;; Work around bad formatted array variables.
868 (let ((sym (cond ((or (string-match "^\\$\\(.\\|@@\\)$" item)
869 (string-match "^\\$\\^[A-Z]$" item))
870 item)
871 ((string-match
872 "^\\([$%@]\\|@@\\)?[_a-zA-Z0-9]+" item)
873 (match-string 0 item))
874 (t ""))))
875 (if (string-match "@@" sym)
876 (setq sym (concat (substring sym 0 (match-beginning 0))
877 (substring sym (1- (match-end 0))))))
878 (if (string-equal sym "") nil sym)))
879 "^" "\\b"))
880 :parse-rule "[$@%]?\\([_a-zA-Z0-9]+\\|[^a-zA-Z]\\)")
882 (info-lookup-maybe-add-help
883 :mode 'cperl-mode
884 :regexp "[$@%][^a-zA-Z]\\|\\$\\^[A-Z]\\|[$@%]?[a-zA-Z][_a-zA-Z0-9]*"
885 :other-modes '(perl-mode))
887 (info-lookup-maybe-add-help
888 :mode 'latex-mode
889 :regexp "\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)"
890 :doc-spec `((,(if (Info-find-file "latex2e" t)
891 ;; From http://home.gna.org/latexrefman
892 "(latex2e)Command Index"
893 "(latex)Command Index")
894 ;; \frac{NUM}{DEN} etc can have more than one {xx} argument.
895 ;; \sqrt[ROOT]{num} and others can have square brackets.
896 nil "[`'‘]" "\\({[^}]*}|\\[[^]]*\\]\\)*['’]")))
899 (info-lookup-maybe-add-help
900 :mode 'emacs-lisp-mode
901 :regexp "[^][()`',\" \t\n]+"
902 :doc-spec '(;; Commands with key sequences appear in nodes as `foo' and
903 ;; those without as `M-x foo'.
904 ("(emacs)Command Index" nil "['`‘]\\(M-x[ \t\n]+\\)?" "['’]")
905 ;; Variables normally appear in nodes as just `foo'.
906 ("(emacs)Variable Index" nil "['`‘]" "['’]")
907 ;; Almost all functions, variables, etc appear in nodes as
908 ;; " -- Function: foo" etc. A small number of aliases and
909 ;; symbols appear only as `foo', and will miss out on exact
910 ;; positions. Allowing `foo' would hit too many false matches
911 ;; for things that should go to Function: etc, and those latter
912 ;; are much more important. Perhaps this could change if some
913 ;; sort of fallback match scheme existed.
914 ("(elisp)Index" nil "^ -+ .*: " "\\( \\|$\\)")))
916 ;; docstrings talk about elisp, so have apropos-mode follow emacs-lisp-mode
917 (info-lookup-maybe-add-help
918 :mode 'apropos-mode
919 :regexp "[^][()`',\" \t\n]+" ;; same as emacs-lisp-mode above
920 :other-modes '(emacs-lisp-mode))
922 (info-lookup-maybe-add-help
923 :mode 'lisp-interaction-mode
924 :regexp "[^][()`',\" \t\n]+"
925 :parse-rule 'ignore
926 :other-modes '(emacs-lisp-mode))
928 (info-lookup-maybe-add-help
929 :mode 'lisp-mode
930 :regexp "[^()`',\" \t\n]+"
931 :parse-rule 'ignore
932 :other-modes '(emacs-lisp-mode))
934 (info-lookup-maybe-add-help
935 :mode 'scheme-mode
936 :regexp "[^()`',\" \t\n]+"
937 :ignore-case t
938 ;; Aubrey Jaffer's rendition from <URL:ftp://ftp-swiss.ai.mit.edu/pub/scm>
939 :doc-spec '(("(r5rs)Index" nil
940 "^[ \t]+-+ [^:]+:[ \t]*" "\\b")))
942 (info-lookup-maybe-add-help
943 :mode 'octave-mode
944 :regexp "[_a-zA-Z0-9]+\\|\\s.+\\|[-!=^|*/.\\,><~&+]\\{1,3\\}\\|[][();,\"']"
945 :doc-spec '(("(octave)Function Index" nil
946 "^ -+ [^:]+:[ ]+\\(\\[[^=]*=[ ]+\\)?" nil)
947 ("(octave)Variable Index" nil "^ -+ [^:]+:[ ]+" nil)
948 ("(octave)Operator Index" nil nil nil)
949 ;; Catch lines of the form "xyz statement"
950 ("(octave)Concept Index"
951 (lambda (item)
952 (cond
953 ((string-match "^\\([A-Z]+\\) statement\\b" item)
954 (match-string 1 item))
955 (t nil)))
956 nil; "^ -+ [^:]+:[ ]+" don't think this prefix is useful here.
957 nil)))
959 (info-lookup-maybe-add-help
960 :mode 'maxima-mode
961 :ignore-case t
962 :regexp "[a-zA-Z0-9_%]+"
963 :doc-spec '( ("(maxima)Function and Variable Index" nil
964 "^ -+ [^:]+:[ ]+\\(\\[[^=]*=[ ]+\\)?" nil)))
966 (info-lookup-maybe-add-help
967 :mode 'inferior-maxima-mode
968 :regexp "[a-zA-Z0-9_%]+"
969 :other-modes '(maxima-mode))
971 ;; coreutils and bash builtins overlap in places, eg. printf, so there's a
972 ;; question which should come first. Some of the coreutils descriptions are
973 ;; more detailed, but if bash is usually /bin/sh on a GNU system then the
974 ;; builtins will be what's normally run.
976 ;; Maybe special variables like $? should be matched as $?, not just ?.
977 ;; This would avoid a clash between variable $! and negation !, or variable
978 ;; $# and comment # (though comment # is not currently indexed in bash).
979 ;; Unfortunately if $? etc is the symbol, then we wouldn't be taken to the
980 ;; exact spot in the relevant node, since the bash manual has just `?' etc
981 ;; there. Maybe an extension to the prefix/suffix scheme could help this.
983 (info-lookup-maybe-add-help
984 :mode 'sh-mode :topic 'symbol
985 ;; bash has "." and ":" in its index, but those chars will probably never
986 ;; work in info, so don't bother matching them in the regexp.
987 :regexp "\\([a-zA-Z0-9_-]+\\|[!{}@*#?$]\\|\\[\\[?\\|]]?\\)"
988 :doc-spec '(("(bash)Builtin Index" nil "^['`‘]" "[ .'’]")
989 ("(bash)Reserved Word Index" nil "^['`‘]" "[ .'’]")
990 ("(bash)Variable Index" nil "^['`‘]" "[ .'’]")
992 ;; coreutils (version 4.5.10) doesn't have a separate program
993 ;; index, so exclude extraneous stuff (most of it) by demanding
994 ;; "[a-z]+" in the trans-func.
995 ;; coreutils version 8.1 has node "Concept Index" and past
996 ;; versions have node "Index", look for both, whichever is
997 ;; absent is quietly ignored
998 ("(coreutils)Index"
999 (lambda (item) (if (string-match "\\`[a-z]+\\'" item) item)))
1000 ("(coreutils)Concept Index"
1001 (lambda (item) (if (string-match "\\`[a-z]+\\'" item) item)))
1003 ;; diff (version 2.8.1) has only a few programs, index entries
1004 ;; are things like "foo invocation".
1005 ("(diff)Index"
1006 (lambda (item)
1007 (if (string-match "\\`\\([a-z]+\\) invocation\\'" item)
1008 (match-string 1 item))))
1009 ;; there's no plain "sed" index entry as such, mung another
1010 ;; hopefully unique one to get to the invocation section
1011 ("(sed)Concept Index"
1012 (lambda (item)
1013 (if (string-equal item "Standard input, processing as input")
1014 "sed")))
1015 ;; there's no plain "awk" or "gawk" index entries, mung other
1016 ;; hopefully unique ones to get to the command line options
1017 ("(gawk)Index"
1018 (lambda (item)
1019 (cond ((string-equal item "gawk, extensions, disabling")
1020 "awk")
1021 ((string-equal item "gawk, versions of, information about, printing")
1022 "gawk"))))))
1024 ;; This misses some things which occur as node names but not in the
1025 ;; index. Unfortunately it also picks up the wrong one of multiple
1026 ;; entries for the same term in some cases. --fx
1027 (info-lookup-maybe-add-help
1028 :mode 'cfengine-mode
1029 :regexp "[[:alnum:]_]+\\(?:()\\)?"
1030 :doc-spec '(("(cfengine-Reference)Variable Index"
1031 (lambda (item)
1032 ;; Index entries may be like `IsPlain()'
1033 (if (string-match "\\([[:alnum:]_]+\\)()" item)
1034 (match-string 1 item)
1035 item))
1036 ;; This gets functions in evaluated classes. Other
1037 ;; possible patterns don't seem to work too well.
1038 "['`‘]" "(")))
1040 (info-lookup-maybe-add-help
1041 :mode 'Custom-mode
1042 :ignore-case t
1043 :regexp "[^][()`',:\" \t\n]+"
1044 :parse-rule 'info-lookup-guess-custom-symbol
1045 :other-modes '(emacs-lisp-mode))
1047 (info-lookup-maybe-add-help
1048 :mode 'help-mode
1049 :regexp "[^][()`',:\" \t\n]+"
1050 :other-modes '(emacs-lisp-mode))
1052 (provide 'info-look)
1054 ;;; info-look.el ends here