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