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