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