comparison lisp/=libc.el @ 17935:5dbdaeeba3fe

Initial revision
author Richard M. Stallman <rms@gnu.org>
date Sat, 24 May 1997 08:13:06 +0000
parents
children 9402fa70b738
comparison
equal deleted inserted replaced
17934:b091b787e3e4 17935:5dbdaeeba3fe
1 ;;; libc.el -- lookup C symbols in the GNU C Library Reference Manual.
2
3 ;; Copyright (C) 1993--1997 Free Software Foundation, Inc.
4
5 ;;; Author: Ralph Schleicher <rs@purple.UL.BaWue.DE>
6 ;;; Keywords: local c info
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; This code has a long history. It started as a minor
28 ;; mode for C mode. This era ended with the release of version 2
29 ;; of the GNU C Library in 1997. The code was therefore rewritten
30 ;; more or less from scratch so that all lookups are performed via
31 ;; indices. Not finding an existing symbol in an index means that
32 ;; there is an error in the manual. Long missed features like a
33 ;; separate input history, symbol name completion in the mini-buffer,
34 ;; highlighting of looked up symbol names in the Info buffer, and
35 ;; implicitly prepending `struct', `union' or `enum' to data types
36 ;; were added in this phase too.
37
38 ;;; Code:
39
40 (require 'info)
41
42
43 (defvar libc-info-file-name "libc"
44 "Basename of the Info file of the GNU C Library Reference Manual.")
45
46 (defvar libc-highlight-face 'highlight
47 "*Face for highlighting looked up symbol names in the Info buffer;
48 `nil' disables highlighting.")
49
50 (defconst libc-symbol-completions nil
51 "Alist of documented C symbols.")
52
53 (defconst libc-file-completions nil
54 "Alist of documented programs or files.")
55
56 (defvar libc-history nil
57 "History of previous input lines.")
58
59 ;;;###autoload
60 (defun libc-describe-symbol (symbol-name)
61 "Display the documentation of a C symbol in another window.
62 SYMBOL-NAME must be documented in the GNU C Library Reference Manual.
63
64 If called interactively, SYMBOL-NAME will be read from the mini-buffer.
65 Optional prefix argument means insert the default symbol (if any) into
66 the mini-buffer so that it can be edited. The default symbol is the
67 one found at point.
68
69 If SYMBOL-NAME is a public function, variable, or data type of the GNU
70 C Library but `libc-describe-symbol' fails to display it's documentation,
71 then you have found a bug in the manual. Please report that to the mail
72 address `bug-glibc-manual@prep.ai.mit.edu' so that it can be fixed."
73 (interactive
74 (let* ((completion-ignore-case nil)
75 (enable-recursive-minibuffers t)
76 (symbol (libc-symbol-at-point))
77 (value (completing-read
78 (if symbol
79 (format "Describe symbol (default %s): " symbol)
80 (format "Describe symbol: "))
81 libc-symbol-completions nil nil
82 (and current-prefix-arg symbol) 'libc-history)))
83 (list (if (equal value "") symbol value))))
84 (or (assoc symbol-name libc-symbol-completions)
85 (error "Not documented as a C symbol: %s" (or symbol-name "")))
86 (or (libc-lookup-function symbol-name)
87 (libc-lookup-variable symbol-name)
88 (libc-lookup-type symbol-name)))
89
90 ;;;###autoload
91 (defun libc-describe-file (file-name)
92 "Display the documentation of a program or file in another window.
93 FILE-NAME must be documented in the GNU C Library Reference Manual."
94 (interactive
95 (let* ((completion-ignore-case nil)
96 (enable-recursive-minibuffers t))
97 (list (completing-read
98 "Describe program or file: "
99 libc-file-completions nil nil nil 'libc-history))))
100 (or (assoc file-name libc-file-completions)
101 (error "Not documented as a program or file: %s" (or file-name "")))
102 (libc-lookup-file file-name))
103
104 ;;;###autoload
105 (defun libc-search (regexp &optional arg)
106 "Search in the GNU C Library Reference Manual for REGEXP.
107 Prefix argument means search should ignore case."
108 (interactive "sSearch `libc.info' for regexp: \nP")
109 (or (get-buffer "*info*")
110 (save-window-excursion
111 (info)))
112 (switch-to-buffer-other-window "*info*")
113 (Info-goto-node (concat "(" libc-info-file-name ")"))
114 (let ((case-fold-search arg))
115 (Info-search regexp)))
116
117
118 (defun libc-make-completion-alist (info-nodes &optional regexp)
119 "Create a unique alist from all menu items in the Info nodes INFO-NODES
120 of the GNU C Reference Manual.
121
122 Optional second argument REGEXP means include only menu items matching the
123 regular expression REGEXP."
124 (condition-case nil
125 (let (completions item)
126 (save-window-excursion
127 (info libc-info-file-name)
128 (while info-nodes
129 (Info-goto-node (car info-nodes))
130 (goto-char (point-min))
131 (and (search-forward "\n* Menu:" nil t)
132 (while (re-search-forward "\n\\* \\([^:\t\n]*\\):" nil t)
133 (setq item (buffer-substring
134 (match-beginning 1) (match-end 1)))
135 (and (not (assoc item completions))
136 (if regexp (string-match regexp item) t)
137 (setq completions (cons (cons item nil)
138 completions)))))
139 (setq info-nodes (cdr info-nodes)))
140 (Info-directory))
141 completions)
142 (error nil)))
143
144 (defun libc-after-manual-update ()
145 "This function must only be called after a new version of the
146 GNU C Library Reference Manual was installed on your system."
147 (setq libc-symbol-completions (libc-make-completion-alist
148 '("Function Index"
149 "Variable Index"
150 "Type Index"))
151 libc-file-completions (libc-make-completion-alist
152 '("File Index") "^[^ \t]+$")))
153
154 (or (and libc-symbol-completions
155 libc-file-completions)
156 (libc-after-manual-update))
157
158 (defun libc-symbol-at-point ()
159 "Get the C symbol at point."
160 (condition-case nil
161 (save-excursion
162 (backward-sexp)
163 (let ((start (point))
164 prefix name)
165 ;; Test for a leading `struct', `union', or `enum' keyword
166 ;; but ignore names like `foo_struct'.
167 (setq prefix (and (< (skip-chars-backward " \t\n") 0)
168 (< (skip-chars-backward "_a-zA-Z0-9") 0)
169 (looking-at "\\(struct\\|union\\|enum\\)\\s ")
170 (concat (buffer-substring
171 (match-beginning 1) (match-end 1))
172 " ")))
173 (goto-char start)
174 (and (looking-at "[_a-zA-Z][_a-zA-Z0-9]*")
175 (setq name (buffer-substring
176 (match-beginning 0) (match-end 0))))
177 ;; Caveat! Look forward if point is at `struct' etc.
178 (and (not prefix)
179 (or (string-equal name "struct")
180 (string-equal name "union")
181 (string-equal name "enum"))
182 (looking-at "[a-z]+\\s +\\([_a-zA-Z][_a-zA-Z0-9]*\\)")
183 (setq prefix (concat name " ")
184 name (buffer-substring
185 (match-beginning 1) (match-end 1))))
186 (and (or prefix name)
187 (concat prefix name))))
188 (error nil)))
189
190 (defun libc-lookup-function (function)
191 (libc-search-index "Function Index" function
192 "^[ \t]+- \\(Function\\|Macro\\): .*\\<" "\\>"))
193
194 (defun libc-lookup-variable (variable)
195 (libc-search-index "Variable Index" variable
196 "^[ \t]+- \\(Variable\\|Macro\\): .*\\<" "\\>"))
197
198 (defun libc-lookup-type (data-type)
199 (libc-search-index "Type Index" data-type
200 "^[ \t]+- Data Type: \\<" "\\>"))
201
202 (defun libc-lookup-file (file-name)
203 (libc-search-index "File Index" file-name))
204
205 (defun libc-search-index (index item &optional prefix suffix)
206 "Search ITEM in the Info index INDEX and go to that Info node.
207
208 Value is ITEM or `nil' if an error occurs.
209
210 If PREFIX and/or SUFFIX are non-`nil', then search the Info node for
211 the first occurrence of the regular expression `PREFIX ITEM SUFFIX' and
212 leave point at the beginning of the first line of the match. ITEM will
213 be highlighted with `libc-highlight-face' iff `libc-highlight-face' is
214 not `nil'."
215 (condition-case nil
216 (save-selected-window
217 (or (get-buffer "*info*")
218 (save-window-excursion
219 (info)))
220 (switch-to-buffer-other-window "*info*")
221 (Info-goto-node (concat "(" libc-info-file-name ")" index))
222 (Info-menu item)
223 (if (or prefix suffix)
224 (let ((case-fold-search nil)
225 (buffer-read-only nil))
226 (goto-char (point-min))
227 (re-search-forward
228 (concat prefix (regexp-quote item) suffix))
229 (goto-char (match-beginning 0))
230 (and window-system libc-highlight-face
231 ;; Search again for ITEM so that the first
232 ;; occurence of ITEM will be highlighted.
233 (save-excursion
234 (re-search-forward (regexp-quote item))
235 (put-text-property
236 (match-beginning 0) (match-end 0)
237 'face libc-highlight-face)))
238 (beginning-of-line)))
239 item)
240 (error nil)))
241
242
243 (provide 'libc)
244
245 ;;; libc.el ends here