(calculate-lisp-indent): Added indentation for the constants of Lisp.
[bpt/emacs.git] / lisp / info-look.el
CommitLineData
e8af40ee 1;;; info-look.el --- major-mode-sensitive Info index lookup facility
5a79736d
RS
2;; An older version of this was known as libc.el.
3
0d30b337 4;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2001, 2002, 2003,
d7a0267c 5;; 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5a79736d 6
3b361901 7;; Author: Ralph Schleicher <rs@nunatak.allgaeu.org>
fdba4ab4 8;; (did not show signs of life (Nov 2001) -stef)
5a79736d
RS
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
086add15
LK
25;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26;; Boston, MA 02110-1301, USA.
5a79736d 27
41b1cae7
SM
28;;; Commentary:
29
30;; Really cool code to lookup info indexes.
b3a3e4e1 31;; Try especially info-lookup-symbol (aka C-h S).
41b1cae7 32
5a79736d
RS
33;;; Code:
34
35(require 'info)
d2c2b883
RS
36
37(defgroup info-lookup nil
38 "Major mode sensitive help agent."
39 :group 'help :group 'languages)
5a79736d
RS
40
41(defvar info-lookup-mode nil
d2c2b883
RS
42 "Symbol of the current buffer's help mode.
43Help is provided according to the buffer's major mode if value is nil.
5a79736d
RS
44Automatically becomes buffer local when set in any fashion.")
45(make-variable-buffer-local 'info-lookup-mode)
46
d2c2b883
RS
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)
5a79736d 50
8270a848 51(defcustom info-lookup-highlight-face 'match
d2c2b883
RS
52 "Face for highlighting looked up help items.
53Setting this variable to nil disables highlighting."
54 :group 'info-lookup :type 'face)
5a79736d
RS
55
56(defvar info-lookup-highlight-overlay nil
57 "Overlay object used for highlighting.")
58
9d37318d 59(defcustom info-lookup-file-name-alist
28a2ca5d 60 '(("\\`ac\\(local\\|site\\|include\\)\\.m4\\'" . autoconf-mode))
9d37318d
KH
61 "Alist of file names handled specially.
62List elements are cons cells of the form
63
64 (REGEXP . MODE)
65
66If a file name matches REGEXP, then use help mode MODE instead of the
67buffer's major mode."
68 :group 'info-lookup :type '(repeat (cons (string :tag "Regexp")
69 (symbol :tag "Mode"))))
70
5a79736d
RS
71(defvar info-lookup-history nil
72 "History of previous input lines.")
73
d2c2b883
RS
74(defvar info-lookup-alist nil
75 "Alist of known help topics.
5a79736d
RS
76Cons cells are of the form
77
d2c2b883 78 (HELP-TOPIC . HELP-DATA)
5a79736d
RS
79
80HELP-TOPIC is the symbol of a help topic.
d2c2b883 81HELP-DATA is a HELP-TOPIC's public data set.
5a79736d
RS
82 Value is an alist with elements of the form
83
84 (HELP-MODE REGEXP IGNORE-CASE DOC-SPEC PARSE-RULE OTHER-MODES)
85
86HELP-MODE is a mode's symbol.
87REGEXP is a regular expression matching those help items whose
88 documentation can be looked up via DOC-SPEC.
89IGNORE-CASE is non-nil if help items are case insensitive.
90DOC-SPEC is a list of documentation specifications of the form
91
92 (INFO-NODE TRANS-FUNC PREFIX SUFFIX)
93
94INFO-NODE is the name (including file name part) of an Info index.
95TRANS-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.
98PREFIX 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.
103PARSE-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.
108OTHER-MODES is a list of cross references to other help modes.")
109
110(defsubst info-lookup->topic-value (topic)
d2c2b883 111 (cdr (assoc topic info-lookup-alist)))
5a79736d
RS
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
d2c2b883
RS
131(defun info-lookup-add-help (&rest arg)
132 "Add or update a help specification.
133Function arguments are one or more options of the form
134
135 KEYWORD ARGUMENT
136
137KEYWORD is either `:topic', `:mode', `:regexp', `:ignore-case',
138 `:doc-spec', `:parse-rule', or `:other-modes'.
139ARGUMENT has a value as explained in the documentation of the
140 variable `info-lookup-alist'.
141
142If no topic or mode option has been specified, then the help topic defaults
143to `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)
41b1cae7 147 "Add a help specification iff none is defined.
d2c2b883
RS
148See the documentation of the function `info-lookup-add-help'
149for 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
5a79736d
RS
195(defvar info-lookup-cache nil
196 "Cache storing data maintained automatically by the program.
197Value is an alist with cons cell of the form
198
199 (HELP-TOPIC . ((HELP-MODE INITIALIZED COMPLETIONS REFER-MODES) ...))
200
201HELP-TOPIC is the symbol of a help topic.
202HELP-MODE is a mode's symbol.
203INITIALIZED is nil if HELP-MODE is uninitialized, t if
204 HELP-MODE is initialized, and `0' means HELP-MODE is
205 initialized but void.
206COMPLETIONS is an alist of documented help items.
207REFER-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
0c10ee28 215(defun info-lookup->topic-cache (topic)
5a79736d
RS
216 (cdr (info-lookup->cache topic)))
217
0c10ee28 218(defun info-lookup->mode-cache (topic mode)
5a79736d
RS
219 (assoc mode (info-lookup->topic-cache topic)))
220
0c10ee28 221(defun info-lookup->initialized (topic mode)
5a79736d
RS
222 (nth 1 (info-lookup->mode-cache topic mode)))
223
0c10ee28 224(defun info-lookup->completions (topic mode)
5a79736d
RS
225 (or (info-lookup->initialized topic mode)
226 (info-lookup-setup-mode topic mode))
227 (nth 2 (info-lookup->mode-cache topic mode)))
228
0c10ee28 229(defun info-lookup->refer-modes (topic mode)
5a79736d
RS
230 (or (info-lookup->initialized topic mode)
231 (info-lookup-setup-mode topic mode))
232 (nth 3 (info-lookup->mode-cache topic mode)))
233
0c10ee28 234(defun info-lookup->all-modes (topic mode)
5a79736d
RS
235 (cons mode (info-lookup->refer-modes topic mode)))
236
0c10ee28
RS
237(defun info-lookup-quick-all-modes (topic mode)
238 (cons mode (info-lookup->other-modes topic mode)))
239
5a79736d
RS
240;;;###autoload
241(defun info-lookup-reset ()
242 "Throw away all cached data.
243This command is useful if the user wants to start at the beginning without
244quitting Emacs, for example, after some Info documents were updated on the
245system."
246 (interactive)
247 (setq info-lookup-cache nil))
248
99400b3e 249;;;###autoload (put 'info-lookup-symbol 'info-file "emacs")
5a79736d
RS
250;;;###autoload
251(defun info-lookup-symbol (symbol &optional mode)
3481a5c0 252 "Display the definition of SYMBOL, as found in the relevant manual.
ec4ef174
EZ
253When this command is called interactively, it reads SYMBOL from the
254minibuffer. In the minibuffer, use M-n to yank the default argument
255value into the minibuffer so you can edit it. The default symbol is the
256one found at point.
b108eac2
KH
257
258With prefix arg a query for the symbol help mode is offered."
5a79736d 259 (interactive
b108eac2 260 (info-lookup-interactive-arguments 'symbol current-prefix-arg))
5a79736d
RS
261 (info-lookup 'symbol symbol mode))
262
99400b3e 263;;;###autoload (put 'info-lookup-file 'info-file "emacs")
5a79736d
RS
264;;;###autoload
265(defun info-lookup-file (file &optional mode)
266 "Display the documentation of a file.
3481a5c0
KH
267When this command is called interactively, it reads FILE from the minibuffer.
268In the minibuffer, use M-n to yank the default file name
269into the minibuffer so you can edit it.
b108eac2
KH
270The default file name is the one found at point.
271
272With prefix arg a query for the file help mode is offered."
5a79736d 273 (interactive
b108eac2 274 (info-lookup-interactive-arguments 'file current-prefix-arg))
5a79736d
RS
275 (info-lookup 'file file mode))
276
b108eac2
KH
277(defun info-lookup-interactive-arguments (topic &optional query)
278 "Read and return argument value (and help mode) for help topic TOPIC.
279If 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))))
5a79736d
RS
285 (completions (info-lookup->completions topic mode))
286 (default (info-lookup-guess-default topic mode))
5a79736d
RS
287 (completion-ignore-case (info-lookup->ignore-case topic mode))
288 (enable-recursive-minibuffers t)
289 (value (completing-read
3481a5c0 290 (if default
5a79736d
RS
291 (format "Describe %s (default %s): " topic default)
292 (format "Describe %s: " topic))
3481a5c0 293 completions nil nil nil 'info-lookup-history default)))
5a79736d
RS
294 (list (if (equal value "") default value) mode)))
295
9d37318d
KH
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
5a79736d
RS
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."
9d37318d 321 (or mode (setq mode (info-lookup-select-mode)))
5a79736d
RS
322 (or (info-lookup->mode-value topic mode)
323 (error "No %s help available for `%s'" topic mode))
7c3da01b
RS
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)
0445150c 327 (assoc-string item completions t)
7c3da01b
RS
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)
947faf4f
JL
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*"))))))
5a79736d
RS
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)))
1d010333 354 (when (condition-case error-data
e35ccb9e 355 (progn
1d010333
RS
356 (Info-goto-node node)
357 (setq doc-found t))
e35ccb9e 358 (error
1d010333
RS
359 (message "Cannot access Info node %s" node)
360 (sit-for 1)
361 nil))
362 (condition-case nil
363 (progn
7c3da01b
RS
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)
1d010333
RS
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
7c3da01b 377 (concat prefix (regexp-quote (car entry)) suffix))
1d010333 378 (goto-char (match-beginning 0))
141d2f67 379 (and (display-color-p) info-lookup-highlight-face
1d010333 380 ;; Search again for ITEM so that the first
a5a08b1f 381 ;; occurrence of ITEM will be highlighted.
7c3da01b 382 (re-search-forward (regexp-quote (car entry)))
1d010333
RS
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)))
5a79736d
RS
393 (setq doc-spec (cdr doc-spec)))
394 (setq modes (cdr modes)))
7c3da01b
RS
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)))
021c54a3 399 (message "Found in different case: %s" (car entry)))
1d010333
RS
400 (or doc-found
401 (error "Info documentation for lookup was not found"))
5a79736d
RS
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.
538996c7
SM
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))))
5a79736d
RS
419 (setq refer-modes (nreverse refer-modes))
420 ;; Build the full completion alist.
421 (setq completions
e12fcc41
KH
422 (nconc (condition-case nil
423 (info-lookup-make-completions topic mode)
424 (error nil))
5a79736d
RS
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."
1eff0ba1
RS
441 (let ((doc-spec (info-lookup->doc-spec topic mode))
442 (regexp (concat "^\\(" (info-lookup->regexp topic mode)
443 "\\)\\([ \t].*\\)?$"))
493cf720 444 Info-fontify-maximum-menu-size
1d010333 445 node trans entry item prefix result doc-found
1eff0ba1
RS
446 (buffer (get-buffer-create " temp-info-look")))
447 (with-current-buffer buffer
448 (Info-mode))
449 (while doc-spec
450 (setq node (nth 0 (car doc-spec))
451 trans (cond ((eq (nth 1 (car doc-spec)) nil)
452 (lambda (arg)
453 (if (string-match regexp arg)
454 (match-string 1 arg))))
455 ((stringp (nth 1 (car doc-spec)))
456 (setq prefix (nth 1 (car doc-spec)))
457 (lambda (arg)
458 (if (string-match "^\\([^: \t\n]+\\)" arg)
459 (concat prefix (match-string 1 arg)))))
460 (t (nth 1 (car doc-spec)))))
1d010333
RS
461 (with-current-buffer buffer
462 (message "Processing Info node `%s'..." node)
463 (when (condition-case error-data
e35ccb9e 464 (progn
1d010333
RS
465 (Info-goto-node node)
466 (setq doc-found t))
e35ccb9e 467 (error
1d010333
RS
468 (message "Cannot access Info node `%s'" node)
469 (sit-for 1)
470 nil))
471 (condition-case nil
472 (progn
473 (goto-char (point-min))
474 (and (search-forward "\n* Menu:" nil t)
bdb0f2d5 475 (while (re-search-forward "\n\\* \\(.*\\): " nil t)
1d010333
RS
476 (setq entry (match-string 1)
477 item (funcall trans entry))
6480a693
DL
478 ;; `trans' can return nil if the regexp doesn't match.
479 (when (and item
480 ;; Sometimes there's more than one Menu:
e35ccb9e 481 (not (string= entry "Menu")))
6480a693
DL
482 (and (info-lookup->ignore-case topic mode)
483 (setq item (downcase item)))
484 (and (string-equal entry item)
485 (setq entry nil))
486 (and (or (assoc item result)
487 (setq result (cons (cons item entry)
488 result))))))))
1d010333 489 (error nil))))
1eff0ba1
RS
490 (message "Processing Info node `%s'...done" node)
491 (setq doc-spec (cdr doc-spec)))
1d010333
RS
492 (or doc-found
493 (error "Info documentation for lookup was not found"))
1eff0ba1 494 result))
5a79736d
RS
495
496(defun info-lookup-guess-default (topic mode)
ce288cb6
KH
497 "Return a guess for a symbol to look up, based on text around point.
498Try all related modes applicable to TOPIC and MODE.
499Return nil if there is nothing appropriate in the buffer near point."
5a79736d 500 (let ((modes (info-lookup->all-modes topic mode))
ce288cb6 501 guess)
5a79736d
RS
502 (while (and (not guess) modes)
503 (setq guess (info-lookup-guess-default* topic (car modes))
ce288cb6 504 modes (cdr modes)))
5a79736d 505 ;; Collapse whitespace characters.
ce288cb6
KH
506 (when guess
507 (let ((pos 0))
508 (while (string-match "[ \t\n]+" guess pos)
509 (setq pos (1+ (match-beginning 0)))
510 (setq guess (replace-match " " t t guess)))))
511 guess))
5a79736d
RS
512
513(defun info-lookup-guess-default* (topic mode)
514 (let ((case-fold-search (info-lookup->ignore-case topic mode))
515 (rule (or (info-lookup->parse-rule topic mode)
516 (info-lookup->regexp topic mode)))
517 (start (point)) end regexp subexp result)
ce288cb6
KH
518 (save-excursion
519 (if (symbolp rule)
520 (setq result (funcall rule))
521 (if (consp rule)
522 (setq regexp (car rule)
523 subexp (cdr rule))
524 (setq regexp rule
525 subexp 0))
e35ccb9e
DL
526 ;; If at start of symbol, don't go back to end of previous one.
527 (if (save-match-data
528 (looking-at "[ \t\n]"))
529 (skip-chars-backward " \t\n"))
530 (setq end (point))
ce288cb6
KH
531 (while (and (re-search-backward regexp nil t)
532 (looking-at regexp)
533 (>= (match-end 0) end))
534 (setq result (match-string subexp)))
535 (if (not result)
536 (progn
537 (goto-char start)
538 (skip-chars-forward " \t\n")
539 (and (looking-at regexp)
540 (setq result (match-string subexp)))))))
5a79736d
RS
541 result))
542
543(defun info-lookup-guess-c-symbol ()
544 "Get the C symbol at point."
545 (condition-case nil
546 (progn
ce288cb6 547 (skip-syntax-backward "w_")
5a79736d
RS
548 (let ((start (point)) prefix name)
549 ;; Test for a leading `struct', `union', or `enum' keyword
550 ;; but ignore names like `foo_struct'.
551 (setq prefix (and (< (skip-chars-backward " \t\n") 0)
552 (< (skip-chars-backward "_a-zA-Z0-9") 0)
553 (looking-at "\\(struct\\|union\\|enum\\)\\s ")
554 (concat (match-string 1) " ")))
555 (goto-char start)
556 (and (looking-at "[_a-zA-Z][_a-zA-Z0-9]*")
557 (setq name (match-string 0)))
558 ;; Caveat! Look forward if point is at `struct' etc.
559 (and (not prefix)
560 (or (string-equal name "struct")
561 (string-equal name "union")
562 (string-equal name "enum"))
563 (looking-at "[a-z]+\\s +\\([_a-zA-Z][_a-zA-Z0-9]*\\)")
564 (setq prefix (concat name " ")
565 name (match-string 1)))
566 (and (or prefix name)
567 (concat prefix name))))
568 (error nil)))
569
ec4ef174
EZ
570(defun info-lookup-guess-custom-symbol ()
571 "Get symbol at point in custom buffers."
572 (condition-case nil
573 (save-excursion
574 (let ((case-fold-search t)
575 (ignored-chars "][()`',:.\" \t\n")
576 (significant-chars "^][()`',:.\" \t\n")
577 beg end)
578 (cond
579 ((and (memq (get-char-property (point) 'face)
580 '(custom-variable-tag custom-variable-tag-face))
581 (setq beg (previous-single-char-property-change
582 (point) 'face nil (line-beginning-position)))
583 (setq end (next-single-char-property-change
584 (point) 'face nil (line-end-position)))
585 (> end beg))
586 (subst-char-in-string
4789a3cb 587 ?\s ?\- (buffer-substring-no-properties beg end)))
ec4ef174
EZ
588 ((or (and (looking-at (concat "[" significant-chars "]"))
589 (save-excursion
590 (skip-chars-backward significant-chars)
591 (setq beg (point)))
592 (skip-chars-forward significant-chars)
593 (setq end (point))
594 (> end beg))
595 (and (looking-at "[ \t\n]")
596 (looking-back (concat "[" significant-chars "]"))
597 (setq end (point))
598 (skip-chars-backward significant-chars)
599 (setq beg (point))
600 (> end beg))
601 (and (skip-chars-forward ignored-chars)
602 (setq beg (point))
603 (skip-chars-forward significant-chars)
604 (setq end (point))
605 (> end beg)))
606 (buffer-substring-no-properties beg end)))))
607 (error nil)))
608
5a79736d
RS
609;;;###autoload
610(defun info-complete-symbol (&optional mode)
611 "Perform completion on symbol preceding point."
82fb111c
RS
612 (interactive)
613 (info-complete 'symbol
614 (or mode
615 (if (info-lookup->mode-value
9d37318d
KH
616 'symbol (info-lookup-select-mode))
617 info-lookup-mode
82fb111c 618 (info-lookup-change-mode 'symbol)))))
5a79736d
RS
619
620;;;###autoload
621(defun info-complete-file (&optional mode)
622 "Perform completion on file preceding point."
a1fee1bc
KH
623 (interactive)
624 (info-complete 'file
625 (or mode
626 (if (info-lookup->mode-value
9d37318d
KH
627 'file (info-lookup-select-mode))
628 info-lookup-mode
a1fee1bc 629 (info-lookup-change-mode 'file)))))
5a79736d
RS
630
631(defun info-complete (topic mode)
632 "Try to complete a help item."
633 (barf-if-buffer-read-only)
9d37318d 634 (or mode (setq mode (info-lookup-select-mode)))
5a79736d
RS
635 (or (info-lookup->mode-value topic mode)
636 (error "No %s completion available for `%s'" topic mode))
0c10ee28
RS
637 (let ((modes (info-lookup-quick-all-modes topic mode))
638 (start (point))
639 try)
5a79736d
RS
640 (while (and (not try) modes)
641 (setq mode (car modes)
642 modes (cdr modes)
643 try (info-lookup-guess-default* topic mode))
644 (goto-char start))
645 (and (not try)
d2c2b883 646 (error "Found no %S to complete" topic))
0c10ee28
RS
647 (let ((completions (info-lookup->completions topic mode))
648 (completion-ignore-case (info-lookup->ignore-case topic mode))
649 completion)
650 (setq completion (try-completion try completions))
651 (cond ((not completion)
652 (ding)
653 (message "No match"))
654 ((stringp completion)
655 (or (assoc completion completions)
656 (setq completion (completing-read
657 (format "Complete %S: " topic)
658 completions nil t completion
659 info-lookup-history)))
e35ccb9e
DL
660 ;; Find the original symbol and zap it.
661 (end-of-line)
662 (while (and (search-backward try nil t)
663 (< start (point))))
664 (replace-match "")
0c10ee28
RS
665 (insert completion))
666 (t
667 (message "%s is complete"
668 (capitalize (prin1-to-string topic))))))))
d2c2b883
RS
669
670\f
d2c2b883
RS
671;;; Initialize some common modes.
672
673(info-lookup-maybe-add-help
674 :mode 'c-mode :topic 'symbol
675 :regexp "\\(struct \\|union \\|enum \\)?[_a-zA-Z][_a-zA-Z0-9]*"
676 :doc-spec '(("(libc)Function Index" nil
947faf4f 677 "^[ \t]+-+ \\(Function\\|Macro\\): .*\\<" "\\>")
3b90e932
EZ
678 ;; prefix/suffix has to match things like
679 ;; " -- Macro: int F_DUPFD"
680 ;; " -- Variable: char * tzname [2]"
681 ;; "`DBL_MAX'" (texinfo @table)
682 ;; suffix "\\>" is not used because that sends DBL_MAX to
683 ;; DBL_MAX_EXP ("_" is a non-word char)
d2c2b883 684 ("(libc)Variable Index" nil
3b90e932
EZ
685 "^\\([ \t]+-+ \\(Variable\\|Macro\\): .*\\<\\|`\\)"
686 "\\( \\|'?$\\)")
d2c2b883 687 ("(libc)Type Index" nil
947faf4f 688 "^[ \t]+-+ Data Type: \\<" "\\>")
d2c2b883
RS
689 ("(termcap)Var Index" nil
690 "^[ \t]*`" "'"))
691 :parse-rule 'info-lookup-guess-c-symbol)
692
693(info-lookup-maybe-add-help
694 :mode 'c-mode :topic 'file
695 :regexp "[_a-zA-Z0-9./+-]+"
696 :doc-spec '(("(libc)File Index")))
697
698(info-lookup-maybe-add-help
699 :mode 'bison-mode
700 :regexp "[:;|]\\|%\\([%{}]\\|[_a-z]+\\)\\|YY[_A-Z]+\\|yy[_a-z]+"
701 :doc-spec '(("(bison)Index" nil
702 "`" "'"))
703 :parse-rule "[:;|]\\|%\\([%{}]\\|[_a-zA-Z][_a-zA-Z0-9]*\\)"
704 :other-modes '(c-mode))
705
706(info-lookup-maybe-add-help
707 :mode 'makefile-mode
708 :regexp "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z][_a-zA-Z0-9-]*"
709 :doc-spec '(("(make)Name Index" nil
fdba4ab4
SM
710 "^[ \t]*`" "'")
711 ("(automake)Macro and Variable Index" nil
d2c2b883 712 "^[ \t]*`" "'"))
fdba4ab4
SM
713 :parse-rule "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z0-9-]+"
714 :other-modes '(automake-mode))
d2c2b883
RS
715
716(info-lookup-maybe-add-help
717 :mode 'texinfo-mode
718 :regexp "@\\([a-zA-Z]+\\|[^a-zA-Z]\\)"
719 :doc-spec '(("(texinfo)Command and Variable Index"
720 ;; Ignore Emacs commands and prepend a `@'.
721 (lambda (item)
722 (if (string-match "^\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\( .*\\)?$" item)
723 (concat "@" (match-string 1 item))))
947faf4f 724 "`" "[' ]")))
d2c2b883
RS
725
726(info-lookup-maybe-add-help
727 :mode 'm4-mode
728 :regexp "[_a-zA-Z][_a-zA-Z0-9]*"
729 :doc-spec '(("(m4)Macro index"))
730 :parse-rule "[_a-zA-Z0-9]+")
731
732(info-lookup-maybe-add-help
733 :mode 'autoconf-mode
734 :regexp "A[CM]_[_A-Z0-9]+"
01e1a819
JB
735 :doc-spec '(;; Autoconf Macro Index entries are without an "AC_" prefix,
736 ;; but with "AH_" or "AU_" for those. So add "AC_" if there
737 ;; isn't already an "A._".
738 ("(autoconf)Autoconf Macro Index"
739 (lambda (item)
740 (if (string-match "^A._" item) item (concat "AC_" item)))
947faf4f 741 "^[ \t]+-+ \\(Macro\\|Variable\\): .*\\<" "\\>")
01e1a819
JB
742 ;; M4 Macro Index entries are without "AS_" prefixes, and
743 ;; mostly without "m4_" prefixes. "dnl" is an exception, not
744 ;; wanting any prefix. So AS_ is added back to upper-case
745 ;; names, m4_ to others which don't already an m4_.
746 ("(autoconf)M4 Macro Index"
747 (lambda (item)
748 (let ((case-fold-search nil))
749 (cond ((or (string-equal item "dnl")
750 (string-match "^m4_" item))
751 item)
752 ((string-match "^[A-Z0-9_]+$" item)
753 (concat "AS_" item))
754 (t
755 (concat "m4_" item)))))
947faf4f 756 "^[ \t]+-+ Macro: .*\\<" "\\>")
01e1a819
JB
757 ;; Autotest Macro Index entries are without "AT_".
758 ("(autoconf)Autotest Macro Index" "AT_"
947faf4f 759 "^[ \t]+-+ Macro: .*\\<" "\\>")
01e1a819 760 ;; This is for older versions (probably pre autoconf 2.5x):
28a2ca5d 761 ("(autoconf)Macro Index" "AC_"
947faf4f 762 "^[ \t]+-+ \\(Macro\\|Variable\\): .*\\<" "\\>")
01e1a819
JB
763 ;; Automake has index entries for its notes on various autoconf
764 ;; macros (eg. AC_PROG_CC). Ensure this is after the autoconf
765 ;; index, so as to prefer the autoconf docs.
fdba4ab4 766 ("(automake)Macro and Variable Index" nil
d2c2b883
RS
767 "^[ \t]*`" "'"))
768 ;; Autoconf symbols are M4 macros. Thus use M4's parser.
769 :parse-rule 'ignore
770 :other-modes '(m4-mode))
771
772(info-lookup-maybe-add-help
773 :mode 'awk-mode
774 :regexp "[_a-zA-Z]+"
775 :doc-spec '(("(gawk)Index"
776 (lambda (item)
777 (let ((case-fold-search nil))
778 (cond
779 ;; `BEGIN' and `END'.
780 ((string-match "^\\([A-Z]+\\) special pattern\\b" item)
781 (match-string 1 item))
782 ;; `if', `while', `do', ...
783 ((string-match "^\\([a-z]+\\) statement\\b" item)
784 (if (not (string-equal (match-string 1 item) "control"))
785 (match-string 1 item)))
786 ;; `NR', `NF', ...
787 ((string-match "^[A-Z]+$" item)
788 item)
789 ;; Built-in functions (matches to many entries).
790 ((string-match "^[a-z]+$" item)
791 item))))
792 "`" "\\([ \t]*([^)]*)\\)?'")))
793
794(info-lookup-maybe-add-help
795 :mode 'perl-mode
796 :regexp "[$@%][^a-zA-Z]\\|\\$\\^[A-Z]\\|[$@%]?[a-zA-Z][_a-zA-Z0-9]*"
797 :doc-spec '(("(perl5)Function Index"
798 (lambda (item)
799 (if (string-match "^\\([a-zA-Z0-9]+\\)" item)
800 (match-string 1 item)))
801 "^" "\\b")
802 ("(perl5)Variable Index"
803 (lambda (item)
804 ;; Work around bad formatted array variables.
805 (let ((sym (cond ((or (string-match "^\\$\\(.\\|@@\\)$" item)
806 (string-match "^\\$\\^[A-Z]$" item))
807 item)
808 ((string-match
809 "^\\([$%@]\\|@@\\)?[_a-zA-Z0-9]+" item)
810 (match-string 0 item))
811 (t ""))))
812 (if (string-match "@@" sym)
813 (setq sym (concat (substring sym 0 (match-beginning 0))
814 (substring sym (1- (match-end 0))))))
815 (if (string-equal sym "") nil sym)))
816 "^" "\\b"))
817 :parse-rule "[$@%]?\\([_a-zA-Z0-9]+\\|[^a-zA-Z]\\)")
818
41b1cae7
SM
819(info-lookup-maybe-add-help
820 :mode 'cperl-mode
821 :regexp "[$@%][^a-zA-Z]\\|\\$\\^[A-Z]\\|[$@%]?[a-zA-Z][_a-zA-Z0-9]*"
822 :other-modes '(perl-mode))
823
d2c2b883
RS
824(info-lookup-maybe-add-help
825 :mode 'latex-mode
826 :regexp "\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)"
e12fcc41 827 :doc-spec '(("(latex)Command Index" nil
d2c2b883
RS
828 "`" "\\({[^}]*}\\)?'")))
829
830(info-lookup-maybe-add-help
831 :mode 'emacs-lisp-mode
ec4ef174 832 :regexp "[^][()`',\" \t\n]+"
16524b9b
JB
833 :doc-spec '(;; Commands with key sequences appear in nodes as `foo' and
834 ;; those without as `M-x foo'.
835 ("(emacs)Command Index" nil "`\\(M-x[ \t\n]+\\)?" "'")
836 ;; Variables normally appear in nodes as just `foo'.
837 ("(emacs)Variable Index" nil "`" "'")
838 ;; Almost all functions, variables, etc appear in nodes as
947faf4f 839 ;; " -- Function: foo" etc. A small number of aliases and
16524b9b
JB
840 ;; symbols appear only as `foo', and will miss out on exact
841 ;; positions. Allowing `foo' would hit too many false matches
842 ;; for things that should go to Function: etc, and those latter
843 ;; are much more important. Perhaps this could change if some
844 ;; sort of fallback match scheme existed.
947faf4f 845 ("(elisp)Index" nil "^ -+ .*: " "\\( \\|$\\)")))
d2c2b883
RS
846
847(info-lookup-maybe-add-help
848 :mode 'lisp-interaction-mode
ec4ef174 849 :regexp "[^][()`',\" \t\n]+"
d2c2b883
RS
850 :parse-rule 'ignore
851 :other-modes '(emacs-lisp-mode))
852
0c10ee28
RS
853(info-lookup-maybe-add-help
854 :mode 'lisp-mode
ec4ef174 855 :regexp "[^()`',\" \t\n]+"
0c10ee28
RS
856 :parse-rule 'ignore
857 :other-modes '(emacs-lisp-mode))
858
9d37318d
KH
859(info-lookup-maybe-add-help
860 :mode 'scheme-mode
08d87b2d 861 :regexp "[^()`',\" \t\n]+"
9d37318d 862 :ignore-case t
6480a693 863 ;; Aubrey Jaffer's rendition from <URL:ftp://ftp-swiss.ai.mit.edu/pub/scm>
9d37318d 864 :doc-spec '(("(r5rs)Index" nil
947faf4f 865 "^[ \t]+-+ [^:]+:[ \t]*" "\\b")))
9d37318d 866
054e5c20
SE
867(info-lookup-maybe-add-help
868 :mode 'octave-mode
869 :regexp "[_a-zA-Z0-9]+"
c6bb2688 870 :doc-spec '(("(octave)Function Index" nil
947faf4f
JL
871 "^ -+ [^:]+:[ ]+\\(\\[[^=]*=[ ]+\\)?" nil)
872 ("(octave)Variable Index" nil "^ -+ [^:]+:[ ]+" nil)
054e5c20 873 ;; Catch lines of the form "xyz statement"
e35ccb9e 874 ("(octave)Concept Index"
054e5c20
SE
875 (lambda (item)
876 (cond
877 ((string-match "^\\([A-Z]+\\) statement\\b" item)
878 (match-string 1 item))
879 (t nil)))
947faf4f 880 nil; "^ -+ [^:]+:[ ]+" don't think this prefix is useful here.
054e5c20 881 nil)))
c0e7fbb8 882
b3a3e4e1
SE
883(info-lookup-maybe-add-help
884 :mode 'maxima-mode
885 :ignore-case t
c99240cc 886 :regexp "[a-zA-Z0-9_%]+"
947faf4f
JL
887 :doc-spec '( ("(maxima)Function and Variable Index" nil
888 "^ -+ [^:]+:[ ]+\\(\\[[^=]*=[ ]+\\)?" nil)))
b3a3e4e1
SE
889
890(info-lookup-maybe-add-help
891 :mode 'inferior-maxima-mode
c99240cc 892 :regexp "[a-zA-Z0-9_%]+"
b3a3e4e1
SE
893 :other-modes '(maxima-mode))
894
c0e7fbb8 895;; coreutils and bash builtins overlap in places, eg. printf, so there's a
c6bb2688 896;; question which should come first. Some of the coreutils descriptions are
c0e7fbb8
JB
897;; more detailed, but if bash is usually /bin/sh on a GNU system then the
898;; builtins will be what's normally run.
899;;
900;; Maybe special variables like $? should be matched as $?, not just ?.
901;; This would avoid a clash between variable $! and negation !, or variable
902;; $# and comment # (though comment # is not currently indexed in bash).
903;; Unfortunately if $? etc is the symbol, then we wouldn't be taken to the
904;; exact spot in the relevant node, since the bash manual has just `?' etc
905;; there. Maybe an extension to the prefix/suffix scheme could help this.
906
907(info-lookup-maybe-add-help
908 :mode 'sh-mode :topic 'symbol
909 ;; bash has "." and ":" in its index, but those chars will probably never
910 ;; work in info, so don't bother matching them in the regexp.
911 :regexp "\\([a-zA-Z0-9_-]+\\|[!{}@*#?$]\\|\\[\\[?\\|]]?\\)"
912 :doc-spec '(("(bash)Builtin Index" nil "^`" "[ .']")
913 ("(bash)Reserved Word Index" nil "^`" "[ .']")
914 ("(bash)Variable Index" nil "^`" "[ .']")
915 ;; coreutils (version 4.5.10) doesn't have a separate program
916 ;; index, so exclude extraneous stuff (most of it) by demanding
917 ;; "[a-z]+" in the trans-func.
918 ("(coreutils)Index"
919 (lambda (item) (if (string-match "\\`[a-z]+\\'" item) item)))
920 ;; diff (version 2.8.1) has only a few programs, index entries
921 ;; are things like "foo invocation".
922 ("(diff)Index"
923 (lambda (item)
924 (if (string-match "\\`\\([a-z]+\\) invocation\\'" item)
925 (match-string 1 item))))
926 ;; there's no plain "sed" index entry as such, mung another
927 ;; hopefully unique one to get to the invocation section
928 ("(sed)Concept Index"
929 (lambda (item)
930 (if (string-equal item "Standard input, processing as input")
931 "sed")))
932 ;; there's no plain "awk" or "gawk" index entries, mung other
933 ;; hopefully unique ones to get to the command line options
934 ("(gawk)Index"
935 (lambda (item)
936 (cond ((string-equal item "gawk, extensions, disabling")
937 "awk")
938 ((string-equal item "gawk, versions of, information about, printing")
939 "gawk"))))))
538996c7 940
7830c895
SM
941;; This misses some things which occur as node names but not in the
942;; index. Unfortunately it also picks up the wrong one of multiple
943;; entries for the same term in some cases. --fx
538996c7
SM
944(info-lookup-maybe-add-help
945 :mode 'cfengine-mode
1bc897ca 946 :regexp "[[:alnum:]_]+\\(?:()\\)?"
7830c895
SM
947 :doc-spec '(("(cfengine-Reference)Variable Index"
948 (lambda (item)
949 ;; Index entries may be like `IsPlain()'
950 (if (string-match "\\([[:alnum:]_]+\\)()" item)
951 (match-string 1 item)
952 item))
953 ;; This gets functions in evaluated classes. Other
954 ;; possible patterns don't seem to work too well.
955 "`" "(")))
ec4ef174
EZ
956
957(info-lookup-maybe-add-help
958 :mode 'custom-mode
959 :ignore-case t
960 :regexp "[^][()`',:\" \t\n]+"
961 :parse-rule 'info-lookup-guess-custom-symbol
962 :other-modes '(emacs-lisp-mode))
963
964(info-lookup-maybe-add-help
965 :mode 'help-mode
966 :regexp "[^][()`',:\" \t\n]+"
967 :other-modes '(emacs-lisp-mode))
d2c2b883 968\f
5a79736d
RS
969(provide 'info-look)
970
ab5796a9 971;;; arch-tag: 0f1e3ea3-32a2-4461-bbab-3cff93539a74
5a79736d 972;;; info-look.el ends here