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