Initial release of Maemo 5 port of gnuplot
[gnuplot] / lisp / info-look.20.2.el
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 not part of GNU Emacs. (but is slightly modified from
10 ;; a file that is a part of GNU Emacs -- see below)
11
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)
15 ;; any later version.
16
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.
21
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.
26
27 ;; Bruce Ravel <ravel@phys.washington.edu> made two chanegs to this
28 ;; file:
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
32 ;;     grabbed.
33
34 ;;; Code:
35
36 (require 'info)
37 ;; next two lines added by Bruce Ravel <ravel@phys.washington.edu> to
38 ;; make this file compile properly under XEmacs.
39 (eval-and-compile
40   (if (string-match "XEmacs" emacs-version)
41       (require 'overlay)))
42
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)
48
49 (defvar info-lookup-other-window-flag t
50   "*Non-nil means pop up the Info buffer in another window.")
51
52 (defvar info-lookup-highlight-face 'highlight
53   "*Face for highlighting looked up help items.
54 Setting this variable to nil disables highlighting.")
55
56 (defvar info-lookup-highlight-overlay nil
57   "Overlay object used for highlighting.")
58
59 (defvar info-lookup-history nil
60   "History of previous input lines.")
61
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
66
67     (HELP-TOPIC . VARIABLE)
68
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
72
73     (HELP-MODE REGEXP IGNORE-CASE DOC-SPEC PARSE-RULE OTHER-MODES)
74
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
80
81     (INFO-NODE TRANS-FUNC PREFIX SUFFIX)
82
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
91  variable is not nil.
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.")
98
99 (defsubst info-lookup->topic-value (topic)
100   (symbol-value (cdr (assoc topic info-lookup-alist))))
101
102 (defsubst info-lookup->mode-value (topic mode)
103   (assoc mode (info-lookup->topic-value topic)))
104
105 (defsubst info-lookup->regexp (topic mode)
106   (nth 1 (info-lookup->mode-value topic mode)))
107
108 (defsubst info-lookup->ignore-case (topic mode)
109   (nth 2 (info-lookup->mode-value topic mode)))
110
111 (defsubst info-lookup->doc-spec (topic mode)
112   (nth 3 (info-lookup->mode-value topic mode)))
113
114 (defsubst info-lookup->parse-rule (topic mode)
115   (nth 4 (info-lookup->mode-value topic mode)))
116
117 (defsubst info-lookup->other-modes (topic mode)
118   (nth 5 (info-lookup->mode-value topic mode)))
119
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
123
124     (HELP-TOPIC . ((HELP-MODE INITIALIZED COMPLETIONS REFER-MODES) ...))
125
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.")
133
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)))))
139
140 (defsubst info-lookup->topic-cache (topic)
141   (cdr (info-lookup->cache topic)))
142
143 (defsubst info-lookup->mode-cache (topic mode)
144   (assoc mode (info-lookup->topic-cache topic)))
145
146 (defsubst info-lookup->initialized (topic mode)
147   (nth 1 (info-lookup->mode-cache topic mode)))
148
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)))
153
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)))
158
159 (defsubst info-lookup->all-modes (topic mode)
160   (cons mode (info-lookup->refer-modes topic mode)))
161
162 (defvar info-lookup-symbol-alist
163   '((autoconf-mode
164      "A[CM]_[_A-Z0-9]+" nil
165      (("(autoconf)Macro Index" "AC_"
166        "^[ \t]+- \\(Macro\\|Variable\\): .*\\<" "\\>")
167       ("(automake)Index" nil
168        "^[ \t]*`" "'"))
169      ;; Autoconf symbols are M4 macros.  Thus use M4's parser.
170      ignore
171      (m4-mode))
172     (bison-mode
173      "[:;|]\\|%\\([%{}]\\|[_a-z]+\\)\\|YY[_A-Z]+\\|yy[_a-z]+" nil
174      (("(bison)Index" nil
175        "`" "'"))
176      "[:;|]\\|%\\([%{}]\\|[_a-zA-Z][_a-zA-Z0-9]*\\)"
177      (c-mode))
178     (c-mode
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
187        "^[ \t]*`" "'"))
188      info-lookup-guess-c-symbol)
189     (m4-mode
190      "[_a-zA-Z][_a-zA-Z0-9]*" nil
191      (("(m4)Macro index"))
192      "[_a-zA-Z0-9]+")
193     (makefile-mode
194      "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z][_a-zA-Z0-9-]*" nil
195      (("(make)Name Index" nil
196        "^[ \t]*`" "'"))
197      "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z0-9-]+")
198     (texinfo-mode
199      "@\\([a-zA-Z]+\\|[^a-zA-Z]\\)" nil
200      (("(texinfo)Command and Variable Index"
201        ;; Ignore Emacs commands and prepend a `@'.
202        (lambda (item)
203          (if (string-match "^\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\( .*\\)?$" item)
204              (concat "@" (match-string 1 item))))
205        "`" "'"))))
206   "*Alist of help specifications for symbol names.
207 See the documentation of the variable `info-lookup-alist' for more details.")
208
209 (defvar info-lookup-file-alist
210   '((c-mode
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.")
215
216 ;;;###autoload
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
221 system."
222   (interactive)
223   (setq info-lookup-cache nil))
224
225 ;;;###autoload
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."
232   (interactive
233    (info-lookup-interactive-arguments 'symbol))
234   (info-lookup 'symbol symbol mode))
235
236 ;;;###autoload
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."
243   (interactive
244    (info-lookup-interactive-arguments 'file))
245   (info-lookup 'file file mode))
246
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)))
256                     default))
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)))
265
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)))
278
279 (defun info-lookup (topic item mode)
280   "Display the documentation of a help item."
281   (if (not mode)
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)
293         (info)
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)))
302         (condition-case nil
303             (progn
304               (Info-goto-node node)
305               (Info-menu (or (cdr entry) item))
306               (setq found t)
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))
312                     (re-search-forward
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))
320                                (end (match-end 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)))))
328           (error nil))
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))))
334
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.
351           (setq completions
352                 (nconc (info-lookup-make-completions topic mode)
353                        (apply 'append
354                               (mapcar (lambda (arg)
355                                         (info-lookup->completions topic arg))
356                                       refer-modes))))
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))
361         (if (not cell)
362             (setcdr (info-lookup->cache topic)
363                     (cons (cons mode data) (info-lookup->topic-cache topic)))
364           (setcdr cell data))
365         initialized)))
366
367 (defun info-lookup-make-completions (topic mode)
368   "Create a unique alist from all index entries."
369   (condition-case nil
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
375           (info)
376           (while doc-spec
377             (setq node (nth 0 (car doc-spec))
378                   trans (cond ((eq (nth 1 (car doc-spec)) nil)
379                                (lambda (arg)
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)))
384                                (lambda (arg)
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)
400                         (setq entry nil))
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)))
405           (Info-directory))
406         result)
407     (error nil)))
408
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))
416             modes (cdr modes))
417       (goto-char start))
418     ;; Collapse whitespace characters.
419     (and guess (concat (delete nil (mapcar (lambda (ch)
420                                              (if (or (char-equal ch ? )
421                                                      (char-equal ch ?\t)
422                                                      (char-equal ch ?\n))
423                                                  (if (not whitespace)
424                                                      (setq whitespace ? ))
425                                                (setq whitespace nil) ch))
426                                            guess))))))
427
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)
433     (if (symbolp rule)
434         (setq result (funcall rule))
435       (if (consp rule)
436           (setq regexp (car rule)
437                 subexp (cdr rule))
438         (setq regexp rule
439               subexp 0))
440       (skip-chars-backward " \t\n") (setq end (point))
441       (while (and (re-search-backward regexp nil t)
442                   (looking-at regexp)
443                   (>= (match-end 0) end))
444         (setq result (match-string subexp)))
445       (if (not result)
446           (progn
447             (goto-char start)
448             (skip-chars-forward " \t\n")
449             (and (looking-at regexp)
450                  (setq result (match-string subexp))))))
451     result))
452
453 (defun info-lookup-guess-c-symbol ()
454   "Get the C symbol at point."
455   (condition-case nil
456       (progn
457         (backward-sexp)
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) " ")))
465           (goto-char start)
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.
469           (and (not prefix)
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))))
478     (error nil)))
479
480 ;;;###autoload
481 (defun info-complete-symbol (&optional mode)
482   "Perform completion on symbol preceding point."
483   (interactive)
484   (info-complete 'symbol
485                  (or mode
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)))))
490
491 ;;;###autoload
492 (defun info-complete-file (&optional mode)
493   "Perform completion on file preceding point."
494   (interactive
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))
500
501 (defun info-complete (topic mode)
502   "Try to complete a help item."
503   (barf-if-buffer-read-only)
504   (if (not mode)
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)
512             modes (cdr modes)
513             try (info-lookup-guess-default* topic mode))
514       (goto-char start))
515     (and (not try)
516          (error "Found no %s to complete" topic))
517     (setq completion (try-completion
518                       try (info-lookup->completions topic mode)))
519     (cond ((not completion)
520            (ding))
521           ((stringp completion)
522            (delete-region (- start (length try)) start)
523            (insert completion)))))
524
525 (provide 'info-look)
526
527 ;;; info-look.el ends here