(replace_buffer_in_all_windows):
[bpt/emacs.git] / lisp / info-look.el
CommitLineData
5a79736d
RS
1;;; info-look.el --- major-mode-sensitive Info index lookup facility.
2;; An older version of this was known as libc.el.
3
4;; Copyright (C) 1995, 1996, 1997 Ralph Schleicher.
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)
29
30(defvar info-lookup-mode nil
31 "*Symbol of the current buffer's help mode.
32Provide help according to the buffer's major mode if value is nil.
33Automatically becomes buffer local when set in any fashion.")
34(make-variable-buffer-local 'info-lookup-mode)
35
36(defvar info-lookup-other-window-flag t
37 "*Non-nil means pop up the Info buffer in another window.")
38
39(defvar info-lookup-highlight-face 'highlight
40 "*Face for highlighting looked up help items.
41Setting this variable to nil disables highlighting.")
42
43(defvar info-lookup-highlight-overlay nil
44 "Overlay object used for highlighting.")
45
46(defvar info-lookup-history nil
47 "History of previous input lines.")
48
49(defvar info-lookup-alist '((symbol . info-lookup-symbol-alist)
50 (file . info-lookup-file-alist))
51 "*Alist of known help topics.
52Cons cells are of the form
53
54 (HELP-TOPIC . VARIABLE)
55
56HELP-TOPIC is the symbol of a help topic.
57VARIABLE is a variable storing HELP-TOPIC's public data.
58 Value is an alist with elements of the form
59
60 (HELP-MODE REGEXP IGNORE-CASE DOC-SPEC PARSE-RULE OTHER-MODES)
61
62HELP-MODE is a mode's symbol.
63REGEXP is a regular expression matching those help items whose
64 documentation can be looked up via DOC-SPEC.
65IGNORE-CASE is non-nil if help items are case insensitive.
66DOC-SPEC is a list of documentation specifications of the form
67
68 (INFO-NODE TRANS-FUNC PREFIX SUFFIX)
69
70INFO-NODE is the name (including file name part) of an Info index.
71TRANS-FUNC is a function translating index entries into help items;
72 nil means add only those index entries matching REGEXP, a string
73 means prepend string to the first word of all index entries.
74PREFIX and SUFFIX are parts of a regular expression. If one of
75 them is non-nil then search the help item's Info node for the
76 first occurrence of the regular expression `PREFIX ITEM SUFFIX'.
77 ITEM will be highlighted with `info-lookup-highlight-face' if this
78 variable is not nil.
79PARSE-RULE is either the symbol name of a function or a regular
80 expression for guessing the default help item at point. Fuzzy
81 regular expressions like \"[_a-zA-Z0-9]+\" do a better job if
82 there are no clear delimiters; do not try to write too complex
83 expressions. PARSE-RULE defaults to REGEXP.
84OTHER-MODES is a list of cross references to other help modes.")
85
86(defsubst info-lookup->topic-value (topic)
87 (symbol-value (cdr (assoc topic info-lookup-alist))))
88
89(defsubst info-lookup->mode-value (topic mode)
90 (assoc mode (info-lookup->topic-value topic)))
91
92(defsubst info-lookup->regexp (topic mode)
93 (nth 1 (info-lookup->mode-value topic mode)))
94
95(defsubst info-lookup->ignore-case (topic mode)
96 (nth 2 (info-lookup->mode-value topic mode)))
97
98(defsubst info-lookup->doc-spec (topic mode)
99 (nth 3 (info-lookup->mode-value topic mode)))
100
101(defsubst info-lookup->parse-rule (topic mode)
102 (nth 4 (info-lookup->mode-value topic mode)))
103
104(defsubst info-lookup->other-modes (topic mode)
105 (nth 5 (info-lookup->mode-value topic mode)))
106
107(defvar info-lookup-cache nil
108 "Cache storing data maintained automatically by the program.
109Value is an alist with cons cell of the form
110
111 (HELP-TOPIC . ((HELP-MODE INITIALIZED COMPLETIONS REFER-MODES) ...))
112
113HELP-TOPIC is the symbol of a help topic.
114HELP-MODE is a mode's symbol.
115INITIALIZED is nil if HELP-MODE is uninitialized, t if
116 HELP-MODE is initialized, and `0' means HELP-MODE is
117 initialized but void.
118COMPLETIONS is an alist of documented help items.
119REFER-MODES is a list of other help modes to use.")
120
121(defsubst info-lookup->cache (topic)
122 (or (assoc topic info-lookup-cache)
123 (car (setq info-lookup-cache
124 (cons (cons topic nil)
125 info-lookup-cache)))))
126
127(defsubst info-lookup->topic-cache (topic)
128 (cdr (info-lookup->cache topic)))
129
130(defsubst info-lookup->mode-cache (topic mode)
131 (assoc mode (info-lookup->topic-cache topic)))
132
133(defsubst info-lookup->initialized (topic mode)
134 (nth 1 (info-lookup->mode-cache topic mode)))
135
136(defsubst info-lookup->completions (topic mode)
137 (or (info-lookup->initialized topic mode)
138 (info-lookup-setup-mode topic mode))
139 (nth 2 (info-lookup->mode-cache topic mode)))
140
141(defsubst info-lookup->refer-modes (topic mode)
142 (or (info-lookup->initialized topic mode)
143 (info-lookup-setup-mode topic mode))
144 (nth 3 (info-lookup->mode-cache topic mode)))
145
146(defsubst info-lookup->all-modes (topic mode)
147 (cons mode (info-lookup->refer-modes topic mode)))
148
149(defvar info-lookup-symbol-alist
150 '((autoconf-mode
151 "A[CM]_[_A-Z0-9]+" nil
152 (("(autoconf)Macro Index" "AC_"
153 "^[ \t]+- \\(Macro\\|Variable\\): .*\\<" "\\>")
154 ("(automake)Index" nil
155 "^[ \t]*`" "'"))
156 ;; Autoconf symbols are M4 macros. Thus use M4's parser.
157 ignore
158 (m4-mode))
159 (bison-mode
160 "[:;|]\\|%\\([%{}]\\|[_a-z]+\\)\\|YY[_A-Z]+\\|yy[_a-z]+" nil
161 (("(bison)Index" nil
162 "`" "'"))
163 "[:;|]\\|%\\([%{}]\\|[_a-zA-Z][_a-zA-Z0-9]*\\)"
164 (c-mode))
165 (c-mode
166 "\\(struct \\|union \\|enum \\)?[_a-zA-Z][_a-zA-Z0-9]*" nil
167 (("(libc)Function Index" nil
168 "^[ \t]+- \\(Function\\|Macro\\): .*\\<" "\\>")
169 ("(libc)Variable Index" nil
170 "^[ \t]+- \\(Variable\\|Macro\\): .*\\<" "\\>")
171 ("(libc)Type Index" nil
172 "^[ \t]+- Data Type: \\<" "\\>")
173 ("(termcap)Var Index" nil
174 "^[ \t]*`" "'"))
175 info-lookup-guess-c-symbol)
176 (m4-mode
177 "[_a-zA-Z][_a-zA-Z0-9]*" nil
178 (("(m4)Macro index"))
179 "[_a-zA-Z0-9]+")
180 (makefile-mode
181 "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z][_a-zA-Z0-9-]*" nil
182 (("(make)Name Index" nil
183 "^[ \t]*`" "'"))
184 "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z0-9-]+")
185 (texinfo-mode
186 "@\\([a-zA-Z]+\\|[^a-zA-Z]\\)" nil
187 (("(texinfo)Command and Variable Index"
188 ;; Ignore Emacs commands and prepend a `@'.
189 (lambda (item)
190 (if (string-match "^\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\( .*\\)?$" item)
191 (concat "@" (match-string 1 item))))
192 "`" "'"))))
193 "*Alist of help specifications for symbol names.
194See the documentation of the variable `info-lookup-alist' for more details.")
195
196(defvar info-lookup-file-alist
197 '((c-mode
198 "[_a-zA-Z0-9./+-]+" nil
199 (("(libc)File Index"))))
200 "*Alist of help specifications for file names.
201See the documentation of the variable `info-lookup-alist' for more details.")
202
203;;;###autoload
204(defun info-lookup-reset ()
205 "Throw away all cached data.
206This command is useful if the user wants to start at the beginning without
207quitting Emacs, for example, after some Info documents were updated on the
208system."
209 (interactive)
210 (setq info-lookup-cache nil))
211
212;;;###autoload
213(defun info-lookup-symbol (symbol &optional mode)
214 "Display the documentation of a symbol.
215If called interactively, SYMBOL will be read from the mini-buffer.
216Prefix argument means unconditionally insert the default symbol name
217into the mini-buffer so that it can be edited.
218The default symbol is the one found at point."
219 (interactive
220 (info-lookup-interactive-arguments 'symbol))
221 (info-lookup 'symbol symbol mode))
222
223;;;###autoload
224(defun info-lookup-file (file &optional mode)
225 "Display the documentation of a file.
226If called interactively, FILE will be read from the mini-buffer.
227Prefix argument means unconditionally insert the default file name
228into the mini-buffer so that it can be edited.
229The default file name is the one found at point."
230 (interactive
231 (info-lookup-interactive-arguments 'file))
232 (info-lookup 'file file mode))
233
234(defun info-lookup-interactive-arguments (topic)
235 "Return default value and help mode for help topic TOPIC."
236 (let* ((mode (if (info-lookup->mode-value
237 topic (or info-lookup-mode major-mode))
238 (or info-lookup-mode major-mode)
239 (info-lookup-change-mode topic)))
240 (completions (info-lookup->completions topic mode))
241 (default (info-lookup-guess-default topic mode))
242 (input (if (or current-prefix-arg (not (assoc default completions)))
243 default))
244 (completion-ignore-case (info-lookup->ignore-case topic mode))
245 (enable-recursive-minibuffers t)
246 (value (completing-read
247 (if (and default (not input))
248 (format "Describe %s (default %s): " topic default)
249 (format "Describe %s: " topic))
250 completions nil nil input 'info-lookup-history)))
251 (list (if (equal value "") default value) mode)))
252
253(defun info-lookup-change-mode (topic)
254 (let* ((completions (mapcar (lambda (arg)
255 (cons (symbol-name (car arg)) (car arg)))
256 (info-lookup->topic-value topic)))
257 (mode (completing-read
258 (format "Use %s help mode: " topic)
259 completions nil t nil 'info-lookup-history)))
260 (or (setq mode (cdr (assoc mode completions)))
261 (error "No %s help available" topic))
262 (or (info-lookup->mode-value topic mode)
263 (error "No %s help available for `%s'" topic mode))
264 (setq info-lookup-mode mode)))
265
266(defun info-lookup (topic item mode)
267 "Display the documentation of a help item."
268 (if (not mode)
269 (setq mode (or info-lookup-mode major-mode)))
270 (or (info-lookup->mode-value topic mode)
271 (error "No %s help available for `%s'" topic mode))
272 (let ((entry (or (assoc (if (info-lookup->ignore-case topic mode)
273 (downcase item) item)
274 (info-lookup->completions topic mode))
275 (error "Not documented as a %s: %s" topic (or item ""))))
276 (modes (info-lookup->all-modes topic mode))
277 (window (selected-window))
278 found doc-spec node prefix suffix)
279 (if (not info-lookup-other-window-flag)
280 (info)
281 (save-window-excursion (info))
282 (switch-to-buffer-other-window "*info*"))
283 (while (and (not found) modes)
284 (setq doc-spec (info-lookup->doc-spec topic (car modes)))
285 (while (and (not found) doc-spec)
286 (setq node (nth 0 (car doc-spec))
287 prefix (nth 2 (car doc-spec))
288 suffix (nth 3 (car doc-spec)))
289 (condition-case nil
290 (progn
291 (Info-goto-node node)
292 (Info-menu (or (cdr entry) item))
293 (setq found t)
294 (if (or prefix suffix)
295 (let ((case-fold-search
296 (info-lookup->ignore-case topic (car modes)))
297 (buffer-read-only nil))
298 (goto-char (point-min))
299 (re-search-forward
300 (concat prefix (regexp-quote item) suffix))
301 (goto-char (match-beginning 0))
302 (and window-system info-lookup-highlight-face
303 ;; Search again for ITEM so that the first
304 ;; occurence of ITEM will be highlighted.
305 (re-search-forward (regexp-quote item))
306 (let ((start (match-beginning 0))
307 (end (match-end 0)))
308 (if (overlayp info-lookup-highlight-overlay)
309 (move-overlay info-lookup-highlight-overlay
310 start end (current-buffer))
311 (setq info-lookup-highlight-overlay
312 (make-overlay start end))))
313 (overlay-put info-lookup-highlight-overlay
314 'face info-lookup-highlight-face)))))
315 (error nil))
316 (setq doc-spec (cdr doc-spec)))
317 (setq modes (cdr modes)))
318 ;; Don't leave the Info buffer if the help item couldn't be looked up.
319 (if (and info-lookup-other-window-flag found)
320 (select-window window))))
321
322(defun info-lookup-setup-mode (topic mode)
323 "Initialize the internal data structure."
324 (or (info-lookup->initialized topic mode)
325 (let (cell data (initialized 0) completions refer-modes)
326 (if (not (info-lookup->mode-value topic mode))
327 (message "No %s help available for `%s'" topic mode)
328 ;; Recursively setup cross references.
329 ;; But refer only to non-void modes.
330 (mapcar (lambda (arg)
331 (or (info-lookup->initialized topic arg)
332 (info-lookup-setup-mode topic arg))
333 (and (eq (info-lookup->initialized topic arg) t)
334 (setq refer-modes (cons arg refer-modes))))
335 (info-lookup->other-modes topic mode))
336 (setq refer-modes (nreverse refer-modes))
337 ;; Build the full completion alist.
338 (setq completions
339 (nconc (info-lookup-make-completions topic mode)
340 (apply 'append
341 (mapcar (lambda (arg)
342 (info-lookup->completions topic arg))
343 refer-modes))))
344 (setq initialized t))
345 ;; Update `info-lookup-cache'.
346 (setq cell (info-lookup->mode-cache topic mode)
347 data (list initialized completions refer-modes))
348 (if (not cell)
349 (setcdr (info-lookup->cache topic)
350 (cons (cons mode data) (info-lookup->topic-cache topic)))
351 (setcdr cell data))
352 initialized)))
353
354(defun info-lookup-make-completions (topic mode)
355 "Create a unique alist from all index entries."
356 (condition-case nil
357 (let ((doc-spec (info-lookup->doc-spec topic mode))
358 (regexp (concat "^\\(" (info-lookup->regexp topic mode)
359 "\\)\\([ \t].*\\)?$"))
360 node trans entry item prefix result)
361 (save-window-excursion
362 (info)
363 (while doc-spec
364 (setq node (nth 0 (car doc-spec))
365 trans (cond ((eq (nth 1 (car doc-spec)) nil)
366 (lambda (arg)
367 (if (string-match regexp arg)
368 (match-string 1 arg))))
369 ((stringp (nth 1 (car doc-spec)))
370 (setq prefix (nth 1 (car doc-spec)))
371 (lambda (arg)
372 (if (string-match "^\\([^: \t\n]+\\)" arg)
373 (concat prefix (match-string 1 arg)))))
374 (t (nth 1 (car doc-spec)))))
375 (message "Processing Info node \"%s\"..." node)
376 (Info-goto-node node)
377 (goto-char (point-min))
378 (and (search-forward "\n* Menu:" nil t)
379 (while (re-search-forward "\n\\* \\([^:\t\n]*\\):" nil t)
380 (setq entry (match-string 1)
381 item (funcall trans entry))
382 (and (info-lookup->ignore-case topic mode)
383 (setq item (downcase item)))
384 (and (string-equal entry item)
385 (setq entry nil))
386 (or (assoc item result)
387 (setq result (cons (cons item entry) result)))))
388 (message "Processing Info node \"%s\"... done" node)
389 (setq doc-spec (cdr doc-spec)))
390 (Info-directory))
391 result)
392 (error nil)))
393
394(defun info-lookup-guess-default (topic mode)
395 "Pick up default item at point (with favor to look back).
396Return nil if there is nothing appropriate."
397 (let ((modes (info-lookup->all-modes topic mode))
398 (start (point)) guess whitespace)
399 (while (and (not guess) modes)
400 (setq guess (info-lookup-guess-default* topic (car modes))
401 modes (cdr modes))
402 (goto-char start))
403 ;; Collapse whitespace characters.
404 (and guess (concat (delete nil (mapcar (lambda (ch)
405 (if (or (char-equal ch ? )
406 (char-equal ch ?\t)
407 (char-equal ch ?\n))
408 (if (not whitespace)
409 (setq whitespace ? ))
410 (setq whitespace nil) ch))
411 guess))))))
412
413(defun info-lookup-guess-default* (topic mode)
414 (let ((case-fold-search (info-lookup->ignore-case topic mode))
415 (rule (or (info-lookup->parse-rule topic mode)
416 (info-lookup->regexp topic mode)))
417 (start (point)) end regexp subexp result)
418 (if (symbolp rule)
419 (setq result (funcall rule))
420 (if (consp rule)
421 (setq regexp (car rule)
422 subexp (cdr rule))
423 (setq regexp rule
424 subexp 0))
425 (skip-chars-backward " \t\n") (setq end (point))
426 (while (and (re-search-backward regexp nil t)
427 (looking-at regexp)
428 (>= (match-end 0) end))
429 (setq result (match-string subexp)))
430 (if (not result)
431 (progn
432 (goto-char start)
433 (skip-chars-forward " \t\n")
434 (and (looking-at regexp)
435 (setq result (match-string subexp))))))
436 result))
437
438(defun info-lookup-guess-c-symbol ()
439 "Get the C symbol at point."
440 (condition-case nil
441 (progn
442 (backward-sexp)
443 (let ((start (point)) prefix name)
444 ;; Test for a leading `struct', `union', or `enum' keyword
445 ;; but ignore names like `foo_struct'.
446 (setq prefix (and (< (skip-chars-backward " \t\n") 0)
447 (< (skip-chars-backward "_a-zA-Z0-9") 0)
448 (looking-at "\\(struct\\|union\\|enum\\)\\s ")
449 (concat (match-string 1) " ")))
450 (goto-char start)
451 (and (looking-at "[_a-zA-Z][_a-zA-Z0-9]*")
452 (setq name (match-string 0)))
453 ;; Caveat! Look forward if point is at `struct' etc.
454 (and (not prefix)
455 (or (string-equal name "struct")
456 (string-equal name "union")
457 (string-equal name "enum"))
458 (looking-at "[a-z]+\\s +\\([_a-zA-Z][_a-zA-Z0-9]*\\)")
459 (setq prefix (concat name " ")
460 name (match-string 1)))
461 (and (or prefix name)
462 (concat prefix name))))
463 (error nil)))
464
465;;;###autoload
466(defun info-complete-symbol (&optional mode)
467 "Perform completion on symbol preceding point."
82fb111c
RS
468 (interactive)
469 (info-complete 'symbol
470 (or mode
471 (if (info-lookup->mode-value
472 'symbol (or info-lookup-mode major-mode))
473 (or info-lookup-mode major-mode)
474 (info-lookup-change-mode 'symbol)))))
5a79736d
RS
475
476;;;###autoload
477(defun info-complete-file (&optional mode)
478 "Perform completion on file preceding point."
479 (interactive
480 (list (if (info-lookup->mode-value
481 'file (or info-lookup-mode major-mode))
482 (or info-lookup-mode major-mode)
483 (info-lookup-change-mode 'file))))
484 (info-complete 'file mode))
485
486(defun info-complete (topic mode)
487 "Try to complete a help item."
488 (barf-if-buffer-read-only)
489 (if (not mode)
490 (setq mode (or info-lookup-mode major-mode)))
491 (or (info-lookup->mode-value topic mode)
492 (error "No %s completion available for `%s'" topic mode))
493 (let ((modes (info-lookup->all-modes topic mode))
494 (start (point)) try completion)
495 (while (and (not try) modes)
496 (setq mode (car modes)
497 modes (cdr modes)
498 try (info-lookup-guess-default* topic mode))
499 (goto-char start))
500 (and (not try)
501 (error "Found no %s to complete" topic))
502 (setq completion (try-completion
503 try (info-lookup->completions topic mode)))
504 (cond ((not completion)
505 (ding))
506 ((stringp completion)
507 (delete-region (- start (length try)) start)
508 (insert completion)))))
509
510(provide 'info-look)
511
512;;; info-look.el ends here