* ffap.el (ffap-rfc-path): Make this a defcustom since
[bpt/emacs.git] / lisp / info-look.el
CommitLineData
e8af40ee 1;;; info-look.el --- major-mode-sensitive Info index lookup facility
5a79736d
RS
2;; An older version of this was known as libc.el.
3
0d30b337 4;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2001, 2002, 2003,
ae940284 5;; 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5a79736d 6
3b361901 7;; Author: Ralph Schleicher <rs@nunatak.allgaeu.org>
fdba4ab4 8;; (did not show signs of life (Nov 2001) -stef)
5a79736d
RS
9;; Keywords: help languages
10
11;; This file is part of GNU Emacs.
12
eb3fa2cf 13;; GNU Emacs is free software: you can redistribute it and/or modify
5a79736d 14;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
5a79736d
RS
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
eb3fa2cf 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
5a79736d 25
41b1cae7
SM
26;;; Commentary:
27
28;; Really cool code to lookup info indexes.
b3a3e4e1 29;; Try especially info-lookup-symbol (aka C-h S).
41b1cae7 30
5a79736d
RS
31;;; Code:
32
33(require 'info)
d2c2b883
RS
34
35(defgroup info-lookup nil
36 "Major mode sensitive help agent."
37 :group 'help :group 'languages)
5a79736d
RS
38
39(defvar info-lookup-mode nil
d2c2b883
RS
40 "Symbol of the current buffer's help mode.
41Help is provided according to the buffer's major mode if value is nil.
5a79736d
RS
42Automatically becomes buffer local when set in any fashion.")
43(make-variable-buffer-local 'info-lookup-mode)
44
d2c2b883
RS
45(defcustom info-lookup-other-window-flag t
46 "Non-nil means pop up the Info buffer in another window."
47 :group 'info-lookup :type 'boolean)
5a79736d 48
8270a848 49(defcustom info-lookup-highlight-face 'match
d2c2b883
RS
50 "Face for highlighting looked up help items.
51Setting this variable to nil disables highlighting."
52 :group 'info-lookup :type 'face)
5a79736d
RS
53
54(defvar info-lookup-highlight-overlay nil
55 "Overlay object used for highlighting.")
56
9d37318d 57(defcustom info-lookup-file-name-alist
28a2ca5d 58 '(("\\`ac\\(local\\|site\\|include\\)\\.m4\\'" . autoconf-mode))
9d37318d
KH
59 "Alist of file names handled specially.
60List elements are cons cells of the form
61
62 (REGEXP . MODE)
63
64If a file name matches REGEXP, then use help mode MODE instead of the
65buffer's major mode."
66 :group 'info-lookup :type '(repeat (cons (string :tag "Regexp")
67 (symbol :tag "Mode"))))
68
5a79736d
RS
69(defvar info-lookup-history nil
70 "History of previous input lines.")
71
d2c2b883
RS
72(defvar info-lookup-alist nil
73 "Alist of known help topics.
5a79736d
RS
74Cons cells are of the form
75
d2c2b883 76 (HELP-TOPIC . HELP-DATA)
5a79736d
RS
77
78HELP-TOPIC is the symbol of a help topic.
d2c2b883 79HELP-DATA is a HELP-TOPIC's public data set.
5a79736d
RS
80 Value is an alist with elements of the form
81
82 (HELP-MODE REGEXP IGNORE-CASE DOC-SPEC PARSE-RULE OTHER-MODES)
83
84HELP-MODE is a mode's symbol.
85REGEXP is a regular expression matching those help items whose
86 documentation can be looked up via DOC-SPEC.
87IGNORE-CASE is non-nil if help items are case insensitive.
88DOC-SPEC is a list of documentation specifications of the form
89
90 (INFO-NODE TRANS-FUNC PREFIX SUFFIX)
91
92INFO-NODE is the name (including file name part) of an Info index.
93TRANS-FUNC is a function translating index entries into help items;
94 nil means add only those index entries matching REGEXP, a string
95 means prepend string to the first word of all index entries.
96PREFIX and SUFFIX are parts of a regular expression. If one of
97 them is non-nil then search the help item's Info node for the
98 first occurrence of the regular expression `PREFIX ITEM SUFFIX'.
99 ITEM will be highlighted with `info-lookup-highlight-face' if this
100 variable is not nil.
101PARSE-RULE is either the symbol name of a function or a regular
102 expression for guessing the default help item at point. Fuzzy
103 regular expressions like \"[_a-zA-Z0-9]+\" do a better job if
104 there are no clear delimiters; do not try to write too complex
105 expressions. PARSE-RULE defaults to REGEXP.
106OTHER-MODES is a list of cross references to other help modes.")
107
108(defsubst info-lookup->topic-value (topic)
d2c2b883 109 (cdr (assoc topic info-lookup-alist)))
5a79736d
RS
110
111(defsubst info-lookup->mode-value (topic mode)
112 (assoc mode (info-lookup->topic-value topic)))
113
114(defsubst info-lookup->regexp (topic mode)
115 (nth 1 (info-lookup->mode-value topic mode)))
116
117(defsubst info-lookup->ignore-case (topic mode)
118 (nth 2 (info-lookup->mode-value topic mode)))
119
120(defsubst info-lookup->doc-spec (topic mode)
121 (nth 3 (info-lookup->mode-value topic mode)))
122
123(defsubst info-lookup->parse-rule (topic mode)
124 (nth 4 (info-lookup->mode-value topic mode)))
125
126(defsubst info-lookup->other-modes (topic mode)
127 (nth 5 (info-lookup->mode-value topic mode)))
128
d2c2b883
RS
129(defun info-lookup-add-help (&rest arg)
130 "Add or update a help specification.
131Function arguments are one or more options of the form
132
133 KEYWORD ARGUMENT
134
135KEYWORD is either `:topic', `:mode', `:regexp', `:ignore-case',
136 `:doc-spec', `:parse-rule', or `:other-modes'.
137ARGUMENT has a value as explained in the documentation of the
138 variable `info-lookup-alist'.
139
140If no topic or mode option has been specified, then the help topic defaults
141to `symbol', and the help mode defaults to the current major mode."
142 (apply 'info-lookup-add-help* nil arg))
143
144(defun info-lookup-maybe-add-help (&rest arg)
4837b516 145 "Add a help specification if none is defined.
d2c2b883
RS
146See the documentation of the function `info-lookup-add-help'
147for more details."
148 (apply 'info-lookup-add-help* t arg))
149
150(defun info-lookup-add-help* (maybe &rest arg)
151 (let (topic mode regexp ignore-case doc-spec
152 parse-rule other-modes keyword value)
153 (setq topic 'symbol
154 mode major-mode
155 regexp "\\w+")
156 (while arg
157 (setq keyword (car arg))
158 (or (symbolp keyword)
159 (error "Junk in argument list \"%S\"" arg))
160 (setq arg (cdr arg))
161 (and (null arg)
162 (error "Keyword \"%S\" is missing an argument" keyword))
163 (setq value (car arg)
164 arg (cdr arg))
165 (cond ((eq keyword :topic)
166 (setq topic value))
167 ((eq keyword :mode)
168 (setq mode value))
169 ((eq keyword :regexp)
170 (setq regexp value))
171 ((eq keyword :ignore-case)
172 (setq ignore-case value))
173 ((eq keyword :doc-spec)
174 (setq doc-spec value))
175 ((eq keyword :parse-rule)
176 (setq parse-rule value))
177 ((eq keyword :other-modes)
178 (setq other-modes value))
179 (t
180 (error "Unknown keyword \"%S\"" keyword))))
181 (or (and maybe (info-lookup->mode-value topic mode))
182 (let* ((data (list regexp ignore-case doc-spec parse-rule other-modes))
183 (topic-cell (or (assoc topic info-lookup-alist)
184 (car (setq info-lookup-alist
185 (cons (cons topic nil)
186 info-lookup-alist)))))
187 (mode-cell (assoc mode topic-cell)))
188 (if (null mode-cell)
189 (setcdr topic-cell (cons (cons mode data) (cdr topic-cell)))
190 (setcdr mode-cell data))))
191 nil))
192
5a79736d
RS
193(defvar info-lookup-cache nil
194 "Cache storing data maintained automatically by the program.
195Value is an alist with cons cell of the form
196
197 (HELP-TOPIC . ((HELP-MODE INITIALIZED COMPLETIONS REFER-MODES) ...))
198
199HELP-TOPIC is the symbol of a help topic.
200HELP-MODE is a mode's symbol.
201INITIALIZED is nil if HELP-MODE is uninitialized, t if
202 HELP-MODE is initialized, and `0' means HELP-MODE is
203 initialized but void.
204COMPLETIONS is an alist of documented help items.
205REFER-MODES is a list of other help modes to use.")
206
207(defsubst info-lookup->cache (topic)
208 (or (assoc topic info-lookup-cache)
209 (car (setq info-lookup-cache
210 (cons (cons topic nil)
211 info-lookup-cache)))))
212
0c10ee28 213(defun info-lookup->topic-cache (topic)
5a79736d
RS
214 (cdr (info-lookup->cache topic)))
215
0c10ee28 216(defun info-lookup->mode-cache (topic mode)
5a79736d
RS
217 (assoc mode (info-lookup->topic-cache topic)))
218
0c10ee28 219(defun info-lookup->initialized (topic mode)
5a79736d
RS
220 (nth 1 (info-lookup->mode-cache topic mode)))
221
0c10ee28 222(defun info-lookup->completions (topic mode)
5a79736d
RS
223 (or (info-lookup->initialized topic mode)
224 (info-lookup-setup-mode topic mode))
225 (nth 2 (info-lookup->mode-cache topic mode)))
226
0c10ee28 227(defun info-lookup->refer-modes (topic mode)
5a79736d
RS
228 (or (info-lookup->initialized topic mode)
229 (info-lookup-setup-mode topic mode))
230 (nth 3 (info-lookup->mode-cache topic mode)))
231
0c10ee28 232(defun info-lookup->all-modes (topic mode)
5a79736d
RS
233 (cons mode (info-lookup->refer-modes topic mode)))
234
0c10ee28
RS
235(defun info-lookup-quick-all-modes (topic mode)
236 (cons mode (info-lookup->other-modes topic mode)))
237
5a79736d
RS
238;;;###autoload
239(defun info-lookup-reset ()
240 "Throw away all cached data.
241This command is useful if the user wants to start at the beginning without
242quitting Emacs, for example, after some Info documents were updated on the
243system."
244 (interactive)
245 (setq info-lookup-cache nil))
246
99400b3e 247;;;###autoload (put 'info-lookup-symbol 'info-file "emacs")
5a79736d
RS
248;;;###autoload
249(defun info-lookup-symbol (symbol &optional mode)
3481a5c0 250 "Display the definition of SYMBOL, as found in the relevant manual.
ec4ef174
EZ
251When this command is called interactively, it reads SYMBOL from the
252minibuffer. In the minibuffer, use M-n to yank the default argument
253value into the minibuffer so you can edit it. The default symbol is the
254one found at point.
b108eac2
KH
255
256With prefix arg a query for the symbol help mode is offered."
5a79736d 257 (interactive
b108eac2 258 (info-lookup-interactive-arguments 'symbol current-prefix-arg))
5a79736d
RS
259 (info-lookup 'symbol symbol mode))
260
99400b3e 261;;;###autoload (put 'info-lookup-file 'info-file "emacs")
5a79736d
RS
262;;;###autoload
263(defun info-lookup-file (file &optional mode)
264 "Display the documentation of a file.
3481a5c0
KH
265When this command is called interactively, it reads FILE from the minibuffer.
266In the minibuffer, use M-n to yank the default file name
267into the minibuffer so you can edit it.
b108eac2
KH
268The default file name is the one found at point.
269
270With prefix arg a query for the file help mode is offered."
5a79736d 271 (interactive
b108eac2 272 (info-lookup-interactive-arguments 'file current-prefix-arg))
5a79736d
RS
273 (info-lookup 'file file mode))
274
b108eac2
KH
275(defun info-lookup-interactive-arguments (topic &optional query)
276 "Read and return argument value (and help mode) for help topic TOPIC.
277If 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))))
5a79736d
RS
283 (completions (info-lookup->completions topic mode))
284 (default (info-lookup-guess-default topic mode))
5a79736d
RS
285 (completion-ignore-case (info-lookup->ignore-case topic mode))
286 (enable-recursive-minibuffers t)
287 (value (completing-read
3481a5c0 288 (if default
5a79736d
RS
289 (format "Describe %s (default %s): " topic default)
290 (format "Describe %s: " topic))
3481a5c0 291 completions nil nil nil 'info-lookup-history default)))
5a79736d
RS
292 (list (if (equal value "") default value) mode)))
293
9d37318d
KH
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
5a79736d
RS
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."
9d37318d 319 (or mode (setq mode (info-lookup-select-mode)))
5a79736d
RS
320 (or (info-lookup->mode-value topic mode)
321 (error "No %s help available for `%s'" topic mode))
7c3da01b
RS
322 (let* ((completions (info-lookup->completions topic mode))
323 (ignore-case (info-lookup->ignore-case topic mode))
324 (entry (or (assoc (if ignore-case (downcase item) item) completions)
0445150c 325 (assoc-string item completions t)
7c3da01b
RS
326 (error "Not documented as a %s: %s" topic (or item ""))))
327 (modes (info-lookup->all-modes topic mode))
328 (window (selected-window))
27b1b62b
MR
329 (new-Info-history
330 ;; Avoid clobbering Info-history with nodes searched during
331 ;; lookup. If lookup succeeds set `Info-history' to
332 ;; `new-Info-history'.
333 (when (get-buffer "*info*")
334 (with-current-buffer "*info*"
335 (cons (list Info-current-file Info-current-node (point))
336 Info-history))))
7c3da01b 337 found doc-spec node prefix suffix doc-found)
2faede35
MR
338 (unless (eq major-mode 'Info-mode)
339 (if (not info-lookup-other-window-flag)
340 (info)
341 (save-window-excursion (info))
342 (let* ((info-window (get-buffer-window "*info*" t))
343 (info-frame (and info-window (window-frame info-window))))
344 (if (and info-frame
345 (not (eq info-frame (selected-frame)))
346 (display-multi-frame-p)
347 (memq info-frame (frames-on-display-list)))
348 ;; *info* is visible in another frame on same display.
349 ;; Raise that frame and select the window.
350 (progn
351 (select-window info-window)
352 (raise-frame info-frame))
353 ;; In any other case, switch to *info* in another window.
354 (switch-to-buffer-other-window "*info*")))))
5a79736d
RS
355 (while (and (not found) modes)
356 (setq doc-spec (info-lookup->doc-spec topic (car modes)))
357 (while (and (not found) doc-spec)
358 (setq node (nth 0 (car doc-spec))
359 prefix (nth 2 (car doc-spec))
360 suffix (nth 3 (car doc-spec)))
1d010333 361 (when (condition-case error-data
e35ccb9e 362 (progn
e8d2103a
EZ
363 ;; Don't need Index menu fontifications here, and
364 ;; they slow down the lookup.
27b1b62b
MR
365 (let (Info-fontify-maximum-menu-size
366 Info-history-list)
e8d2103a
EZ
367 (Info-goto-node node)
368 (setq doc-found t)))
e35ccb9e 369 (error
1d010333
RS
370 (message "Cannot access Info node %s" node)
371 (sit-for 1)
372 nil))
373 (condition-case nil
374 (progn
7c3da01b
RS
375 ;; Don't use Info-menu, it forces case-fold-search to t
376 (let ((case-fold-search nil))
377 (re-search-forward
378 (concat "^\\* " (regexp-quote (or (cdr entry) (car entry)))
379 ":")))
380 (Info-follow-nearest-node)
1d010333
RS
381 (setq found t)
382 (if (or prefix suffix)
383 (let ((case-fold-search
384 (info-lookup->ignore-case topic (car modes)))
385 (buffer-read-only nil))
386 (goto-char (point-min))
387 (re-search-forward
7c3da01b 388 (concat prefix (regexp-quote (car entry)) suffix))
1d010333 389 (goto-char (match-beginning 0))
141d2f67 390 (and (display-color-p) info-lookup-highlight-face
1d010333 391 ;; Search again for ITEM so that the first
a5a08b1f 392 ;; occurrence of ITEM will be highlighted.
7c3da01b 393 (re-search-forward (regexp-quote (car entry)))
1d010333
RS
394 (let ((start (match-beginning 0))
395 (end (match-end 0)))
396 (if (overlayp info-lookup-highlight-overlay)
397 (move-overlay info-lookup-highlight-overlay
398 start end (current-buffer))
399 (setq info-lookup-highlight-overlay
400 (make-overlay start end))))
401 (overlay-put info-lookup-highlight-overlay
402 'face info-lookup-highlight-face)))))
403 (error nil)))
5a79736d
RS
404 (setq doc-spec (cdr doc-spec)))
405 (setq modes (cdr modes)))
7c3da01b
RS
406 ;; Alert the user if case was munged, and do this after bringing up the
407 ;; info buffer since that can print messages
408 (unless (or ignore-case
409 (string-equal item (car entry)))
021c54a3 410 (message "Found in different case: %s" (car entry)))
27b1b62b
MR
411 (when found
412 (setq Info-history new-Info-history))
1d010333
RS
413 (or doc-found
414 (error "Info documentation for lookup was not found"))
5a79736d
RS
415 ;; Don't leave the Info buffer if the help item couldn't be looked up.
416 (if (and info-lookup-other-window-flag found)
417 (select-window window))))
418
419(defun info-lookup-setup-mode (topic mode)
420 "Initialize the internal data structure."
421 (or (info-lookup->initialized topic mode)
27b1b62b
MR
422 (let ((initialized 0)
423 cell data completions refer-modes Info-history-list)
5a79736d
RS
424 (if (not (info-lookup->mode-value topic mode))
425 (message "No %s help available for `%s'" topic mode)
426 ;; Recursively setup cross references.
427 ;; But refer only to non-void modes.
538996c7
SM
428 (dolist (arg (info-lookup->other-modes topic mode))
429 (or (info-lookup->initialized topic arg)
430 (info-lookup-setup-mode topic arg))
431 (and (eq (info-lookup->initialized topic arg) t)
432 (setq refer-modes (cons arg refer-modes))))
5a79736d
RS
433 (setq refer-modes (nreverse refer-modes))
434 ;; Build the full completion alist.
435 (setq completions
e12fcc41
KH
436 (nconc (condition-case nil
437 (info-lookup-make-completions topic mode)
438 (error nil))
5a79736d
RS
439 (apply 'append
440 (mapcar (lambda (arg)
441 (info-lookup->completions topic arg))
442 refer-modes))))
443 (setq initialized t))
444 ;; Update `info-lookup-cache'.
445 (setq cell (info-lookup->mode-cache topic mode)
446 data (list initialized completions refer-modes))
447 (if (not cell)
448 (setcdr (info-lookup->cache topic)
449 (cons (cons mode data) (info-lookup->topic-cache topic)))
450 (setcdr cell data))
451 initialized)))
452
453(defun info-lookup-make-completions (topic mode)
454 "Create a unique alist from all index entries."
1eff0ba1
RS
455 (let ((doc-spec (info-lookup->doc-spec topic mode))
456 (regexp (concat "^\\(" (info-lookup->regexp topic mode)
457 "\\)\\([ \t].*\\)?$"))
27b1b62b 458 Info-history-list Info-fontify-maximum-menu-size
1d010333 459 node trans entry item prefix result doc-found
1eff0ba1
RS
460 (buffer (get-buffer-create " temp-info-look")))
461 (with-current-buffer buffer
462 (Info-mode))
463 (while doc-spec
464 (setq node (nth 0 (car doc-spec))
465 trans (cond ((eq (nth 1 (car doc-spec)) nil)
466 (lambda (arg)
467 (if (string-match regexp arg)
468 (match-string 1 arg))))
469 ((stringp (nth 1 (car doc-spec)))
470 (setq prefix (nth 1 (car doc-spec)))
471 (lambda (arg)
472 (if (string-match "^\\([^: \t\n]+\\)" arg)
473 (concat prefix (match-string 1 arg)))))
474 (t (nth 1 (car doc-spec)))))
1d010333
RS
475 (with-current-buffer buffer
476 (message "Processing Info node `%s'..." node)
477 (when (condition-case error-data
e35ccb9e 478 (progn
1d010333
RS
479 (Info-goto-node node)
480 (setq doc-found t))
e35ccb9e 481 (error
1d010333
RS
482 (message "Cannot access Info node `%s'" node)
483 (sit-for 1)
484 nil))
485 (condition-case nil
486 (progn
487 (goto-char (point-min))
488 (and (search-forward "\n* Menu:" nil t)
bdb0f2d5 489 (while (re-search-forward "\n\\* \\(.*\\): " nil t)
1d010333
RS
490 (setq entry (match-string 1)
491 item (funcall trans entry))
6480a693
DL
492 ;; `trans' can return nil if the regexp doesn't match.
493 (when (and item
494 ;; Sometimes there's more than one Menu:
e35ccb9e 495 (not (string= entry "Menu")))
6480a693
DL
496 (and (info-lookup->ignore-case topic mode)
497 (setq item (downcase item)))
498 (and (string-equal entry item)
499 (setq entry nil))
500 (and (or (assoc item result)
501 (setq result (cons (cons item entry)
502 result))))))))
1d010333 503 (error nil))))
1eff0ba1
RS
504 (message "Processing Info node `%s'...done" node)
505 (setq doc-spec (cdr doc-spec)))
1d010333
RS
506 (or doc-found
507 (error "Info documentation for lookup was not found"))
1eff0ba1 508 result))
5a79736d
RS
509
510(defun info-lookup-guess-default (topic mode)
ce288cb6
KH
511 "Return a guess for a symbol to look up, based on text around point.
512Try all related modes applicable to TOPIC and MODE.
513Return nil if there is nothing appropriate in the buffer near point."
5a79736d 514 (let ((modes (info-lookup->all-modes topic mode))
ce288cb6 515 guess)
5a79736d
RS
516 (while (and (not guess) modes)
517 (setq guess (info-lookup-guess-default* topic (car modes))
ce288cb6 518 modes (cdr modes)))
5a79736d 519 ;; Collapse whitespace characters.
ce288cb6
KH
520 (when guess
521 (let ((pos 0))
522 (while (string-match "[ \t\n]+" guess pos)
523 (setq pos (1+ (match-beginning 0)))
524 (setq guess (replace-match " " t t guess)))))
525 guess))
5a79736d
RS
526
527(defun info-lookup-guess-default* (topic mode)
528 (let ((case-fold-search (info-lookup->ignore-case topic mode))
529 (rule (or (info-lookup->parse-rule topic mode)
530 (info-lookup->regexp topic mode)))
531 (start (point)) end regexp subexp result)
ce288cb6
KH
532 (save-excursion
533 (if (symbolp rule)
534 (setq result (funcall rule))
535 (if (consp rule)
536 (setq regexp (car rule)
537 subexp (cdr rule))
538 (setq regexp rule
539 subexp 0))
e35ccb9e
DL
540 ;; If at start of symbol, don't go back to end of previous one.
541 (if (save-match-data
542 (looking-at "[ \t\n]"))
543 (skip-chars-backward " \t\n"))
544 (setq end (point))
ce288cb6
KH
545 (while (and (re-search-backward regexp nil t)
546 (looking-at regexp)
547 (>= (match-end 0) end))
548 (setq result (match-string subexp)))
549 (if (not result)
550 (progn
551 (goto-char start)
552 (skip-chars-forward " \t\n")
553 (and (looking-at regexp)
554 (setq result (match-string subexp)))))))
5a79736d
RS
555 result))
556
557(defun info-lookup-guess-c-symbol ()
558 "Get the C symbol at point."
559 (condition-case nil
560 (progn
ce288cb6 561 (skip-syntax-backward "w_")
5a79736d
RS
562 (let ((start (point)) prefix name)
563 ;; Test for a leading `struct', `union', or `enum' keyword
564 ;; but ignore names like `foo_struct'.
565 (setq prefix (and (< (skip-chars-backward " \t\n") 0)
566 (< (skip-chars-backward "_a-zA-Z0-9") 0)
567 (looking-at "\\(struct\\|union\\|enum\\)\\s ")
568 (concat (match-string 1) " ")))
569 (goto-char start)
570 (and (looking-at "[_a-zA-Z][_a-zA-Z0-9]*")
571 (setq name (match-string 0)))
572 ;; Caveat! Look forward if point is at `struct' etc.
573 (and (not prefix)
574 (or (string-equal name "struct")
575 (string-equal name "union")
576 (string-equal name "enum"))
577 (looking-at "[a-z]+\\s +\\([_a-zA-Z][_a-zA-Z0-9]*\\)")
578 (setq prefix (concat name " ")
579 name (match-string 1)))
580 (and (or prefix name)
581 (concat prefix name))))
582 (error nil)))
583
ec4ef174
EZ
584(defun info-lookup-guess-custom-symbol ()
585 "Get symbol at point in custom buffers."
586 (condition-case nil
587 (save-excursion
588 (let ((case-fold-search t)
589 (ignored-chars "][()`',:.\" \t\n")
590 (significant-chars "^][()`',:.\" \t\n")
591 beg end)
592 (cond
593 ((and (memq (get-char-property (point) 'face)
594 '(custom-variable-tag custom-variable-tag-face))
595 (setq beg (previous-single-char-property-change
596 (point) 'face nil (line-beginning-position)))
597 (setq end (next-single-char-property-change
598 (point) 'face nil (line-end-position)))
599 (> end beg))
600 (subst-char-in-string
4789a3cb 601 ?\s ?\- (buffer-substring-no-properties beg end)))
ec4ef174
EZ
602 ((or (and (looking-at (concat "[" significant-chars "]"))
603 (save-excursion
604 (skip-chars-backward significant-chars)
605 (setq beg (point)))
606 (skip-chars-forward significant-chars)
607 (setq end (point))
608 (> end beg))
609 (and (looking-at "[ \t\n]")
610 (looking-back (concat "[" significant-chars "]"))
611 (setq end (point))
612 (skip-chars-backward significant-chars)
613 (setq beg (point))
614 (> end beg))
615 (and (skip-chars-forward ignored-chars)
616 (setq beg (point))
617 (skip-chars-forward significant-chars)
618 (setq end (point))
619 (> end beg)))
620 (buffer-substring-no-properties beg end)))))
621 (error nil)))
622
5a79736d
RS
623;;;###autoload
624(defun info-complete-symbol (&optional mode)
625 "Perform completion on symbol preceding point."
82fb111c
RS
626 (interactive)
627 (info-complete 'symbol
628 (or mode
629 (if (info-lookup->mode-value
9d37318d
KH
630 'symbol (info-lookup-select-mode))
631 info-lookup-mode
82fb111c 632 (info-lookup-change-mode 'symbol)))))
5a79736d
RS
633
634;;;###autoload
635(defun info-complete-file (&optional mode)
636 "Perform completion on file preceding point."
a1fee1bc
KH
637 (interactive)
638 (info-complete 'file
639 (or mode
640 (if (info-lookup->mode-value
9d37318d
KH
641 'file (info-lookup-select-mode))
642 info-lookup-mode
a1fee1bc 643 (info-lookup-change-mode 'file)))))
5a79736d
RS
644
645(defun info-complete (topic mode)
646 "Try to complete a help item."
647 (barf-if-buffer-read-only)
9d37318d 648 (or mode (setq mode (info-lookup-select-mode)))
5a79736d
RS
649 (or (info-lookup->mode-value topic mode)
650 (error "No %s completion available for `%s'" topic mode))
0c10ee28
RS
651 (let ((modes (info-lookup-quick-all-modes topic mode))
652 (start (point))
653 try)
5a79736d
RS
654 (while (and (not try) modes)
655 (setq mode (car modes)
656 modes (cdr modes)
657 try (info-lookup-guess-default* topic mode))
658 (goto-char start))
659 (and (not try)
d2c2b883 660 (error "Found no %S to complete" topic))
0c10ee28
RS
661 (let ((completions (info-lookup->completions topic mode))
662 (completion-ignore-case (info-lookup->ignore-case topic mode))
663 completion)
664 (setq completion (try-completion try completions))
665 (cond ((not completion)
666 (ding)
667 (message "No match"))
668 ((stringp completion)
669 (or (assoc completion completions)
670 (setq completion (completing-read
671 (format "Complete %S: " topic)
672 completions nil t completion
673 info-lookup-history)))
e35ccb9e
DL
674 ;; Find the original symbol and zap it.
675 (end-of-line)
676 (while (and (search-backward try nil t)
677 (< start (point))))
678 (replace-match "")
0c10ee28
RS
679 (insert completion))
680 (t
681 (message "%s is complete"
682 (capitalize (prin1-to-string topic))))))))
d2c2b883
RS
683
684\f
d2c2b883
RS
685;;; Initialize some common modes.
686
687(info-lookup-maybe-add-help
688 :mode 'c-mode :topic 'symbol
689 :regexp "\\(struct \\|union \\|enum \\)?[_a-zA-Z][_a-zA-Z0-9]*"
690 :doc-spec '(("(libc)Function Index" nil
947faf4f 691 "^[ \t]+-+ \\(Function\\|Macro\\): .*\\<" "\\>")
3b90e932
EZ
692 ;; prefix/suffix has to match things like
693 ;; " -- Macro: int F_DUPFD"
694 ;; " -- Variable: char * tzname [2]"
695 ;; "`DBL_MAX'" (texinfo @table)
696 ;; suffix "\\>" is not used because that sends DBL_MAX to
697 ;; DBL_MAX_EXP ("_" is a non-word char)
d2c2b883 698 ("(libc)Variable Index" nil
3b90e932
EZ
699 "^\\([ \t]+-+ \\(Variable\\|Macro\\): .*\\<\\|`\\)"
700 "\\( \\|'?$\\)")
d2c2b883 701 ("(libc)Type Index" nil
947faf4f 702 "^[ \t]+-+ Data Type: \\<" "\\>")
d2c2b883
RS
703 ("(termcap)Var Index" nil
704 "^[ \t]*`" "'"))
705 :parse-rule 'info-lookup-guess-c-symbol)
706
707(info-lookup-maybe-add-help
708 :mode 'c-mode :topic 'file
709 :regexp "[_a-zA-Z0-9./+-]+"
710 :doc-spec '(("(libc)File Index")))
711
712(info-lookup-maybe-add-help
713 :mode 'bison-mode
714 :regexp "[:;|]\\|%\\([%{}]\\|[_a-z]+\\)\\|YY[_A-Z]+\\|yy[_a-z]+"
715 :doc-spec '(("(bison)Index" nil
716 "`" "'"))
717 :parse-rule "[:;|]\\|%\\([%{}]\\|[_a-zA-Z][_a-zA-Z0-9]*\\)"
718 :other-modes '(c-mode))
719
720(info-lookup-maybe-add-help
721 :mode 'makefile-mode
722 :regexp "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z][_a-zA-Z0-9-]*"
723 :doc-spec '(("(make)Name Index" nil
fdba4ab4
SM
724 "^[ \t]*`" "'")
725 ("(automake)Macro and Variable Index" nil
d2c2b883 726 "^[ \t]*`" "'"))
fdba4ab4
SM
727 :parse-rule "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z0-9-]+"
728 :other-modes '(automake-mode))
d2c2b883
RS
729
730(info-lookup-maybe-add-help
731 :mode 'texinfo-mode
732 :regexp "@\\([a-zA-Z]+\\|[^a-zA-Z]\\)"
733 :doc-spec '(("(texinfo)Command and Variable Index"
734 ;; Ignore Emacs commands and prepend a `@'.
735 (lambda (item)
736 (if (string-match "^\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\( .*\\)?$" item)
737 (concat "@" (match-string 1 item))))
947faf4f 738 "`" "[' ]")))
d2c2b883
RS
739
740(info-lookup-maybe-add-help
741 :mode 'm4-mode
742 :regexp "[_a-zA-Z][_a-zA-Z0-9]*"
743 :doc-spec '(("(m4)Macro index"))
744 :parse-rule "[_a-zA-Z0-9]+")
745
746(info-lookup-maybe-add-help
747 :mode 'autoconf-mode
748 :regexp "A[CM]_[_A-Z0-9]+"
01e1a819
JB
749 :doc-spec '(;; Autoconf Macro Index entries are without an "AC_" prefix,
750 ;; but with "AH_" or "AU_" for those. So add "AC_" if there
751 ;; isn't already an "A._".
752 ("(autoconf)Autoconf Macro Index"
753 (lambda (item)
754 (if (string-match "^A._" item) item (concat "AC_" item)))
947faf4f 755 "^[ \t]+-+ \\(Macro\\|Variable\\): .*\\<" "\\>")
01e1a819
JB
756 ;; M4 Macro Index entries are without "AS_" prefixes, and
757 ;; mostly without "m4_" prefixes. "dnl" is an exception, not
758 ;; wanting any prefix. So AS_ is added back to upper-case
00fade45 759 ;; names (if needed), m4_ to others which don't already an m4_.
01e1a819
JB
760 ("(autoconf)M4 Macro Index"
761 (lambda (item)
762 (let ((case-fold-search nil))
763 (cond ((or (string-equal item "dnl")
00fade45
TTN
764 (string-match "^m4_" item)
765 ;; Autoconf 2.62 index includes some macros
766 ;; (e.g., AS_HELP_STRING), so avoid prefixing.
767 (string-match "^AS_" item))
01e1a819
JB
768 item)
769 ((string-match "^[A-Z0-9_]+$" item)
770 (concat "AS_" item))
771 (t
772 (concat "m4_" item)))))
947faf4f 773 "^[ \t]+-+ Macro: .*\\<" "\\>")
01e1a819
JB
774 ;; Autotest Macro Index entries are without "AT_".
775 ("(autoconf)Autotest Macro Index" "AT_"
947faf4f 776 "^[ \t]+-+ Macro: .*\\<" "\\>")
01e1a819 777 ;; This is for older versions (probably pre autoconf 2.5x):
28a2ca5d 778 ("(autoconf)Macro Index" "AC_"
947faf4f 779 "^[ \t]+-+ \\(Macro\\|Variable\\): .*\\<" "\\>")
01e1a819
JB
780 ;; Automake has index entries for its notes on various autoconf
781 ;; macros (eg. AC_PROG_CC). Ensure this is after the autoconf
782 ;; index, so as to prefer the autoconf docs.
fdba4ab4 783 ("(automake)Macro and Variable Index" nil
d2c2b883
RS
784 "^[ \t]*`" "'"))
785 ;; Autoconf symbols are M4 macros. Thus use M4's parser.
786 :parse-rule 'ignore
787 :other-modes '(m4-mode))
788
789(info-lookup-maybe-add-help
790 :mode 'awk-mode
791 :regexp "[_a-zA-Z]+"
792 :doc-spec '(("(gawk)Index"
793 (lambda (item)
794 (let ((case-fold-search nil))
795 (cond
796 ;; `BEGIN' and `END'.
797 ((string-match "^\\([A-Z]+\\) special pattern\\b" item)
798 (match-string 1 item))
799 ;; `if', `while', `do', ...
800 ((string-match "^\\([a-z]+\\) statement\\b" item)
801 (if (not (string-equal (match-string 1 item) "control"))
802 (match-string 1 item)))
803 ;; `NR', `NF', ...
804 ((string-match "^[A-Z]+$" item)
805 item)
806 ;; Built-in functions (matches to many entries).
807 ((string-match "^[a-z]+$" item)
808 item))))
809 "`" "\\([ \t]*([^)]*)\\)?'")))
810
811(info-lookup-maybe-add-help
812 :mode 'perl-mode
813 :regexp "[$@%][^a-zA-Z]\\|\\$\\^[A-Z]\\|[$@%]?[a-zA-Z][_a-zA-Z0-9]*"
814 :doc-spec '(("(perl5)Function Index"
815 (lambda (item)
816 (if (string-match "^\\([a-zA-Z0-9]+\\)" item)
817 (match-string 1 item)))
818 "^" "\\b")
819 ("(perl5)Variable Index"
820 (lambda (item)
821 ;; Work around bad formatted array variables.
822 (let ((sym (cond ((or (string-match "^\\$\\(.\\|@@\\)$" item)
823 (string-match "^\\$\\^[A-Z]$" item))
824 item)
825 ((string-match
826 "^\\([$%@]\\|@@\\)?[_a-zA-Z0-9]+" item)
827 (match-string 0 item))
828 (t ""))))
829 (if (string-match "@@" sym)
830 (setq sym (concat (substring sym 0 (match-beginning 0))
831 (substring sym (1- (match-end 0))))))
832 (if (string-equal sym "") nil sym)))
833 "^" "\\b"))
834 :parse-rule "[$@%]?\\([_a-zA-Z0-9]+\\|[^a-zA-Z]\\)")
835
41b1cae7
SM
836(info-lookup-maybe-add-help
837 :mode 'cperl-mode
838 :regexp "[$@%][^a-zA-Z]\\|\\$\\^[A-Z]\\|[$@%]?[a-zA-Z][_a-zA-Z0-9]*"
839 :other-modes '(perl-mode))
840
d2c2b883
RS
841(info-lookup-maybe-add-help
842 :mode 'latex-mode
843 :regexp "\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)"
e12fcc41 844 :doc-spec '(("(latex)Command Index" nil
d2c2b883
RS
845 "`" "\\({[^}]*}\\)?'")))
846
847(info-lookup-maybe-add-help
848 :mode 'emacs-lisp-mode
ec4ef174 849 :regexp "[^][()`',\" \t\n]+"
16524b9b
JB
850 :doc-spec '(;; Commands with key sequences appear in nodes as `foo' and
851 ;; those without as `M-x foo'.
852 ("(emacs)Command Index" nil "`\\(M-x[ \t\n]+\\)?" "'")
853 ;; Variables normally appear in nodes as just `foo'.
854 ("(emacs)Variable Index" nil "`" "'")
855 ;; Almost all functions, variables, etc appear in nodes as
947faf4f 856 ;; " -- Function: foo" etc. A small number of aliases and
16524b9b
JB
857 ;; symbols appear only as `foo', and will miss out on exact
858 ;; positions. Allowing `foo' would hit too many false matches
859 ;; for things that should go to Function: etc, and those latter
860 ;; are much more important. Perhaps this could change if some
861 ;; sort of fallback match scheme existed.
947faf4f 862 ("(elisp)Index" nil "^ -+ .*: " "\\( \\|$\\)")))
d2c2b883
RS
863
864(info-lookup-maybe-add-help
865 :mode 'lisp-interaction-mode
ec4ef174 866 :regexp "[^][()`',\" \t\n]+"
d2c2b883
RS
867 :parse-rule 'ignore
868 :other-modes '(emacs-lisp-mode))
869
0c10ee28
RS
870(info-lookup-maybe-add-help
871 :mode 'lisp-mode
ec4ef174 872 :regexp "[^()`',\" \t\n]+"
0c10ee28
RS
873 :parse-rule 'ignore
874 :other-modes '(emacs-lisp-mode))
875
9d37318d
KH
876(info-lookup-maybe-add-help
877 :mode 'scheme-mode
08d87b2d 878 :regexp "[^()`',\" \t\n]+"
9d37318d 879 :ignore-case t
6480a693 880 ;; Aubrey Jaffer's rendition from <URL:ftp://ftp-swiss.ai.mit.edu/pub/scm>
9d37318d 881 :doc-spec '(("(r5rs)Index" nil
947faf4f 882 "^[ \t]+-+ [^:]+:[ \t]*" "\\b")))
9d37318d 883
054e5c20
SE
884(info-lookup-maybe-add-help
885 :mode 'octave-mode
5d8137ab 886 :regexp "[_a-zA-Z0-9]+\\|\\s.+\\|[-!=^|*/.\\,><~&+]\\{1,3\\}\\|[][();,\"']"
c6bb2688 887 :doc-spec '(("(octave)Function Index" nil
947faf4f
JL
888 "^ -+ [^:]+:[ ]+\\(\\[[^=]*=[ ]+\\)?" nil)
889 ("(octave)Variable Index" nil "^ -+ [^:]+:[ ]+" nil)
5d8137ab 890 ("(octave)Operator Index" nil nil nil)
054e5c20 891 ;; Catch lines of the form "xyz statement"
e35ccb9e 892 ("(octave)Concept Index"
054e5c20
SE
893 (lambda (item)
894 (cond
895 ((string-match "^\\([A-Z]+\\) statement\\b" item)
896 (match-string 1 item))
897 (t nil)))
947faf4f 898 nil; "^ -+ [^:]+:[ ]+" don't think this prefix is useful here.
054e5c20 899 nil)))
c0e7fbb8 900
b3a3e4e1
SE
901(info-lookup-maybe-add-help
902 :mode 'maxima-mode
903 :ignore-case t
c99240cc 904 :regexp "[a-zA-Z0-9_%]+"
947faf4f
JL
905 :doc-spec '( ("(maxima)Function and Variable Index" nil
906 "^ -+ [^:]+:[ ]+\\(\\[[^=]*=[ ]+\\)?" nil)))
b3a3e4e1
SE
907
908(info-lookup-maybe-add-help
909 :mode 'inferior-maxima-mode
c99240cc 910 :regexp "[a-zA-Z0-9_%]+"
b3a3e4e1
SE
911 :other-modes '(maxima-mode))
912
c0e7fbb8 913;; coreutils and bash builtins overlap in places, eg. printf, so there's a
c6bb2688 914;; question which should come first. Some of the coreutils descriptions are
c0e7fbb8
JB
915;; more detailed, but if bash is usually /bin/sh on a GNU system then the
916;; builtins will be what's normally run.
917;;
918;; Maybe special variables like $? should be matched as $?, not just ?.
919;; This would avoid a clash between variable $! and negation !, or variable
920;; $# and comment # (though comment # is not currently indexed in bash).
921;; Unfortunately if $? etc is the symbol, then we wouldn't be taken to the
922;; exact spot in the relevant node, since the bash manual has just `?' etc
923;; there. Maybe an extension to the prefix/suffix scheme could help this.
924
925(info-lookup-maybe-add-help
926 :mode 'sh-mode :topic 'symbol
927 ;; bash has "." and ":" in its index, but those chars will probably never
928 ;; work in info, so don't bother matching them in the regexp.
929 :regexp "\\([a-zA-Z0-9_-]+\\|[!{}@*#?$]\\|\\[\\[?\\|]]?\\)"
930 :doc-spec '(("(bash)Builtin Index" nil "^`" "[ .']")
931 ("(bash)Reserved Word Index" nil "^`" "[ .']")
932 ("(bash)Variable Index" nil "^`" "[ .']")
933 ;; coreutils (version 4.5.10) doesn't have a separate program
934 ;; index, so exclude extraneous stuff (most of it) by demanding
935 ;; "[a-z]+" in the trans-func.
936 ("(coreutils)Index"
937 (lambda (item) (if (string-match "\\`[a-z]+\\'" item) item)))
938 ;; diff (version 2.8.1) has only a few programs, index entries
939 ;; are things like "foo invocation".
940 ("(diff)Index"
941 (lambda (item)
942 (if (string-match "\\`\\([a-z]+\\) invocation\\'" item)
943 (match-string 1 item))))
944 ;; there's no plain "sed" index entry as such, mung another
945 ;; hopefully unique one to get to the invocation section
946 ("(sed)Concept Index"
947 (lambda (item)
948 (if (string-equal item "Standard input, processing as input")
949 "sed")))
950 ;; there's no plain "awk" or "gawk" index entries, mung other
951 ;; hopefully unique ones to get to the command line options
952 ("(gawk)Index"
953 (lambda (item)
954 (cond ((string-equal item "gawk, extensions, disabling")
955 "awk")
956 ((string-equal item "gawk, versions of, information about, printing")
957 "gawk"))))))
538996c7 958
7830c895
SM
959;; This misses some things which occur as node names but not in the
960;; index. Unfortunately it also picks up the wrong one of multiple
961;; entries for the same term in some cases. --fx
538996c7
SM
962(info-lookup-maybe-add-help
963 :mode 'cfengine-mode
1bc897ca 964 :regexp "[[:alnum:]_]+\\(?:()\\)?"
7830c895
SM
965 :doc-spec '(("(cfengine-Reference)Variable Index"
966 (lambda (item)
967 ;; Index entries may be like `IsPlain()'
968 (if (string-match "\\([[:alnum:]_]+\\)()" item)
969 (match-string 1 item)
970 item))
971 ;; This gets functions in evaluated classes. Other
972 ;; possible patterns don't seem to work too well.
973 "`" "(")))
ec4ef174
EZ
974
975(info-lookup-maybe-add-help
e8b36335 976 :mode 'Custom-mode
ec4ef174
EZ
977 :ignore-case t
978 :regexp "[^][()`',:\" \t\n]+"
979 :parse-rule 'info-lookup-guess-custom-symbol
980 :other-modes '(emacs-lisp-mode))
981
982(info-lookup-maybe-add-help
983 :mode 'help-mode
984 :regexp "[^][()`',:\" \t\n]+"
985 :other-modes '(emacs-lisp-mode))
d2c2b883 986\f
5a79736d
RS
987(provide 'info-look)
988
cbee283d 989;; arch-tag: 0f1e3ea3-32a2-4461-bbab-3cff93539a74
5a79736d 990;;; info-look.el ends here