]> code.delx.au - gnu-emacs/blob - lisp/finder.el
(finder-compile-keywords): Don't insert a keyword to the generated
[gnu-emacs] / lisp / finder.el
1 ;;; finder.el --- topic & keyword-based code finder
2
3 ;; Copyright (C) 1992, 1997, 1998, 1999, 2001 Free Software Foundation, Inc.
4
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
6 ;; Created: 16 Jun 1992
7 ;; Version: 1.0
8 ;; Keywords: help
9
10 ;; This file is part of GNU Emacs.
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 ;;; Commentary:
28
29 ;; This mode uses the Keywords library header to provide code-finding
30 ;; services by keyword.
31 ;;
32 ;; Things to do:
33 ;; 1. Support multiple keywords per search. This could be extremely hairy;
34 ;; there doesn't seem to be any way to get completing-read to exit on
35 ;; an EOL with no substring pending, which is what we'd want to end the loop.
36 ;; 2. Search by string in synopsis line?
37 ;; 3. Function to check finder-package-info for unknown keywords.
38
39 ;;; Code:
40
41 (require 'lisp-mnt)
42 (require 'find-func) ;for find-library(-suffixes)
43 ;; Use `load' rather than `require' so that it doesn't get loaded
44 ;; during byte-compilation (at which point it might be missing).
45 (load "finder-inf" t t)
46
47 ;; Local variable in finder buffer.
48 (defvar finder-headmark)
49
50 ;; These are supposed to correspond to top-level customization groups,
51 ;; says rms.
52 (defvar finder-known-keywords
53 '(
54 (abbrev . "abbreviation handling, typing shortcuts, macros")
55 ;; Too specific:
56 (bib . "code related to the `bib' bibliography processor")
57 (c . "support for the C language and related languages")
58 (calendar . "calendar and time management support")
59 (comm . "communications, networking, remote access to files")
60 (convenience . "convenience features for faster editing")
61 (data . "support editing files of data")
62 (docs . "support for Emacs documentation")
63 (emulations . "emulations of other editors")
64 (extensions . "Emacs Lisp language extensions")
65 (faces . "support for multiple fonts")
66 (files . "support for editing and manipulating files")
67 (frames . "support for Emacs frames and window systems")
68 (games . "games, jokes and amusements")
69 (hardware . "support for interfacing with exotic hardware")
70 (help . "support for on-line help systems")
71 (hypermedia . "support for links between text or other media types")
72 (i18n . "internationalization and alternate character-set support")
73 (internal . "code for Emacs internals, build process, defaults")
74 (languages . "specialized modes for editing programming languages")
75 (lisp . "Lisp support, including Emacs Lisp")
76 (local . "code local to your site")
77 (maint . "maintenance aids for the Emacs development group")
78 (mail . "modes for electronic-mail handling")
79 (matching . "various sorts of searching and matching")
80 (mouse . "mouse support")
81 (multimedia . "images and sound support")
82 (news . "support for netnews reading and posting")
83 (oop . "support for object-oriented programming")
84 (outlines . "support for hierarchical outlining")
85 (processes . "process, subshell, compilation, and job control support")
86 (terminals . "support for terminal types")
87 (tex . "code related to the TeX formatter")
88 (tools . "programming tools")
89 (unix . "front-ends/assistants for, or emulators of, UNIX features")
90 ;; Not a custom group and not currently useful.
91 ;; (vms . "support code for vms")
92 (wp . "word processing")
93 ))
94
95 (defvar finder-mode-map nil)
96 (or finder-mode-map
97 (let ((map (make-sparse-keymap)))
98 (define-key map " " 'finder-select)
99 (define-key map "f" 'finder-select)
100 (define-key map [mouse-2] 'finder-mouse-select)
101 (define-key map "\C-m" 'finder-select)
102 (define-key map "?" 'finder-summary)
103 (define-key map "q" 'finder-exit)
104 (define-key map "d" 'finder-list-keywords)
105 (setq finder-mode-map map)))
106
107
108 ;;; Code for regenerating the keyword list.
109
110 (defvar finder-package-info nil
111 "Assoc list mapping file names to description & keyword lists.")
112
113 (defun finder-compile-keywords (&rest dirs)
114 "Regenerate the keywords association list into the file `finder-inf.el'.
115 Optional arguments DIRS are a list of Emacs Lisp directories to compile from;
116 no arguments compiles from `load-path'."
117 (save-excursion
118 (let ((processed nil))
119 (find-file "finder-inf.el")
120 (erase-buffer)
121 (insert ";;; finder-inf.el --- keyword-to-package mapping\n")
122 (insert ";; This file is part of GNU Emacs.\n")
123 (insert ";;; Commentary:\n")
124 (insert ";; Don't edit this file. It's generated by finder.el\n\n")
125 (insert ";;; Code:\n")
126 (insert "\n(setq finder-package-info '(\n")
127 (mapcar
128 (lambda (d)
129 (when (file-exists-p (directory-file-name d))
130 (message "Directory %s" d)
131 (mapcar
132 (lambda (f)
133 (if (and (or (string-match "^[^=].*\\.el$" f)
134 ;; Allow compressed files also. Fixme:
135 ;; generalize this, especially for
136 ;; MS-DOG-type filenames.
137 (and (string-match "^[^=].*\\.el\\.\\(gz\\|Z\\)$" f)
138 (require 'jka-compr)))
139 ;; Ignore lock files.
140 (not (string-match "^.#" f))
141 (not (member f processed)))
142 (let (summary keystart keywords)
143 (setq processed (cons f processed))
144 (save-excursion
145 (set-buffer (get-buffer-create "*finder-scratch*"))
146 (buffer-disable-undo (current-buffer))
147 (erase-buffer)
148 (insert-file-contents
149 (concat (file-name-as-directory (or d ".")) f))
150 (setq summary (lm-synopsis))
151 (setq keywords (lm-keywords)))
152 (insert
153 (format " (\"%s\"\n "
154 (if (string-match "\\.\\(gz\\|Z\\)$" f)
155 (file-name-sans-extension f)
156 f)))
157 (prin1 summary (current-buffer))
158 (insert
159 "\n ")
160 (setq keystart (point))
161 (insert
162 (if keywords (format "(%s)" keywords) "nil")
163 ")\n")
164 (subst-char-in-region keystart (point) ?, ? )
165 )))
166 (directory-files (or d ".")))))
167 (or dirs load-path))
168 (insert "))\n
169 \(provide 'finder-inf)
170
171 ;;; Local Variables:
172 ;;; version-control: never
173 ;;; no-byte-compile: t
174 ;;; no-update-autoloads: t
175 ;;; End:
176 ;;; finder-inf.el ends here\n")
177 (kill-buffer "*finder-scratch*")
178 (eval-current-buffer) ;; So we get the new keyword list immediately
179 (basic-save-buffer))))
180
181 (defun finder-compile-keywords-make-dist ()
182 "Regenerate `finder-inf.el' for the Emacs distribution."
183 (apply 'finder-compile-keywords command-line-args-left)
184 (kill-emacs))
185
186 ;;; Now the retrieval code
187
188 (defun finder-insert-at-column (column &rest strings)
189 "Insert, at column COLUMN, other args STRINGS."
190 (if (>= (current-column) column) (insert "\n"))
191 (move-to-column column t)
192 (apply 'insert strings))
193
194 (defvar finder-help-echo nil)
195
196 (defun finder-mouse-face-on-line ()
197 "Put `mouse-face' and `help-echo' properties on the previous line."
198 (save-excursion
199 (previous-line 1)
200 (unless finder-help-echo
201 (setq finder-help-echo
202 (let* ((keys1 (where-is-internal 'finder-select
203 finder-mode-map))
204 (keys (nconc (where-is-internal
205 'finder-mouse-select finder-mode-map)
206 keys1)))
207 (concat (mapconcat 'key-description keys ", ")
208 ": select item"))))
209 (add-text-properties
210 (line-beginning-position) (line-end-position)
211 '(mouse-face highlight
212 help-echo finder-help-echo))))
213
214 ;;;###autoload
215 (defun finder-list-keywords ()
216 "Display descriptions of the keywords in the Finder buffer."
217 (interactive)
218 (if (get-buffer "*Finder*")
219 (pop-to-buffer "*Finder*")
220 (pop-to-buffer (set-buffer (get-buffer-create "*Finder*")))
221 (finder-mode)
222 (setq buffer-read-only nil)
223 (erase-buffer)
224 (mapc
225 (lambda (assoc)
226 (let ((keyword (car assoc)))
227 (insert (symbol-name keyword))
228 (finder-insert-at-column 14 (concat (cdr assoc) "\n"))
229 (finder-mouse-face-on-line)))
230 finder-known-keywords)
231 (goto-char (point-min))
232 (setq finder-headmark (point))
233 (setq buffer-read-only t)
234 (set-buffer-modified-p nil)
235 (balance-windows)
236 (finder-summary)))
237
238 (defun finder-list-matches (key)
239 (pop-to-buffer (set-buffer (get-buffer-create "*Finder Category*")))
240 (finder-mode)
241 (setq buffer-read-only nil)
242 (erase-buffer)
243 (let ((id (intern key)))
244 (insert
245 "The following packages match the keyword `" key "':\n\n")
246 (setq finder-headmark (point))
247 (mapc
248 (lambda (x)
249 (if (memq id (car (cdr (cdr x))))
250 (progn
251 (insert (car x))
252 (finder-insert-at-column 16 (concat (nth 1 x) "\n"))
253 (finder-mouse-face-on-line))))
254 finder-package-info)
255 (goto-char (point-min))
256 (forward-line)
257 (setq buffer-read-only t)
258 (set-buffer-modified-p nil)
259 (shrink-window-if-larger-than-buffer)
260 (finder-summary)))
261
262 ;;;###autoload
263 (defun finder-commentary (file)
264 "Display FILE's commentary section.
265 FILE should be in a form suitable for passing to `locate-library'."
266 (interactive
267 (list
268 (completing-read "Library name: "
269 'locate-file-completion
270 (cons (or find-function-source-path load-path)
271 (find-library-suffixes)))))
272 (let* ((str (lm-commentary (find-library-name file))))
273 (if (null str)
274 (error "Can't find any Commentary section"))
275 ;; This used to use *Finder* but that would clobber the
276 ;; directory of categories.
277 (pop-to-buffer "*Finder-package*")
278 (setq buffer-read-only nil)
279 (erase-buffer)
280 (insert str)
281 (goto-char (point-min))
282 (delete-blank-lines)
283 (goto-char (point-max))
284 (delete-blank-lines)
285 (goto-char (point-min))
286 (while (re-search-forward "^;+ ?" nil t)
287 (replace-match "" nil nil))
288 (goto-char (point-min))
289 (setq buffer-read-only t)
290 (set-buffer-modified-p nil)
291 (shrink-window-if-larger-than-buffer)
292 (finder-mode)
293 (finder-summary)))
294
295 (defun finder-current-item ()
296 (if (and finder-headmark (< (point) finder-headmark))
297 (error "No keyword or filename on this line")
298 (save-excursion
299 (beginning-of-line)
300 (current-word))))
301
302 (defun finder-select ()
303 "Select item on current line in a finder buffer."
304 (interactive)
305 (let ((key (finder-current-item)))
306 (if (string-match "\\.el$" key)
307 (finder-commentary key)
308 (finder-list-matches key))))
309
310 (defun finder-mouse-select (event)
311 "Select item in a finder buffer with the mouse."
312 (interactive "e")
313 (save-excursion
314 (set-buffer (window-buffer (posn-window (event-start event))))
315 (goto-char (posn-point (event-start event)))
316 (finder-select)))
317
318 ;;;###autoload
319 (defun finder-by-keyword ()
320 "Find packages matching a given keyword."
321 (interactive)
322 (finder-list-keywords))
323
324 (defun finder-mode ()
325 "Major mode for browsing package documentation.
326 \\<finder-mode-map>
327 \\[finder-select] more help for the item on the current line
328 \\[finder-exit] exit Finder mode and kill the Finder buffer."
329 (interactive)
330 (use-local-map finder-mode-map)
331 (set-syntax-table emacs-lisp-mode-syntax-table)
332 (setq mode-name "Finder")
333 (setq major-mode 'finder-mode)
334 (make-local-variable 'finder-headmark)
335 (setq finder-headmark nil))
336
337 (defun finder-summary ()
338 "Summarize basic Finder commands."
339 (interactive)
340 (message "%s"
341 (substitute-command-keys
342 "\\<finder-mode-map>\\[finder-select] = select, \
343 \\[finder-mouse-select] = select, \\[finder-list-keywords] = to \
344 finder directory, \\[finder-exit] = quit, \\[finder-summary] = help")))
345
346 (defun finder-exit ()
347 "Exit Finder mode and kill the buffer."
348 (interactive)
349 (or (one-window-p t)
350 (delete-window))
351 ;; Can happen in either buffer -- kill each of the two that exists
352 (and (get-buffer "*Finder*")
353 (kill-buffer "*Finder*"))
354 (and (get-buffer "*Finder Category*")
355 (kill-buffer "*Finder Category*")))
356
357 \f
358 (provide 'finder)
359
360 ;;; finder.el ends here