new version
[emacs.git] / lisp / finder.el
blob7d638bd4edd93e1be8f00dc35b3e6ca911d4e8c2
1 ;;; finder.el --- topic & keyword-based code finder
3 ;; Copyright (C) 1992, 1997, 1998 Free Software Foundation, Inc.
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
6 ;; Created: 16 Jun 1992
7 ;; Version: 1.0
8 ;; Keywords: help
10 ;; This file is part of GNU Emacs.
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.
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 ;;; Commentary:
29 ;; This mode uses the Keywords library header to provide code-finding
30 ;; services by keyword.
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.
39 ;;; Code:
41 (require 'lisp-mnt)
42 (require 'finder-inf)
44 ;; Local variable in finder buffer.
45 (defvar finder-headmark)
47 (defvar finder-known-keywords
49 (abbrev . "abbreviation handling, typing shortcuts, macros")
50 (bib . "code related to the `bib' bibliography processor")
51 (c . "support for the C language and related languages")
52 (calendar . "calendar and time management support")
53 (comm . "communications, networking, remote access to files")
54 (data . "support editing files of data")
55 (docs . "support for Emacs documentation")
56 (emulations . "emulations of other editors")
57 (extensions . "Emacs Lisp language extensions")
58 (faces . "support for multiple fonts")
59 (frames . "support for Emacs frames and window systems")
60 (games . "games, jokes and amusements")
61 (hardware . "support for interfacing with exotic hardware")
62 (help . "support for on-line help systems")
63 (hypermedia . "support for links between text or other media types")
64 (i18n . "internationalization and alternate character-set support")
65 (internal . "code for Emacs internals, build process, defaults")
66 (languages . "specialized modes for editing programming languages")
67 (lisp . "Lisp support, including Emacs Lisp")
68 (local . "code local to your site")
69 (maint . "maintenance aids for the Emacs development group")
70 (mail . "modes for electronic-mail handling")
71 (matching . "various sorts of searching and matching")
72 (mouse . "mouse support")
73 (news . "support for netnews reading and posting")
74 (oop . "support for object-oriented programming")
75 (outlines . "support for hierarchical outlining")
76 (processes . "process, subshell, compilation, and job control support")
77 (terminals . "support for terminal types")
78 (tex . "code related to the TeX formatter")
79 (tools . "programming tools")
80 (unix . "front-ends/assistants for, or emulators of, UNIX features")
81 (vms . "support code for vms")
82 (wp . "word processing")
85 (defvar finder-mode-map nil)
86 (or finder-mode-map
87 (let ((map (make-sparse-keymap)))
88 (define-key map " " 'finder-select)
89 (define-key map "f" 'finder-select)
90 (define-key map [mouse-2] 'finder-mouse-select)
91 (define-key map "\C-m" 'finder-select)
92 (define-key map "?" 'finder-summary)
93 (define-key map "q" 'finder-exit)
94 (define-key map "d" 'finder-list-keywords)
95 (setq finder-mode-map map)))
98 ;;; Code for regenerating the keyword list.
100 (defvar finder-package-info nil
101 "Assoc list mapping file names to description & keyword lists.")
103 (defun finder-compile-keywords (&rest dirs)
104 "Regenerate the keywords association list into the file `finder-inf.el'.
105 Optional arguments are a list of Emacs Lisp directories to compile from; no
106 arguments compiles from `load-path'."
107 (save-excursion
108 (let ((processed nil))
109 (find-file "finder-inf.el")
110 (erase-buffer)
111 (insert ";;; finder-inf.el --- keyword-to-package mapping\n")
112 (insert ";; Keywords: help\n")
113 (insert ";;; Commentary:\n")
114 (insert ";; Don't edit this file. It's generated by finder.el\n\n")
115 (insert ";;; Code:\n")
116 (insert "\n(setq finder-package-info '(\n")
117 (mapcar
118 (lambda (d)
119 (when (file-exists-p (directory-file-name d))
120 (message "Directory %s" d)
121 (mapcar
122 (lambda (f)
123 (if (and (or (string-match "^[^=].*\\.el$" f)
124 ;; Allow compressed files also. Fixme:
125 ;; generalize this, especially for
126 ;; MS-DOG-type filenames.
127 (and (string-match "^[^=].*\\.el\\.\\(gz\\|Z\\)$" f)
128 (require 'jka-compr)))
129 ;; Ignore lock files.
130 (not (string-match "^.#" f))
131 (not (member f processed)))
132 (let (summary keystart keywords)
133 (setq processed (cons f processed))
134 (save-excursion
135 (set-buffer (get-buffer-create "*finder-scratch*"))
136 (buffer-disable-undo (current-buffer))
137 (erase-buffer)
138 (insert-file-contents
139 (concat (file-name-as-directory (or d ".")) f))
140 (setq summary (lm-synopsis))
141 (setq keywords (lm-keywords)))
142 (insert
143 (format " (\"%s\"\n "
144 (if (string-match "\\.\\(gz\\|Z\\)$" f)
145 (file-name-sans-extension f)
146 f)))
147 (prin1 summary (current-buffer))
148 (insert
149 "\n ")
150 (setq keystart (point))
151 (insert
152 (if keywords (format "(%s)" keywords) "nil")
153 ")\n")
154 (subst-char-in-region keystart (point) ?, ? )
156 (directory-files (or d ".")))))
157 (or dirs load-path))
158 (insert "))\n\n(provide 'finder-inf)\n\n;;; finder-inf.el ends here\n")
159 (kill-buffer "*finder-scratch*")
160 (eval-current-buffer) ;; So we get the new keyword list immediately
161 (basic-save-buffer))))
163 (defun finder-compile-keywords-make-dist ()
164 "Regenerate `finder-inf.el' for the Emacs distribution."
165 (apply 'finder-compile-keywords command-line-args-left)
166 (kill-emacs))
168 ;;; Now the retrieval code
170 (defun finder-insert-at-column (column &rest strings)
171 "Insert, at column COLUMN, other args STRINGS."
172 (if (> (current-column) column) (insert "\n"))
173 (move-to-column column t)
174 (apply 'insert strings))
176 (defun finder-mouse-face-on-line ()
177 "Put a `mouse-face' property on the previous line."
178 (save-excursion
179 (previous-line 1)
180 (put-text-property (save-excursion (beginning-of-line) (point))
181 (progn (end-of-line) (point))
182 'mouse-face 'highlight)))
184 (defun finder-list-keywords ()
185 "Display descriptions of the keywords in the Finder buffer."
186 (interactive)
187 (if (get-buffer "*Finder*")
188 (pop-to-buffer "*Finder*")
189 (pop-to-buffer (set-buffer (get-buffer-create "*Finder*")))
190 (finder-mode)
191 (setq buffer-read-only nil)
192 (erase-buffer)
193 (mapcar
194 (lambda (assoc)
195 (let ((keyword (car assoc)))
196 (insert (symbol-name keyword))
197 (finder-insert-at-column 14 (concat (cdr assoc) "\n"))
198 (finder-mouse-face-on-line)))
199 finder-known-keywords)
200 (goto-char (point-min))
201 (setq finder-headmark (point))
202 (setq buffer-read-only t)
203 (set-buffer-modified-p nil)
204 (balance-windows)
205 (finder-summary)))
207 (defun finder-list-matches (key)
208 (pop-to-buffer (set-buffer (get-buffer-create "*Finder Category*")))
209 (finder-mode)
210 (setq buffer-read-only nil)
211 (erase-buffer)
212 (let ((id (intern key)))
213 (insert
214 "The following packages match the keyword `" key "':\n\n")
215 (setq finder-headmark (point))
216 (mapcar
217 (lambda (x)
218 (if (memq id (car (cdr (cdr x))))
219 (progn
220 (insert (car x))
221 (finder-insert-at-column 16 (concat (nth 1 x) "\n"))
222 (finder-mouse-face-on-line))))
223 finder-package-info)
224 (goto-char (point-min))
225 (forward-line)
226 (setq buffer-read-only t)
227 (set-buffer-modified-p nil)
228 (shrink-window-if-larger-than-buffer)
229 (finder-summary)))
231 ;; Search for a file named FILE on `load-path', also trying compressed
232 ;; versions if jka-compr is in use.
233 (defun finder-find-library (library)
234 (or (locate-library library t)
235 (if (rassq 'jka-compr-handler file-name-handler-alist)
236 (or (locate-library (concat library ".gz") t)
237 (locate-library (concat library ".Z") t)
238 ;; last resort for MS-DOG et al
239 (locate-library (concat library "z"))))))
241 (defun finder-commentary (file)
242 "Display FILE's commentary section."
243 (interactive)
244 (let* ((str (lm-commentary (finder-find-library file))))
245 (if (null str)
246 (error "Can't find any Commentary section"))
247 (pop-to-buffer "*Finder*")
248 (setq buffer-read-only nil)
249 (erase-buffer)
250 (insert str)
251 (goto-char (point-min))
252 (delete-blank-lines)
253 (goto-char (point-max))
254 (delete-blank-lines)
255 (goto-char (point-min))
256 (while (re-search-forward "^;+ ?" nil t)
257 (replace-match "" nil nil))
258 (goto-char (point-min))
259 (setq buffer-read-only t)
260 (set-buffer-modified-p nil)
261 (shrink-window-if-larger-than-buffer)
262 (finder-summary)))
264 (defun finder-current-item ()
265 (if (and finder-headmark (< (point) finder-headmark))
266 (error "No keyword or filename on this line")
267 (save-excursion
268 (beginning-of-line)
269 (current-word))))
271 (defun finder-select ()
272 "Select item on current line in a finder buffer."
273 (interactive)
274 (let ((key (finder-current-item)))
275 (if (string-match "\\.el$" key)
276 (finder-commentary key)
277 (finder-list-matches key))))
279 (defun finder-mouse-select (event)
280 "Select item in a finder buffer with the mouse."
281 (interactive "e")
282 (save-excursion
283 (set-buffer (window-buffer (posn-window (event-start event))))
284 (goto-char (posn-point (event-start event)))
285 (finder-select)))
287 (defun finder-by-keyword ()
288 "Find packages matching a given keyword."
289 (interactive)
290 (finder-list-keywords))
292 (defun finder-mode ()
293 "Major mode for browsing package documentation.
294 \\<finder-mode-map>
295 \\[finder-select] more help for the item on the current line
296 \\[finder-exit] exit Finder mode and kill the Finder buffer.
298 (interactive)
299 (use-local-map finder-mode-map)
300 (set-syntax-table emacs-lisp-mode-syntax-table)
301 (setq mode-name "Finder")
302 (setq major-mode 'finder-mode)
303 (make-local-variable 'finder-headmark)
304 (setq finder-headmark nil))
306 (defun finder-summary ()
307 "Summarize basic Finder commands."
308 (interactive)
309 (message "%s"
310 (substitute-command-keys
311 "\\<finder-mode-map>\\[finder-select] = select, \
312 \\[finder-mouse-select] = select, \\[finder-list-keywords] = to \
313 finder directory, \\[finder-exit] = quit, \\[finder-summary] = help")))
315 (defun finder-exit ()
316 "Exit Finder mode and kill the buffer."
317 (interactive)
318 (or (one-window-p t)
319 (delete-window))
320 ;; Can happen in either buffer -- kill each of the two that exists
321 (and (get-buffer "*Finder*")
322 (kill-buffer "*Finder*"))
323 (and (get-buffer "*Finder Category*")
324 (kill-buffer "*Finder Category*")))
326 (provide 'finder)
328 ;;; finder.el ends here