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