* os.texi (Startup Summary): Copyedits. The init file is not
[emacs.git] / lisp / info-look.el
bloba061611aa9b7acc23d0811481c4abd2a8c83c17f
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, 2009 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 (unless (eq major-mode 'Info-mode)
339 (if (not info-lookup-other-window-flag)
340 (info)
341 (save-window-excursion (info))
342 (let* ((info-window (get-buffer-window "*info*" t))
343 (info-frame (and info-window (window-frame info-window))))
344 (if (and info-frame
345 (not (eq info-frame (selected-frame)))
346 (display-multi-frame-p)
347 (memq info-frame (frames-on-display-list)))
348 ;; *info* is visible in another frame on same display.
349 ;; Raise that frame and select the window.
350 (progn
351 (select-window info-window)
352 (raise-frame info-frame))
353 ;; In any other case, switch to *info* in another window.
354 (switch-to-buffer-other-window "*info*")))))
355 (while (and (not found) modes)
356 (setq doc-spec (info-lookup->doc-spec topic (car modes)))
357 (while (and (not found) doc-spec)
358 (setq node (nth 0 (car doc-spec))
359 prefix (nth 2 (car doc-spec))
360 suffix (nth 3 (car doc-spec)))
361 (when (condition-case error-data
362 (progn
363 ;; Don't need Index menu fontifications here, and
364 ;; they slow down the lookup.
365 (let (Info-fontify-maximum-menu-size
366 Info-history-list)
367 (Info-goto-node node)
368 (setq doc-found t)))
369 (error
370 (message "Cannot access Info node %s" node)
371 (sit-for 1)
372 nil))
373 (condition-case nil
374 (progn
375 ;; Don't use Info-menu, it forces case-fold-search to t
376 (let ((case-fold-search nil))
377 (re-search-forward
378 (concat "^\\* " (regexp-quote (or (cdr entry) (car entry)))
379 ":")))
380 (Info-follow-nearest-node)
381 (setq found t)
382 (if (or prefix suffix)
383 (let ((case-fold-search
384 (info-lookup->ignore-case topic (car modes)))
385 (buffer-read-only nil))
386 (goto-char (point-min))
387 (re-search-forward
388 (concat prefix (regexp-quote (car entry)) suffix))
389 (goto-char (match-beginning 0))
390 (and (display-color-p) info-lookup-highlight-face
391 ;; Search again for ITEM so that the first
392 ;; occurrence of ITEM will be highlighted.
393 (re-search-forward (regexp-quote (car entry)))
394 (let ((start (match-beginning 0))
395 (end (match-end 0)))
396 (if (overlayp info-lookup-highlight-overlay)
397 (move-overlay info-lookup-highlight-overlay
398 start end (current-buffer))
399 (setq info-lookup-highlight-overlay
400 (make-overlay start end))))
401 (overlay-put info-lookup-highlight-overlay
402 'face info-lookup-highlight-face)))))
403 (error nil)))
404 (setq doc-spec (cdr doc-spec)))
405 (setq modes (cdr modes)))
406 ;; Alert the user if case was munged, and do this after bringing up the
407 ;; info buffer since that can print messages
408 (unless (or ignore-case
409 (string-equal item (car entry)))
410 (message "Found in different case: %s" (car entry)))
411 (when found
412 (setq Info-history new-Info-history))
413 (or doc-found
414 (error "Info documentation for lookup was not found"))
415 ;; Don't leave the Info buffer if the help item couldn't be looked up.
416 (if (and info-lookup-other-window-flag found)
417 (select-window window))))
419 (defun info-lookup-setup-mode (topic mode)
420 "Initialize the internal data structure."
421 (or (info-lookup->initialized topic mode)
422 (let ((initialized 0)
423 cell data completions refer-modes Info-history-list)
424 (if (not (info-lookup->mode-value topic mode))
425 (message "No %s help available for `%s'" topic mode)
426 ;; Recursively setup cross references.
427 ;; But refer only to non-void modes.
428 (dolist (arg (info-lookup->other-modes topic mode))
429 (or (info-lookup->initialized topic arg)
430 (info-lookup-setup-mode topic arg))
431 (and (eq (info-lookup->initialized topic arg) t)
432 (setq refer-modes (cons arg refer-modes))))
433 (setq refer-modes (nreverse refer-modes))
434 ;; Build the full completion alist.
435 (setq completions
436 (nconc (condition-case nil
437 (info-lookup-make-completions topic mode)
438 (error nil))
439 (apply 'append
440 (mapcar (lambda (arg)
441 (info-lookup->completions topic arg))
442 refer-modes))))
443 (setq initialized t))
444 ;; Update `info-lookup-cache'.
445 (setq cell (info-lookup->mode-cache topic mode)
446 data (list initialized completions refer-modes))
447 (if (not cell)
448 (setcdr (info-lookup->cache topic)
449 (cons (cons mode data) (info-lookup->topic-cache topic)))
450 (setcdr cell data))
451 initialized)))
453 (defun info-lookup-make-completions (topic mode)
454 "Create a unique alist from all index entries."
455 (let ((doc-spec (info-lookup->doc-spec topic mode))
456 (regexp (concat "^\\(" (info-lookup->regexp topic mode)
457 "\\)\\([ \t].*\\)?$"))
458 Info-history-list Info-fontify-maximum-menu-size
459 node trans entry item prefix result doc-found
460 (buffer (get-buffer-create " temp-info-look")))
461 (with-current-buffer buffer
462 (Info-mode))
463 (while doc-spec
464 (setq node (nth 0 (car doc-spec))
465 trans (cond ((eq (nth 1 (car doc-spec)) nil)
466 (lambda (arg)
467 (if (string-match regexp arg)
468 (match-string 1 arg))))
469 ((stringp (nth 1 (car doc-spec)))
470 (setq prefix (nth 1 (car doc-spec)))
471 (lambda (arg)
472 (if (string-match "^\\([^: \t\n]+\\)" arg)
473 (concat prefix (match-string 1 arg)))))
474 (t (nth 1 (car doc-spec)))))
475 (with-current-buffer buffer
476 (message "Processing Info node `%s'..." node)
477 (when (condition-case error-data
478 (progn
479 (Info-goto-node node)
480 (setq doc-found t))
481 (error
482 (message "Cannot access Info node `%s'" node)
483 (sit-for 1)
484 nil))
485 (condition-case nil
486 (progn
487 (goto-char (point-min))
488 (and (search-forward "\n* Menu:" nil t)
489 (while (re-search-forward "\n\\* \\(.*\\): " nil t)
490 (setq entry (match-string 1)
491 item (funcall trans entry))
492 ;; `trans' can return nil if the regexp doesn't match.
493 (when (and item
494 ;; Sometimes there's more than one Menu:
495 (not (string= entry "Menu")))
496 (and (info-lookup->ignore-case topic mode)
497 (setq item (downcase item)))
498 (and (string-equal entry item)
499 (setq entry nil))
500 (and (or (assoc item result)
501 (setq result (cons (cons item entry)
502 result))))))))
503 (error nil))))
504 (message "Processing Info node `%s'...done" node)
505 (setq doc-spec (cdr doc-spec)))
506 (or doc-found
507 (error "Info documentation for lookup was not found"))
508 result))
510 (defun info-lookup-guess-default (topic mode)
511 "Return a guess for a symbol to look up, based on text around point.
512 Try all related modes applicable to TOPIC and MODE.
513 Return nil if there is nothing appropriate in the buffer near point."
514 (let ((modes (info-lookup->all-modes topic mode))
515 guess)
516 (while (and (not guess) modes)
517 (setq guess (info-lookup-guess-default* topic (car modes))
518 modes (cdr modes)))
519 ;; Collapse whitespace characters.
520 (when guess
521 (let ((pos 0))
522 (while (string-match "[ \t\n]+" guess pos)
523 (setq pos (1+ (match-beginning 0)))
524 (setq guess (replace-match " " t t guess)))))
525 guess))
527 (defun info-lookup-guess-default* (topic mode)
528 (let ((case-fold-search (info-lookup->ignore-case topic mode))
529 (rule (or (info-lookup->parse-rule topic mode)
530 (info-lookup->regexp topic mode)))
531 (start (point)) end regexp subexp result)
532 (save-excursion
533 (if (symbolp rule)
534 (setq result (funcall rule))
535 (if (consp rule)
536 (setq regexp (car rule)
537 subexp (cdr rule))
538 (setq regexp rule
539 subexp 0))
540 ;; If at start of symbol, don't go back to end of previous one.
541 (if (save-match-data
542 (looking-at "[ \t\n]"))
543 (skip-chars-backward " \t\n"))
544 (setq end (point))
545 (while (and (re-search-backward regexp nil t)
546 (looking-at regexp)
547 (>= (match-end 0) end))
548 (setq result (match-string subexp)))
549 (if (not result)
550 (progn
551 (goto-char start)
552 (skip-chars-forward " \t\n")
553 (and (looking-at regexp)
554 (setq result (match-string subexp)))))))
555 result))
557 (defun info-lookup-guess-c-symbol ()
558 "Get the C symbol at point."
559 (condition-case nil
560 (progn
561 (skip-syntax-backward "w_")
562 (let ((start (point)) prefix name)
563 ;; Test for a leading `struct', `union', or `enum' keyword
564 ;; but ignore names like `foo_struct'.
565 (setq prefix (and (< (skip-chars-backward " \t\n") 0)
566 (< (skip-chars-backward "_a-zA-Z0-9") 0)
567 (looking-at "\\(struct\\|union\\|enum\\)\\s ")
568 (concat (match-string 1) " ")))
569 (goto-char start)
570 (and (looking-at "[_a-zA-Z][_a-zA-Z0-9]*")
571 (setq name (match-string 0)))
572 ;; Caveat! Look forward if point is at `struct' etc.
573 (and (not prefix)
574 (or (string-equal name "struct")
575 (string-equal name "union")
576 (string-equal name "enum"))
577 (looking-at "[a-z]+\\s +\\([_a-zA-Z][_a-zA-Z0-9]*\\)")
578 (setq prefix (concat name " ")
579 name (match-string 1)))
580 (and (or prefix name)
581 (concat prefix name))))
582 (error nil)))
584 (defun info-lookup-guess-custom-symbol ()
585 "Get symbol at point in custom buffers."
586 (condition-case nil
587 (save-excursion
588 (let ((case-fold-search t)
589 (ignored-chars "][()`',:.\" \t\n")
590 (significant-chars "^][()`',:.\" \t\n")
591 beg end)
592 (cond
593 ((and (memq (get-char-property (point) 'face)
594 '(custom-variable-tag custom-variable-tag-face))
595 (setq beg (previous-single-char-property-change
596 (point) 'face nil (line-beginning-position)))
597 (setq end (next-single-char-property-change
598 (point) 'face nil (line-end-position)))
599 (> end beg))
600 (subst-char-in-string
601 ?\s ?\- (buffer-substring-no-properties beg end)))
602 ((or (and (looking-at (concat "[" significant-chars "]"))
603 (save-excursion
604 (skip-chars-backward significant-chars)
605 (setq beg (point)))
606 (skip-chars-forward significant-chars)
607 (setq end (point))
608 (> end beg))
609 (and (looking-at "[ \t\n]")
610 (looking-back (concat "[" significant-chars "]"))
611 (setq end (point))
612 (skip-chars-backward significant-chars)
613 (setq beg (point))
614 (> end beg))
615 (and (skip-chars-forward ignored-chars)
616 (setq beg (point))
617 (skip-chars-forward significant-chars)
618 (setq end (point))
619 (> end beg)))
620 (buffer-substring-no-properties beg end)))))
621 (error nil)))
623 ;;;###autoload
624 (defun info-complete-symbol (&optional mode)
625 "Perform completion on symbol preceding point."
626 (interactive)
627 (info-complete 'symbol
628 (or mode
629 (if (info-lookup->mode-value
630 'symbol (info-lookup-select-mode))
631 info-lookup-mode
632 (info-lookup-change-mode 'symbol)))))
634 ;;;###autoload
635 (defun info-complete-file (&optional mode)
636 "Perform completion on file preceding point."
637 (interactive)
638 (info-complete 'file
639 (or mode
640 (if (info-lookup->mode-value
641 'file (info-lookup-select-mode))
642 info-lookup-mode
643 (info-lookup-change-mode 'file)))))
645 (defun info-complete (topic mode)
646 "Try to complete a help item."
647 (barf-if-buffer-read-only)
648 (or mode (setq mode (info-lookup-select-mode)))
649 (or (info-lookup->mode-value topic mode)
650 (error "No %s completion available for `%s'" topic mode))
651 (let ((modes (info-lookup-quick-all-modes topic mode))
652 (start (point))
653 try)
654 (while (and (not try) modes)
655 (setq mode (car modes)
656 modes (cdr modes)
657 try (info-lookup-guess-default* topic mode))
658 (goto-char start))
659 (and (not try)
660 (error "Found no %S to complete" topic))
661 (let ((completions (info-lookup->completions topic mode))
662 (completion-ignore-case (info-lookup->ignore-case topic mode))
663 completion)
664 (setq completion (try-completion try completions))
665 (cond ((not completion)
666 (ding)
667 (message "No match"))
668 ((stringp completion)
669 (or (assoc completion completions)
670 (setq completion (completing-read
671 (format "Complete %S: " topic)
672 completions nil t completion
673 info-lookup-history)))
674 ;; Find the original symbol and zap it.
675 (end-of-line)
676 (while (and (search-backward try nil t)
677 (< start (point))))
678 (replace-match "")
679 (insert completion))
681 (message "%s is complete"
682 (capitalize (prin1-to-string topic))))))))
685 ;;; Initialize some common modes.
687 (info-lookup-maybe-add-help
688 :mode 'c-mode :topic 'symbol
689 :regexp "\\(struct \\|union \\|enum \\)?[_a-zA-Z][_a-zA-Z0-9]*"
690 :doc-spec '(("(libc)Function Index" nil
691 "^[ \t]+-+ \\(Function\\|Macro\\): .*\\<" "\\>")
692 ;; prefix/suffix has to match things like
693 ;; " -- Macro: int F_DUPFD"
694 ;; " -- Variable: char * tzname [2]"
695 ;; "`DBL_MAX'" (texinfo @table)
696 ;; suffix "\\>" is not used because that sends DBL_MAX to
697 ;; DBL_MAX_EXP ("_" is a non-word char)
698 ("(libc)Variable Index" nil
699 "^\\([ \t]+-+ \\(Variable\\|Macro\\): .*\\<\\|`\\)"
700 "\\( \\|'?$\\)")
701 ("(libc)Type Index" nil
702 "^[ \t]+-+ Data Type: \\<" "\\>")
703 ("(termcap)Var Index" nil
704 "^[ \t]*`" "'"))
705 :parse-rule 'info-lookup-guess-c-symbol)
707 (info-lookup-maybe-add-help
708 :mode 'c-mode :topic 'file
709 :regexp "[_a-zA-Z0-9./+-]+"
710 :doc-spec '(("(libc)File Index")))
712 (info-lookup-maybe-add-help
713 :mode 'bison-mode
714 :regexp "[:;|]\\|%\\([%{}]\\|[_a-z]+\\)\\|YY[_A-Z]+\\|yy[_a-z]+"
715 :doc-spec '(("(bison)Index" nil
716 "`" "'"))
717 :parse-rule "[:;|]\\|%\\([%{}]\\|[_a-zA-Z][_a-zA-Z0-9]*\\)"
718 :other-modes '(c-mode))
720 (info-lookup-maybe-add-help
721 :mode 'makefile-mode
722 :regexp "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z][_a-zA-Z0-9-]*"
723 :doc-spec '(("(make)Name Index" nil
724 "^[ \t]*`" "'")
725 ("(automake)Macro and Variable Index" nil
726 "^[ \t]*`" "'"))
727 :parse-rule "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z0-9-]+"
728 :other-modes '(automake-mode))
730 (info-lookup-maybe-add-help
731 :mode 'texinfo-mode
732 :regexp "@\\([a-zA-Z]+\\|[^a-zA-Z]\\)"
733 :doc-spec '(("(texinfo)Command and Variable Index"
734 ;; Ignore Emacs commands and prepend a `@'.
735 (lambda (item)
736 (if (string-match "^\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\( .*\\)?$" item)
737 (concat "@" (match-string 1 item))))
738 "`" "[' ]")))
740 (info-lookup-maybe-add-help
741 :mode 'm4-mode
742 :regexp "[_a-zA-Z][_a-zA-Z0-9]*"
743 :doc-spec '(("(m4)Macro index"))
744 :parse-rule "[_a-zA-Z0-9]+")
746 (info-lookup-maybe-add-help
747 :mode 'autoconf-mode
748 :regexp "A[CM]_[_A-Z0-9]+"
749 :doc-spec '(;; Autoconf Macro Index entries are without an "AC_" prefix,
750 ;; but with "AH_" or "AU_" for those. So add "AC_" if there
751 ;; isn't already an "A._".
752 ("(autoconf)Autoconf Macro Index"
753 (lambda (item)
754 (if (string-match "^A._" item) item (concat "AC_" item)))
755 "^[ \t]+-+ \\(Macro\\|Variable\\): .*\\<" "\\>")
756 ;; M4 Macro Index entries are without "AS_" prefixes, and
757 ;; mostly without "m4_" prefixes. "dnl" is an exception, not
758 ;; wanting any prefix. So AS_ is added back to upper-case
759 ;; names (if needed), m4_ to others which don't already an m4_.
760 ("(autoconf)M4 Macro Index"
761 (lambda (item)
762 (let ((case-fold-search nil))
763 (cond ((or (string-equal item "dnl")
764 (string-match "^m4_" item)
765 ;; Autoconf 2.62 index includes some macros
766 ;; (e.g., AS_HELP_STRING), so avoid prefixing.
767 (string-match "^AS_" item))
768 item)
769 ((string-match "^[A-Z0-9_]+$" item)
770 (concat "AS_" item))
772 (concat "m4_" item)))))
773 "^[ \t]+-+ Macro: .*\\<" "\\>")
774 ;; Autotest Macro Index entries are without "AT_".
775 ("(autoconf)Autotest Macro Index" "AT_"
776 "^[ \t]+-+ Macro: .*\\<" "\\>")
777 ;; This is for older versions (probably pre autoconf 2.5x):
778 ("(autoconf)Macro Index" "AC_"
779 "^[ \t]+-+ \\(Macro\\|Variable\\): .*\\<" "\\>")
780 ;; Automake has index entries for its notes on various autoconf
781 ;; macros (eg. AC_PROG_CC). Ensure this is after the autoconf
782 ;; index, so as to prefer the autoconf docs.
783 ("(automake)Macro and Variable Index" nil
784 "^[ \t]*`" "'"))
785 ;; Autoconf symbols are M4 macros. Thus use M4's parser.
786 :parse-rule 'ignore
787 :other-modes '(m4-mode))
789 (info-lookup-maybe-add-help
790 :mode 'awk-mode
791 :regexp "[_a-zA-Z]+"
792 :doc-spec '(("(gawk)Index"
793 (lambda (item)
794 (let ((case-fold-search nil))
795 (cond
796 ;; `BEGIN' and `END'.
797 ((string-match "^\\([A-Z]+\\) special pattern\\b" item)
798 (match-string 1 item))
799 ;; `if', `while', `do', ...
800 ((string-match "^\\([a-z]+\\) statement\\b" item)
801 (if (not (string-equal (match-string 1 item) "control"))
802 (match-string 1 item)))
803 ;; `NR', `NF', ...
804 ((string-match "^[A-Z]+$" item)
805 item)
806 ;; Built-in functions (matches to many entries).
807 ((string-match "^[a-z]+$" item)
808 item))))
809 "`" "\\([ \t]*([^)]*)\\)?'")))
811 (info-lookup-maybe-add-help
812 :mode 'perl-mode
813 :regexp "[$@%][^a-zA-Z]\\|\\$\\^[A-Z]\\|[$@%]?[a-zA-Z][_a-zA-Z0-9]*"
814 :doc-spec '(("(perl5)Function Index"
815 (lambda (item)
816 (if (string-match "^\\([a-zA-Z0-9]+\\)" item)
817 (match-string 1 item)))
818 "^" "\\b")
819 ("(perl5)Variable Index"
820 (lambda (item)
821 ;; Work around bad formatted array variables.
822 (let ((sym (cond ((or (string-match "^\\$\\(.\\|@@\\)$" item)
823 (string-match "^\\$\\^[A-Z]$" item))
824 item)
825 ((string-match
826 "^\\([$%@]\\|@@\\)?[_a-zA-Z0-9]+" item)
827 (match-string 0 item))
828 (t ""))))
829 (if (string-match "@@" sym)
830 (setq sym (concat (substring sym 0 (match-beginning 0))
831 (substring sym (1- (match-end 0))))))
832 (if (string-equal sym "") nil sym)))
833 "^" "\\b"))
834 :parse-rule "[$@%]?\\([_a-zA-Z0-9]+\\|[^a-zA-Z]\\)")
836 (info-lookup-maybe-add-help
837 :mode 'cperl-mode
838 :regexp "[$@%][^a-zA-Z]\\|\\$\\^[A-Z]\\|[$@%]?[a-zA-Z][_a-zA-Z0-9]*"
839 :other-modes '(perl-mode))
841 (info-lookup-maybe-add-help
842 :mode 'latex-mode
843 :regexp "\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)"
844 :doc-spec '(("(latex)Command Index" nil
845 "`" "\\({[^}]*}\\)?'")))
847 (info-lookup-maybe-add-help
848 :mode 'emacs-lisp-mode
849 :regexp "[^][()`',\" \t\n]+"
850 :doc-spec '(;; Commands with key sequences appear in nodes as `foo' and
851 ;; those without as `M-x foo'.
852 ("(emacs)Command Index" nil "`\\(M-x[ \t\n]+\\)?" "'")
853 ;; Variables normally appear in nodes as just `foo'.
854 ("(emacs)Variable Index" nil "`" "'")
855 ;; Almost all functions, variables, etc appear in nodes as
856 ;; " -- Function: foo" etc. A small number of aliases and
857 ;; symbols appear only as `foo', and will miss out on exact
858 ;; positions. Allowing `foo' would hit too many false matches
859 ;; for things that should go to Function: etc, and those latter
860 ;; are much more important. Perhaps this could change if some
861 ;; sort of fallback match scheme existed.
862 ("(elisp)Index" nil "^ -+ .*: " "\\( \\|$\\)")))
864 (info-lookup-maybe-add-help
865 :mode 'lisp-interaction-mode
866 :regexp "[^][()`',\" \t\n]+"
867 :parse-rule 'ignore
868 :other-modes '(emacs-lisp-mode))
870 (info-lookup-maybe-add-help
871 :mode 'lisp-mode
872 :regexp "[^()`',\" \t\n]+"
873 :parse-rule 'ignore
874 :other-modes '(emacs-lisp-mode))
876 (info-lookup-maybe-add-help
877 :mode 'scheme-mode
878 :regexp "[^()`',\" \t\n]+"
879 :ignore-case t
880 ;; Aubrey Jaffer's rendition from <URL:ftp://ftp-swiss.ai.mit.edu/pub/scm>
881 :doc-spec '(("(r5rs)Index" nil
882 "^[ \t]+-+ [^:]+:[ \t]*" "\\b")))
884 (info-lookup-maybe-add-help
885 :mode 'octave-mode
886 :regexp "[_a-zA-Z0-9]+\\|\\s.+\\|[-!=^|*/.\\,><~&+]\\{1,3\\}\\|[][();,\"']"
887 :doc-spec '(("(octave)Function Index" nil
888 "^ -+ [^:]+:[ ]+\\(\\[[^=]*=[ ]+\\)?" nil)
889 ("(octave)Variable Index" nil "^ -+ [^:]+:[ ]+" nil)
890 ("(octave)Operator Index" nil nil nil)
891 ;; Catch lines of the form "xyz statement"
892 ("(octave)Concept Index"
893 (lambda (item)
894 (cond
895 ((string-match "^\\([A-Z]+\\) statement\\b" item)
896 (match-string 1 item))
897 (t nil)))
898 nil; "^ -+ [^:]+:[ ]+" don't think this prefix is useful here.
899 nil)))
901 (info-lookup-maybe-add-help
902 :mode 'maxima-mode
903 :ignore-case t
904 :regexp "[a-zA-Z0-9_%]+"
905 :doc-spec '( ("(maxima)Function and Variable Index" nil
906 "^ -+ [^:]+:[ ]+\\(\\[[^=]*=[ ]+\\)?" nil)))
908 (info-lookup-maybe-add-help
909 :mode 'inferior-maxima-mode
910 :regexp "[a-zA-Z0-9_%]+"
911 :other-modes '(maxima-mode))
913 ;; coreutils and bash builtins overlap in places, eg. printf, so there's a
914 ;; question which should come first. Some of the coreutils descriptions are
915 ;; more detailed, but if bash is usually /bin/sh on a GNU system then the
916 ;; builtins will be what's normally run.
918 ;; Maybe special variables like $? should be matched as $?, not just ?.
919 ;; This would avoid a clash between variable $! and negation !, or variable
920 ;; $# and comment # (though comment # is not currently indexed in bash).
921 ;; Unfortunately if $? etc is the symbol, then we wouldn't be taken to the
922 ;; exact spot in the relevant node, since the bash manual has just `?' etc
923 ;; there. Maybe an extension to the prefix/suffix scheme could help this.
925 (info-lookup-maybe-add-help
926 :mode 'sh-mode :topic 'symbol
927 ;; bash has "." and ":" in its index, but those chars will probably never
928 ;; work in info, so don't bother matching them in the regexp.
929 :regexp "\\([a-zA-Z0-9_-]+\\|[!{}@*#?$]\\|\\[\\[?\\|]]?\\)"
930 :doc-spec '(("(bash)Builtin Index" nil "^`" "[ .']")
931 ("(bash)Reserved Word Index" nil "^`" "[ .']")
932 ("(bash)Variable Index" nil "^`" "[ .']")
933 ;; coreutils (version 4.5.10) doesn't have a separate program
934 ;; index, so exclude extraneous stuff (most of it) by demanding
935 ;; "[a-z]+" in the trans-func.
936 ("(coreutils)Index"
937 (lambda (item) (if (string-match "\\`[a-z]+\\'" item) item)))
938 ;; diff (version 2.8.1) has only a few programs, index entries
939 ;; are things like "foo invocation".
940 ("(diff)Index"
941 (lambda (item)
942 (if (string-match "\\`\\([a-z]+\\) invocation\\'" item)
943 (match-string 1 item))))
944 ;; there's no plain "sed" index entry as such, mung another
945 ;; hopefully unique one to get to the invocation section
946 ("(sed)Concept Index"
947 (lambda (item)
948 (if (string-equal item "Standard input, processing as input")
949 "sed")))
950 ;; there's no plain "awk" or "gawk" index entries, mung other
951 ;; hopefully unique ones to get to the command line options
952 ("(gawk)Index"
953 (lambda (item)
954 (cond ((string-equal item "gawk, extensions, disabling")
955 "awk")
956 ((string-equal item "gawk, versions of, information about, printing")
957 "gawk"))))))
959 ;; This misses some things which occur as node names but not in the
960 ;; index. Unfortunately it also picks up the wrong one of multiple
961 ;; entries for the same term in some cases. --fx
962 (info-lookup-maybe-add-help
963 :mode 'cfengine-mode
964 :regexp "[[:alnum:]_]+\\(?:()\\)?"
965 :doc-spec '(("(cfengine-Reference)Variable Index"
966 (lambda (item)
967 ;; Index entries may be like `IsPlain()'
968 (if (string-match "\\([[:alnum:]_]+\\)()" item)
969 (match-string 1 item)
970 item))
971 ;; This gets functions in evaluated classes. Other
972 ;; possible patterns don't seem to work too well.
973 "`" "(")))
975 (info-lookup-maybe-add-help
976 :mode 'Custom-mode
977 :ignore-case t
978 :regexp "[^][()`',:\" \t\n]+"
979 :parse-rule 'info-lookup-guess-custom-symbol
980 :other-modes '(emacs-lisp-mode))
982 (info-lookup-maybe-add-help
983 :mode 'help-mode
984 :regexp "[^][()`',:\" \t\n]+"
985 :other-modes '(emacs-lisp-mode))
987 (provide 'info-look)
989 ;; arch-tag: 0f1e3ea3-32a2-4461-bbab-3cff93539a74
990 ;;; info-look.el ends here