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