Correct translation and the Dutch text (typos).
[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
538996c7 4;; Copyright (C) 1995,96,97,98,99,2001,03,04 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
8270a848 50(defcustom info-lookup-highlight-face 'match
d2c2b883
RS
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
99400b3e 248;;;###autoload (put 'info-lookup-symbol 'info-file "emacs")
5a79736d
RS
249;;;###autoload
250(defun info-lookup-symbol (symbol &optional mode)
3481a5c0
KH
251 "Display the definition of SYMBOL, as found in the relevant manual.
252When this command is called interactively, it reads SYMBOL from the minibuffer.
253In the minibuffer, use M-n to yank the default argument value
254into the minibuffer so you can edit it.
b108eac2
KH
255The default symbol is the one found at point.
256
257With prefix arg a query for the symbol help mode is offered."
5a79736d 258 (interactive
b108eac2 259 (info-lookup-interactive-arguments 'symbol current-prefix-arg))
5a79736d
RS
260 (info-lookup 'symbol symbol mode))
261
99400b3e 262;;;###autoload (put 'info-lookup-file 'info-file "emacs")
5a79736d
RS
263;;;###autoload
264(defun info-lookup-file (file &optional mode)
265 "Display the documentation of a file.
3481a5c0
KH
266When this command is called interactively, it reads FILE from the minibuffer.
267In the minibuffer, use M-n to yank the default file name
268into the minibuffer so you can edit it.
b108eac2
KH
269The default file name is the one found at point.
270
271With prefix arg a query for the file help mode is offered."
5a79736d 272 (interactive
b108eac2 273 (info-lookup-interactive-arguments 'file current-prefix-arg))
5a79736d
RS
274 (info-lookup 'file file mode))
275
b108eac2
KH
276(defun info-lookup-interactive-arguments (topic &optional query)
277 "Read and return argument value (and help mode) for help topic TOPIC.
278If optional argument QUERY is non-nil, query for the help mode."
279 (let* ((mode (cond (query
280 (info-lookup-change-mode topic))
281 ((info-lookup->mode-value topic (info-lookup-select-mode))
282 info-lookup-mode)
283 ((info-lookup-change-mode topic))))
5a79736d
RS
284 (completions (info-lookup->completions topic mode))
285 (default (info-lookup-guess-default topic mode))
5a79736d
RS
286 (completion-ignore-case (info-lookup->ignore-case topic mode))
287 (enable-recursive-minibuffers t)
288 (value (completing-read
3481a5c0 289 (if default
5a79736d
RS
290 (format "Describe %s (default %s): " topic default)
291 (format "Describe %s: " topic))
3481a5c0 292 completions nil nil nil 'info-lookup-history default)))
5a79736d
RS
293 (list (if (equal value "") default value) mode)))
294
9d37318d
KH
295(defun info-lookup-select-mode ()
296 (when (and (not info-lookup-mode) (buffer-file-name))
297 (let ((file-name (file-name-nondirectory (buffer-file-name)))
298 (file-name-alist info-lookup-file-name-alist))
299 (while (and (not info-lookup-mode) file-name-alist)
300 (when (string-match (caar file-name-alist) file-name)
301 (setq info-lookup-mode (cdar file-name-alist)))
302 (setq file-name-alist (cdr file-name-alist)))))
303 (or info-lookup-mode (setq info-lookup-mode major-mode)))
304
5a79736d
RS
305(defun info-lookup-change-mode (topic)
306 (let* ((completions (mapcar (lambda (arg)
307 (cons (symbol-name (car arg)) (car arg)))
308 (info-lookup->topic-value topic)))
309 (mode (completing-read
310 (format "Use %s help mode: " topic)
311 completions nil t nil 'info-lookup-history)))
312 (or (setq mode (cdr (assoc mode completions)))
313 (error "No %s help available" topic))
314 (or (info-lookup->mode-value topic mode)
315 (error "No %s help available for `%s'" topic mode))
316 (setq info-lookup-mode mode)))
317
318(defun info-lookup (topic item mode)
319 "Display the documentation of a help item."
9d37318d 320 (or mode (setq mode (info-lookup-select-mode)))
5a79736d
RS
321 (or (info-lookup->mode-value topic mode)
322 (error "No %s help available for `%s'" topic mode))
7c3da01b
RS
323 (let* ((completions (info-lookup->completions topic mode))
324 (ignore-case (info-lookup->ignore-case topic mode))
325 (entry (or (assoc (if ignore-case (downcase item) item) completions)
0445150c 326 (assoc-string item completions t)
7c3da01b
RS
327 (error "Not documented as a %s: %s" topic (or item ""))))
328 (modes (info-lookup->all-modes topic mode))
329 (window (selected-window))
330 found doc-spec node prefix suffix doc-found)
947faf4f
JL
331 (if (not (eq major-mode 'Info-mode))
332 (if (not info-lookup-other-window-flag)
333 (info)
334 (progn
335 (save-window-excursion (info))
336 ;; Determine whether or not the Info buffer is visible in
337 ;; another frame on the same display. If it is, simply raise
338 ;; that frame. Otherwise, display it in another window.
339 (let* ((window (get-buffer-window "*info*" t))
340 (info-frame (and window (window-frame window))))
341 (if (and info-frame
342 (display-multi-frame-p)
343 (memq info-frame (frames-on-display-list))
344 (not (eq info-frame (selected-frame))))
345 (select-frame info-frame)
346 (switch-to-buffer-other-window "*info*"))))))
5a79736d
RS
347 (while (and (not found) modes)
348 (setq doc-spec (info-lookup->doc-spec topic (car modes)))
349 (while (and (not found) doc-spec)
350 (setq node (nth 0 (car doc-spec))
351 prefix (nth 2 (car doc-spec))
352 suffix (nth 3 (car doc-spec)))
1d010333 353 (when (condition-case error-data
e35ccb9e 354 (progn
1d010333
RS
355 (Info-goto-node node)
356 (setq doc-found t))
e35ccb9e 357 (error
1d010333
RS
358 (message "Cannot access Info node %s" node)
359 (sit-for 1)
360 nil))
361 (condition-case nil
362 (progn
7c3da01b
RS
363 ;; Don't use Info-menu, it forces case-fold-search to t
364 (let ((case-fold-search nil))
365 (re-search-forward
366 (concat "^\\* " (regexp-quote (or (cdr entry) (car entry)))
367 ":")))
368 (Info-follow-nearest-node)
1d010333
RS
369 (setq found t)
370 (if (or prefix suffix)
371 (let ((case-fold-search
372 (info-lookup->ignore-case topic (car modes)))
373 (buffer-read-only nil))
374 (goto-char (point-min))
375 (re-search-forward
7c3da01b 376 (concat prefix (regexp-quote (car entry)) suffix))
1d010333 377 (goto-char (match-beginning 0))
141d2f67 378 (and (display-color-p) info-lookup-highlight-face
1d010333 379 ;; Search again for ITEM so that the first
a5a08b1f 380 ;; occurrence of ITEM will be highlighted.
7c3da01b 381 (re-search-forward (regexp-quote (car entry)))
1d010333
RS
382 (let ((start (match-beginning 0))
383 (end (match-end 0)))
384 (if (overlayp info-lookup-highlight-overlay)
385 (move-overlay info-lookup-highlight-overlay
386 start end (current-buffer))
387 (setq info-lookup-highlight-overlay
388 (make-overlay start end))))
389 (overlay-put info-lookup-highlight-overlay
390 'face info-lookup-highlight-face)))))
391 (error nil)))
5a79736d
RS
392 (setq doc-spec (cdr doc-spec)))
393 (setq modes (cdr modes)))
7c3da01b
RS
394 ;; Alert the user if case was munged, and do this after bringing up the
395 ;; info buffer since that can print messages
396 (unless (or ignore-case
397 (string-equal item (car entry)))
021c54a3 398 (message "Found in different case: %s" (car entry)))
1d010333
RS
399 (or doc-found
400 (error "Info documentation for lookup was not found"))
5a79736d
RS
401 ;; Don't leave the Info buffer if the help item couldn't be looked up.
402 (if (and info-lookup-other-window-flag found)
403 (select-window window))))
404
405(defun info-lookup-setup-mode (topic mode)
406 "Initialize the internal data structure."
407 (or (info-lookup->initialized topic mode)
408 (let (cell data (initialized 0) completions refer-modes)
409 (if (not (info-lookup->mode-value topic mode))
410 (message "No %s help available for `%s'" topic mode)
411 ;; Recursively setup cross references.
412 ;; But refer only to non-void modes.
538996c7
SM
413 (dolist (arg (info-lookup->other-modes topic mode))
414 (or (info-lookup->initialized topic arg)
415 (info-lookup-setup-mode topic arg))
416 (and (eq (info-lookup->initialized topic arg) t)
417 (setq refer-modes (cons arg refer-modes))))
5a79736d
RS
418 (setq refer-modes (nreverse refer-modes))
419 ;; Build the full completion alist.
420 (setq completions
e12fcc41
KH
421 (nconc (condition-case nil
422 (info-lookup-make-completions topic mode)
423 (error nil))
5a79736d
RS
424 (apply 'append
425 (mapcar (lambda (arg)
426 (info-lookup->completions topic arg))
427 refer-modes))))
428 (setq initialized t))
429 ;; Update `info-lookup-cache'.
430 (setq cell (info-lookup->mode-cache topic mode)
431 data (list initialized completions refer-modes))
432 (if (not cell)
433 (setcdr (info-lookup->cache topic)
434 (cons (cons mode data) (info-lookup->topic-cache topic)))
435 (setcdr cell data))
436 initialized)))
437
438(defun info-lookup-make-completions (topic mode)
439 "Create a unique alist from all index entries."
1eff0ba1
RS
440 (let ((doc-spec (info-lookup->doc-spec topic mode))
441 (regexp (concat "^\\(" (info-lookup->regexp topic mode)
442 "\\)\\([ \t].*\\)?$"))
1d010333 443 node trans entry item prefix result doc-found
1eff0ba1
RS
444 (buffer (get-buffer-create " temp-info-look")))
445 (with-current-buffer buffer
446 (Info-mode))
447 (while doc-spec
448 (setq node (nth 0 (car doc-spec))
449 trans (cond ((eq (nth 1 (car doc-spec)) nil)
450 (lambda (arg)
451 (if (string-match regexp arg)
452 (match-string 1 arg))))
453 ((stringp (nth 1 (car doc-spec)))
454 (setq prefix (nth 1 (car doc-spec)))
455 (lambda (arg)
456 (if (string-match "^\\([^: \t\n]+\\)" arg)
457 (concat prefix (match-string 1 arg)))))
458 (t (nth 1 (car doc-spec)))))
1d010333
RS
459 (with-current-buffer buffer
460 (message "Processing Info node `%s'..." node)
461 (when (condition-case error-data
e35ccb9e 462 (progn
1d010333
RS
463 (Info-goto-node node)
464 (setq doc-found t))
e35ccb9e 465 (error
1d010333
RS
466 (message "Cannot access Info node `%s'" node)
467 (sit-for 1)
468 nil))
469 (condition-case nil
470 (progn
471 (goto-char (point-min))
472 (and (search-forward "\n* Menu:" nil t)
bdb0f2d5 473 (while (re-search-forward "\n\\* \\(.*\\): " nil t)
1d010333
RS
474 (setq entry (match-string 1)
475 item (funcall trans entry))
6480a693
DL
476 ;; `trans' can return nil if the regexp doesn't match.
477 (when (and item
478 ;; Sometimes there's more than one Menu:
e35ccb9e 479 (not (string= entry "Menu")))
6480a693
DL
480 (and (info-lookup->ignore-case topic mode)
481 (setq item (downcase item)))
482 (and (string-equal entry item)
483 (setq entry nil))
484 (and (or (assoc item result)
485 (setq result (cons (cons item entry)
486 result))))))))
1d010333 487 (error nil))))
1eff0ba1
RS
488 (message "Processing Info node `%s'...done" node)
489 (setq doc-spec (cdr doc-spec)))
1d010333
RS
490 (or doc-found
491 (error "Info documentation for lookup was not found"))
1eff0ba1 492 result))
5a79736d
RS
493
494(defun info-lookup-guess-default (topic mode)
ce288cb6
KH
495 "Return a guess for a symbol to look up, based on text around point.
496Try all related modes applicable to TOPIC and MODE.
497Return nil if there is nothing appropriate in the buffer near point."
5a79736d 498 (let ((modes (info-lookup->all-modes topic mode))
ce288cb6 499 guess)
5a79736d
RS
500 (while (and (not guess) modes)
501 (setq guess (info-lookup-guess-default* topic (car modes))
ce288cb6 502 modes (cdr modes)))
5a79736d 503 ;; Collapse whitespace characters.
ce288cb6
KH
504 (when guess
505 (let ((pos 0))
506 (while (string-match "[ \t\n]+" guess pos)
507 (setq pos (1+ (match-beginning 0)))
508 (setq guess (replace-match " " t t guess)))))
509 guess))
5a79736d
RS
510
511(defun info-lookup-guess-default* (topic mode)
512 (let ((case-fold-search (info-lookup->ignore-case topic mode))
513 (rule (or (info-lookup->parse-rule topic mode)
514 (info-lookup->regexp topic mode)))
515 (start (point)) end regexp subexp result)
ce288cb6
KH
516 (save-excursion
517 (if (symbolp rule)
518 (setq result (funcall rule))
519 (if (consp rule)
520 (setq regexp (car rule)
521 subexp (cdr rule))
522 (setq regexp rule
523 subexp 0))
e35ccb9e
DL
524 ;; If at start of symbol, don't go back to end of previous one.
525 (if (save-match-data
526 (looking-at "[ \t\n]"))
527 (skip-chars-backward " \t\n"))
528 (setq end (point))
ce288cb6
KH
529 (while (and (re-search-backward regexp nil t)
530 (looking-at regexp)
531 (>= (match-end 0) end))
532 (setq result (match-string subexp)))
533 (if (not result)
534 (progn
535 (goto-char start)
536 (skip-chars-forward " \t\n")
537 (and (looking-at regexp)
538 (setq result (match-string subexp)))))))
5a79736d
RS
539 result))
540
541(defun info-lookup-guess-c-symbol ()
542 "Get the C symbol at point."
543 (condition-case nil
544 (progn
ce288cb6 545 (skip-syntax-backward "w_")
5a79736d
RS
546 (let ((start (point)) prefix name)
547 ;; Test for a leading `struct', `union', or `enum' keyword
548 ;; but ignore names like `foo_struct'.
549 (setq prefix (and (< (skip-chars-backward " \t\n") 0)
550 (< (skip-chars-backward "_a-zA-Z0-9") 0)
551 (looking-at "\\(struct\\|union\\|enum\\)\\s ")
552 (concat (match-string 1) " ")))
553 (goto-char start)
554 (and (looking-at "[_a-zA-Z][_a-zA-Z0-9]*")
555 (setq name (match-string 0)))
556 ;; Caveat! Look forward if point is at `struct' etc.
557 (and (not prefix)
558 (or (string-equal name "struct")
559 (string-equal name "union")
560 (string-equal name "enum"))
561 (looking-at "[a-z]+\\s +\\([_a-zA-Z][_a-zA-Z0-9]*\\)")
562 (setq prefix (concat name " ")
563 name (match-string 1)))
564 (and (or prefix name)
565 (concat prefix name))))
566 (error nil)))
567
568;;;###autoload
569(defun info-complete-symbol (&optional mode)
570 "Perform completion on symbol preceding point."
82fb111c
RS
571 (interactive)
572 (info-complete 'symbol
573 (or mode
574 (if (info-lookup->mode-value
9d37318d
KH
575 'symbol (info-lookup-select-mode))
576 info-lookup-mode
82fb111c 577 (info-lookup-change-mode 'symbol)))))
5a79736d
RS
578
579;;;###autoload
580(defun info-complete-file (&optional mode)
581 "Perform completion on file preceding point."
a1fee1bc
KH
582 (interactive)
583 (info-complete 'file
584 (or mode
585 (if (info-lookup->mode-value
9d37318d
KH
586 'file (info-lookup-select-mode))
587 info-lookup-mode
a1fee1bc 588 (info-lookup-change-mode 'file)))))
5a79736d
RS
589
590(defun info-complete (topic mode)
591 "Try to complete a help item."
592 (barf-if-buffer-read-only)
9d37318d 593 (or mode (setq mode (info-lookup-select-mode)))
5a79736d
RS
594 (or (info-lookup->mode-value topic mode)
595 (error "No %s completion available for `%s'" topic mode))
0c10ee28
RS
596 (let ((modes (info-lookup-quick-all-modes topic mode))
597 (start (point))
598 try)
5a79736d
RS
599 (while (and (not try) modes)
600 (setq mode (car modes)
601 modes (cdr modes)
602 try (info-lookup-guess-default* topic mode))
603 (goto-char start))
604 (and (not try)
d2c2b883 605 (error "Found no %S to complete" topic))
0c10ee28
RS
606 (let ((completions (info-lookup->completions topic mode))
607 (completion-ignore-case (info-lookup->ignore-case topic mode))
608 completion)
609 (setq completion (try-completion try completions))
610 (cond ((not completion)
611 (ding)
612 (message "No match"))
613 ((stringp completion)
614 (or (assoc completion completions)
615 (setq completion (completing-read
616 (format "Complete %S: " topic)
617 completions nil t completion
618 info-lookup-history)))
e35ccb9e
DL
619 ;; Find the original symbol and zap it.
620 (end-of-line)
621 (while (and (search-backward try nil t)
622 (< start (point))))
623 (replace-match "")
0c10ee28
RS
624 (insert completion))
625 (t
626 (message "%s is complete"
627 (capitalize (prin1-to-string topic))))))))
d2c2b883
RS
628
629\f
d2c2b883
RS
630;;; Initialize some common modes.
631
632(info-lookup-maybe-add-help
633 :mode 'c-mode :topic 'symbol
634 :regexp "\\(struct \\|union \\|enum \\)?[_a-zA-Z][_a-zA-Z0-9]*"
635 :doc-spec '(("(libc)Function Index" nil
947faf4f 636 "^[ \t]+-+ \\(Function\\|Macro\\): .*\\<" "\\>")
d2c2b883 637 ("(libc)Variable Index" nil
947faf4f 638 "^[ \t]+-+ \\(Variable\\|Macro\\): .*\\<" "\\>")
d2c2b883 639 ("(libc)Type Index" nil
947faf4f 640 "^[ \t]+-+ Data Type: \\<" "\\>")
d2c2b883
RS
641 ("(termcap)Var Index" nil
642 "^[ \t]*`" "'"))
643 :parse-rule 'info-lookup-guess-c-symbol)
644
645(info-lookup-maybe-add-help
646 :mode 'c-mode :topic 'file
647 :regexp "[_a-zA-Z0-9./+-]+"
648 :doc-spec '(("(libc)File Index")))
649
650(info-lookup-maybe-add-help
651 :mode 'bison-mode
652 :regexp "[:;|]\\|%\\([%{}]\\|[_a-z]+\\)\\|YY[_A-Z]+\\|yy[_a-z]+"
653 :doc-spec '(("(bison)Index" nil
654 "`" "'"))
655 :parse-rule "[:;|]\\|%\\([%{}]\\|[_a-zA-Z][_a-zA-Z0-9]*\\)"
656 :other-modes '(c-mode))
657
658(info-lookup-maybe-add-help
659 :mode 'makefile-mode
660 :regexp "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z][_a-zA-Z0-9-]*"
661 :doc-spec '(("(make)Name Index" nil
fdba4ab4
SM
662 "^[ \t]*`" "'")
663 ("(automake)Macro and Variable Index" nil
d2c2b883 664 "^[ \t]*`" "'"))
fdba4ab4
SM
665 :parse-rule "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z0-9-]+"
666 :other-modes '(automake-mode))
d2c2b883
RS
667
668(info-lookup-maybe-add-help
669 :mode 'texinfo-mode
670 :regexp "@\\([a-zA-Z]+\\|[^a-zA-Z]\\)"
671 :doc-spec '(("(texinfo)Command and Variable Index"
672 ;; Ignore Emacs commands and prepend a `@'.
673 (lambda (item)
674 (if (string-match "^\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\( .*\\)?$" item)
675 (concat "@" (match-string 1 item))))
947faf4f 676 "`" "[' ]")))
d2c2b883
RS
677
678(info-lookup-maybe-add-help
679 :mode 'm4-mode
680 :regexp "[_a-zA-Z][_a-zA-Z0-9]*"
681 :doc-spec '(("(m4)Macro index"))
682 :parse-rule "[_a-zA-Z0-9]+")
683
684(info-lookup-maybe-add-help
685 :mode 'autoconf-mode
686 :regexp "A[CM]_[_A-Z0-9]+"
01e1a819
JB
687 :doc-spec '(;; Autoconf Macro Index entries are without an "AC_" prefix,
688 ;; but with "AH_" or "AU_" for those. So add "AC_" if there
689 ;; isn't already an "A._".
690 ("(autoconf)Autoconf Macro Index"
691 (lambda (item)
692 (if (string-match "^A._" item) item (concat "AC_" item)))
947faf4f 693 "^[ \t]+-+ \\(Macro\\|Variable\\): .*\\<" "\\>")
01e1a819
JB
694 ;; M4 Macro Index entries are without "AS_" prefixes, and
695 ;; mostly without "m4_" prefixes. "dnl" is an exception, not
696 ;; wanting any prefix. So AS_ is added back to upper-case
697 ;; names, m4_ to others which don't already an m4_.
698 ("(autoconf)M4 Macro Index"
699 (lambda (item)
700 (let ((case-fold-search nil))
701 (cond ((or (string-equal item "dnl")
702 (string-match "^m4_" item))
703 item)
704 ((string-match "^[A-Z0-9_]+$" item)
705 (concat "AS_" item))
706 (t
707 (concat "m4_" item)))))
947faf4f 708 "^[ \t]+-+ Macro: .*\\<" "\\>")
01e1a819
JB
709 ;; Autotest Macro Index entries are without "AT_".
710 ("(autoconf)Autotest Macro Index" "AT_"
947faf4f 711 "^[ \t]+-+ Macro: .*\\<" "\\>")
01e1a819 712 ;; This is for older versions (probably pre autoconf 2.5x):
28a2ca5d 713 ("(autoconf)Macro Index" "AC_"
947faf4f 714 "^[ \t]+-+ \\(Macro\\|Variable\\): .*\\<" "\\>")
01e1a819
JB
715 ;; Automake has index entries for its notes on various autoconf
716 ;; macros (eg. AC_PROG_CC). Ensure this is after the autoconf
717 ;; index, so as to prefer the autoconf docs.
fdba4ab4 718 ("(automake)Macro and Variable Index" nil
d2c2b883
RS
719 "^[ \t]*`" "'"))
720 ;; Autoconf symbols are M4 macros. Thus use M4's parser.
721 :parse-rule 'ignore
722 :other-modes '(m4-mode))
723
724(info-lookup-maybe-add-help
725 :mode 'awk-mode
726 :regexp "[_a-zA-Z]+"
727 :doc-spec '(("(gawk)Index"
728 (lambda (item)
729 (let ((case-fold-search nil))
730 (cond
731 ;; `BEGIN' and `END'.
732 ((string-match "^\\([A-Z]+\\) special pattern\\b" item)
733 (match-string 1 item))
734 ;; `if', `while', `do', ...
735 ((string-match "^\\([a-z]+\\) statement\\b" item)
736 (if (not (string-equal (match-string 1 item) "control"))
737 (match-string 1 item)))
738 ;; `NR', `NF', ...
739 ((string-match "^[A-Z]+$" item)
740 item)
741 ;; Built-in functions (matches to many entries).
742 ((string-match "^[a-z]+$" item)
743 item))))
744 "`" "\\([ \t]*([^)]*)\\)?'")))
745
746(info-lookup-maybe-add-help
747 :mode 'perl-mode
748 :regexp "[$@%][^a-zA-Z]\\|\\$\\^[A-Z]\\|[$@%]?[a-zA-Z][_a-zA-Z0-9]*"
749 :doc-spec '(("(perl5)Function Index"
750 (lambda (item)
751 (if (string-match "^\\([a-zA-Z0-9]+\\)" item)
752 (match-string 1 item)))
753 "^" "\\b")
754 ("(perl5)Variable Index"
755 (lambda (item)
756 ;; Work around bad formatted array variables.
757 (let ((sym (cond ((or (string-match "^\\$\\(.\\|@@\\)$" item)
758 (string-match "^\\$\\^[A-Z]$" item))
759 item)
760 ((string-match
761 "^\\([$%@]\\|@@\\)?[_a-zA-Z0-9]+" item)
762 (match-string 0 item))
763 (t ""))))
764 (if (string-match "@@" sym)
765 (setq sym (concat (substring sym 0 (match-beginning 0))
766 (substring sym (1- (match-end 0))))))
767 (if (string-equal sym "") nil sym)))
768 "^" "\\b"))
769 :parse-rule "[$@%]?\\([_a-zA-Z0-9]+\\|[^a-zA-Z]\\)")
770
41b1cae7
SM
771(info-lookup-maybe-add-help
772 :mode 'cperl-mode
773 :regexp "[$@%][^a-zA-Z]\\|\\$\\^[A-Z]\\|[$@%]?[a-zA-Z][_a-zA-Z0-9]*"
774 :other-modes '(perl-mode))
775
d2c2b883
RS
776(info-lookup-maybe-add-help
777 :mode 'latex-mode
778 :regexp "\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)"
e12fcc41 779 :doc-spec '(("(latex)Command Index" nil
d2c2b883
RS
780 "`" "\\({[^}]*}\\)?'")))
781
782(info-lookup-maybe-add-help
783 :mode 'emacs-lisp-mode
41b1cae7 784 :regexp "[^][()'\" \t\n]+"
16524b9b
JB
785 :doc-spec '(;; Commands with key sequences appear in nodes as `foo' and
786 ;; those without as `M-x foo'.
787 ("(emacs)Command Index" nil "`\\(M-x[ \t\n]+\\)?" "'")
788 ;; Variables normally appear in nodes as just `foo'.
789 ("(emacs)Variable Index" nil "`" "'")
790 ;; Almost all functions, variables, etc appear in nodes as
947faf4f 791 ;; " -- Function: foo" etc. A small number of aliases and
16524b9b
JB
792 ;; symbols appear only as `foo', and will miss out on exact
793 ;; positions. Allowing `foo' would hit too many false matches
794 ;; for things that should go to Function: etc, and those latter
795 ;; are much more important. Perhaps this could change if some
796 ;; sort of fallback match scheme existed.
947faf4f 797 ("(elisp)Index" nil "^ -+ .*: " "\\( \\|$\\)")))
d2c2b883
RS
798
799(info-lookup-maybe-add-help
800 :mode 'lisp-interaction-mode
41b1cae7 801 :regexp "[^][()'\" \t\n]+"
d2c2b883
RS
802 :parse-rule 'ignore
803 :other-modes '(emacs-lisp-mode))
804
0c10ee28
RS
805(info-lookup-maybe-add-help
806 :mode 'lisp-mode
41b1cae7 807 :regexp "[^()'\" \t\n]+"
0c10ee28
RS
808 :parse-rule 'ignore
809 :other-modes '(emacs-lisp-mode))
810
9d37318d
KH
811(info-lookup-maybe-add-help
812 :mode 'scheme-mode
08d87b2d 813 :regexp "[^()`',\" \t\n]+"
9d37318d 814 :ignore-case t
6480a693 815 ;; Aubrey Jaffer's rendition from <URL:ftp://ftp-swiss.ai.mit.edu/pub/scm>
9d37318d 816 :doc-spec '(("(r5rs)Index" nil
947faf4f 817 "^[ \t]+-+ [^:]+:[ \t]*" "\\b")))
9d37318d 818
054e5c20
SE
819(info-lookup-maybe-add-help
820 :mode 'octave-mode
821 :regexp "[_a-zA-Z0-9]+"
c6bb2688 822 :doc-spec '(("(octave)Function Index" nil
947faf4f
JL
823 "^ -+ [^:]+:[ ]+\\(\\[[^=]*=[ ]+\\)?" nil)
824 ("(octave)Variable Index" nil "^ -+ [^:]+:[ ]+" nil)
054e5c20 825 ;; Catch lines of the form "xyz statement"
e35ccb9e 826 ("(octave)Concept Index"
054e5c20
SE
827 (lambda (item)
828 (cond
829 ((string-match "^\\([A-Z]+\\) statement\\b" item)
830 (match-string 1 item))
831 (t nil)))
947faf4f 832 nil; "^ -+ [^:]+:[ ]+" don't think this prefix is useful here.
054e5c20 833 nil)))
c0e7fbb8 834
b3a3e4e1
SE
835(info-lookup-maybe-add-help
836 :mode 'maxima-mode
837 :ignore-case t
838 :regexp "[a-zA-Z_%]+"
947faf4f
JL
839 :doc-spec '( ("(maxima)Function and Variable Index" nil
840 "^ -+ [^:]+:[ ]+\\(\\[[^=]*=[ ]+\\)?" nil)))
b3a3e4e1
SE
841
842(info-lookup-maybe-add-help
843 :mode 'inferior-maxima-mode
844 :other-modes '(maxima-mode))
845
c0e7fbb8 846;; coreutils and bash builtins overlap in places, eg. printf, so there's a
c6bb2688 847;; question which should come first. Some of the coreutils descriptions are
c0e7fbb8
JB
848;; more detailed, but if bash is usually /bin/sh on a GNU system then the
849;; builtins will be what's normally run.
850;;
851;; Maybe special variables like $? should be matched as $?, not just ?.
852;; This would avoid a clash between variable $! and negation !, or variable
853;; $# and comment # (though comment # is not currently indexed in bash).
854;; Unfortunately if $? etc is the symbol, then we wouldn't be taken to the
855;; exact spot in the relevant node, since the bash manual has just `?' etc
856;; there. Maybe an extension to the prefix/suffix scheme could help this.
857
858(info-lookup-maybe-add-help
859 :mode 'sh-mode :topic 'symbol
860 ;; bash has "." and ":" in its index, but those chars will probably never
861 ;; work in info, so don't bother matching them in the regexp.
862 :regexp "\\([a-zA-Z0-9_-]+\\|[!{}@*#?$]\\|\\[\\[?\\|]]?\\)"
863 :doc-spec '(("(bash)Builtin Index" nil "^`" "[ .']")
864 ("(bash)Reserved Word Index" nil "^`" "[ .']")
865 ("(bash)Variable Index" nil "^`" "[ .']")
866 ;; coreutils (version 4.5.10) doesn't have a separate program
867 ;; index, so exclude extraneous stuff (most of it) by demanding
868 ;; "[a-z]+" in the trans-func.
869 ("(coreutils)Index"
870 (lambda (item) (if (string-match "\\`[a-z]+\\'" item) item)))
871 ;; diff (version 2.8.1) has only a few programs, index entries
872 ;; are things like "foo invocation".
873 ("(diff)Index"
874 (lambda (item)
875 (if (string-match "\\`\\([a-z]+\\) invocation\\'" item)
876 (match-string 1 item))))
877 ;; there's no plain "sed" index entry as such, mung another
878 ;; hopefully unique one to get to the invocation section
879 ("(sed)Concept Index"
880 (lambda (item)
881 (if (string-equal item "Standard input, processing as input")
882 "sed")))
883 ;; there's no plain "awk" or "gawk" index entries, mung other
884 ;; hopefully unique ones to get to the command line options
885 ("(gawk)Index"
886 (lambda (item)
887 (cond ((string-equal item "gawk, extensions, disabling")
888 "awk")
889 ((string-equal item "gawk, versions of, information about, printing")
890 "gawk"))))))
538996c7 891
7830c895
SM
892;; This misses some things which occur as node names but not in the
893;; index. Unfortunately it also picks up the wrong one of multiple
894;; entries for the same term in some cases. --fx
538996c7
SM
895(info-lookup-maybe-add-help
896 :mode 'cfengine-mode
1bc897ca 897 :regexp "[[:alnum:]_]+\\(?:()\\)?"
7830c895
SM
898 :doc-spec '(("(cfengine-Reference)Variable Index"
899 (lambda (item)
900 ;; Index entries may be like `IsPlain()'
901 (if (string-match "\\([[:alnum:]_]+\\)()" item)
902 (match-string 1 item)
903 item))
904 ;; This gets functions in evaluated classes. Other
905 ;; possible patterns don't seem to work too well.
906 "`" "(")))
d2c2b883 907\f
5a79736d
RS
908(provide 'info-look)
909
ab5796a9 910;;; arch-tag: 0f1e3ea3-32a2-4461-bbab-3cff93539a74
5a79736d 911;;; info-look.el ends here