(diary-face-attrs): Fix type of `width'.
[emacs.git] / lisp / finder.el
blobe5a41ca7e4621dedad6269fc3e00110c3888d85a
1 ;;; finder.el --- topic & keyword-based code finder
3 ;; Copyright (C) 1992, 1997, 1998, 1999, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
7 ;; Created: 16 Jun 1992
8 ;; Version: 1.0
9 ;; Keywords: help
11 ;; This file is part of GNU Emacs.
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 3, or (at your option)
16 ;; any later version.
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.
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.
28 ;;; Commentary:
30 ;; This mode uses the Keywords library header to provide code-finding
31 ;; services by keyword.
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.
40 ;;; Code:
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)
48 (defvar finder-mode-hook nil
49 "*Hook run when function `finder-mode' is called.")
51 ;; Local variable in finder buffer.
52 (defvar finder-headmark)
54 ;; These are supposed to correspond to top-level customization groups,
55 ;; says rms.
56 (defvar finder-known-keywords
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")
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 "n" 'next-line)
108 (define-key map "p" 'previous-line)
109 (define-key map "q" 'finder-exit)
110 (define-key map "d" 'finder-list-keywords)
111 map))
113 (defvar finder-mode-syntax-table
114 (let ((st (make-syntax-table emacs-lisp-mode-syntax-table)))
115 (modify-syntax-entry ?\; ". " st)
117 "Syntax table used while in `finder-mode'.")
119 (defvar finder-font-lock-keywords
120 '(("`\\([^']+\\)'" 1 font-lock-constant-face prepend))
121 "Font-lock keywords for Finder mode.")
124 ;;; Code for regenerating the keyword list.
126 (defvar finder-package-info nil
127 "Assoc list mapping file names to description & keyword lists.")
129 (defvar generated-finder-keywords-file "finder-inf.el"
130 "File \\[finder-compile-keywords] puts finder keywords into.")
132 (defun finder-compile-keywords (&rest dirs)
133 "Regenerate the keywords association list into `generated-finder-keywords-file'.
134 Optional arguments DIRS are a list of Emacs Lisp directories to compile from;
135 no arguments compiles from `load-path'."
136 (save-excursion
137 (let ((processed nil))
138 (find-file generated-finder-keywords-file)
139 (erase-buffer)
140 (insert ";;; " (file-name-nondirectory generated-finder-keywords-file)
141 " --- keyword-to-package mapping\n")
142 (insert ";; This file is part of GNU Emacs.\n")
143 (insert ";;; Commentary:\n")
144 (insert ";; Don't edit this file. It's generated by finder.el\n\n")
145 (insert ";;; Code:\n")
146 (insert "\n(setq finder-package-info '(\n")
147 (mapc
148 (lambda (d)
149 (when (file-exists-p (directory-file-name d))
150 (message "Directory %s" d)
151 (mapcar
152 (lambda (f)
153 (if (and (or (string-match "^[^=].*\\.el$" f)
154 ;; Allow compressed files also. Fixme:
155 ;; generalize this, especially for
156 ;; MS-DOG-type filenames.
157 (and (string-match "^[^=].*\\.el\\.\\(gz\\|Z\\)$" f)
158 (require 'jka-compr)))
159 ;; Ignore lock files.
160 (not (string-match "^.#" f))
161 (not (member f processed)))
162 (let (summary keystart keywords)
163 (setq processed (cons f processed))
164 (save-excursion
165 (set-buffer (get-buffer-create "*finder-scratch*"))
166 (buffer-disable-undo (current-buffer))
167 (erase-buffer)
168 (insert-file-contents
169 (concat (file-name-as-directory (or d ".")) f))
170 (setq summary (lm-synopsis))
171 (setq keywords (lm-keywords)))
172 (insert
173 (format " (\"%s\"\n "
174 (if (string-match "\\.\\(gz\\|Z\\)$" f)
175 (file-name-sans-extension f)
176 f)))
177 (prin1 summary (current-buffer))
178 (insert
179 "\n ")
180 (setq keystart (point))
181 (insert
182 (if keywords (format "(%s)" keywords) "nil")
183 ")\n")
184 (subst-char-in-region keystart (point) ?, ? )
186 (directory-files (or d ".")))))
187 (or dirs load-path))
188 (insert "))\n
189 \(provide '" (file-name-sans-extension
190 (file-name-nondirectory generated-finder-keywords-file)) ")
192 ;; Local Variables:
193 ;; version-control: never
194 ;; no-byte-compile: t
195 ;; no-update-autoloads: t
196 ;; End:
197 \;;; " (file-name-nondirectory generated-finder-keywords-file) " ends here\n")
198 (kill-buffer "*finder-scratch*")
199 (eval-buffer) ;; So we get the new keyword list immediately
200 (basic-save-buffer))))
202 (defun finder-compile-keywords-make-dist ()
203 "Regenerate `finder-inf.el' for the Emacs distribution."
204 (apply 'finder-compile-keywords command-line-args-left)
205 (kill-emacs))
207 ;;; Now the retrieval code
209 (defun finder-insert-at-column (column &rest strings)
210 "Insert, at column COLUMN, other args STRINGS."
211 (if (>= (current-column) column) (insert "\n"))
212 (move-to-column column t)
213 (apply 'insert strings))
215 (defvar finder-help-echo nil)
217 (defun finder-mouse-face-on-line ()
218 "Put `mouse-face' and `help-echo' properties on the previous line."
219 (save-excursion
220 (forward-line -1)
221 (unless finder-help-echo
222 (setq finder-help-echo
223 (let* ((keys1 (where-is-internal 'finder-select
224 finder-mode-map))
225 (keys (nconc (where-is-internal
226 'finder-mouse-select finder-mode-map)
227 keys1)))
228 (concat (mapconcat 'key-description keys ", ")
229 ": select item"))))
230 (add-text-properties
231 (line-beginning-position) (line-end-position)
232 '(mouse-face highlight
233 help-echo finder-help-echo))))
235 ;;;###autoload
236 (defun finder-list-keywords ()
237 "Display descriptions of the keywords in the Finder buffer."
238 (interactive)
239 (if (get-buffer "*Finder*")
240 (pop-to-buffer "*Finder*")
241 (pop-to-buffer (set-buffer (get-buffer-create "*Finder*")))
242 (finder-mode)
243 (setq buffer-read-only nil)
244 (erase-buffer)
245 (mapc
246 (lambda (assoc)
247 (let ((keyword (car assoc)))
248 (insert (symbol-name keyword))
249 (finder-insert-at-column 14 (concat (cdr assoc) "\n"))
250 (finder-mouse-face-on-line)))
251 finder-known-keywords)
252 (goto-char (point-min))
253 (setq finder-headmark (point))
254 (setq buffer-read-only t)
255 (set-buffer-modified-p nil)
256 (balance-windows)
257 (finder-summary)))
259 (defun finder-list-matches (key)
260 (pop-to-buffer (set-buffer (get-buffer-create "*Finder Category*")))
261 (finder-mode)
262 (setq buffer-read-only nil)
263 (erase-buffer)
264 (let ((id (intern key)))
265 (insert
266 "The following packages match the keyword `" key "':\n\n")
267 (setq finder-headmark (point))
268 (mapc
269 (lambda (x)
270 (if (memq id (car (cdr (cdr x))))
271 (progn
272 (insert (car x))
273 (finder-insert-at-column 16 (concat (nth 1 x) "\n"))
274 (finder-mouse-face-on-line))))
275 finder-package-info)
276 (goto-char (point-min))
277 (forward-line)
278 (setq buffer-read-only t)
279 (set-buffer-modified-p nil)
280 (shrink-window-if-larger-than-buffer)
281 (finder-summary)))
283 ;;;###autoload
284 (defun finder-commentary (file)
285 "Display FILE's commentary section.
286 FILE should be in a form suitable for passing to `locate-library'."
287 (interactive
288 (list
289 (completing-read "Library name: "
290 'locate-file-completion
291 (cons (or find-function-source-path load-path)
292 (find-library-suffixes)))))
293 (let* ((str (lm-commentary (find-library-name file))))
294 (if (null str)
295 (error "Can't find any Commentary section"))
296 ;; This used to use *Finder* but that would clobber the
297 ;; directory of categories.
298 (delete-other-windows)
299 (pop-to-buffer "*Finder-package*")
300 (setq buffer-read-only nil)
301 (erase-buffer)
302 (insert str)
303 (goto-char (point-min))
304 (delete-blank-lines)
305 (goto-char (point-max))
306 (delete-blank-lines)
307 (goto-char (point-min))
308 (while (re-search-forward "^;+ ?" nil t)
309 (replace-match "" nil nil))
310 (goto-char (point-min))
311 (setq buffer-read-only t)
312 (set-buffer-modified-p nil)
313 (shrink-window-if-larger-than-buffer)
314 (finder-mode)
315 (finder-summary)))
317 (defun finder-current-item ()
318 (let ((key (save-excursion
319 (beginning-of-line)
320 (current-word))))
321 (if (or (and finder-headmark (< (point) finder-headmark))
322 (= (length key) 0))
323 (error "No keyword or filename on this line")
324 key)))
326 (defun finder-select ()
327 "Select item on current line in a finder buffer."
328 (interactive)
329 (let ((key (finder-current-item)))
330 (if (string-match "\\.el$" key)
331 (finder-commentary key)
332 (finder-list-matches key))))
334 (defun finder-mouse-select (event)
335 "Select item in a finder buffer with the mouse."
336 (interactive "e")
337 (save-excursion
338 (set-buffer (window-buffer (posn-window (event-start event))))
339 (goto-char (posn-point (event-start event)))
340 (finder-select)))
342 ;;;###autoload
343 (defun finder-by-keyword ()
344 "Find packages matching a given keyword."
345 (interactive)
346 (finder-list-keywords))
348 (defun finder-mode ()
349 "Major mode for browsing package documentation.
350 \\<finder-mode-map>
351 \\[finder-select] more help for the item on the current line
352 \\[finder-exit] exit Finder mode and kill the Finder buffer."
353 (interactive)
354 (kill-all-local-variables)
355 (use-local-map finder-mode-map)
356 (set-syntax-table finder-mode-syntax-table)
357 (setq font-lock-defaults '(finder-font-lock-keywords nil nil
358 (("+-*/.<>=!?$%_&~^:@" . "w")) nil))
359 (setq mode-name "Finder")
360 (setq major-mode 'finder-mode)
361 (set (make-local-variable 'finder-headmark) nil)
362 (run-mode-hooks 'finder-mode-hook))
364 (defun finder-summary ()
365 "Summarize basic Finder commands."
366 (interactive)
367 (message "%s"
368 (substitute-command-keys
369 "\\<finder-mode-map>\\[finder-select] = select, \
370 \\[finder-mouse-select] = select, \\[finder-list-keywords] = to \
371 finder directory, \\[finder-exit] = quit, \\[finder-summary] = help")))
373 (defun finder-exit ()
374 "Exit Finder mode.
375 Delete the window and kill the buffer."
376 (interactive)
377 (condition-case nil (delete-window) (error nil))
378 (when (get-buffer "*Finder*") (kill-buffer "*Finder*"))
379 (when (get-buffer "*Finder-package*") (kill-buffer "*Finder-package*"))
380 (when (get-buffer "*Finder Category*") (kill-buffer "*Finder Category*")))
383 (provide 'finder)
385 ;; arch-tag: ec85ff49-8cb8-41f5-a63f-9131d53ce2c5
386 ;;; finder.el ends here