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