Add Keywords.
[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.
b108eac2
KH
267The default symbol is the one found at point.
268
269With prefix arg a query for the symbol help mode is offered."
5a79736d 270 (interactive
b108eac2 271 (info-lookup-interactive-arguments 'symbol current-prefix-arg))
5a79736d
RS
272 (info-lookup 'symbol symbol mode))
273
274;;;###autoload
275(defun info-lookup-file (file &optional mode)
276 "Display the documentation of a file.
3481a5c0
KH
277When this command is called interactively, it reads FILE from the minibuffer.
278In the minibuffer, use M-n to yank the default file name
279into the minibuffer so you can edit it.
b108eac2
KH
280The default file name is the one found at point.
281
282With prefix arg a query for the file help mode is offered."
5a79736d 283 (interactive
b108eac2 284 (info-lookup-interactive-arguments 'file current-prefix-arg))
5a79736d
RS
285 (info-lookup 'file file mode))
286
b108eac2
KH
287(defun info-lookup-interactive-arguments (topic &optional query)
288 "Read and return argument value (and help mode) for help topic TOPIC.
289If optional argument QUERY is non-nil, query for the help mode."
290 (let* ((mode (cond (query
291 (info-lookup-change-mode topic))
292 ((info-lookup->mode-value topic (info-lookup-select-mode))
293 info-lookup-mode)
294 ((info-lookup-change-mode topic))))
5a79736d
RS
295 (completions (info-lookup->completions topic mode))
296 (default (info-lookup-guess-default topic mode))
5a79736d
RS
297 (completion-ignore-case (info-lookup->ignore-case topic mode))
298 (enable-recursive-minibuffers t)
299 (value (completing-read
3481a5c0 300 (if default
5a79736d
RS
301 (format "Describe %s (default %s): " topic default)
302 (format "Describe %s: " topic))
3481a5c0 303 completions nil nil nil 'info-lookup-history default)))
5a79736d
RS
304 (list (if (equal value "") default value) mode)))
305
9d37318d
KH
306(defun info-lookup-select-mode ()
307 (when (and (not info-lookup-mode) (buffer-file-name))
308 (let ((file-name (file-name-nondirectory (buffer-file-name)))
309 (file-name-alist info-lookup-file-name-alist))
310 (while (and (not info-lookup-mode) file-name-alist)
311 (when (string-match (caar file-name-alist) file-name)
312 (setq info-lookup-mode (cdar file-name-alist)))
313 (setq file-name-alist (cdr file-name-alist)))))
314 (or info-lookup-mode (setq info-lookup-mode major-mode)))
315
5a79736d
RS
316(defun info-lookup-change-mode (topic)
317 (let* ((completions (mapcar (lambda (arg)
318 (cons (symbol-name (car arg)) (car arg)))
319 (info-lookup->topic-value topic)))
320 (mode (completing-read
321 (format "Use %s help mode: " topic)
322 completions nil t nil 'info-lookup-history)))
323 (or (setq mode (cdr (assoc mode completions)))
324 (error "No %s help available" topic))
325 (or (info-lookup->mode-value topic mode)
326 (error "No %s help available for `%s'" topic mode))
327 (setq info-lookup-mode mode)))
328
329(defun info-lookup (topic item mode)
330 "Display the documentation of a help item."
9d37318d 331 (or mode (setq mode (info-lookup-select-mode)))
5a79736d
RS
332 (or (info-lookup->mode-value topic mode)
333 (error "No %s help available for `%s'" topic mode))
334 (let ((entry (or (assoc (if (info-lookup->ignore-case topic mode)
335 (downcase item) item)
336 (info-lookup->completions topic mode))
337 (error "Not documented as a %s: %s" topic (or item ""))))
338 (modes (info-lookup->all-modes topic mode))
339 (window (selected-window))
1d010333 340 found doc-spec node prefix suffix doc-found)
b108eac2
KH
341 (if (or (not info-lookup-other-window-flag)
342 (eq (current-buffer) (get-buffer "*info*")))
5a79736d
RS
343 (info)
344 (save-window-excursion (info))
345 (switch-to-buffer-other-window "*info*"))
346 (while (and (not found) modes)
347 (setq doc-spec (info-lookup->doc-spec topic (car modes)))
348 (while (and (not found) doc-spec)
349 (setq node (nth 0 (car doc-spec))
350 prefix (nth 2 (car doc-spec))
351 suffix (nth 3 (car doc-spec)))
1d010333
RS
352 (when (condition-case error-data
353 (progn
354 (Info-goto-node node)
355 (setq doc-found t))
356 (error
357 (message "Cannot access Info node %s" node)
358 (sit-for 1)
359 nil))
360 (condition-case nil
361 (progn
362 (Info-menu (or (cdr entry) item))
363 (setq found t)
364 (if (or prefix suffix)
365 (let ((case-fold-search
366 (info-lookup->ignore-case topic (car modes)))
367 (buffer-read-only nil))
368 (goto-char (point-min))
369 (re-search-forward
370 (concat prefix (regexp-quote item) suffix))
371 (goto-char (match-beginning 0))
372 (and window-system info-lookup-highlight-face
373 ;; Search again for ITEM so that the first
374 ;; occurence of ITEM will be highlighted.
375 (re-search-forward (regexp-quote item))
376 (let ((start (match-beginning 0))
377 (end (match-end 0)))
378 (if (overlayp info-lookup-highlight-overlay)
379 (move-overlay info-lookup-highlight-overlay
380 start end (current-buffer))
381 (setq info-lookup-highlight-overlay
382 (make-overlay start end))))
383 (overlay-put info-lookup-highlight-overlay
384 'face info-lookup-highlight-face)))))
385 (error nil)))
5a79736d
RS
386 (setq doc-spec (cdr doc-spec)))
387 (setq modes (cdr modes)))
1d010333
RS
388 (or doc-found
389 (error "Info documentation for lookup was not found"))
5a79736d
RS
390 ;; Don't leave the Info buffer if the help item couldn't be looked up.
391 (if (and info-lookup-other-window-flag found)
392 (select-window window))))
393
394(defun info-lookup-setup-mode (topic mode)
395 "Initialize the internal data structure."
396 (or (info-lookup->initialized topic mode)
397 (let (cell data (initialized 0) completions refer-modes)
398 (if (not (info-lookup->mode-value topic mode))
399 (message "No %s help available for `%s'" topic mode)
400 ;; Recursively setup cross references.
401 ;; But refer only to non-void modes.
402 (mapcar (lambda (arg)
403 (or (info-lookup->initialized topic arg)
404 (info-lookup-setup-mode topic arg))
405 (and (eq (info-lookup->initialized topic arg) t)
406 (setq refer-modes (cons arg refer-modes))))
407 (info-lookup->other-modes topic mode))
408 (setq refer-modes (nreverse refer-modes))
409 ;; Build the full completion alist.
410 (setq completions
e12fcc41
KH
411 (nconc (condition-case nil
412 (info-lookup-make-completions topic mode)
413 (error nil))
5a79736d
RS
414 (apply 'append
415 (mapcar (lambda (arg)
416 (info-lookup->completions topic arg))
417 refer-modes))))
418 (setq initialized t))
419 ;; Update `info-lookup-cache'.
420 (setq cell (info-lookup->mode-cache topic mode)
421 data (list initialized completions refer-modes))
422 (if (not cell)
423 (setcdr (info-lookup->cache topic)
424 (cons (cons mode data) (info-lookup->topic-cache topic)))
425 (setcdr cell data))
426 initialized)))
427
428(defun info-lookup-make-completions (topic mode)
429 "Create a unique alist from all index entries."
1eff0ba1
RS
430 (let ((doc-spec (info-lookup->doc-spec topic mode))
431 (regexp (concat "^\\(" (info-lookup->regexp topic mode)
432 "\\)\\([ \t].*\\)?$"))
1d010333 433 node trans entry item prefix result doc-found
1eff0ba1
RS
434 (buffer (get-buffer-create " temp-info-look")))
435 (with-current-buffer buffer
436 (Info-mode))
437 (while doc-spec
438 (setq node (nth 0 (car doc-spec))
439 trans (cond ((eq (nth 1 (car doc-spec)) nil)
440 (lambda (arg)
441 (if (string-match regexp arg)
442 (match-string 1 arg))))
443 ((stringp (nth 1 (car doc-spec)))
444 (setq prefix (nth 1 (car doc-spec)))
445 (lambda (arg)
446 (if (string-match "^\\([^: \t\n]+\\)" arg)
447 (concat prefix (match-string 1 arg)))))
448 (t (nth 1 (car doc-spec)))))
1d010333
RS
449 (with-current-buffer buffer
450 (message "Processing Info node `%s'..." node)
451 (when (condition-case error-data
452 (progn
453 (Info-goto-node node)
454 (setq doc-found t))
455 (error
456 (message "Cannot access Info node `%s'" node)
457 (sit-for 1)
458 nil))
459 (condition-case nil
460 (progn
461 (goto-char (point-min))
462 (and (search-forward "\n* Menu:" nil t)
463 (while (re-search-forward "\n\\* \\([^:\t\n]*\\):" nil t)
464 (setq entry (match-string 1)
465 item (funcall trans entry))
6480a693
DL
466 ;; `trans' can return nil if the regexp doesn't match.
467 (when (and item
468 ;; Sometimes there's more than one Menu:
469 (not (string= entry "Menu")))
470 (and (info-lookup->ignore-case topic mode)
471 (setq item (downcase item)))
472 (and (string-equal entry item)
473 (setq entry nil))
474 (and (or (assoc item result)
475 (setq result (cons (cons item entry)
476 result))))))))
1d010333 477 (error nil))))
1eff0ba1
RS
478 (message "Processing Info node `%s'...done" node)
479 (setq doc-spec (cdr doc-spec)))
1d010333
RS
480 (or doc-found
481 (error "Info documentation for lookup was not found"))
1eff0ba1 482 result))
5a79736d
RS
483
484(defun info-lookup-guess-default (topic mode)
ce288cb6
KH
485 "Return a guess for a symbol to look up, based on text around point.
486Try all related modes applicable to TOPIC and MODE.
487Return nil if there is nothing appropriate in the buffer near point."
5a79736d 488 (let ((modes (info-lookup->all-modes topic mode))
ce288cb6 489 guess)
5a79736d
RS
490 (while (and (not guess) modes)
491 (setq guess (info-lookup-guess-default* topic (car modes))
ce288cb6 492 modes (cdr modes)))
5a79736d 493 ;; Collapse whitespace characters.
ce288cb6
KH
494 (when guess
495 (let ((pos 0))
496 (while (string-match "[ \t\n]+" guess pos)
497 (setq pos (1+ (match-beginning 0)))
498 (setq guess (replace-match " " t t guess)))))
499 guess))
5a79736d
RS
500
501(defun info-lookup-guess-default* (topic mode)
502 (let ((case-fold-search (info-lookup->ignore-case topic mode))
503 (rule (or (info-lookup->parse-rule topic mode)
504 (info-lookup->regexp topic mode)))
505 (start (point)) end regexp subexp result)
ce288cb6
KH
506 (save-excursion
507 (if (symbolp rule)
508 (setq result (funcall rule))
509 (if (consp rule)
510 (setq regexp (car rule)
511 subexp (cdr rule))
512 (setq regexp rule
513 subexp 0))
514 (skip-chars-backward " \t\n") (setq end (point))
515 (while (and (re-search-backward regexp nil t)
516 (looking-at regexp)
517 (>= (match-end 0) end))
518 (setq result (match-string subexp)))
519 (if (not result)
520 (progn
521 (goto-char start)
522 (skip-chars-forward " \t\n")
523 (and (looking-at regexp)
524 (setq result (match-string subexp)))))))
5a79736d
RS
525 result))
526
527(defun info-lookup-guess-c-symbol ()
528 "Get the C symbol at point."
529 (condition-case nil
530 (progn
ce288cb6 531 (skip-syntax-backward "w_")
5a79736d
RS
532 (let ((start (point)) prefix name)
533 ;; Test for a leading `struct', `union', or `enum' keyword
534 ;; but ignore names like `foo_struct'.
535 (setq prefix (and (< (skip-chars-backward " \t\n") 0)
536 (< (skip-chars-backward "_a-zA-Z0-9") 0)
537 (looking-at "\\(struct\\|union\\|enum\\)\\s ")
538 (concat (match-string 1) " ")))
539 (goto-char start)
540 (and (looking-at "[_a-zA-Z][_a-zA-Z0-9]*")
541 (setq name (match-string 0)))
542 ;; Caveat! Look forward if point is at `struct' etc.
543 (and (not prefix)
544 (or (string-equal name "struct")
545 (string-equal name "union")
546 (string-equal name "enum"))
547 (looking-at "[a-z]+\\s +\\([_a-zA-Z][_a-zA-Z0-9]*\\)")
548 (setq prefix (concat name " ")
549 name (match-string 1)))
550 (and (or prefix name)
551 (concat prefix name))))
552 (error nil)))
553
554;;;###autoload
555(defun info-complete-symbol (&optional mode)
556 "Perform completion on symbol preceding point."
82fb111c
RS
557 (interactive)
558 (info-complete 'symbol
559 (or mode
560 (if (info-lookup->mode-value
9d37318d
KH
561 'symbol (info-lookup-select-mode))
562 info-lookup-mode
82fb111c 563 (info-lookup-change-mode 'symbol)))))
5a79736d
RS
564
565;;;###autoload
566(defun info-complete-file (&optional mode)
567 "Perform completion on file preceding point."
a1fee1bc
KH
568 (interactive)
569 (info-complete 'file
570 (or mode
571 (if (info-lookup->mode-value
9d37318d
KH
572 'file (info-lookup-select-mode))
573 info-lookup-mode
a1fee1bc 574 (info-lookup-change-mode 'file)))))
5a79736d
RS
575
576(defun info-complete (topic mode)
577 "Try to complete a help item."
578 (barf-if-buffer-read-only)
9d37318d 579 (or mode (setq mode (info-lookup-select-mode)))
5a79736d
RS
580 (or (info-lookup->mode-value topic mode)
581 (error "No %s completion available for `%s'" topic mode))
0c10ee28
RS
582 (let ((modes (info-lookup-quick-all-modes topic mode))
583 (start (point))
584 try)
5a79736d
RS
585 (while (and (not try) modes)
586 (setq mode (car modes)
587 modes (cdr modes)
588 try (info-lookup-guess-default* topic mode))
589 (goto-char start))
590 (and (not try)
d2c2b883 591 (error "Found no %S to complete" topic))
0c10ee28
RS
592 (let ((completions (info-lookup->completions topic mode))
593 (completion-ignore-case (info-lookup->ignore-case topic mode))
594 completion)
595 (setq completion (try-completion try completions))
596 (cond ((not completion)
597 (ding)
598 (message "No match"))
599 ((stringp completion)
600 (or (assoc completion completions)
601 (setq completion (completing-read
602 (format "Complete %S: " topic)
603 completions nil t completion
604 info-lookup-history)))
605 (delete-region (- start (length try)) start)
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]+"
764 :doc-spec '(("(octave)Function Index" nil "^ - [^:]+:[ ]+" nil)
765 ("(octave)Variable Index" nil "^ - [^:]+:[ ]+" nil)
766 ;; Catch lines of the form "xyz statement"
767 ("(octave)Concept Index"
768 (lambda (item)
769 (cond
770 ((string-match "^\\([A-Z]+\\) statement\\b" item)
771 (match-string 1 item))
772 (t nil)))
773 nil; "^ - [^:]+:[ ]+" don't think this prefix is useful here.
774 nil)))
d2c2b883 775\f
5a79736d
RS
776(provide 'info-look)
777
778;;; info-look.el ends here