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