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 Ralph Schleicher.
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)
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.
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
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
151 "A[CM]_[_A-Z0-9]+" nil
152 (("(autoconf)Macro Index" "AC_"
153 "^[ \t]+- \\(Macro\\|Variable\\): .*\\<" "\\>")
154 ("(automake)Index" nil
156 ;; Autoconf symbols are M4 macros. Thus use M4's parser.
160 "[:;|]\\|%\\([%{}]\\|[_a-z]+\\)\\|YY[_A-Z]+\\|yy[_a-z]+" nil
163 "[:;|]\\|%\\([%{}]\\|[_a-zA-Z][_a-zA-Z0-9]*\\)"
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
175 info-lookup-guess-c-symbol
)
177 "[_a-zA-Z][_a-zA-Z0-9]*" nil
178 (("(m4)Macro index"))
181 "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z][_a-zA-Z0-9-]*" nil
182 (("(make)Name Index" nil
184 "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z0-9-]+")
186 "@\\([a-zA-Z]+\\|[^a-zA-Z]\\)" nil
187 (("(texinfo)Command and Variable Index"
188 ;; Ignore Emacs commands and prepend a `@'.
190 (if (string-match "^\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\( .*\\)?$" item
)
191 (concat "@" (match-string 1 item
))))
193 "*Alist of help specifications for symbol names.
194 See the documentation of the variable `info-lookup-alist' for more details.")
196 (defvar info-lookup-file-alist
198 "[_a-zA-Z0-9./+-]+" nil
199 (("(libc)File Index"))))
200 "*Alist of help specifications for file names.
201 See the documentation of the variable `info-lookup-alist' for more details.")
204 (defun info-lookup-reset ()
205 "Throw away all cached data.
206 This command is useful if the user wants to start at the beginning without
207 quitting Emacs, for example, after some Info documents were updated on the
210 (setq info-lookup-cache nil
))
213 (defun info-lookup-symbol (symbol &optional mode
)
214 "Display the documentation of a symbol.
215 If called interactively, SYMBOL will be read from the mini-buffer.
216 Prefix argument means unconditionally insert the default symbol name
217 into the mini-buffer so that it can be edited.
218 The default symbol is the one found at point."
220 (info-lookup-interactive-arguments 'symbol
))
221 (info-lookup 'symbol symbol mode
))
224 (defun info-lookup-file (file &optional mode
)
225 "Display the documentation of a file.
226 If called interactively, FILE will be read from the mini-buffer.
227 Prefix argument means unconditionally insert the default file name
228 into the mini-buffer so that it can be edited.
229 The default file name is the one found at point."
231 (info-lookup-interactive-arguments 'file
))
232 (info-lookup 'file file mode
))
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
)))
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
)))
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
)))
266 (defun info-lookup (topic item mode
)
267 "Display the documentation of a help item."
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
)
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
)))
291 (Info-goto-node node
)
292 (Info-menu (or (cdr entry
) item
))
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))
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))
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
)))))
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
))))
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.
339 (nconc (info-lookup-make-completions topic mode
)
341 (mapcar (lambda (arg)
342 (info-lookup->completions topic arg
))
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
))
349 (setcdr (info-lookup->cache topic
)
350 (cons (cons mode data
) (info-lookup->topic-cache topic
)))
354 (defun info-lookup-make-completions (topic mode
)
355 "Create a unique alist from all index entries."
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
364 (setq node
(nth 0 (car doc-spec
))
365 trans
(cond ((eq (nth 1 (car doc-spec
)) nil
)
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
)))
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
)
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
)))
394 (defun info-lookup-guess-default (topic mode
)
395 "Pick up default item at point (with favor to look back).
396 Return 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
))
403 ;; Collapse whitespace characters.
404 (and guess
(concat (delete nil
(mapcar (lambda (ch)
405 (if (or (char-equal ch ?
)
409 (setq whitespace ?
))
410 (setq whitespace nil
) ch
))
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
)
419 (setq result
(funcall rule
))
421 (setq regexp
(car rule
)
425 (skip-chars-backward " \t\n") (setq end
(point))
426 (while (and (re-search-backward regexp nil t
)
428 (>= (match-end 0) end
))
429 (setq result
(match-string subexp
)))
433 (skip-chars-forward " \t\n")
434 (and (looking-at regexp
)
435 (setq result
(match-string subexp
))))))
438 (defun info-lookup-guess-c-symbol ()
439 "Get the C symbol at point."
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) " ")))
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.
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
))))
466 (defun info-complete-symbol (&optional mode
)
467 "Perform completion on symbol preceding point."
469 (info-complete 'symbol
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
)))))
477 (defun info-complete-file (&optional mode
)
478 "Perform completion on file preceding point."
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
))
486 (defun info-complete (topic mode
)
487 "Try to complete a help item."
488 (barf-if-buffer-read-only)
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
)
498 try
(info-lookup-guess-default* topic mode
))
501 (error "Found no %s to complete" topic
))
502 (setq completion
(try-completion
503 try
(info-lookup->completions topic mode
)))
504 (cond ((not completion
)
506 ((stringp completion
)
507 (delete-region (- start
(length try
)) start
)
508 (insert completion
)))))
512 ;;; info-look.el ends here