(dired-show-file-type): Format filename with "%s" to escape any format-like
[bpt/emacs.git] / 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 ";; Keywords: help\n")
124 (insert ";;; Commentary:\n")
125 (insert ";; Don't edit this file. It's generated by finder.el\n\n")
126 (insert ";;; Code:\n")
127 (insert "\n(setq finder-package-info '(\n")
128 (mapcar
129 (lambda (d)
130 (when (file-exists-p (directory-file-name d))
131 (message "Directory %s" d)
132 (mapcar
133 (lambda (f)
134 (if (and (or (string-match "^[^=].*\\.el$" f)
135 ;; Allow compressed files also. Fixme:
136 ;; generalize this, especially for
137 ;; MS-DOG-type filenames.
138 (and (string-match "^[^=].*\\.el\\.\\(gz\\|Z\\)$" f)
139 (require 'jka-compr)))
140 ;; Ignore lock files.
141 (not (string-match "^.#" f))
142 (not (member f processed)))
143 (let (summary keystart keywords)
144 (setq processed (cons f processed))
145 (save-excursion
146 (set-buffer (get-buffer-create "*finder-scratch*"))
147 (buffer-disable-undo (current-buffer))
148 (erase-buffer)
149 (insert-file-contents
150 (concat (file-name-as-directory (or d ".")) f))
151 (setq summary (lm-synopsis))
152 (setq keywords (lm-keywords)))
153 (insert
154 (format " (\"%s\"\n "
155 (if (string-match "\\.\\(gz\\|Z\\)$" f)
156 (file-name-sans-extension f)
157 f)))
158 (prin1 summary (current-buffer))
159 (insert
160 "\n ")
161 (setq keystart (point))
162 (insert
163 (if keywords (format "(%s)" keywords) "nil")
164 ")\n")
165 (subst-char-in-region keystart (point) ?, ? )
166 )))
167 (directory-files (or d ".")))))
168 (or dirs load-path))
169 (insert "))\n
170 \(provide 'finder-inf)
171
172 ;;; Local Variables:
173 ;;; version-control: never
174 ;;; no-byte-compile: t
175 ;;; no-update-autoloads: t
176 ;;; End:
177 ;;; finder-inf.el ends here\n")
178 (kill-buffer "*finder-scratch*")
179 (eval-current-buffer) ;; So we get the new keyword list immediately
180 (basic-save-buffer))))
181
182 (defun finder-compile-keywords-make-dist ()
183 "Regenerate `finder-inf.el' for the Emacs distribution."
184 (apply 'finder-compile-keywords command-line-args-left)
185 (kill-emacs))
186
187 ;;; Now the retrieval code
188
189 (defun finder-insert-at-column (column &rest strings)
190 "Insert, at column COLUMN, other args STRINGS."
191 (if (>= (current-column) column) (insert "\n"))
192 (move-to-column column t)
193 (apply 'insert strings))
194
195 (defvar finder-help-echo nil)
196
197 (defun finder-mouse-face-on-line ()
198 "Put `mouse-face' and `help-echo' properties on the previous line."
199 (save-excursion
200 (previous-line 1)
201 (unless finder-help-echo
202 (setq finder-help-echo
203 (let* ((keys1 (where-is-internal 'finder-select
204 finder-mode-map))
205 (keys (nconc (where-is-internal
206 'finder-mouse-select finder-mode-map)
207 keys1)))
208 (concat (mapconcat 'key-description keys ", ")
209 ": select item"))))
210 (add-text-properties
211 (line-beginning-position) (line-end-position)
212 '(mouse-face highlight
213 help-echo finder-help-echo))))
214
215 ;;;###autoload
216 (defun finder-list-keywords ()
217 "Display descriptions of the keywords in the Finder buffer."
218 (interactive)
219 (if (get-buffer "*Finder*")
220 (pop-to-buffer "*Finder*")
221 (pop-to-buffer (set-buffer (get-buffer-create "*Finder*")))
222 (finder-mode)
223 (setq buffer-read-only nil)
224 (erase-buffer)
225 (mapc
226 (lambda (assoc)
227 (let ((keyword (car assoc)))
228 (insert (symbol-name keyword))
229 (finder-insert-at-column 14 (concat (cdr assoc) "\n"))
230 (finder-mouse-face-on-line)))
231 finder-known-keywords)
232 (goto-char (point-min))
233 (setq finder-headmark (point))
234 (setq buffer-read-only t)
235 (set-buffer-modified-p nil)
236 (balance-windows)
237 (finder-summary)))
238
239 (defun finder-list-matches (key)
240 (pop-to-buffer (set-buffer (get-buffer-create "*Finder Category*")))
241 (finder-mode)
242 (setq buffer-read-only nil)
243 (erase-buffer)
244 (let ((id (intern key)))
245 (insert
246 "The following packages match the keyword `" key "':\n\n")
247 (setq finder-headmark (point))
248 (mapc
249 (lambda (x)
250 (if (memq id (car (cdr (cdr x))))
251 (progn
252 (insert (car x))
253 (finder-insert-at-column 16 (concat (nth 1 x) "\n"))
254 (finder-mouse-face-on-line))))
255 finder-package-info)
256 (goto-char (point-min))
257 (forward-line)
258 (setq buffer-read-only t)
259 (set-buffer-modified-p nil)
260 (shrink-window-if-larger-than-buffer)
261 (finder-summary)))
262
263 ;;;###autoload
264 (defun finder-commentary (file)
265 "Display FILE's commentary section.
266 FILE should be in a form suitable for passing to `locate-library'."
267 (interactive
268 (list
269 (completing-read "Library name: "
270 'locate-file-completion
271 (cons (or find-function-source-path load-path)
272 (find-library-suffixes)))))
273 (let* ((str (lm-commentary (find-library-name file))))
274 (if (null str)
275 (error "Can't find any Commentary section"))
276 (pop-to-buffer "*Finder*")
277 (setq buffer-read-only nil)
278 (erase-buffer)
279 (insert str)
280 (goto-char (point-min))
281 (delete-blank-lines)
282 (goto-char (point-max))
283 (delete-blank-lines)
284 (goto-char (point-min))
285 (while (re-search-forward "^;+ ?" nil t)
286 (replace-match "" nil nil))
287 (goto-char (point-min))
288 (setq buffer-read-only t)
289 (set-buffer-modified-p nil)
290 (shrink-window-if-larger-than-buffer)
291 (finder-mode)
292 (finder-summary)))
293
294 (defun finder-current-item ()
295 (if (and finder-headmark (< (point) finder-headmark))
296 (error "No keyword or filename on this line")
297 (save-excursion
298 (beginning-of-line)
299 (current-word))))
300
301 (defun finder-select ()
302 "Select item on current line in a finder buffer."
303 (interactive)
304 (let ((key (finder-current-item)))
305 (if (string-match "\\.el$" key)
306 (finder-commentary key)
307 (finder-list-matches key))))
308
309 (defun finder-mouse-select (event)
310 "Select item in a finder buffer with the mouse."
311 (interactive "e")
312 (save-excursion
313 (set-buffer (window-buffer (posn-window (event-start event))))
314 (goto-char (posn-point (event-start event)))
315 (finder-select)))
316
317 ;;;###autoload
318 (defun finder-by-keyword ()
319 "Find packages matching a given keyword."
320 (interactive)
321 (finder-list-keywords))
322
323 (defun finder-mode ()
324 "Major mode for browsing package documentation.
325 \\<finder-mode-map>
326 \\[finder-select] more help for the item on the current line
327 \\[finder-exit] exit Finder mode and kill the Finder buffer."
328 (interactive)
329 (use-local-map finder-mode-map)
330 (set-syntax-table emacs-lisp-mode-syntax-table)
331 (setq mode-name "Finder")
332 (setq major-mode 'finder-mode)
333 (make-local-variable 'finder-headmark)
334 (setq finder-headmark nil))
335
336 (defun finder-summary ()
337 "Summarize basic Finder commands."
338 (interactive)
339 (message "%s"
340 (substitute-command-keys
341 "\\<finder-mode-map>\\[finder-select] = select, \
342 \\[finder-mouse-select] = select, \\[finder-list-keywords] = to \
343 finder directory, \\[finder-exit] = quit, \\[finder-summary] = help")))
344
345 (defun finder-exit ()
346 "Exit Finder mode and kill the buffer."
347 (interactive)
348 (or (one-window-p t)
349 (delete-window))
350 ;; Can happen in either buffer -- kill each of the two that exists
351 (and (get-buffer "*Finder*")
352 (kill-buffer "*Finder*"))
353 (and (get-buffer "*Finder Category*")
354 (kill-buffer "*Finder Category*")))
355
356 \f
357 (provide 'finder)
358
359 ;;; finder.el ends here