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