Set encoding to utf-8.
[emacs.git] / lisp / finder.el
blob56a13c09a371534f559d3b1b4767d8668c98d54a
1 ;;; finder.el --- topic & keyword-based code finder
3 ;; Copyright (C) 1992, 1997, 1998, 1999, 2001 Free Software Foundation, Inc.
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
6 ;; Created: 16 Jun 1992
7 ;; Version: 1.0
8 ;; Keywords: help
10 ;; This file is part of GNU Emacs.
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.
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.
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.
27 ;;; Commentary:
29 ;; This mode uses the Keywords library header to provide code-finding
30 ;; services by keyword.
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.
39 ;;; Code:
41 (require 'lisp-mnt)
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" nil t)
46 ;; Local variable in finder buffer.
47 (defvar finder-headmark)
49 ;; These are supposed to correspond to top-level customization groups,
50 ;; says rms.
51 (defvar finder-known-keywords
53 (abbrev . "abbreviation handling, typing shortcuts, macros")
54 ;; Too specific:
55 (bib . "code related to the `bib' bibliography processor")
56 (c . "support for the C language and related languages")
57 (calendar . "calendar and time management support")
58 (comm . "communications, networking, remote access to files")
59 (convenience . "convenience features for faster editing")
60 (data . "support editing files of data")
61 (docs . "support for Emacs documentation")
62 (emulations . "emulations of other editors")
63 (extensions . "Emacs Lisp language extensions")
64 (faces . "support for multiple fonts")
65 (files . "support for editing and manipulating files")
66 (frames . "support for Emacs frames and window systems")
67 (games . "games, jokes and amusements")
68 (hardware . "support for interfacing with exotic hardware")
69 (help . "support for on-line help systems")
70 (hypermedia . "support for links between text or other media types")
71 (i18n . "internationalization and alternate character-set support")
72 (internal . "code for Emacs internals, build process, defaults")
73 (languages . "specialized modes for editing programming languages")
74 (lisp . "Lisp support, including Emacs Lisp")
75 (local . "code local to your site")
76 (maint . "maintenance aids for the Emacs development group")
77 (mail . "modes for electronic-mail handling")
78 (matching . "various sorts of searching and matching")
79 (mouse . "mouse support")
80 (multimedia . "images and sound support")
81 (news . "support for netnews reading and posting")
82 (oop . "support for object-oriented programming")
83 (outlines . "support for hierarchical outlining")
84 (processes . "process, subshell, compilation, and job control support")
85 (terminals . "support for terminal types")
86 (tex . "code related to the TeX formatter")
87 (tools . "programming tools")
88 (unix . "front-ends/assistants for, or emulators of, UNIX features")
89 ;; Not a custom group and not currently useful.
90 ;; (vms . "support code for vms")
91 (wp . "word processing")
94 (defvar finder-mode-map nil)
95 (or finder-mode-map
96 (let ((map (make-sparse-keymap)))
97 (define-key map " " 'finder-select)
98 (define-key map "f" 'finder-select)
99 (define-key map [mouse-2] 'finder-mouse-select)
100 (define-key map "\C-m" 'finder-select)
101 (define-key map "?" 'finder-summary)
102 (define-key map "q" 'finder-exit)
103 (define-key map "d" 'finder-list-keywords)
104 (setq finder-mode-map map)))
107 ;;; Code for regenerating the keyword list.
109 (defvar finder-package-info nil
110 "Assoc list mapping file names to description & keyword lists.")
112 (defun finder-compile-keywords (&rest dirs)
113 "Regenerate the keywords association list into the file `finder-inf.el'.
114 Optional arguments DIRS are a list of Emacs Lisp directories to compile from;
115 no arguments compiles from `load-path'."
116 (save-excursion
117 (let ((processed nil))
118 (find-file "finder-inf.el")
119 (erase-buffer)
120 (insert ";;; finder-inf.el --- keyword-to-package mapping\n")
121 (insert ";; This file is part of GNU Emacs.\n")
122 (insert ";; Keywords: help\n")
123 (insert ";;; Commentary:\n")
124 (insert ";; Don't edit this file. It's generated by finder.el\n\n")
125 (insert ";;; Code:\n")
126 (insert "\n(setq finder-package-info '(\n")
127 (mapcar
128 (lambda (d)
129 (when (file-exists-p (directory-file-name d))
130 (message "Directory %s" d)
131 (mapcar
132 (lambda (f)
133 (if (and (or (string-match "^[^=].*\\.el$" f)
134 ;; Allow compressed files also. Fixme:
135 ;; generalize this, especially for
136 ;; MS-DOG-type filenames.
137 (and (string-match "^[^=].*\\.el\\.\\(gz\\|Z\\)$" f)
138 (require 'jka-compr)))
139 ;; Ignore lock files.
140 (not (string-match "^.#" f))
141 (not (member f processed)))
142 (let (summary keystart keywords)
143 (setq processed (cons f processed))
144 (save-excursion
145 (set-buffer (get-buffer-create "*finder-scratch*"))
146 (buffer-disable-undo (current-buffer))
147 (erase-buffer)
148 (insert-file-contents
149 (concat (file-name-as-directory (or d ".")) f))
150 (setq summary (lm-synopsis))
151 (setq keywords (lm-keywords)))
152 (insert
153 (format " (\"%s\"\n "
154 (if (string-match "\\.\\(gz\\|Z\\)$" f)
155 (file-name-sans-extension f)
156 f)))
157 (prin1 summary (current-buffer))
158 (insert
159 "\n ")
160 (setq keystart (point))
161 (insert
162 (if keywords (format "(%s)" keywords) "nil")
163 ")\n")
164 (subst-char-in-region keystart (point) ?, ? )
166 (directory-files (or d ".")))))
167 (or dirs load-path))
168 (insert "))\n
169 \(provide 'finder-inf)
171 ;;; Local Variables:
172 ;;; version-control: never
173 ;;; no-byte-compile: t
174 ;;; no-update-autoloads: t
175 ;;; End:
176 ;;; finder-inf.el ends here\n")
177 (kill-buffer "*finder-scratch*")
178 (eval-current-buffer) ;; So we get the new keyword list immediately
179 (basic-save-buffer))))
181 (defun finder-compile-keywords-make-dist ()
182 "Regenerate `finder-inf.el' for the Emacs distribution."
183 (apply 'finder-compile-keywords command-line-args-left)
184 (kill-emacs))
186 ;;; Now the retrieval code
188 (defun finder-insert-at-column (column &rest strings)
189 "Insert, at column COLUMN, other args STRINGS."
190 (if (>= (current-column) column) (insert "\n"))
191 (move-to-column column t)
192 (apply 'insert strings))
194 (defvar finder-help-echo nil)
196 (defun finder-mouse-face-on-line ()
197 "Put `mouse-face' and `help-echo' properties on the previous line."
198 (save-excursion
199 (previous-line 1)
200 (unless finder-help-echo
201 (setq finder-help-echo
202 (let* ((keys1 (where-is-internal 'finder-select
203 finder-mode-map))
204 (keys (nconc (where-is-internal
205 'finder-mouse-select finder-mode-map)
206 keys1)))
207 (concat (mapconcat 'key-description keys ", ")
208 ": select item"))))
209 (add-text-properties
210 (line-beginning-position) (line-end-position)
211 '(mouse-face highlight
212 help-echo finder-help-echo))))
214 ;;;###autoload
215 (defun finder-list-keywords ()
216 "Display descriptions of the keywords in the Finder buffer."
217 (interactive)
218 (if (get-buffer "*Finder*")
219 (pop-to-buffer "*Finder*")
220 (pop-to-buffer (set-buffer (get-buffer-create "*Finder*")))
221 (finder-mode)
222 (setq buffer-read-only nil)
223 (erase-buffer)
224 (mapc
225 (lambda (assoc)
226 (let ((keyword (car assoc)))
227 (insert (symbol-name keyword))
228 (finder-insert-at-column 14 (concat (cdr assoc) "\n"))
229 (finder-mouse-face-on-line)))
230 finder-known-keywords)
231 (goto-char (point-min))
232 (setq finder-headmark (point))
233 (setq buffer-read-only t)
234 (set-buffer-modified-p nil)
235 (balance-windows)
236 (finder-summary)))
238 (defun finder-list-matches (key)
239 (pop-to-buffer (set-buffer (get-buffer-create "*Finder Category*")))
240 (finder-mode)
241 (setq buffer-read-only nil)
242 (erase-buffer)
243 (let ((id (intern key)))
244 (insert
245 "The following packages match the keyword `" key "':\n\n")
246 (setq finder-headmark (point))
247 (mapc
248 (lambda (x)
249 (if (memq id (car (cdr (cdr x))))
250 (progn
251 (insert (car x))
252 (finder-insert-at-column 16 (concat (nth 1 x) "\n"))
253 (finder-mouse-face-on-line))))
254 finder-package-info)
255 (goto-char (point-min))
256 (forward-line)
257 (setq buffer-read-only t)
258 (set-buffer-modified-p nil)
259 (shrink-window-if-larger-than-buffer)
260 (finder-summary)))
262 (defun finder-find-library (library)
263 "Search for file LIBRARY on `load-path'.
264 Try compressed versions if jka-compr is in use."
265 (or (locate-library library t)
266 (if (rassq 'jka-compr-handler file-name-handler-alist)
267 (or (locate-library (concat library ".gz") t)
268 (locate-library (concat library ".Z") t)
269 ;; last resort for MS-DOG et al
270 (locate-library (concat library "z"))))))
272 ;;;###autoload
273 (defun finder-commentary (file)
274 "Display FILE's commentary section.
275 FILE should be in a form suitable for passing to `locate-library'."
276 (interactive "sLibrary name: ")
277 (let* ((str (lm-commentary (or (finder-find-library file)
278 (finder-find-library (concat file ".el"))
279 (error "Can't find library %s" file)))))
280 (if (null str)
281 (error "Can't find any Commentary section"))
282 (pop-to-buffer "*Finder*")
283 (setq buffer-read-only nil)
284 (erase-buffer)
285 (insert str)
286 (goto-char (point-min))
287 (delete-blank-lines)
288 (goto-char (point-max))
289 (delete-blank-lines)
290 (goto-char (point-min))
291 (while (re-search-forward "^;+ ?" nil t)
292 (replace-match "" nil nil))
293 (goto-char (point-min))
294 (setq buffer-read-only t)
295 (set-buffer-modified-p nil)
296 (shrink-window-if-larger-than-buffer)
297 (finder-mode)
298 (finder-summary)))
300 (defun finder-current-item ()
301 (if (and finder-headmark (< (point) finder-headmark))
302 (error "No keyword or filename on this line")
303 (save-excursion
304 (beginning-of-line)
305 (current-word))))
307 (defun finder-select ()
308 "Select item on current line in a finder buffer."
309 (interactive)
310 (let ((key (finder-current-item)))
311 (if (string-match "\\.el$" key)
312 (finder-commentary key)
313 (finder-list-matches key))))
315 (defun finder-mouse-select (event)
316 "Select item in a finder buffer with the mouse."
317 (interactive "e")
318 (save-excursion
319 (set-buffer (window-buffer (posn-window (event-start event))))
320 (goto-char (posn-point (event-start event)))
321 (finder-select)))
323 ;;;###autoload
324 (defun finder-by-keyword ()
325 "Find packages matching a given keyword."
326 (interactive)
327 (finder-list-keywords))
329 (defun finder-mode ()
330 "Major mode for browsing package documentation.
331 \\<finder-mode-map>
332 \\[finder-select] more help for the item on the current line
333 \\[finder-exit] exit Finder mode and kill the Finder buffer."
334 (interactive)
335 (use-local-map finder-mode-map)
336 (set-syntax-table emacs-lisp-mode-syntax-table)
337 (setq mode-name "Finder")
338 (setq major-mode 'finder-mode)
339 (make-local-variable 'finder-headmark)
340 (setq finder-headmark nil))
342 (defun finder-summary ()
343 "Summarize basic Finder commands."
344 (interactive)
345 (message "%s"
346 (substitute-command-keys
347 "\\<finder-mode-map>\\[finder-select] = select, \
348 \\[finder-mouse-select] = select, \\[finder-list-keywords] = to \
349 finder directory, \\[finder-exit] = quit, \\[finder-summary] = help")))
351 (defun finder-exit ()
352 "Exit Finder mode and kill the buffer."
353 (interactive)
354 (or (one-window-p t)
355 (delete-window))
356 ;; Can happen in either buffer -- kill each of the two that exists
357 (and (get-buffer "*Finder*")
358 (kill-buffer "*Finder*"))
359 (and (get-buffer "*Finder Category*")
360 (kill-buffer "*Finder Category*")))
363 (provide 'finder)
365 ;;; finder.el ends here