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