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