Switch to recommended form of GPLv3 permissions notice.
[emacs.git] / lisp / info-look.el
blobe7ac5bd0d54e5ce97597e704828069f1092e4cc5
1 ;;; info-look.el --- major-mode-sensitive Info index lookup facility
2 ;; An older version of this was known as libc.el.
4 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2001, 2002, 2003,
5 ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
7 ;; Author: Ralph Schleicher <rs@nunatak.allgaeu.org>
8 ;; (did not show signs of life (Nov 2001) -stef)
9 ;; Keywords: help languages
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; Really cool code to lookup info indexes.
29 ;; Try especially info-lookup-symbol (aka C-h S).
31 ;;; Code:
33 (require 'info)
35 (defgroup info-lookup nil
36 "Major mode sensitive help agent."
37 :group 'help :group 'languages)
39 (defvar info-lookup-mode nil
40 "Symbol of the current buffer's help mode.
41 Help is provided according to the buffer's major mode if value is nil.
42 Automatically becomes buffer local when set in any fashion.")
43 (make-variable-buffer-local 'info-lookup-mode)
45 (defcustom info-lookup-other-window-flag t
46 "Non-nil means pop up the Info buffer in another window."
47 :group 'info-lookup :type 'boolean)
49 (defcustom info-lookup-highlight-face 'match
50 "Face for highlighting looked up help items.
51 Setting this variable to nil disables highlighting."
52 :group 'info-lookup :type 'face)
54 (defvar info-lookup-highlight-overlay nil
55 "Overlay object used for highlighting.")
57 (defcustom info-lookup-file-name-alist
58 '(("\\`ac\\(local\\|site\\|include\\)\\.m4\\'" . autoconf-mode))
59 "Alist of file names handled specially.
60 List elements are cons cells of the form
62 (REGEXP . MODE)
64 If a file name matches REGEXP, then use help mode MODE instead of the
65 buffer's major mode."
66 :group 'info-lookup :type '(repeat (cons (string :tag "Regexp")
67 (symbol :tag "Mode"))))
69 (defvar info-lookup-history nil
70 "History of previous input lines.")
72 (defvar info-lookup-alist nil
73 "Alist of known help topics.
74 Cons cells are of the form
76 (HELP-TOPIC . HELP-DATA)
78 HELP-TOPIC is the symbol of a help topic.
79 HELP-DATA is a HELP-TOPIC's public data set.
80 Value is an alist with elements of the form
82 (HELP-MODE REGEXP IGNORE-CASE DOC-SPEC PARSE-RULE OTHER-MODES)
84 HELP-MODE is a mode's symbol.
85 REGEXP is a regular expression matching those help items whose
86 documentation can be looked up via DOC-SPEC.
87 IGNORE-CASE is non-nil if help items are case insensitive.
88 DOC-SPEC is a list of documentation specifications of the form
90 (INFO-NODE TRANS-FUNC PREFIX SUFFIX)
92 INFO-NODE is the name (including file name part) of an Info index.
93 TRANS-FUNC is a function translating index entries into help items;
94 nil means add only those index entries matching REGEXP, a string
95 means prepend string to the first word of all index entries.
96 PREFIX and SUFFIX are parts of a regular expression. If one of
97 them is non-nil then search the help item's Info node for the
98 first occurrence of the regular expression `PREFIX ITEM SUFFIX'.
99 ITEM will be highlighted with `info-lookup-highlight-face' if this
100 variable is not nil.
101 PARSE-RULE is either the symbol name of a function or a regular
102 expression for guessing the default help item at point. Fuzzy
103 regular expressions like \"[_a-zA-Z0-9]+\" do a better job if
104 there are no clear delimiters; do not try to write too complex
105 expressions. PARSE-RULE defaults to REGEXP.
106 OTHER-MODES is a list of cross references to other help modes.")
108 (defsubst info-lookup->topic-value (topic)
109 (cdr (assoc topic info-lookup-alist)))
111 (defsubst info-lookup->mode-value (topic mode)
112 (assoc mode (info-lookup->topic-value topic)))
114 (defsubst info-lookup->regexp (topic mode)
115 (nth 1 (info-lookup->mode-value topic mode)))
117 (defsubst info-lookup->ignore-case (topic mode)
118 (nth 2 (info-lookup->mode-value topic mode)))
120 (defsubst info-lookup->doc-spec (topic mode)
121 (nth 3 (info-lookup->mode-value topic mode)))
123 (defsubst info-lookup->parse-rule (topic mode)
124 (nth 4 (info-lookup->mode-value topic mode)))
126 (defsubst info-lookup->other-modes (topic mode)
127 (nth 5 (info-lookup->mode-value topic mode)))
129 (defun info-lookup-add-help (&rest arg)
130 "Add or update a help specification.
131 Function arguments are one or more options of the form
133 KEYWORD ARGUMENT
135 KEYWORD is either `:topic', `:mode', `:regexp', `:ignore-case',
136 `:doc-spec', `:parse-rule', or `:other-modes'.
137 ARGUMENT has a value as explained in the documentation of the
138 variable `info-lookup-alist'.
140 If no topic or mode option has been specified, then the help topic defaults
141 to `symbol', and the help mode defaults to the current major mode."
142 (apply 'info-lookup-add-help* nil arg))
144 (defun info-lookup-maybe-add-help (&rest arg)
145 "Add a help specification if none is defined.
146 See the documentation of the function `info-lookup-add-help'
147 for more details."
148 (apply 'info-lookup-add-help* t arg))
150 (defun info-lookup-add-help* (maybe &rest arg)
151 (let (topic mode regexp ignore-case doc-spec
152 parse-rule other-modes keyword value)
153 (setq topic 'symbol
154 mode major-mode
155 regexp "\\w+")
156 (while arg
157 (setq keyword (car arg))
158 (or (symbolp keyword)
159 (error "Junk in argument list \"%S\"" arg))
160 (setq arg (cdr arg))
161 (and (null arg)
162 (error "Keyword \"%S\" is missing an argument" keyword))
163 (setq value (car arg)
164 arg (cdr arg))
165 (cond ((eq keyword :topic)
166 (setq topic value))
167 ((eq keyword :mode)
168 (setq mode value))
169 ((eq keyword :regexp)
170 (setq regexp value))
171 ((eq keyword :ignore-case)
172 (setq ignore-case value))
173 ((eq keyword :doc-spec)
174 (setq doc-spec value))
175 ((eq keyword :parse-rule)
176 (setq parse-rule value))
177 ((eq keyword :other-modes)
178 (setq other-modes value))
180 (error "Unknown keyword \"%S\"" keyword))))
181 (or (and maybe (info-lookup->mode-value topic mode))
182 (let* ((data (list regexp ignore-case doc-spec parse-rule other-modes))
183 (topic-cell (or (assoc topic info-lookup-alist)
184 (car (setq info-lookup-alist
185 (cons (cons topic nil)
186 info-lookup-alist)))))
187 (mode-cell (assoc mode topic-cell)))
188 (if (null mode-cell)
189 (setcdr topic-cell (cons (cons mode data) (cdr topic-cell)))
190 (setcdr mode-cell data))))
191 nil))
193 (defvar info-lookup-cache nil
194 "Cache storing data maintained automatically by the program.
195 Value is an alist with cons cell of the form
197 (HELP-TOPIC . ((HELP-MODE INITIALIZED COMPLETIONS REFER-MODES) ...))
199 HELP-TOPIC is the symbol of a help topic.
200 HELP-MODE is a mode's symbol.
201 INITIALIZED is nil if HELP-MODE is uninitialized, t if
202 HELP-MODE is initialized, and `0' means HELP-MODE is
203 initialized but void.
204 COMPLETIONS is an alist of documented help items.
205 REFER-MODES is a list of other help modes to use.")
207 (defsubst info-lookup->cache (topic)
208 (or (assoc topic info-lookup-cache)
209 (car (setq info-lookup-cache
210 (cons (cons topic nil)
211 info-lookup-cache)))))
213 (defun info-lookup->topic-cache (topic)
214 (cdr (info-lookup->cache topic)))
216 (defun info-lookup->mode-cache (topic mode)
217 (assoc mode (info-lookup->topic-cache topic)))
219 (defun info-lookup->initialized (topic mode)
220 (nth 1 (info-lookup->mode-cache topic mode)))
222 (defun info-lookup->completions (topic mode)
223 (or (info-lookup->initialized topic mode)
224 (info-lookup-setup-mode topic mode))
225 (nth 2 (info-lookup->mode-cache topic mode)))
227 (defun info-lookup->refer-modes (topic mode)
228 (or (info-lookup->initialized topic mode)
229 (info-lookup-setup-mode topic mode))
230 (nth 3 (info-lookup->mode-cache topic mode)))
232 (defun info-lookup->all-modes (topic mode)
233 (cons mode (info-lookup->refer-modes topic mode)))
235 (defun info-lookup-quick-all-modes (topic mode)
236 (cons mode (info-lookup->other-modes topic mode)))
238 ;;;###autoload
239 (defun info-lookup-reset ()
240 "Throw away all cached data.
241 This command is useful if the user wants to start at the beginning without
242 quitting Emacs, for example, after some Info documents were updated on the
243 system."
244 (interactive)
245 (setq info-lookup-cache nil))
247 ;;;###autoload (put 'info-lookup-symbol 'info-file "emacs")
248 ;;;###autoload
249 (defun info-lookup-symbol (symbol &optional mode)
250 "Display the definition of SYMBOL, as found in the relevant manual.
251 When this command is called interactively, it reads SYMBOL from the
252 minibuffer. In the minibuffer, use M-n to yank the default argument
253 value into the minibuffer so you can edit it. The default symbol is the
254 one found at point.
256 With prefix arg a query for the symbol help mode is offered."
257 (interactive
258 (info-lookup-interactive-arguments 'symbol current-prefix-arg))
259 (info-lookup 'symbol symbol mode))
261 ;;;###autoload (put 'info-lookup-file 'info-file "emacs")
262 ;;;###autoload
263 (defun info-lookup-file (file &optional mode)
264 "Display the documentation of a file.
265 When this command is called interactively, it reads FILE from the minibuffer.
266 In the minibuffer, use M-n to yank the default file name
267 into the minibuffer so you can edit it.
268 The default file name is the one found at point.
270 With prefix arg a query for the file help mode is offered."
271 (interactive
272 (info-lookup-interactive-arguments 'file current-prefix-arg))
273 (info-lookup 'file file mode))
275 (defun info-lookup-interactive-arguments (topic &optional query)
276 "Read and return argument value (and help mode) for help topic TOPIC.
277 If optional argument QUERY is non-nil, query for the help mode."
278 (let* ((mode (cond (query
279 (info-lookup-change-mode topic))
280 ((info-lookup->mode-value topic (info-lookup-select-mode))
281 info-lookup-mode)
282 ((info-lookup-change-mode topic))))
283 (completions (info-lookup->completions topic mode))
284 (default (info-lookup-guess-default topic mode))
285 (completion-ignore-case (info-lookup->ignore-case topic mode))
286 (enable-recursive-minibuffers t)
287 (value (completing-read
288 (if default
289 (format "Describe %s (default %s): " topic default)
290 (format "Describe %s: " topic))
291 completions nil nil nil 'info-lookup-history default)))
292 (list (if (equal value "") default value) mode)))
294 (defun info-lookup-select-mode ()
295 (when (and (not info-lookup-mode) (buffer-file-name))
296 (let ((file-name (file-name-nondirectory (buffer-file-name)))
297 (file-name-alist info-lookup-file-name-alist))
298 (while (and (not info-lookup-mode) file-name-alist)
299 (when (string-match (caar file-name-alist) file-name)
300 (setq info-lookup-mode (cdar file-name-alist)))
301 (setq file-name-alist (cdr file-name-alist)))))
302 (or info-lookup-mode (setq info-lookup-mode major-mode)))
304 (defun info-lookup-change-mode (topic)
305 (let* ((completions (mapcar (lambda (arg)
306 (cons (symbol-name (car arg)) (car arg)))
307 (info-lookup->topic-value topic)))
308 (mode (completing-read
309 (format "Use %s help mode: " topic)
310 completions nil t nil 'info-lookup-history)))
311 (or (setq mode (cdr (assoc mode completions)))
312 (error "No %s help available" topic))
313 (or (info-lookup->mode-value topic mode)
314 (error "No %s help available for `%s'" topic mode))
315 (setq info-lookup-mode mode)))
317 (defun info-lookup (topic item mode)
318 "Display the documentation of a help item."
319 (or mode (setq mode (info-lookup-select-mode)))
320 (or (info-lookup->mode-value topic mode)
321 (error "No %s help available for `%s'" topic mode))
322 (let* ((completions (info-lookup->completions topic mode))
323 (ignore-case (info-lookup->ignore-case topic mode))
324 (entry (or (assoc (if ignore-case (downcase item) item) completions)
325 (assoc-string item completions t)
326 (error "Not documented as a %s: %s" topic (or item ""))))
327 (modes (info-lookup->all-modes topic mode))
328 (window (selected-window))
329 (new-Info-history
330 ;; Avoid clobbering Info-history with nodes searched during
331 ;; lookup. If lookup succeeds set `Info-history' to
332 ;; `new-Info-history'.
333 (when (get-buffer "*info*")
334 (with-current-buffer "*info*"
335 (cons (list Info-current-file Info-current-node (point))
336 Info-history))))
337 found doc-spec node prefix suffix doc-found)
338 (if (not (eq major-mode 'Info-mode))
339 (if (not info-lookup-other-window-flag)
340 (info)
341 (progn
342 (save-window-excursion (info))
343 ;; Determine whether or not the Info buffer is visible in
344 ;; another frame on the same display. If it is, simply raise
345 ;; that frame. Otherwise, display it in another window.
346 (let* ((window (get-buffer-window "*info*" t))
347 (info-frame (and window (window-frame window))))
348 (if (and info-frame
349 (display-multi-frame-p)
350 (memq info-frame (frames-on-display-list))
351 (not (eq info-frame (selected-frame))))
352 (select-frame info-frame)
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 error-data
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 error-data
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-complete (topic mode)
645 "Try to complete a help item."
646 (barf-if-buffer-read-only)
647 (or mode (setq mode (info-lookup-select-mode)))
648 (or (info-lookup->mode-value topic mode)
649 (error "No %s completion available for `%s'" topic mode))
650 (let ((modes (info-lookup-quick-all-modes topic mode))
651 (start (point))
652 try)
653 (while (and (not try) modes)
654 (setq mode (car modes)
655 modes (cdr modes)
656 try (info-lookup-guess-default* topic mode))
657 (goto-char start))
658 (and (not try)
659 (error "Found no %S to complete" topic))
660 (let ((completions (info-lookup->completions topic mode))
661 (completion-ignore-case (info-lookup->ignore-case topic mode))
662 completion)
663 (setq completion (try-completion try completions))
664 (cond ((not completion)
665 (ding)
666 (message "No match"))
667 ((stringp completion)
668 (or (assoc completion completions)
669 (setq completion (completing-read
670 (format "Complete %S: " topic)
671 completions nil t completion
672 info-lookup-history)))
673 ;; Find the original symbol and zap it.
674 (end-of-line)
675 (while (and (search-backward try nil t)
676 (< start (point))))
677 (replace-match "")
678 (insert completion))
680 (message "%s is complete"
681 (capitalize (prin1-to-string topic))))))))
684 ;;; Initialize some common modes.
686 (info-lookup-maybe-add-help
687 :mode 'c-mode :topic 'symbol
688 :regexp "\\(struct \\|union \\|enum \\)?[_a-zA-Z][_a-zA-Z0-9]*"
689 :doc-spec '(("(libc)Function Index" nil
690 "^[ \t]+-+ \\(Function\\|Macro\\): .*\\<" "\\>")
691 ;; prefix/suffix has to match things like
692 ;; " -- Macro: int F_DUPFD"
693 ;; " -- Variable: char * tzname [2]"
694 ;; "`DBL_MAX'" (texinfo @table)
695 ;; suffix "\\>" is not used because that sends DBL_MAX to
696 ;; DBL_MAX_EXP ("_" is a non-word char)
697 ("(libc)Variable Index" nil
698 "^\\([ \t]+-+ \\(Variable\\|Macro\\): .*\\<\\|`\\)"
699 "\\( \\|'?$\\)")
700 ("(libc)Type Index" nil
701 "^[ \t]+-+ Data Type: \\<" "\\>")
702 ("(termcap)Var Index" nil
703 "^[ \t]*`" "'"))
704 :parse-rule 'info-lookup-guess-c-symbol)
706 (info-lookup-maybe-add-help
707 :mode 'c-mode :topic 'file
708 :regexp "[_a-zA-Z0-9./+-]+"
709 :doc-spec '(("(libc)File Index")))
711 (info-lookup-maybe-add-help
712 :mode 'bison-mode
713 :regexp "[:;|]\\|%\\([%{}]\\|[_a-z]+\\)\\|YY[_A-Z]+\\|yy[_a-z]+"
714 :doc-spec '(("(bison)Index" nil
715 "`" "'"))
716 :parse-rule "[:;|]\\|%\\([%{}]\\|[_a-zA-Z][_a-zA-Z0-9]*\\)"
717 :other-modes '(c-mode))
719 (info-lookup-maybe-add-help
720 :mode 'makefile-mode
721 :regexp "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z][_a-zA-Z0-9-]*"
722 :doc-spec '(("(make)Name Index" nil
723 "^[ \t]*`" "'")
724 ("(automake)Macro and Variable Index" nil
725 "^[ \t]*`" "'"))
726 :parse-rule "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z0-9-]+"
727 :other-modes '(automake-mode))
729 (info-lookup-maybe-add-help
730 :mode 'texinfo-mode
731 :regexp "@\\([a-zA-Z]+\\|[^a-zA-Z]\\)"
732 :doc-spec '(("(texinfo)Command and Variable Index"
733 ;; Ignore Emacs commands and prepend a `@'.
734 (lambda (item)
735 (if (string-match "^\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\( .*\\)?$" item)
736 (concat "@" (match-string 1 item))))
737 "`" "[' ]")))
739 (info-lookup-maybe-add-help
740 :mode 'm4-mode
741 :regexp "[_a-zA-Z][_a-zA-Z0-9]*"
742 :doc-spec '(("(m4)Macro index"))
743 :parse-rule "[_a-zA-Z0-9]+")
745 (info-lookup-maybe-add-help
746 :mode 'autoconf-mode
747 :regexp "A[CM]_[_A-Z0-9]+"
748 :doc-spec '(;; Autoconf Macro Index entries are without an "AC_" prefix,
749 ;; but with "AH_" or "AU_" for those. So add "AC_" if there
750 ;; isn't already an "A._".
751 ("(autoconf)Autoconf Macro Index"
752 (lambda (item)
753 (if (string-match "^A._" item) item (concat "AC_" item)))
754 "^[ \t]+-+ \\(Macro\\|Variable\\): .*\\<" "\\>")
755 ;; M4 Macro Index entries are without "AS_" prefixes, and
756 ;; mostly without "m4_" prefixes. "dnl" is an exception, not
757 ;; wanting any prefix. So AS_ is added back to upper-case
758 ;; names, m4_ to others which don't already an m4_.
759 ("(autoconf)M4 Macro Index"
760 (lambda (item)
761 (let ((case-fold-search nil))
762 (cond ((or (string-equal item "dnl")
763 (string-match "^m4_" item))
764 item)
765 ((string-match "^[A-Z0-9_]+$" item)
766 (concat "AS_" item))
768 (concat "m4_" item)))))
769 "^[ \t]+-+ Macro: .*\\<" "\\>")
770 ;; Autotest Macro Index entries are without "AT_".
771 ("(autoconf)Autotest Macro Index" "AT_"
772 "^[ \t]+-+ Macro: .*\\<" "\\>")
773 ;; This is for older versions (probably pre autoconf 2.5x):
774 ("(autoconf)Macro Index" "AC_"
775 "^[ \t]+-+ \\(Macro\\|Variable\\): .*\\<" "\\>")
776 ;; Automake has index entries for its notes on various autoconf
777 ;; macros (eg. AC_PROG_CC). Ensure this is after the autoconf
778 ;; index, so as to prefer the autoconf docs.
779 ("(automake)Macro and Variable Index" nil
780 "^[ \t]*`" "'"))
781 ;; Autoconf symbols are M4 macros. Thus use M4's parser.
782 :parse-rule 'ignore
783 :other-modes '(m4-mode))
785 (info-lookup-maybe-add-help
786 :mode 'awk-mode
787 :regexp "[_a-zA-Z]+"
788 :doc-spec '(("(gawk)Index"
789 (lambda (item)
790 (let ((case-fold-search nil))
791 (cond
792 ;; `BEGIN' and `END'.
793 ((string-match "^\\([A-Z]+\\) special pattern\\b" item)
794 (match-string 1 item))
795 ;; `if', `while', `do', ...
796 ((string-match "^\\([a-z]+\\) statement\\b" item)
797 (if (not (string-equal (match-string 1 item) "control"))
798 (match-string 1 item)))
799 ;; `NR', `NF', ...
800 ((string-match "^[A-Z]+$" item)
801 item)
802 ;; Built-in functions (matches to many entries).
803 ((string-match "^[a-z]+$" item)
804 item))))
805 "`" "\\([ \t]*([^)]*)\\)?'")))
807 (info-lookup-maybe-add-help
808 :mode 'perl-mode
809 :regexp "[$@%][^a-zA-Z]\\|\\$\\^[A-Z]\\|[$@%]?[a-zA-Z][_a-zA-Z0-9]*"
810 :doc-spec '(("(perl5)Function Index"
811 (lambda (item)
812 (if (string-match "^\\([a-zA-Z0-9]+\\)" item)
813 (match-string 1 item)))
814 "^" "\\b")
815 ("(perl5)Variable Index"
816 (lambda (item)
817 ;; Work around bad formatted array variables.
818 (let ((sym (cond ((or (string-match "^\\$\\(.\\|@@\\)$" item)
819 (string-match "^\\$\\^[A-Z]$" item))
820 item)
821 ((string-match
822 "^\\([$%@]\\|@@\\)?[_a-zA-Z0-9]+" item)
823 (match-string 0 item))
824 (t ""))))
825 (if (string-match "@@" sym)
826 (setq sym (concat (substring sym 0 (match-beginning 0))
827 (substring sym (1- (match-end 0))))))
828 (if (string-equal sym "") nil sym)))
829 "^" "\\b"))
830 :parse-rule "[$@%]?\\([_a-zA-Z0-9]+\\|[^a-zA-Z]\\)")
832 (info-lookup-maybe-add-help
833 :mode 'cperl-mode
834 :regexp "[$@%][^a-zA-Z]\\|\\$\\^[A-Z]\\|[$@%]?[a-zA-Z][_a-zA-Z0-9]*"
835 :other-modes '(perl-mode))
837 (info-lookup-maybe-add-help
838 :mode 'latex-mode
839 :regexp "\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)"
840 :doc-spec '(("(latex)Command Index" nil
841 "`" "\\({[^}]*}\\)?'")))
843 (info-lookup-maybe-add-help
844 :mode 'emacs-lisp-mode
845 :regexp "[^][()`',\" \t\n]+"
846 :doc-spec '(;; Commands with key sequences appear in nodes as `foo' and
847 ;; those without as `M-x foo'.
848 ("(emacs)Command Index" nil "`\\(M-x[ \t\n]+\\)?" "'")
849 ;; Variables normally appear in nodes as just `foo'.
850 ("(emacs)Variable Index" nil "`" "'")
851 ;; Almost all functions, variables, etc appear in nodes as
852 ;; " -- Function: foo" etc. A small number of aliases and
853 ;; symbols appear only as `foo', and will miss out on exact
854 ;; positions. Allowing `foo' would hit too many false matches
855 ;; for things that should go to Function: etc, and those latter
856 ;; are much more important. Perhaps this could change if some
857 ;; sort of fallback match scheme existed.
858 ("(elisp)Index" nil "^ -+ .*: " "\\( \\|$\\)")))
860 (info-lookup-maybe-add-help
861 :mode 'lisp-interaction-mode
862 :regexp "[^][()`',\" \t\n]+"
863 :parse-rule 'ignore
864 :other-modes '(emacs-lisp-mode))
866 (info-lookup-maybe-add-help
867 :mode 'lisp-mode
868 :regexp "[^()`',\" \t\n]+"
869 :parse-rule 'ignore
870 :other-modes '(emacs-lisp-mode))
872 (info-lookup-maybe-add-help
873 :mode 'scheme-mode
874 :regexp "[^()`',\" \t\n]+"
875 :ignore-case t
876 ;; Aubrey Jaffer's rendition from <URL:ftp://ftp-swiss.ai.mit.edu/pub/scm>
877 :doc-spec '(("(r5rs)Index" nil
878 "^[ \t]+-+ [^:]+:[ \t]*" "\\b")))
880 (info-lookup-maybe-add-help
881 :mode 'octave-mode
882 :regexp "[_a-zA-Z0-9]+\\|\\s.+\\|[-!=^|*/.\\,><~&+]\\{1,3\\}\\|[][();,\"']"
883 :doc-spec '(("(octave)Function Index" nil
884 "^ -+ [^:]+:[ ]+\\(\\[[^=]*=[ ]+\\)?" nil)
885 ("(octave)Variable Index" nil "^ -+ [^:]+:[ ]+" nil)
886 ("(octave)Operator Index" nil nil nil)
887 ;; Catch lines of the form "xyz statement"
888 ("(octave)Concept Index"
889 (lambda (item)
890 (cond
891 ((string-match "^\\([A-Z]+\\) statement\\b" item)
892 (match-string 1 item))
893 (t nil)))
894 nil; "^ -+ [^:]+:[ ]+" don't think this prefix is useful here.
895 nil)))
897 (info-lookup-maybe-add-help
898 :mode 'maxima-mode
899 :ignore-case t
900 :regexp "[a-zA-Z0-9_%]+"
901 :doc-spec '( ("(maxima)Function and Variable Index" nil
902 "^ -+ [^:]+:[ ]+\\(\\[[^=]*=[ ]+\\)?" nil)))
904 (info-lookup-maybe-add-help
905 :mode 'inferior-maxima-mode
906 :regexp "[a-zA-Z0-9_%]+"
907 :other-modes '(maxima-mode))
909 ;; coreutils and bash builtins overlap in places, eg. printf, so there's a
910 ;; question which should come first. Some of the coreutils descriptions are
911 ;; more detailed, but if bash is usually /bin/sh on a GNU system then the
912 ;; builtins will be what's normally run.
914 ;; Maybe special variables like $? should be matched as $?, not just ?.
915 ;; This would avoid a clash between variable $! and negation !, or variable
916 ;; $# and comment # (though comment # is not currently indexed in bash).
917 ;; Unfortunately if $? etc is the symbol, then we wouldn't be taken to the
918 ;; exact spot in the relevant node, since the bash manual has just `?' etc
919 ;; there. Maybe an extension to the prefix/suffix scheme could help this.
921 (info-lookup-maybe-add-help
922 :mode 'sh-mode :topic 'symbol
923 ;; bash has "." and ":" in its index, but those chars will probably never
924 ;; work in info, so don't bother matching them in the regexp.
925 :regexp "\\([a-zA-Z0-9_-]+\\|[!{}@*#?$]\\|\\[\\[?\\|]]?\\)"
926 :doc-spec '(("(bash)Builtin Index" nil "^`" "[ .']")
927 ("(bash)Reserved Word Index" nil "^`" "[ .']")
928 ("(bash)Variable Index" nil "^`" "[ .']")
929 ;; coreutils (version 4.5.10) doesn't have a separate program
930 ;; index, so exclude extraneous stuff (most of it) by demanding
931 ;; "[a-z]+" in the trans-func.
932 ("(coreutils)Index"
933 (lambda (item) (if (string-match "\\`[a-z]+\\'" item) item)))
934 ;; diff (version 2.8.1) has only a few programs, index entries
935 ;; are things like "foo invocation".
936 ("(diff)Index"
937 (lambda (item)
938 (if (string-match "\\`\\([a-z]+\\) invocation\\'" item)
939 (match-string 1 item))))
940 ;; there's no plain "sed" index entry as such, mung another
941 ;; hopefully unique one to get to the invocation section
942 ("(sed)Concept Index"
943 (lambda (item)
944 (if (string-equal item "Standard input, processing as input")
945 "sed")))
946 ;; there's no plain "awk" or "gawk" index entries, mung other
947 ;; hopefully unique ones to get to the command line options
948 ("(gawk)Index"
949 (lambda (item)
950 (cond ((string-equal item "gawk, extensions, disabling")
951 "awk")
952 ((string-equal item "gawk, versions of, information about, printing")
953 "gawk"))))))
955 ;; This misses some things which occur as node names but not in the
956 ;; index. Unfortunately it also picks up the wrong one of multiple
957 ;; entries for the same term in some cases. --fx
958 (info-lookup-maybe-add-help
959 :mode 'cfengine-mode
960 :regexp "[[:alnum:]_]+\\(?:()\\)?"
961 :doc-spec '(("(cfengine-Reference)Variable Index"
962 (lambda (item)
963 ;; Index entries may be like `IsPlain()'
964 (if (string-match "\\([[:alnum:]_]+\\)()" item)
965 (match-string 1 item)
966 item))
967 ;; This gets functions in evaluated classes. Other
968 ;; possible patterns don't seem to work too well.
969 "`" "(")))
971 (info-lookup-maybe-add-help
972 :mode 'Custom-mode
973 :ignore-case t
974 :regexp "[^][()`',:\" \t\n]+"
975 :parse-rule 'info-lookup-guess-custom-symbol
976 :other-modes '(emacs-lisp-mode))
978 (info-lookup-maybe-add-help
979 :mode 'help-mode
980 :regexp "[^][()`',:\" \t\n]+"
981 :other-modes '(emacs-lisp-mode))
983 (provide 'info-look)
985 ;; arch-tag: 0f1e3ea3-32a2-4461-bbab-3cff93539a74
986 ;;; info-look.el ends here