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