(rmail-convert-babyl-to-mbox): Bind coding-system-for-read to `raw-text', and
[emacs.git] / lisp / finder.el
blobdd85d09d6da62319b833fdc4f3bcc0c88a1ef419
1 ;;; finder.el --- topic & keyword-based code finder
3 ;; Copyright (C) 1992, 1997, 1998, 1999, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008, 2009 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 (wp . "word processing")
89 (defvar finder-mode-map
90 (let ((map (make-sparse-keymap)))
91 (define-key map " " 'finder-select)
92 (define-key map "f" 'finder-select)
93 (define-key map [follow-link] 'mouse-face)
94 (define-key map [mouse-2] 'finder-mouse-select)
95 (define-key map "\C-m" 'finder-select)
96 (define-key map "?" 'finder-summary)
97 (define-key map "n" 'next-line)
98 (define-key map "p" 'previous-line)
99 (define-key map "q" 'finder-exit)
100 (define-key map "d" 'finder-list-keywords)
101 map))
103 (defvar finder-mode-syntax-table
104 (let ((st (make-syntax-table emacs-lisp-mode-syntax-table)))
105 (modify-syntax-entry ?\; ". " st)
107 "Syntax table used while in `finder-mode'.")
109 (defvar finder-font-lock-keywords
110 '(("`\\([^'`]+\\)'" 1 font-lock-constant-face prepend))
111 "Font-lock keywords for Finder mode.")
113 (defvar finder-headmark nil
114 "Internal finder-mode variable, local in finder buffer.")
116 ;;; Code for regenerating the keyword list.
118 (defvar finder-package-info nil
119 "Assoc list mapping file names to description & keyword lists.")
121 (defvar generated-finder-keywords-file "finder-inf.el"
122 "The function `finder-compile-keywords' writes keywords into this file.")
124 ;; Skip autogenerated files, because they will never contain anything
125 ;; useful, and because in parallel builds of Emacs they may get
126 ;; modified while we are trying to read them.
127 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-01/msg00469.html
128 ;; ldefs-boot is not auto-generated, but has nothing useful.
129 (defvar finder-no-scan-regexp "\\(^\\.#\\|\\(loaddefs\\|ldefs-boot\\|\
130 cus-load\\|finder-inf\\|esh-groups\\|subdirs\\)\\.el$\\)"
131 "Regexp matching file names not to scan for keywords.")
133 (autoload 'autoload-rubric "autoload")
135 (defun finder-compile-keywords (&rest dirs)
136 "Regenerate the keywords association list into `generated-finder-keywords-file'.
137 Optional arguments DIRS are a list of Emacs Lisp directories to compile from;
138 no arguments compiles from `load-path'."
139 (save-excursion
140 (find-file generated-finder-keywords-file)
141 (setq buffer-undo-list t)
142 (erase-buffer)
143 (insert (autoload-rubric generated-finder-keywords-file
144 "keyword-to-package mapping"))
145 (search-backward "\f")
146 (insert "(setq finder-package-info '(\n")
147 (let (processed summary keywords)
148 (mapc
149 (lambda (d)
150 (when (file-exists-p (directory-file-name d))
151 (message "Directory %s" d)
152 (mapc
153 (lambda (f)
154 ;; FIXME should this not be using (expand-file-name f d)?
155 (unless (or (member f processed)
156 (string-match finder-no-scan-regexp f))
157 (setq processed (cons f processed))
158 (with-temp-buffer
159 (insert-file-contents (expand-file-name f d))
160 (setq summary (lm-synopsis)
161 keywords (lm-keywords-list)))
162 (insert
163 (format " (\"%s\"\n "
164 (if (string-match "\\.\\(gz\\|Z\\)$" f)
165 (file-name-sans-extension f)
166 f)))
167 (prin1 summary (current-buffer))
168 (insert "\n ")
169 (princ keywords (current-buffer))
170 (insert ")\n")))
171 (directory-files d nil
172 ;; Allow compressed files also. FIXME:
173 ;; generalize this, especially for
174 ;; MS-DOG-type filenames.
175 "^[^=].*\\.el\\(\\.\\(gz\\|Z\\)\\)?$"
176 ))))
177 (or dirs load-path)))
178 (insert " ))\n")
179 (eval-buffer) ; so we get the new keyword list immediately
180 (basic-save-buffer)))
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))
187 ;;; Now the retrieval code
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))
195 (defvar finder-help-echo nil)
197 (defun finder-mouse-face-on-line ()
198 "Put `mouse-face' and `help-echo' properties on the previous line."
199 (save-excursion
200 (forward-line -1)
201 ;; If finder-insert-at-column moved us to a new line, go back one more.
202 (if (looking-at "[ \t]") (forward-line -1))
203 (unless finder-help-echo
204 (setq finder-help-echo
205 (let* ((keys1 (where-is-internal 'finder-select
206 finder-mode-map))
207 (keys (nconc (where-is-internal
208 'finder-mouse-select finder-mode-map)
209 keys1)))
210 (concat (mapconcat 'key-description keys ", ")
211 ": select item"))))
212 (add-text-properties
213 (line-beginning-position) (line-end-position)
214 '(mouse-face highlight
215 help-echo finder-help-echo))))
217 ;;;###autoload
218 (defun finder-list-keywords ()
219 "Display descriptions of the keywords in the Finder buffer."
220 (interactive)
221 (if (get-buffer "*Finder*")
222 (pop-to-buffer "*Finder*")
223 (pop-to-buffer (get-buffer-create "*Finder*"))
224 (finder-mode)
225 (setq buffer-read-only nil
226 buffer-undo-list t)
227 (erase-buffer)
228 (mapc
229 (lambda (assoc)
230 (let ((keyword (car assoc)))
231 (insert (symbol-name keyword))
232 (finder-insert-at-column 14 (concat (cdr assoc) "\n"))
233 (finder-mouse-face-on-line)))
234 finder-known-keywords)
235 (goto-char (point-min))
236 (setq finder-headmark (point)
237 buffer-read-only t)
238 (set-buffer-modified-p nil)
239 (balance-windows)
240 (finder-summary)))
242 (defun finder-list-matches (key)
243 (pop-to-buffer (set-buffer (get-buffer-create "*Finder Category*")))
244 (finder-mode)
245 (setq buffer-read-only nil
246 buffer-undo-list t)
247 (erase-buffer)
248 (let ((id (intern key)))
249 (insert
250 "The following packages match the keyword `" key "':\n\n")
251 (setq finder-headmark (point))
252 (mapc
253 (lambda (x)
254 (when (memq id (cadr (cdr x)))
255 (insert (car x))
256 (finder-insert-at-column 16 (concat (cadr x) "\n"))
257 (finder-mouse-face-on-line)))
258 finder-package-info)
259 (goto-char (point-min))
260 (forward-line)
261 (setq buffer-read-only t)
262 (set-buffer-modified-p nil)
263 (shrink-window-if-larger-than-buffer)
264 (finder-summary)))
266 (define-button-type 'finder-xref 'action #'finder-goto-xref)
268 (defun finder-goto-xref (button)
269 "Jump to a lisp file for the BUTTON at point."
270 (let* ((file (button-get button 'xref))
271 (lib (locate-library file)))
272 (if lib (finder-commentary lib)
273 (message "Unable to locate `%s'" file))))
275 ;;;###autoload
276 (defun finder-commentary (file)
277 "Display FILE's commentary section.
278 FILE should be in a form suitable for passing to `locate-library'."
279 (interactive
280 (list
281 (completing-read "Library name: "
282 (apply-partially 'locate-file-completion-table
283 (or find-function-source-path load-path)
284 (find-library-suffixes)))))
285 (let ((str (lm-commentary (find-library-name file))))
286 (or str (error "Can't find any Commentary section"))
287 ;; This used to use *Finder* but that would clobber the
288 ;; directory of categories.
289 (delete-other-windows)
290 (pop-to-buffer "*Finder-package*")
291 (setq buffer-read-only nil
292 buffer-undo-list t)
293 (erase-buffer)
294 (insert str)
295 (goto-char (point-min))
296 (delete-blank-lines)
297 (goto-char (point-max))
298 (delete-blank-lines)
299 (goto-char (point-min))
300 (while (re-search-forward "^;+ ?" nil t)
301 (replace-match "" nil nil))
302 (goto-char (point-min))
303 (while (re-search-forward "\\<\\([-[:alnum:]]+\\.el\\)\\>" nil t)
304 (if (locate-library (match-string 1))
305 (make-text-button (match-beginning 1) (match-end 1)
306 'xref (match-string-no-properties 1)
307 'help-echo "Read this file's commentary"
308 :type 'finder-xref)))
309 (goto-char (point-min))
310 (setq buffer-read-only t)
311 (set-buffer-modified-p nil)
312 (shrink-window-if-larger-than-buffer)
313 (finder-mode)
314 (finder-summary)))
316 (defun finder-current-item ()
317 (let ((key (save-excursion
318 (beginning-of-line)
319 (current-word))))
320 (if (or (and finder-headmark (< (point) finder-headmark))
321 (zerop (length key)))
322 (error "No keyword or filename on this line")
323 key)))
325 (defun finder-select ()
326 "Select item on current line in a finder buffer."
327 (interactive)
328 (let ((key (finder-current-item)))
329 (if (string-match "\\.el$" key)
330 (finder-commentary key)
331 (finder-list-matches key))))
333 (defun finder-mouse-select (event)
334 "Select item in a finder buffer with the mouse."
335 (interactive "e")
336 (save-excursion
337 (set-buffer (window-buffer (posn-window (event-start event))))
338 (goto-char (posn-point (event-start event)))
339 (finder-select)))
341 ;;;###autoload
342 (defun finder-by-keyword ()
343 "Find packages matching a given keyword."
344 (interactive)
345 (finder-list-keywords))
347 (define-derived-mode finder-mode nil "Finder"
348 "Major mode for browsing package documentation.
349 \\<finder-mode-map>
350 \\[finder-select] more help for the item on the current line
351 \\[finder-exit] exit Finder mode and kill the Finder buffer."
352 :syntax-table finder-mode-syntax-table
353 (setq font-lock-defaults '(finder-font-lock-keywords nil nil
354 (("+-*/.<>=!?$%_&~^:@" . "w")) nil))
355 (set (make-local-variable 'finder-headmark) nil))
357 (defun finder-summary ()
358 "Summarize basic Finder commands."
359 (interactive)
360 (message "%s"
361 (substitute-command-keys
362 "\\<finder-mode-map>\\[finder-select] = select, \
363 \\[finder-mouse-select] = select, \\[finder-list-keywords] = to \
364 finder directory, \\[finder-exit] = quit, \\[finder-summary] = help")))
366 (defun finder-exit ()
367 "Exit Finder mode.
368 Delete the window and kill all Finder-related buffers."
369 (interactive)
370 (ignore-errors (delete-window))
371 (dolist (buff '("*Finder*" "*Finder-package*" "*Finder Category*"))
372 (and (get-buffer buff) (kill-buffer buff))))
375 (provide 'finder)
377 ;; arch-tag: ec85ff49-8cb8-41f5-a63f-9131d53ce2c5
378 ;;; finder.el ends here