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 not part of GNU Emacs. (but is slightly modified from
10 ;; a file that is a part of GNU Emacs -- see below)
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;; Bruce Ravel <ravel@phys.washington.edu> made two chanegs to this
29 ;; 1. Added a check for XEmacs
30 ;; 2. Added (format "%s" (match-string 1)) in function
31 ;; `info-lookup-make-completions' so that text properties are not
37 ;; next two lines added by Bruce Ravel <ravel@phys.washington.edu> to
38 ;; make this file compile properly under XEmacs.
40 (if (string-match "XEmacs" emacs-version)
43 (defvar info-lookup-mode nil
44 "*Symbol of the current buffer's help mode.
45 Provide help according to the buffer's major mode if value is nil.
46 Automatically becomes buffer local when set in any fashion.")
47 (make-variable-buffer-local 'info-lookup-mode)
49 (defvar info-lookup-other-window-flag t
50 "*Non-nil means pop up the Info buffer in another window.")
52 (defvar info-lookup-highlight-face 'highlight
53 "*Face for highlighting looked up help items.
54 Setting this variable to nil disables highlighting.")
56 (defvar info-lookup-highlight-overlay nil
57 "Overlay object used for highlighting.")
59 (defvar info-lookup-history nil
60 "History of previous input lines.")
62 (defvar info-lookup-alist '((symbol . info-lookup-symbol-alist)
63 (file . info-lookup-file-alist))
64 "*Alist of known help topics.
65 Cons cells are of the form
67 (HELP-TOPIC . VARIABLE)
69 HELP-TOPIC is the symbol of a help topic.
70 VARIABLE is a variable storing HELP-TOPIC's public data.
71 Value is an alist with elements of the form
73 (HELP-MODE REGEXP IGNORE-CASE DOC-SPEC PARSE-RULE OTHER-MODES)
75 HELP-MODE is a mode's symbol.
76 REGEXP is a regular expression matching those help items whose
77 documentation can be looked up via DOC-SPEC.
78 IGNORE-CASE is non-nil if help items are case insensitive.
79 DOC-SPEC is a list of documentation specifications of the form
81 (INFO-NODE TRANS-FUNC PREFIX SUFFIX)
83 INFO-NODE is the name (including file name part) of an Info index.
84 TRANS-FUNC is a function translating index entries into help items;
85 nil means add only those index entries matching REGEXP, a string
86 means prepend string to the first word of all index entries.
87 PREFIX and SUFFIX are parts of a regular expression. If one of
88 them is non-nil then search the help item's Info node for the
89 first occurrence of the regular expression `PREFIX ITEM SUFFIX'.
90 ITEM will be highlighted with `info-lookup-highlight-face' if this
92 PARSE-RULE is either the symbol name of a function or a regular
93 expression for guessing the default help item at point. Fuzzy
94 regular expressions like \"[_a-zA-Z0-9]+\" do a better job if
95 there are no clear delimiters; do not try to write too complex
96 expressions. PARSE-RULE defaults to REGEXP.
97 OTHER-MODES is a list of cross references to other help modes.")
99 (defsubst info-lookup->topic-value (topic)
100 (symbol-value (cdr (assoc topic info-lookup-alist))))
102 (defsubst info-lookup->mode-value (topic mode)
103 (assoc mode (info-lookup->topic-value topic)))
105 (defsubst info-lookup->regexp (topic mode)
106 (nth 1 (info-lookup->mode-value topic mode)))
108 (defsubst info-lookup->ignore-case (topic mode)
109 (nth 2 (info-lookup->mode-value topic mode)))
111 (defsubst info-lookup->doc-spec (topic mode)
112 (nth 3 (info-lookup->mode-value topic mode)))
114 (defsubst info-lookup->parse-rule (topic mode)
115 (nth 4 (info-lookup->mode-value topic mode)))
117 (defsubst info-lookup->other-modes (topic mode)
118 (nth 5 (info-lookup->mode-value topic mode)))
120 (defvar info-lookup-cache nil
121 "Cache storing data maintained automatically by the program.
122 Value is an alist with cons cell of the form
124 (HELP-TOPIC . ((HELP-MODE INITIALIZED COMPLETIONS REFER-MODES) ...))
126 HELP-TOPIC is the symbol of a help topic.
127 HELP-MODE is a mode's symbol.
128 INITIALIZED is nil if HELP-MODE is uninitialized, t if
129 HELP-MODE is initialized, and `0' means HELP-MODE is
130 initialized but void.
131 COMPLETIONS is an alist of documented help items.
132 REFER-MODES is a list of other help modes to use.")
134 (defsubst info-lookup->cache (topic)
135 (or (assoc topic info-lookup-cache)
136 (car (setq info-lookup-cache
137 (cons (cons topic nil)
138 info-lookup-cache)))))
140 (defsubst info-lookup->topic-cache (topic)
141 (cdr (info-lookup->cache topic)))
143 (defsubst info-lookup->mode-cache (topic mode)
144 (assoc mode (info-lookup->topic-cache topic)))
146 (defsubst info-lookup->initialized (topic mode)
147 (nth 1 (info-lookup->mode-cache topic mode)))
149 (defsubst info-lookup->completions (topic mode)
150 (or (info-lookup->initialized topic mode)
151 (info-lookup-setup-mode topic mode))
152 (nth 2 (info-lookup->mode-cache topic mode)))
154 (defsubst info-lookup->refer-modes (topic mode)
155 (or (info-lookup->initialized topic mode)
156 (info-lookup-setup-mode topic mode))
157 (nth 3 (info-lookup->mode-cache topic mode)))
159 (defsubst info-lookup->all-modes (topic mode)
160 (cons mode (info-lookup->refer-modes topic mode)))
162 (defvar info-lookup-symbol-alist
164 "A[CM]_[_A-Z0-9]+" nil
165 (("(autoconf)Macro Index" "AC_"
166 "^[ \t]+- \\(Macro\\|Variable\\): .*\\<" "\\>")
167 ("(automake)Index" nil
169 ;; Autoconf symbols are M4 macros. Thus use M4's parser.
173 "[:;|]\\|%\\([%{}]\\|[_a-z]+\\)\\|YY[_A-Z]+\\|yy[_a-z]+" nil
176 "[:;|]\\|%\\([%{}]\\|[_a-zA-Z][_a-zA-Z0-9]*\\)"
179 "\\(struct \\|union \\|enum \\)?[_a-zA-Z][_a-zA-Z0-9]*" nil
180 (("(libc)Function Index" nil
181 "^[ \t]+- \\(Function\\|Macro\\): .*\\<" "\\>")
182 ("(libc)Variable Index" nil
183 "^[ \t]+- \\(Variable\\|Macro\\): .*\\<" "\\>")
184 ("(libc)Type Index" nil
185 "^[ \t]+- Data Type: \\<" "\\>")
186 ("(termcap)Var Index" nil
188 info-lookup-guess-c-symbol)
190 "[_a-zA-Z][_a-zA-Z0-9]*" nil
191 (("(m4)Macro index"))
194 "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z][_a-zA-Z0-9-]*" nil
195 (("(make)Name Index" nil
197 "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z0-9-]+")
199 "@\\([a-zA-Z]+\\|[^a-zA-Z]\\)" nil
200 (("(texinfo)Command and Variable Index"
201 ;; Ignore Emacs commands and prepend a `@'.
203 (if (string-match "^\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\( .*\\)?$" item)
204 (concat "@" (match-string 1 item))))
206 "*Alist of help specifications for symbol names.
207 See the documentation of the variable `info-lookup-alist' for more details.")
209 (defvar info-lookup-file-alist
211 "[_a-zA-Z0-9./+-]+" nil
212 (("(libc)File Index"))))
213 "*Alist of help specifications for file names.
214 See the documentation of the variable `info-lookup-alist' for more details.")
217 (defun info-lookup-reset ()
218 "Throw away all cached data.
219 This command is useful if the user wants to start at the beginning without
220 quitting Emacs, for example, after some Info documents were updated on the
223 (setq info-lookup-cache nil))
226 (defun info-lookup-symbol (symbol &optional mode)
227 "Display the documentation of a symbol.
228 If called interactively, SYMBOL will be read from the mini-buffer.
229 Prefix argument means unconditionally insert the default symbol name
230 into the mini-buffer so that it can be edited.
231 The default symbol is the one found at point."
233 (info-lookup-interactive-arguments 'symbol))
234 (info-lookup 'symbol symbol mode))
237 (defun info-lookup-file (file &optional mode)
238 "Display the documentation of a file.
239 If called interactively, FILE will be read from the mini-buffer.
240 Prefix argument means unconditionally insert the default file name
241 into the mini-buffer so that it can be edited.
242 The default file name is the one found at point."
244 (info-lookup-interactive-arguments 'file))
245 (info-lookup 'file file mode))
247 (defun info-lookup-interactive-arguments (topic)
248 "Return default value and help mode for help topic TOPIC."
249 (let* ((mode (if (info-lookup->mode-value
250 topic (or info-lookup-mode major-mode))
251 (or info-lookup-mode major-mode)
252 (info-lookup-change-mode topic)))
253 (completions (info-lookup->completions topic mode))
254 (default (info-lookup-guess-default topic mode))
255 (input (if (or current-prefix-arg (not (assoc default completions)))
257 (completion-ignore-case (info-lookup->ignore-case topic mode))
258 (enable-recursive-minibuffers t)
259 (value (completing-read
260 (if (and default (not input))
261 (format "Describe %s (default %s): " topic default)
262 (format "Describe %s: " topic))
263 completions nil nil input 'info-lookup-history)))
264 (list (if (equal value "") default value) mode)))
266 (defun info-lookup-change-mode (topic)
267 (let* ((completions (mapcar (lambda (arg)
268 (cons (symbol-name (car arg)) (car arg)))
269 (info-lookup->topic-value topic)))
270 (mode (completing-read
271 (format "Use %s help mode: " topic)
272 completions nil t nil 'info-lookup-history)))
273 (or (setq mode (cdr (assoc mode completions)))
274 (error "No %s help available" topic))
275 (or (info-lookup->mode-value topic mode)
276 (error "No %s help available for `%s'" topic mode))
277 (setq info-lookup-mode mode)))
279 (defun info-lookup (topic item mode)
280 "Display the documentation of a help item."
282 (setq mode (or info-lookup-mode major-mode)))
283 (or (info-lookup->mode-value topic mode)
284 (error "No %s help available for `%s'" topic mode))
285 (let ((entry (or (assoc (if (info-lookup->ignore-case topic mode)
286 (downcase item) item)
287 (info-lookup->completions topic mode))
288 (error "Not documented as a %s: %s" topic (or item ""))))
289 (modes (info-lookup->all-modes topic mode))
290 (window (selected-window))
291 found doc-spec node prefix suffix)
292 (if (not info-lookup-other-window-flag)
294 (save-window-excursion (info))
295 (switch-to-buffer-other-window "*info*"))
296 (while (and (not found) modes)
297 (setq doc-spec (info-lookup->doc-spec topic (car modes)))
298 (while (and (not found) doc-spec)
299 (setq node (nth 0 (car doc-spec))
300 prefix (nth 2 (car doc-spec))
301 suffix (nth 3 (car doc-spec)))
304 (Info-goto-node node)
305 (Info-menu (or (cdr entry) item))
307 (if (or prefix suffix)
308 (let ((case-fold-search
309 (info-lookup->ignore-case topic (car modes)))
310 (buffer-read-only nil))
311 (goto-char (point-min))
313 (concat prefix (regexp-quote item) suffix))
314 (goto-char (match-beginning 0))
315 (and window-system info-lookup-highlight-face
316 ;; Search again for ITEM so that the first
317 ;; occurence of ITEM will be highlighted.
318 (re-search-forward (regexp-quote item))
319 (let ((start (match-beginning 0))
321 (if (overlayp info-lookup-highlight-overlay)
322 (move-overlay info-lookup-highlight-overlay
323 start end (current-buffer))
324 (setq info-lookup-highlight-overlay
325 (make-overlay start end))))
326 (overlay-put info-lookup-highlight-overlay
327 'face info-lookup-highlight-face)))))
329 (setq doc-spec (cdr doc-spec)))
330 (setq modes (cdr modes)))
331 ;; Don't leave the Info buffer if the help item couldn't be looked up.
332 (if (and info-lookup-other-window-flag found)
333 (select-window window))))
335 (defun info-lookup-setup-mode (topic mode)
336 "Initialize the internal data structure."
337 (or (info-lookup->initialized topic mode)
338 (let (cell data (initialized 0) completions refer-modes)
339 (if (not (info-lookup->mode-value topic mode))
340 (message "No %s help available for `%s'" topic mode)
341 ;; Recursively setup cross references.
342 ;; But refer only to non-void modes.
343 (mapcar (lambda (arg)
344 (or (info-lookup->initialized topic arg)
345 (info-lookup-setup-mode topic arg))
346 (and (eq (info-lookup->initialized topic arg) t)
347 (setq refer-modes (cons arg refer-modes))))
348 (info-lookup->other-modes topic mode))
349 (setq refer-modes (nreverse refer-modes))
350 ;; Build the full completion alist.
352 (nconc (info-lookup-make-completions topic mode)
354 (mapcar (lambda (arg)
355 (info-lookup->completions topic arg))
357 (setq initialized t))
358 ;; Update `info-lookup-cache'.
359 (setq cell (info-lookup->mode-cache topic mode)
360 data (list initialized completions refer-modes))
362 (setcdr (info-lookup->cache topic)
363 (cons (cons mode data) (info-lookup->topic-cache topic)))
367 (defun info-lookup-make-completions (topic mode)
368 "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)
374 (save-window-excursion
377 (setq node (nth 0 (car doc-spec))
378 trans (cond ((eq (nth 1 (car doc-spec)) nil)
380 (if (string-match regexp arg)
381 (match-string 1 arg))))
382 ((stringp (nth 1 (car doc-spec)))
383 (setq prefix (nth 1 (car doc-spec)))
385 (if (string-match "^\\([^: \t\n]+\\)" arg)
386 (concat prefix (match-string 1 arg)))))
387 (t (nth 1 (car doc-spec)))))
388 (message "Processing Info node \"%s\"..." node)
389 (Info-goto-node node)
390 (goto-char (point-min))
391 (and (search-forward "\n* Menu:" nil t)
392 (while (re-search-forward "\n\\* \\([^:\t\n]*\\):" nil t)
393 ;; Bruce Ravel added format
394 ;; w/o format, this grabs text properties
395 (setq entry (format "%s" (match-string 1))
396 item (funcall trans entry))
397 (and (info-lookup->ignore-case topic mode)
398 (setq item (downcase item)))
399 (and (string-equal entry item)
401 (or (assoc item result)
402 (setq result (cons (cons item entry) result)))))
403 (message "Processing Info node \"%s\"... done" node)
404 (setq doc-spec (cdr doc-spec)))
409 (defun info-lookup-guess-default (topic mode)
410 "Pick up default item at point (with favor to look back).
411 Return nil if there is nothing appropriate."
412 (let ((modes (info-lookup->all-modes topic mode))
413 (start (point)) guess whitespace)
414 (while (and (not guess) modes)
415 (setq guess (info-lookup-guess-default* topic (car modes))
418 ;; Collapse whitespace characters.
419 (and guess (concat (delete nil (mapcar (lambda (ch)
420 (if (or (char-equal ch ? )
424 (setq whitespace ? ))
425 (setq whitespace nil) ch))
428 (defun info-lookup-guess-default* (topic mode)
429 (let ((case-fold-search (info-lookup->ignore-case topic mode))
430 (rule (or (info-lookup->parse-rule topic mode)
431 (info-lookup->regexp topic mode)))
432 (start (point)) end regexp subexp result)
434 (setq result (funcall rule))
436 (setq regexp (car rule)
440 (skip-chars-backward " \t\n") (setq end (point))
441 (while (and (re-search-backward regexp nil t)
443 (>= (match-end 0) end))
444 (setq result (match-string subexp)))
448 (skip-chars-forward " \t\n")
449 (and (looking-at regexp)
450 (setq result (match-string subexp))))))
453 (defun info-lookup-guess-c-symbol ()
454 "Get the C symbol at point."
458 (let ((start (point)) prefix name)
459 ;; Test for a leading `struct', `union', or `enum' keyword
460 ;; but ignore names like `foo_struct'.
461 (setq prefix (and (< (skip-chars-backward " \t\n") 0)
462 (< (skip-chars-backward "_a-zA-Z0-9") 0)
463 (looking-at "\\(struct\\|union\\|enum\\)\\s ")
464 (concat (match-string 1) " ")))
466 (and (looking-at "[_a-zA-Z][_a-zA-Z0-9]*")
467 (setq name (match-string 0)))
468 ;; Caveat! Look forward if point is at `struct' etc.
470 (or (string-equal name "struct")
471 (string-equal name "union")
472 (string-equal name "enum"))
473 (looking-at "[a-z]+\\s +\\([_a-zA-Z][_a-zA-Z0-9]*\\)")
474 (setq prefix (concat name " ")
475 name (match-string 1)))
476 (and (or prefix name)
477 (concat prefix name))))
481 (defun info-complete-symbol (&optional mode)
482 "Perform completion on symbol preceding point."
484 (info-complete 'symbol
486 (if (info-lookup->mode-value
487 'symbol (or info-lookup-mode major-mode))
488 (or info-lookup-mode major-mode)
489 (info-lookup-change-mode 'symbol)))))
492 (defun info-complete-file (&optional mode)
493 "Perform completion on file preceding point."
495 (list (if (info-lookup->mode-value
496 'file (or info-lookup-mode major-mode))
497 (or info-lookup-mode major-mode)
498 (info-lookup-change-mode 'file))))
499 (info-complete 'file mode))
501 (defun info-complete (topic mode)
502 "Try to complete a help item."
503 (barf-if-buffer-read-only)
505 (setq mode (or info-lookup-mode major-mode)))
506 (or (info-lookup->mode-value topic mode)
507 (error "No %s completion available for `%s'" topic mode))
508 (let ((modes (info-lookup->all-modes topic mode))
509 (start (point)) try completion)
510 (while (and (not try) modes)
511 (setq mode (car modes)
513 try (info-lookup-guess-default* topic mode))
516 (error "Found no %s to complete" topic))
517 (setq completion (try-completion
518 try (info-lookup->completions topic mode)))
519 (cond ((not completion)
521 ((stringp completion)
522 (delete-region (- start (length try)) start)
523 (insert completion)))))
527 ;;; info-look.el ends here