(Info-last-search): Variable removed.
[bpt/emacs.git] / lisp / info-look.el
CommitLineData
5a79736d
RS
1;;; info-look.el --- major-mode-sensitive Info index lookup facility.
2;; An older version of this was known as libc.el.
3
e35ccb9e 4;; Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
5a79736d
RS
5
6;; Author: Ralph Schleicher <rs@purple.UL.BaWue.DE>
ba20f077 7;; Maintainers: FSF (unless Schleicher can be found)
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
27;;; Code:
28
29(require 'info)
d2c2b883
RS
30
31(defgroup info-lookup nil
32 "Major mode sensitive help agent."
33 :group 'help :group 'languages)
5a79736d
RS
34
35(defvar info-lookup-mode nil
d2c2b883
RS
36 "Symbol of the current buffer's help mode.
37Help is provided according to the buffer's major mode if value is nil.
5a79736d
RS
38Automatically becomes buffer local when set in any fashion.")
39(make-variable-buffer-local 'info-lookup-mode)
40
d2c2b883
RS
41(defcustom info-lookup-other-window-flag t
42 "Non-nil means pop up the Info buffer in another window."
43 :group 'info-lookup :type 'boolean)
5a79736d 44
d2c2b883
RS
45(defcustom info-lookup-highlight-face 'highlight
46 "Face for highlighting looked up help items.
47Setting this variable to nil disables highlighting."
48 :group 'info-lookup :type 'face)
5a79736d
RS
49
50(defvar info-lookup-highlight-overlay nil
51 "Overlay object used for highlighting.")
52
9d37318d
KH
53(defcustom info-lookup-file-name-alist
54 '(("\\`configure\\.in\\'" . autoconf-mode)
55 ("\\`aclocal\\.m4\\'" . autoconf-mode)
56 ("\\`acsite\\.m4\\'" . autoconf-mode)
57 ("\\`acinclude\\.m4\\'" . autoconf-mode))
58 "Alist of file names handled specially.
59List elements are cons cells of the form
60
61 (REGEXP . MODE)
62
63If a file name matches REGEXP, then use help mode MODE instead of the
64buffer's major mode."
65 :group 'info-lookup :type '(repeat (cons (string :tag "Regexp")
66 (symbol :tag "Mode"))))
67
5a79736d
RS
68(defvar info-lookup-history nil
69 "History of previous input lines.")
70
d2c2b883
RS
71(defvar info-lookup-alist nil
72 "Alist of known help topics.
5a79736d
RS
73Cons cells are of the form
74
d2c2b883 75 (HELP-TOPIC . HELP-DATA)
5a79736d
RS
76
77HELP-TOPIC is the symbol of a help topic.
d2c2b883 78HELP-DATA is a HELP-TOPIC's public data set.
5a79736d
RS
79 Value is an alist with elements of the form
80
81 (HELP-MODE REGEXP IGNORE-CASE DOC-SPEC PARSE-RULE OTHER-MODES)
82
83HELP-MODE is a mode's symbol.
84REGEXP is a regular expression matching those help items whose
85 documentation can be looked up via DOC-SPEC.
86IGNORE-CASE is non-nil if help items are case insensitive.
87DOC-SPEC is a list of documentation specifications of the form
88
89 (INFO-NODE TRANS-FUNC PREFIX SUFFIX)
90
91INFO-NODE is the name (including file name part) of an Info index.
92TRANS-FUNC is a function translating index entries into help items;
93 nil means add only those index entries matching REGEXP, a string
94 means prepend string to the first word of all index entries.
95PREFIX and SUFFIX are parts of a regular expression. If one of
96 them is non-nil then search the help item's Info node for the
97 first occurrence of the regular expression `PREFIX ITEM SUFFIX'.
98 ITEM will be highlighted with `info-lookup-highlight-face' if this
99 variable is not nil.
100PARSE-RULE is either the symbol name of a function or a regular
101 expression for guessing the default help item at point. Fuzzy
102 regular expressions like \"[_a-zA-Z0-9]+\" do a better job if
103 there are no clear delimiters; do not try to write too complex
104 expressions. PARSE-RULE defaults to REGEXP.
105OTHER-MODES is a list of cross references to other help modes.")
106
107(defsubst info-lookup->topic-value (topic)
d2c2b883 108 (cdr (assoc topic info-lookup-alist)))
5a79736d
RS
109
110(defsubst info-lookup->mode-value (topic mode)
111 (assoc mode (info-lookup->topic-value topic)))
112
113(defsubst info-lookup->regexp (topic mode)
114 (nth 1 (info-lookup->mode-value topic mode)))
115
116(defsubst info-lookup->ignore-case (topic mode)
117 (nth 2 (info-lookup->mode-value topic mode)))
118
119(defsubst info-lookup->doc-spec (topic mode)
120 (nth 3 (info-lookup->mode-value topic mode)))
121
122(defsubst info-lookup->parse-rule (topic mode)
123 (nth 4 (info-lookup->mode-value topic mode)))
124
125(defsubst info-lookup->other-modes (topic mode)
126 (nth 5 (info-lookup->mode-value topic mode)))
127
d2c2b883
RS
128(eval-and-compile
129 (mapcar (lambda (keyword)
130 (or (boundp keyword)
131 (set keyword keyword)))
132 '(:topic :mode :regexp :ignore-case
133 :doc-spec :parse-rule :other-modes)))
134
135(defun info-lookup-add-help (&rest arg)
136 "Add or update a help specification.
137Function arguments are one or more options of the form
138
139 KEYWORD ARGUMENT
140
141KEYWORD is either `:topic', `:mode', `:regexp', `:ignore-case',
142 `:doc-spec', `:parse-rule', or `:other-modes'.
143ARGUMENT has a value as explained in the documentation of the
144 variable `info-lookup-alist'.
145
146If no topic or mode option has been specified, then the help topic defaults
147to `symbol', and the help mode defaults to the current major mode."
148 (apply 'info-lookup-add-help* nil arg))
149
150(defun info-lookup-maybe-add-help (&rest arg)
151 "Add a help specification iff no one is defined.
152See the documentation of the function `info-lookup-add-help'
153for more details."
154 (apply 'info-lookup-add-help* t arg))
155
156(defun info-lookup-add-help* (maybe &rest arg)
157 (let (topic mode regexp ignore-case doc-spec
158 parse-rule other-modes keyword value)
159 (setq topic 'symbol
160 mode major-mode
161 regexp "\\w+")
162 (while arg
163 (setq keyword (car arg))
164 (or (symbolp keyword)
165 (error "Junk in argument list \"%S\"" arg))
166 (setq arg (cdr arg))
167 (and (null arg)
168 (error "Keyword \"%S\" is missing an argument" keyword))
169 (setq value (car arg)
170 arg (cdr arg))
171 (cond ((eq keyword :topic)
172 (setq topic value))
173 ((eq keyword :mode)
174 (setq mode value))
175 ((eq keyword :regexp)
176 (setq regexp value))
177 ((eq keyword :ignore-case)
178 (setq ignore-case value))
179 ((eq keyword :doc-spec)
180 (setq doc-spec value))
181 ((eq keyword :parse-rule)
182 (setq parse-rule value))
183 ((eq keyword :other-modes)
184 (setq other-modes value))
185 (t
186 (error "Unknown keyword \"%S\"" keyword))))
187 (or (and maybe (info-lookup->mode-value topic mode))
188 (let* ((data (list regexp ignore-case doc-spec parse-rule other-modes))
189 (topic-cell (or (assoc topic info-lookup-alist)
190 (car (setq info-lookup-alist
191 (cons (cons topic nil)
192 info-lookup-alist)))))
193 (mode-cell (assoc mode topic-cell)))
194 (if (null mode-cell)
195 (setcdr topic-cell (cons (cons mode data) (cdr topic-cell)))
196 (setcdr mode-cell data))))
197 nil))
198
5a79736d
RS
199(defvar info-lookup-cache nil
200 "Cache storing data maintained automatically by the program.
201Value is an alist with cons cell of the form
202
203 (HELP-TOPIC . ((HELP-MODE INITIALIZED COMPLETIONS REFER-MODES) ...))
204
205HELP-TOPIC is the symbol of a help topic.
206HELP-MODE is a mode's symbol.
207INITIALIZED is nil if HELP-MODE is uninitialized, t if
208 HELP-MODE is initialized, and `0' means HELP-MODE is
209 initialized but void.
210COMPLETIONS is an alist of documented help items.
211REFER-MODES is a list of other help modes to use.")
212
213(defsubst info-lookup->cache (topic)
214 (or (assoc topic info-lookup-cache)
215 (car (setq info-lookup-cache
216 (cons (cons topic nil)
217 info-lookup-cache)))))
218
0c10ee28 219(defun info-lookup->topic-cache (topic)
5a79736d
RS
220 (cdr (info-lookup->cache topic)))
221
0c10ee28 222(defun info-lookup->mode-cache (topic mode)
5a79736d
RS
223 (assoc mode (info-lookup->topic-cache topic)))
224
0c10ee28 225(defun info-lookup->initialized (topic mode)
5a79736d
RS
226 (nth 1 (info-lookup->mode-cache topic mode)))
227
0c10ee28 228(defun info-lookup->completions (topic mode)
5a79736d
RS
229 (or (info-lookup->initialized topic mode)
230 (info-lookup-setup-mode topic mode))
231 (nth 2 (info-lookup->mode-cache topic mode)))
232
0c10ee28 233(defun info-lookup->refer-modes (topic mode)
5a79736d
RS
234 (or (info-lookup->initialized topic mode)
235 (info-lookup-setup-mode topic mode))
236 (nth 3 (info-lookup->mode-cache topic mode)))
237
0c10ee28 238(defun info-lookup->all-modes (topic mode)
5a79736d
RS
239 (cons mode (info-lookup->refer-modes topic mode)))
240
0c10ee28
RS
241(defun info-lookup-quick-all-modes (topic mode)
242 (cons mode (info-lookup->other-modes topic mode)))
243
5a79736d
RS
244;;;###autoload
245(defun info-lookup-reset ()
246 "Throw away all cached data.
247This command is useful if the user wants to start at the beginning without
248quitting Emacs, for example, after some Info documents were updated on the
249system."
250 (interactive)
251 (setq info-lookup-cache nil))
252
253;;;###autoload
254(defun info-lookup-symbol (symbol &optional mode)
3481a5c0
KH
255 "Display the definition of SYMBOL, as found in the relevant manual.
256When this command is called interactively, it reads SYMBOL from the minibuffer.
257In the minibuffer, use M-n to yank the default argument value
258into the minibuffer so you can edit it.
b108eac2
KH
259The default symbol is the one found at point.
260
261With prefix arg a query for the symbol help mode is offered."
5a79736d 262 (interactive
b108eac2 263 (info-lookup-interactive-arguments 'symbol current-prefix-arg))
5a79736d
RS
264 (info-lookup 'symbol symbol mode))
265
266;;;###autoload
267(defun info-lookup-file (file &optional mode)
268 "Display the documentation of a file.
3481a5c0
KH
269When this command is called interactively, it reads FILE from the minibuffer.
270In the minibuffer, use M-n to yank the default file name
271into the minibuffer so you can edit it.
b108eac2
KH
272The default file name is the one found at point.
273
274With prefix arg a query for the file help mode is offered."
5a79736d 275 (interactive
b108eac2 276 (info-lookup-interactive-arguments 'file current-prefix-arg))
5a79736d
RS
277 (info-lookup 'file file mode))
278
b108eac2
KH
279(defun info-lookup-interactive-arguments (topic &optional query)
280 "Read and return argument value (and help mode) for help topic TOPIC.
281If optional argument QUERY is non-nil, query for the help mode."
282 (let* ((mode (cond (query
283 (info-lookup-change-mode topic))
284 ((info-lookup->mode-value topic (info-lookup-select-mode))
285 info-lookup-mode)
286 ((info-lookup-change-mode topic))))
5a79736d
RS
287 (completions (info-lookup->completions topic mode))
288 (default (info-lookup-guess-default topic mode))
5a79736d
RS
289 (completion-ignore-case (info-lookup->ignore-case topic mode))
290 (enable-recursive-minibuffers t)
291 (value (completing-read
3481a5c0 292 (if default
5a79736d
RS
293 (format "Describe %s (default %s): " topic default)
294 (format "Describe %s: " topic))
3481a5c0 295 completions nil nil nil 'info-lookup-history default)))
5a79736d
RS
296 (list (if (equal value "") default value) mode)))
297
9d37318d
KH
298(defun info-lookup-select-mode ()
299 (when (and (not info-lookup-mode) (buffer-file-name))
300 (let ((file-name (file-name-nondirectory (buffer-file-name)))
301 (file-name-alist info-lookup-file-name-alist))
302 (while (and (not info-lookup-mode) file-name-alist)
303 (when (string-match (caar file-name-alist) file-name)
304 (setq info-lookup-mode (cdar file-name-alist)))
305 (setq file-name-alist (cdr file-name-alist)))))
306 (or info-lookup-mode (setq info-lookup-mode major-mode)))
307
5a79736d
RS
308(defun info-lookup-change-mode (topic)
309 (let* ((completions (mapcar (lambda (arg)
310 (cons (symbol-name (car arg)) (car arg)))
311 (info-lookup->topic-value topic)))
312 (mode (completing-read
313 (format "Use %s help mode: " topic)
314 completions nil t nil 'info-lookup-history)))
315 (or (setq mode (cdr (assoc mode completions)))
316 (error "No %s help available" topic))
317 (or (info-lookup->mode-value topic mode)
318 (error "No %s help available for `%s'" topic mode))
319 (setq info-lookup-mode mode)))
320
321(defun info-lookup (topic item mode)
322 "Display the documentation of a help item."
9d37318d 323 (or mode (setq mode (info-lookup-select-mode)))
5a79736d
RS
324 (or (info-lookup->mode-value topic mode)
325 (error "No %s help available for `%s'" topic mode))
326 (let ((entry (or (assoc (if (info-lookup->ignore-case topic mode)
327 (downcase item) item)
328 (info-lookup->completions topic mode))
329 (error "Not documented as a %s: %s" topic (or item ""))))
330 (modes (info-lookup->all-modes topic mode))
331 (window (selected-window))
1d010333 332 found doc-spec node prefix suffix doc-found)
b108eac2
KH
333 (if (or (not info-lookup-other-window-flag)
334 (eq (current-buffer) (get-buffer "*info*")))
5a79736d
RS
335 (info)
336 (save-window-excursion (info))
337 (switch-to-buffer-other-window "*info*"))
338 (while (and (not found) modes)
339 (setq doc-spec (info-lookup->doc-spec topic (car modes)))
340 (while (and (not found) doc-spec)
341 (setq node (nth 0 (car doc-spec))
342 prefix (nth 2 (car doc-spec))
343 suffix (nth 3 (car doc-spec)))
1d010333 344 (when (condition-case error-data
e35ccb9e 345 (progn
1d010333
RS
346 (Info-goto-node node)
347 (setq doc-found t))
e35ccb9e 348 (error
1d010333
RS
349 (message "Cannot access Info node %s" node)
350 (sit-for 1)
351 nil))
352 (condition-case nil
353 (progn
354 (Info-menu (or (cdr entry) item))
355 (setq found t)
356 (if (or prefix suffix)
357 (let ((case-fold-search
358 (info-lookup->ignore-case topic (car modes)))
359 (buffer-read-only nil))
360 (goto-char (point-min))
361 (re-search-forward
362 (concat prefix (regexp-quote item) suffix))
363 (goto-char (match-beginning 0))
141d2f67 364 (and (display-color-p) info-lookup-highlight-face
1d010333
RS
365 ;; Search again for ITEM so that the first
366 ;; occurence of ITEM will be highlighted.
367 (re-search-forward (regexp-quote item))
368 (let ((start (match-beginning 0))
369 (end (match-end 0)))
370 (if (overlayp info-lookup-highlight-overlay)
371 (move-overlay info-lookup-highlight-overlay
372 start end (current-buffer))
373 (setq info-lookup-highlight-overlay
374 (make-overlay start end))))
375 (overlay-put info-lookup-highlight-overlay
376 'face info-lookup-highlight-face)))))
377 (error nil)))
5a79736d
RS
378 (setq doc-spec (cdr doc-spec)))
379 (setq modes (cdr modes)))
1d010333
RS
380 (or doc-found
381 (error "Info documentation for lookup was not found"))
5a79736d
RS
382 ;; Don't leave the Info buffer if the help item couldn't be looked up.
383 (if (and info-lookup-other-window-flag found)
384 (select-window window))))
385
386(defun info-lookup-setup-mode (topic mode)
387 "Initialize the internal data structure."
388 (or (info-lookup->initialized topic mode)
389 (let (cell data (initialized 0) completions refer-modes)
390 (if (not (info-lookup->mode-value topic mode))
391 (message "No %s help available for `%s'" topic mode)
392 ;; Recursively setup cross references.
393 ;; But refer only to non-void modes.
394 (mapcar (lambda (arg)
395 (or (info-lookup->initialized topic arg)
396 (info-lookup-setup-mode topic arg))
397 (and (eq (info-lookup->initialized topic arg) t)
398 (setq refer-modes (cons arg refer-modes))))
399 (info-lookup->other-modes topic mode))
400 (setq refer-modes (nreverse refer-modes))
401 ;; Build the full completion alist.
402 (setq completions
e12fcc41
KH
403 (nconc (condition-case nil
404 (info-lookup-make-completions topic mode)
405 (error nil))
5a79736d
RS
406 (apply 'append
407 (mapcar (lambda (arg)
408 (info-lookup->completions topic arg))
409 refer-modes))))
410 (setq initialized t))
411 ;; Update `info-lookup-cache'.
412 (setq cell (info-lookup->mode-cache topic mode)
413 data (list initialized completions refer-modes))
414 (if (not cell)
415 (setcdr (info-lookup->cache topic)
416 (cons (cons mode data) (info-lookup->topic-cache topic)))
417 (setcdr cell data))
418 initialized)))
419
420(defun info-lookup-make-completions (topic mode)
421 "Create a unique alist from all index entries."
1eff0ba1
RS
422 (let ((doc-spec (info-lookup->doc-spec topic mode))
423 (regexp (concat "^\\(" (info-lookup->regexp topic mode)
424 "\\)\\([ \t].*\\)?$"))
1d010333 425 node trans entry item prefix result doc-found
1eff0ba1
RS
426 (buffer (get-buffer-create " temp-info-look")))
427 (with-current-buffer buffer
428 (Info-mode))
429 (while doc-spec
430 (setq node (nth 0 (car doc-spec))
431 trans (cond ((eq (nth 1 (car doc-spec)) nil)
432 (lambda (arg)
433 (if (string-match regexp arg)
434 (match-string 1 arg))))
435 ((stringp (nth 1 (car doc-spec)))
436 (setq prefix (nth 1 (car doc-spec)))
437 (lambda (arg)
438 (if (string-match "^\\([^: \t\n]+\\)" arg)
439 (concat prefix (match-string 1 arg)))))
440 (t (nth 1 (car doc-spec)))))
1d010333
RS
441 (with-current-buffer buffer
442 (message "Processing Info node `%s'..." node)
443 (when (condition-case error-data
e35ccb9e 444 (progn
1d010333
RS
445 (Info-goto-node node)
446 (setq doc-found t))
e35ccb9e 447 (error
1d010333
RS
448 (message "Cannot access Info node `%s'" node)
449 (sit-for 1)
450 nil))
451 (condition-case nil
452 (progn
453 (goto-char (point-min))
454 (and (search-forward "\n* Menu:" nil t)
455 (while (re-search-forward "\n\\* \\([^:\t\n]*\\):" nil t)
456 (setq entry (match-string 1)
457 item (funcall trans entry))
6480a693
DL
458 ;; `trans' can return nil if the regexp doesn't match.
459 (when (and item
460 ;; Sometimes there's more than one Menu:
e35ccb9e 461 (not (string= entry "Menu")))
6480a693
DL
462 (and (info-lookup->ignore-case topic mode)
463 (setq item (downcase item)))
464 (and (string-equal entry item)
465 (setq entry nil))
466 (and (or (assoc item result)
467 (setq result (cons (cons item entry)
468 result))))))))
1d010333 469 (error nil))))
1eff0ba1
RS
470 (message "Processing Info node `%s'...done" node)
471 (setq doc-spec (cdr doc-spec)))
1d010333
RS
472 (or doc-found
473 (error "Info documentation for lookup was not found"))
1eff0ba1 474 result))
5a79736d
RS
475
476(defun info-lookup-guess-default (topic mode)
ce288cb6
KH
477 "Return a guess for a symbol to look up, based on text around point.
478Try all related modes applicable to TOPIC and MODE.
479Return nil if there is nothing appropriate in the buffer near point."
5a79736d 480 (let ((modes (info-lookup->all-modes topic mode))
ce288cb6 481 guess)
5a79736d
RS
482 (while (and (not guess) modes)
483 (setq guess (info-lookup-guess-default* topic (car modes))
ce288cb6 484 modes (cdr modes)))
5a79736d 485 ;; Collapse whitespace characters.
ce288cb6
KH
486 (when guess
487 (let ((pos 0))
488 (while (string-match "[ \t\n]+" guess pos)
489 (setq pos (1+ (match-beginning 0)))
490 (setq guess (replace-match " " t t guess)))))
491 guess))
5a79736d
RS
492
493(defun info-lookup-guess-default* (topic mode)
494 (let ((case-fold-search (info-lookup->ignore-case topic mode))
495 (rule (or (info-lookup->parse-rule topic mode)
496 (info-lookup->regexp topic mode)))
497 (start (point)) end regexp subexp result)
ce288cb6
KH
498 (save-excursion
499 (if (symbolp rule)
500 (setq result (funcall rule))
501 (if (consp rule)
502 (setq regexp (car rule)
503 subexp (cdr rule))
504 (setq regexp rule
505 subexp 0))
e35ccb9e
DL
506 ;; If at start of symbol, don't go back to end of previous one.
507 (if (save-match-data
508 (looking-at "[ \t\n]"))
509 (skip-chars-backward " \t\n"))
510 (setq end (point))
ce288cb6
KH
511 (while (and (re-search-backward regexp nil t)
512 (looking-at regexp)
513 (>= (match-end 0) end))
514 (setq result (match-string subexp)))
515 (if (not result)
516 (progn
517 (goto-char start)
518 (skip-chars-forward " \t\n")
519 (and (looking-at regexp)
520 (setq result (match-string subexp)))))))
5a79736d
RS
521 result))
522
523(defun info-lookup-guess-c-symbol ()
524 "Get the C symbol at point."
525 (condition-case nil
526 (progn
ce288cb6 527 (skip-syntax-backward "w_")
5a79736d
RS
528 (let ((start (point)) prefix name)
529 ;; Test for a leading `struct', `union', or `enum' keyword
530 ;; but ignore names like `foo_struct'.
531 (setq prefix (and (< (skip-chars-backward " \t\n") 0)
532 (< (skip-chars-backward "_a-zA-Z0-9") 0)
533 (looking-at "\\(struct\\|union\\|enum\\)\\s ")
534 (concat (match-string 1) " ")))
535 (goto-char start)
536 (and (looking-at "[_a-zA-Z][_a-zA-Z0-9]*")
537 (setq name (match-string 0)))
538 ;; Caveat! Look forward if point is at `struct' etc.
539 (and (not prefix)
540 (or (string-equal name "struct")
541 (string-equal name "union")
542 (string-equal name "enum"))
543 (looking-at "[a-z]+\\s +\\([_a-zA-Z][_a-zA-Z0-9]*\\)")
544 (setq prefix (concat name " ")
545 name (match-string 1)))
546 (and (or prefix name)
547 (concat prefix name))))
548 (error nil)))
549
550;;;###autoload
551(defun info-complete-symbol (&optional mode)
552 "Perform completion on symbol preceding point."
82fb111c
RS
553 (interactive)
554 (info-complete 'symbol
555 (or mode
556 (if (info-lookup->mode-value
9d37318d
KH
557 'symbol (info-lookup-select-mode))
558 info-lookup-mode
82fb111c 559 (info-lookup-change-mode 'symbol)))))
5a79736d
RS
560
561;;;###autoload
562(defun info-complete-file (&optional mode)
563 "Perform completion on file preceding point."
a1fee1bc
KH
564 (interactive)
565 (info-complete 'file
566 (or mode
567 (if (info-lookup->mode-value
9d37318d
KH
568 'file (info-lookup-select-mode))
569 info-lookup-mode
a1fee1bc 570 (info-lookup-change-mode 'file)))))
5a79736d
RS
571
572(defun info-complete (topic mode)
573 "Try to complete a help item."
574 (barf-if-buffer-read-only)
9d37318d 575 (or mode (setq mode (info-lookup-select-mode)))
5a79736d
RS
576 (or (info-lookup->mode-value topic mode)
577 (error "No %s completion available for `%s'" topic mode))
0c10ee28
RS
578 (let ((modes (info-lookup-quick-all-modes topic mode))
579 (start (point))
580 try)
5a79736d
RS
581 (while (and (not try) modes)
582 (setq mode (car modes)
583 modes (cdr modes)
584 try (info-lookup-guess-default* topic mode))
585 (goto-char start))
586 (and (not try)
d2c2b883 587 (error "Found no %S to complete" topic))
0c10ee28
RS
588 (let ((completions (info-lookup->completions topic mode))
589 (completion-ignore-case (info-lookup->ignore-case topic mode))
590 completion)
591 (setq completion (try-completion try completions))
592 (cond ((not completion)
593 (ding)
594 (message "No match"))
595 ((stringp completion)
596 (or (assoc completion completions)
597 (setq completion (completing-read
598 (format "Complete %S: " topic)
599 completions nil t completion
600 info-lookup-history)))
e35ccb9e
DL
601 ;; Find the original symbol and zap it.
602 (end-of-line)
603 (while (and (search-backward try nil t)
604 (< start (point))))
605 (replace-match "")
0c10ee28
RS
606 (insert completion))
607 (t
608 (message "%s is complete"
609 (capitalize (prin1-to-string topic))))))))
d2c2b883
RS
610
611\f
d2c2b883
RS
612;;; Initialize some common modes.
613
614(info-lookup-maybe-add-help
615 :mode 'c-mode :topic 'symbol
616 :regexp "\\(struct \\|union \\|enum \\)?[_a-zA-Z][_a-zA-Z0-9]*"
617 :doc-spec '(("(libc)Function Index" nil
618 "^[ \t]+- \\(Function\\|Macro\\): .*\\<" "\\>")
619 ("(libc)Variable Index" nil
620 "^[ \t]+- \\(Variable\\|Macro\\): .*\\<" "\\>")
621 ("(libc)Type Index" nil
622 "^[ \t]+- Data Type: \\<" "\\>")
623 ("(termcap)Var Index" nil
624 "^[ \t]*`" "'"))
625 :parse-rule 'info-lookup-guess-c-symbol)
626
627(info-lookup-maybe-add-help
628 :mode 'c-mode :topic 'file
629 :regexp "[_a-zA-Z0-9./+-]+"
630 :doc-spec '(("(libc)File Index")))
631
632(info-lookup-maybe-add-help
633 :mode 'bison-mode
634 :regexp "[:;|]\\|%\\([%{}]\\|[_a-z]+\\)\\|YY[_A-Z]+\\|yy[_a-z]+"
635 :doc-spec '(("(bison)Index" nil
636 "`" "'"))
637 :parse-rule "[:;|]\\|%\\([%{}]\\|[_a-zA-Z][_a-zA-Z0-9]*\\)"
638 :other-modes '(c-mode))
639
640(info-lookup-maybe-add-help
641 :mode 'makefile-mode
642 :regexp "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z][_a-zA-Z0-9-]*"
643 :doc-spec '(("(make)Name Index" nil
644 "^[ \t]*`" "'"))
645 :parse-rule "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z0-9-]+")
646
647(info-lookup-maybe-add-help
648 :mode 'texinfo-mode
649 :regexp "@\\([a-zA-Z]+\\|[^a-zA-Z]\\)"
650 :doc-spec '(("(texinfo)Command and Variable Index"
651 ;; Ignore Emacs commands and prepend a `@'.
652 (lambda (item)
653 (if (string-match "^\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\( .*\\)?$" item)
654 (concat "@" (match-string 1 item))))
655 "`" "'")))
656
657(info-lookup-maybe-add-help
658 :mode 'm4-mode
659 :regexp "[_a-zA-Z][_a-zA-Z0-9]*"
660 :doc-spec '(("(m4)Macro index"))
661 :parse-rule "[_a-zA-Z0-9]+")
662
663(info-lookup-maybe-add-help
664 :mode 'autoconf-mode
665 :regexp "A[CM]_[_A-Z0-9]+"
666 :doc-spec '(("(autoconf)Macro Index" "AC_"
667 "^[ \t]+- \\(Macro\\|Variable\\): .*\\<" "\\>")
668 ("(automake)Index" nil
669 "^[ \t]*`" "'"))
670 ;; Autoconf symbols are M4 macros. Thus use M4's parser.
671 :parse-rule 'ignore
672 :other-modes '(m4-mode))
673
674(info-lookup-maybe-add-help
675 :mode 'awk-mode
676 :regexp "[_a-zA-Z]+"
677 :doc-spec '(("(gawk)Index"
678 (lambda (item)
679 (let ((case-fold-search nil))
680 (cond
681 ;; `BEGIN' and `END'.
682 ((string-match "^\\([A-Z]+\\) special pattern\\b" item)
683 (match-string 1 item))
684 ;; `if', `while', `do', ...
685 ((string-match "^\\([a-z]+\\) statement\\b" item)
686 (if (not (string-equal (match-string 1 item) "control"))
687 (match-string 1 item)))
688 ;; `NR', `NF', ...
689 ((string-match "^[A-Z]+$" item)
690 item)
691 ;; Built-in functions (matches to many entries).
692 ((string-match "^[a-z]+$" item)
693 item))))
694 "`" "\\([ \t]*([^)]*)\\)?'")))
695
696(info-lookup-maybe-add-help
697 :mode 'perl-mode
698 :regexp "[$@%][^a-zA-Z]\\|\\$\\^[A-Z]\\|[$@%]?[a-zA-Z][_a-zA-Z0-9]*"
699 :doc-spec '(("(perl5)Function Index"
700 (lambda (item)
701 (if (string-match "^\\([a-zA-Z0-9]+\\)" item)
702 (match-string 1 item)))
703 "^" "\\b")
704 ("(perl5)Variable Index"
705 (lambda (item)
706 ;; Work around bad formatted array variables.
707 (let ((sym (cond ((or (string-match "^\\$\\(.\\|@@\\)$" item)
708 (string-match "^\\$\\^[A-Z]$" item))
709 item)
710 ((string-match
711 "^\\([$%@]\\|@@\\)?[_a-zA-Z0-9]+" item)
712 (match-string 0 item))
713 (t ""))))
714 (if (string-match "@@" sym)
715 (setq sym (concat (substring sym 0 (match-beginning 0))
716 (substring sym (1- (match-end 0))))))
717 (if (string-equal sym "") nil sym)))
718 "^" "\\b"))
719 :parse-rule "[$@%]?\\([_a-zA-Z0-9]+\\|[^a-zA-Z]\\)")
720
721(info-lookup-maybe-add-help
722 :mode 'latex-mode
723 :regexp "\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)"
e12fcc41 724 :doc-spec '(("(latex)Command Index" nil
d2c2b883
RS
725 "`" "\\({[^}]*}\\)?'")))
726
727(info-lookup-maybe-add-help
728 :mode 'emacs-lisp-mode
729 :regexp "[^()' \t\n]+"
730 :doc-spec '(("(emacs)Command Index")
a1fee1bc
KH
731 ("(emacs)Variable Index")
732 ("(elisp)Index"
733 (lambda (item)
734 (let ((sym (intern-soft item)))
735 (cond ((null sym)
736 (if (string-equal item "nil") item))
737 ((or (boundp sym) (fboundp sym))
738 item))))
739 "^[ \t]+- [^:]+:[ \t]*" "\\b")))
d2c2b883
RS
740
741(info-lookup-maybe-add-help
742 :mode 'lisp-interaction-mode
743 :regexp "[^()' \t\n]+"
744 :parse-rule 'ignore
745 :other-modes '(emacs-lisp-mode))
746
0c10ee28
RS
747(info-lookup-maybe-add-help
748 :mode 'lisp-mode
749 :regexp "[^()' \t\n]+"
750 :parse-rule 'ignore
751 :other-modes '(emacs-lisp-mode))
752
9d37318d
KH
753(info-lookup-maybe-add-help
754 :mode 'scheme-mode
755 :regexp "[^()' \t\n]+"
756 :ignore-case t
6480a693 757 ;; Aubrey Jaffer's rendition from <URL:ftp://ftp-swiss.ai.mit.edu/pub/scm>
9d37318d
KH
758 :doc-spec '(("(r5rs)Index" nil
759 "^[ \t]+- [^:]+:[ \t]*" "\\b")))
760
054e5c20
SE
761(info-lookup-maybe-add-help
762 :mode 'octave-mode
763 :regexp "[_a-zA-Z0-9]+"
57f3e9c9
SE
764 :doc-spec '(("(octave)Function Index" nil
765 "^ - [^:]+:[ ]+\\(\\[[^=]*=[ ]+\\)?" nil)
054e5c20
SE
766 ("(octave)Variable Index" nil "^ - [^:]+:[ ]+" nil)
767 ;; Catch lines of the form "xyz statement"
e35ccb9e 768 ("(octave)Concept Index"
054e5c20
SE
769 (lambda (item)
770 (cond
771 ((string-match "^\\([A-Z]+\\) statement\\b" item)
772 (match-string 1 item))
773 (t nil)))
774 nil; "^ - [^:]+:[ ]+" don't think this prefix is useful here.
775 nil)))
d2c2b883 776\f
5a79736d
RS
777(provide 'info-look)
778
779;;; info-look.el ends here