(vc-cvs-registered): Consider a directory with a CVS subdirectory to be registered.
[emacs.git] / lisp / finder.el
blob2ff4dc9c30bb4fa884e719985f20ca4d8fb42e00
1 ;;; finder.el --- topic & keyword-based code finder
3 ;; Copyright (C) 1992, 1997, 1998, 1999, 2001, 2002, 2003, 2004, 2005,
4 ;; 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 of the License, or
16 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; This mode uses the Keywords library header to provide code-finding
29 ;; services by keyword.
31 ;; Things to do:
32 ;; 1. Support multiple keywords per search. This could be extremely hairy;
33 ;; there doesn't seem to be any way to get completing-read to exit on
34 ;; an EOL with no substring pending, which is what we'd want to end the loop.
35 ;; 2. Search by string in synopsis line?
36 ;; 3. Function to check finder-package-info for unknown keywords.
38 ;;; Code:
40 (require 'lisp-mnt)
41 (require 'find-func) ;for find-library(-suffixes)
42 ;; Use `load' rather than `require' so that it doesn't get loaded
43 ;; during byte-compilation (at which point it might be missing).
44 (load "finder-inf" t t)
46 ;; These are supposed to correspond to top-level customization groups,
47 ;; says rms.
48 (defvar finder-known-keywords
50 (abbrev . "abbreviation handling, typing shortcuts, macros")
51 ;; Too specific:
52 (bib . "code related to the `bib' bibliography processor")
53 (c . "support for the C language and related languages")
54 (calendar . "calendar and time management support")
55 (comm . "communications, networking, remote access to files")
56 (convenience . "convenience features for faster editing")
57 (data . "support for editing files of data")
58 (docs . "support for Emacs documentation")
59 (emulations . "emulations of other editors")
60 (extensions . "Emacs Lisp language extensions")
61 (faces . "support for multiple fonts")
62 (files . "support for editing and manipulating files")
63 (frames . "support for Emacs frames and window systems")
64 (games . "games, jokes and amusements")
65 (hardware . "support for interfacing with exotic hardware")
66 (help . "support for on-line help systems")
67 (hypermedia . "support for links between text or other media types")
68 (i18n . "internationalization and alternate character-set support")
69 (internal . "code for Emacs internals, build process, defaults")
70 (languages . "specialized modes for editing programming languages")
71 (lisp . "Lisp support, including Emacs Lisp")
72 (local . "code local to your site")
73 (maint . "maintenance aids for the Emacs development group")
74 (mail . "modes for electronic-mail handling")
75 (matching . "various sorts of searching and matching")
76 (mouse . "mouse support")
77 (multimedia . "images and sound support")
78 (news . "support for netnews reading and posting")
79 (oop . "support for object-oriented programming")
80 (outlines . "support for hierarchical outlining")
81 (processes . "process, subshell, compilation, and job control support")
82 (terminals . "support for terminal types")
83 (tex . "supporting code for the TeX formatter")
84 (tools . "programming tools")
85 (unix . "front-ends/assistants for, or emulators of, UNIX-like features")
86 ;; Not a custom group and not currently useful.
87 ;; (vms . "support code for vms")
88 (wp . "word processing")
91 (defvar finder-mode-map
92 (let ((map (make-sparse-keymap)))
93 (define-key map " " 'finder-select)
94 (define-key map "f" 'finder-select)
95 (define-key map [follow-link] 'mouse-face)
96 (define-key map [mouse-2] 'finder-mouse-select)
97 (define-key map "\C-m" 'finder-select)
98 (define-key map "?" 'finder-summary)
99 (define-key map "n" 'next-line)
100 (define-key map "p" 'previous-line)
101 (define-key map "q" 'finder-exit)
102 (define-key map "d" 'finder-list-keywords)
103 map))
105 (defvar finder-mode-syntax-table
106 (let ((st (make-syntax-table emacs-lisp-mode-syntax-table)))
107 (modify-syntax-entry ?\; ". " st)
109 "Syntax table used while in `finder-mode'.")
111 (defvar finder-font-lock-keywords
112 '(("`\\([^']+\\)'" 1 font-lock-constant-face prepend))
113 "Font-lock keywords for Finder mode.")
115 (defvar finder-headmark nil
116 "Internal finder-mode variable, local in finder buffer.")
118 ;;; Code for regenerating the keyword list.
120 (defvar finder-package-info nil
121 "Assoc list mapping file names to description & keyword lists.")
123 (defvar generated-finder-keywords-file "finder-inf.el"
124 "The function `finder-compile-keywords' writes keywords into this file.")
126 ;; Skip autogenerated files, because they will never contain anything
127 ;; useful, and because in parallel builds of Emacs they may get
128 ;; modified while we are trying to read them.
129 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-01/msg00469.html
130 (defvar finder-no-scan-regexp "\\(^\\.#\\|\\(loaddefs\\|cus-load\\|\
131 finder-inf\\|esh-groups\\|subdirs\\)\\.el$\\)"
132 "Regexp matching file names not to scan for keywords.")
134 (autoload 'autoload-rubric "autoload")
136 (defun finder-compile-keywords (&rest dirs)
137 "Regenerate the keywords association list into `generated-finder-keywords-file'.
138 Optional arguments DIRS are a list of Emacs Lisp directories to compile from;
139 no arguments compiles from `load-path'."
140 (save-excursion
141 (let (processed summary keystart keywords)
142 (find-file generated-finder-keywords-file)
143 (setq buffer-undo-list t)
144 (erase-buffer)
145 (insert (autoload-rubric generated-finder-keywords-file
146 "keyword-to-package mapping"))
147 (search-backward "\f")
148 (insert "(setq finder-package-info '(\n")
149 (mapc
150 (lambda (d)
151 (when (file-exists-p (directory-file-name d))
152 (message "Directory %s" d)
153 (mapc
154 (lambda (f)
155 ;; FIXME should this not be using (expand-file-name f d)?
156 (unless (or (member f processed)
157 (string-match finder-no-scan-regexp f))
158 (setq processed (cons f processed))
159 (with-temp-buffer
160 (insert-file-contents (expand-file-name f d))
161 (setq summary (lm-synopsis)
162 keywords (lm-keywords)))
163 (insert
164 (format " (\"%s\"\n "
165 (if (string-match "\\.\\(gz\\|Z\\)$" f)
166 (file-name-sans-extension f)
167 f)))
168 (prin1 summary (current-buffer))
169 (insert "\n ")
170 (setq keystart (point))
171 (insert (if keywords (format "(%s)" keywords) "nil")
172 ")\n")
173 (subst-char-in-region keystart (point) ?, ? )))
174 (directory-files d nil
175 ;; Allow compressed files also. FIXME:
176 ;; generalize this, especially for
177 ;; MS-DOG-type filenames.
178 "^[^=].*\\.el\\(\\.\\(gz\\|Z\\)\\)?$"
179 ))))
180 (or dirs load-path))
181 (insert " ))\n")
182 (eval-buffer) ; so we get the new keyword list immediately
183 (basic-save-buffer))))
185 (defun finder-compile-keywords-make-dist ()
186 "Regenerate `finder-inf.el' for the Emacs distribution."
187 (apply 'finder-compile-keywords command-line-args-left)
188 (kill-emacs))
190 ;;; Now the retrieval code
192 (defun finder-insert-at-column (column &rest strings)
193 "Insert, at column COLUMN, other args STRINGS."
194 (if (>= (current-column) column) (insert "\n"))
195 (move-to-column column t)
196 (apply 'insert strings))
198 (defvar finder-help-echo nil)
200 (defun finder-mouse-face-on-line ()
201 "Put `mouse-face' and `help-echo' properties on the previous line."
202 (save-excursion
203 (forward-line -1)
204 (unless finder-help-echo
205 (setq finder-help-echo
206 (let* ((keys1 (where-is-internal 'finder-select
207 finder-mode-map))
208 (keys (nconc (where-is-internal
209 'finder-mouse-select finder-mode-map)
210 keys1)))
211 (concat (mapconcat 'key-description keys ", ")
212 ": select item"))))
213 (add-text-properties
214 (line-beginning-position) (line-end-position)
215 '(mouse-face highlight
216 help-echo finder-help-echo))))
218 ;;;###autoload
219 (defun finder-list-keywords ()
220 "Display descriptions of the keywords in the Finder buffer."
221 (interactive)
222 (if (get-buffer "*Finder*")
223 (pop-to-buffer "*Finder*")
224 (pop-to-buffer (get-buffer-create "*Finder*"))
225 (finder-mode)
226 (setq buffer-read-only nil
227 buffer-undo-list t)
228 (erase-buffer)
229 (mapc
230 (lambda (assoc)
231 (let ((keyword (car assoc)))
232 (insert (symbol-name keyword))
233 (finder-insert-at-column 14 (concat (cdr assoc) "\n"))
234 (finder-mouse-face-on-line)))
235 finder-known-keywords)
236 (goto-char (point-min))
237 (setq finder-headmark (point)
238 buffer-read-only t)
239 (set-buffer-modified-p nil)
240 (balance-windows)
241 (finder-summary)))
243 (defun finder-list-matches (key)
244 (pop-to-buffer (set-buffer (get-buffer-create "*Finder Category*")))
245 (finder-mode)
246 (setq buffer-read-only nil
247 buffer-undo-list t)
248 (erase-buffer)
249 (let ((id (intern key)))
250 (insert
251 "The following packages match the keyword `" key "':\n\n")
252 (setq finder-headmark (point))
253 (mapc
254 (lambda (x)
255 (if (memq id (car (cdr (cdr x))))
256 (progn
257 (insert (car x))
258 (finder-insert-at-column 16 (concat (nth 1 x) "\n"))
259 (finder-mouse-face-on-line))))
260 finder-package-info)
261 (goto-char (point-min))
262 (forward-line)
263 (setq buffer-read-only t)
264 (set-buffer-modified-p nil)
265 (shrink-window-if-larger-than-buffer)
266 (finder-summary)))
268 ;;;###autoload
269 (defun finder-commentary (file)
270 "Display FILE's commentary section.
271 FILE should be in a form suitable for passing to `locate-library'."
272 (interactive
273 (list
274 (completing-read "Library name: "
275 (apply-partially 'locate-file-completion-table
276 (or find-function-source-path load-path)
277 (find-library-suffixes)))))
278 (let ((str (lm-commentary (find-library-name file))))
279 (or str (error "Can't find any Commentary section"))
280 ;; This used to use *Finder* but that would clobber the
281 ;; directory of categories.
282 (delete-other-windows)
283 (pop-to-buffer "*Finder-package*")
284 (setq buffer-read-only nil
285 buffer-undo-list t)
286 (erase-buffer)
287 (insert str)
288 (goto-char (point-min))
289 (delete-blank-lines)
290 (goto-char (point-max))
291 (delete-blank-lines)
292 (goto-char (point-min))
293 (while (re-search-forward "^;+ ?" nil t)
294 (replace-match "" nil nil))
295 (goto-char (point-min))
296 (setq buffer-read-only t)
297 (set-buffer-modified-p nil)
298 (shrink-window-if-larger-than-buffer)
299 (finder-mode)
300 (finder-summary)))
302 (defun finder-current-item ()
303 (let ((key (save-excursion
304 (beginning-of-line)
305 (current-word))))
306 (if (or (and finder-headmark (< (point) finder-headmark))
307 (zerop (length key)))
308 (error "No keyword or filename on this line")
309 key)))
311 (defun finder-select ()
312 "Select item on current line in a finder buffer."
313 (interactive)
314 (let ((key (finder-current-item)))
315 (if (string-match "\\.el$" key)
316 (finder-commentary key)
317 (finder-list-matches key))))
319 (defun finder-mouse-select (event)
320 "Select item in a finder buffer with the mouse."
321 (interactive "e")
322 (save-excursion
323 (set-buffer (window-buffer (posn-window (event-start event))))
324 (goto-char (posn-point (event-start event)))
325 (finder-select)))
327 ;;;###autoload
328 (defun finder-by-keyword ()
329 "Find packages matching a given keyword."
330 (interactive)
331 (finder-list-keywords))
333 (define-derived-mode finder-mode nil "Finder"
334 "Major mode for browsing package documentation.
335 \\<finder-mode-map>
336 \\[finder-select] more help for the item on the current line
337 \\[finder-exit] exit Finder mode and kill the Finder buffer."
338 :syntax-table finder-mode-syntax-table
339 (setq font-lock-defaults '(finder-font-lock-keywords nil nil
340 (("+-*/.<>=!?$%_&~^:@" . "w")) nil))
341 (set (make-local-variable 'finder-headmark) nil))
343 (defun finder-summary ()
344 "Summarize basic Finder commands."
345 (interactive)
346 (message "%s"
347 (substitute-command-keys
348 "\\<finder-mode-map>\\[finder-select] = select, \
349 \\[finder-mouse-select] = select, \\[finder-list-keywords] = to \
350 finder directory, \\[finder-exit] = quit, \\[finder-summary] = help")))
352 (defun finder-exit ()
353 "Exit Finder mode.
354 Delete the window and kill all Finder-related buffers."
355 (interactive)
356 (ignore-errors (delete-window))
357 (dolist (buff '("*Finder*" "*Finder-package*" "*Finder Category*"))
358 (and (get-buffer buff) (kill-buffer buff))))
361 (provide 'finder)
363 ;; arch-tag: ec85ff49-8cb8-41f5-a63f-9131d53ce2c5
364 ;;; finder.el ends here