(browse-url): Re-fix case of
[emacs.git] / lisp / finder.el
blob5e00ea2c68364770d97e46413ee05cdf09be05c1
1 ;;; finder.el --- topic & keyword-based code finder
3 ;; Copyright (C) 1992, 1997, 1998, 1999 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 (require 'finder-inf)
44 ;; Local variable in finder buffer.
45 (defvar finder-headmark)
47 ;; These are supposed to correspond to top-level customization groups,
48 ;; says rms.
49 (defvar finder-known-keywords
51 (abbrev . "abbreviation handling, typing shortcuts, macros")
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 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 (frames . "support for Emacs frames and window systems")
63 (games . "games, jokes and amusements")
64 (hardware . "support for interfacing with exotic hardware")
65 (help . "support for on-line help systems")
66 (hypermedia . "support for links between text or other media types")
67 (i18n . "internationalization and alternate character-set support")
68 (internal . "code for Emacs internals, build process, defaults")
69 (languages . "specialized modes for editing programming languages")
70 (lisp . "Lisp support, including Emacs Lisp")
71 (local . "code local to your site")
72 (maint . "maintenance aids for the Emacs development group")
73 (mail . "modes for electronic-mail handling")
74 (matching . "various sorts of searching and matching")
75 (mouse . "mouse support")
76 (multimedia . "images and sound support")
77 (news . "support for netnews reading and posting")
78 (oop . "support for object-oriented programming")
79 (outlines . "support for hierarchical outlining")
80 (processes . "process, subshell, compilation, and job control support")
81 (terminals . "support for terminal types")
82 (tex . "code related to the TeX formatter")
83 (tools . "programming tools")
84 (unix . "front-ends/assistants for, or emulators of, UNIX features")
85 (vms . "support code for vms")
86 (wp . "word processing")
89 (defvar finder-mode-map nil)
90 (or finder-mode-map
91 (let ((map (make-sparse-keymap)))
92 (define-key map " " 'finder-select)
93 (define-key map "f" 'finder-select)
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 "q" 'finder-exit)
98 (define-key map "d" 'finder-list-keywords)
99 (setq finder-mode-map map)))
102 ;;; Code for regenerating the keyword list.
104 (defvar finder-package-info nil
105 "Assoc list mapping file names to description & keyword lists.")
107 (defun finder-compile-keywords (&rest dirs)
108 "Regenerate the keywords association list into the file `finder-inf.el'.
109 Optional arguments DIRS are a list of Emacs Lisp directories to compile from;
110 no arguments compiles from `load-path'."
111 (save-excursion
112 (let ((processed nil))
113 (find-file "finder-inf.el")
114 (erase-buffer)
115 (insert ";;; finder-inf.el --- keyword-to-package mapping\n")
116 (insert ";; Keywords: help\n")
117 (insert ";;; Commentary:\n")
118 (insert ";; Don't edit this file. It's generated by finder.el\n\n")
119 (insert ";;; Code:\n")
120 (insert "\n(setq finder-package-info '(\n")
121 (mapcar
122 (lambda (d)
123 (when (file-exists-p (directory-file-name d))
124 (message "Directory %s" d)
125 (mapcar
126 (lambda (f)
127 (if (and (or (string-match "^[^=].*\\.el$" f)
128 ;; Allow compressed files also. Fixme:
129 ;; generalize this, especially for
130 ;; MS-DOG-type filenames.
131 (and (string-match "^[^=].*\\.el\\.\\(gz\\|Z\\)$" f)
132 (require 'jka-compr)))
133 ;; Ignore lock files.
134 (not (string-match "^.#" f))
135 (not (member f processed)))
136 (let (summary keystart keywords)
137 (setq processed (cons f processed))
138 (save-excursion
139 (set-buffer (get-buffer-create "*finder-scratch*"))
140 (buffer-disable-undo (current-buffer))
141 (erase-buffer)
142 (insert-file-contents
143 (concat (file-name-as-directory (or d ".")) f))
144 (setq summary (lm-synopsis))
145 (setq keywords (lm-keywords)))
146 (insert
147 (format " (\"%s\"\n "
148 (if (string-match "\\.\\(gz\\|Z\\)$" f)
149 (file-name-sans-extension f)
150 f)))
151 (prin1 summary (current-buffer))
152 (insert
153 "\n ")
154 (setq keystart (point))
155 (insert
156 (if keywords (format "(%s)" keywords) "nil")
157 ")\n")
158 (subst-char-in-region keystart (point) ?, ? )
160 (directory-files (or d ".")))))
161 (or dirs load-path))
162 (insert "))\n\n(provide 'finder-inf)\n\n;;; finder-inf.el ends here\n")
163 (kill-buffer "*finder-scratch*")
164 (eval-current-buffer) ;; So we get the new keyword list immediately
165 (basic-save-buffer))))
167 (defun finder-compile-keywords-make-dist ()
168 "Regenerate `finder-inf.el' for the Emacs distribution."
169 (apply 'finder-compile-keywords command-line-args-left)
170 (kill-emacs))
172 ;;; Now the retrieval code
174 (defun finder-insert-at-column (column &rest strings)
175 "Insert, at column COLUMN, other args STRINGS."
176 (if (> (current-column) column) (insert "\n"))
177 (move-to-column column t)
178 (apply 'insert strings))
180 (defun finder-mouse-face-on-line ()
181 "Put a `mouse-face' property on the previous line."
182 (save-excursion
183 (previous-line 1)
184 (put-text-property (save-excursion (beginning-of-line) (point))
185 (progn (end-of-line) (point))
186 'mouse-face 'highlight)))
188 (defun finder-list-keywords ()
189 "Display descriptions of the keywords in the Finder buffer."
190 (interactive)
191 (if (get-buffer "*Finder*")
192 (pop-to-buffer "*Finder*")
193 (pop-to-buffer (set-buffer (get-buffer-create "*Finder*")))
194 (finder-mode)
195 (setq buffer-read-only nil)
196 (erase-buffer)
197 (mapcar
198 (lambda (assoc)
199 (let ((keyword (car assoc)))
200 (insert (symbol-name keyword))
201 (finder-insert-at-column 14 (concat (cdr assoc) "\n"))
202 (finder-mouse-face-on-line)))
203 finder-known-keywords)
204 (goto-char (point-min))
205 (setq finder-headmark (point))
206 (setq buffer-read-only t)
207 (set-buffer-modified-p nil)
208 (balance-windows)
209 (finder-summary)))
211 (defun finder-list-matches (key)
212 (pop-to-buffer (set-buffer (get-buffer-create "*Finder Category*")))
213 (finder-mode)
214 (setq buffer-read-only nil)
215 (erase-buffer)
216 (let ((id (intern key)))
217 (insert
218 "The following packages match the keyword `" key "':\n\n")
219 (setq finder-headmark (point))
220 (mapcar
221 (lambda (x)
222 (if (memq id (car (cdr (cdr x))))
223 (progn
224 (insert (car x))
225 (finder-insert-at-column 16 (concat (nth 1 x) "\n"))
226 (finder-mouse-face-on-line))))
227 finder-package-info)
228 (goto-char (point-min))
229 (forward-line)
230 (setq buffer-read-only t)
231 (set-buffer-modified-p nil)
232 (shrink-window-if-larger-than-buffer)
233 (finder-summary)))
235 (defun finder-find-library (library)
236 "Search for file LIBRARY on `load-path'.
237 Try compressed versions if jka-compr is in use."
238 (or (locate-library library t)
239 (if (rassq 'jka-compr-handler file-name-handler-alist)
240 (or (locate-library (concat library ".gz") t)
241 (locate-library (concat library ".Z") t)
242 ;; last resort for MS-DOG et al
243 (locate-library (concat library "z"))))))
245 (defun finder-commentary (file)
246 "Display FILE's commentary section.
247 FILE should be in a form suitable for passing to `locate-library'."
248 (interactive "sLibrary name: ")
249 (let* ((str (lm-commentary (or (finder-find-library file)
250 (finder-find-library (concat file ".el"))
251 (error "Can't find library %s" file)))))
252 (if (null str)
253 (error "Can't find any Commentary section"))
254 (pop-to-buffer "*Finder*")
255 (setq buffer-read-only nil)
256 (erase-buffer)
257 (insert str)
258 (goto-char (point-min))
259 (delete-blank-lines)
260 (goto-char (point-max))
261 (delete-blank-lines)
262 (goto-char (point-min))
263 (while (re-search-forward "^;+ ?" nil t)
264 (replace-match "" nil nil))
265 (goto-char (point-min))
266 (setq buffer-read-only t)
267 (set-buffer-modified-p nil)
268 (shrink-window-if-larger-than-buffer)
269 (finder-mode)
270 (finder-summary)))
272 (defun finder-current-item ()
273 (if (and finder-headmark (< (point) finder-headmark))
274 (error "No keyword or filename on this line")
275 (save-excursion
276 (beginning-of-line)
277 (current-word))))
279 (defun finder-select ()
280 "Select item on current line in a finder buffer."
281 (interactive)
282 (let ((key (finder-current-item)))
283 (if (string-match "\\.el$" key)
284 (finder-commentary key)
285 (finder-list-matches key))))
287 (defun finder-mouse-select (event)
288 "Select item in a finder buffer with the mouse."
289 (interactive "e")
290 (save-excursion
291 (set-buffer (window-buffer (posn-window (event-start event))))
292 (goto-char (posn-point (event-start event)))
293 (finder-select)))
295 (defun finder-by-keyword ()
296 "Find packages matching a given keyword."
297 (interactive)
298 (finder-list-keywords))
300 (defun finder-mode ()
301 "Major mode for browsing package documentation.
302 \\<finder-mode-map>
303 \\[finder-select] more help for the item on the current line
304 \\[finder-exit] exit Finder mode and kill the Finder buffer."
305 (interactive)
306 (use-local-map finder-mode-map)
307 (set-syntax-table emacs-lisp-mode-syntax-table)
308 (setq mode-name "Finder")
309 (setq major-mode 'finder-mode)
310 (make-local-variable 'finder-headmark)
311 (setq finder-headmark nil))
313 (defun finder-summary ()
314 "Summarize basic Finder commands."
315 (interactive)
316 (message "%s"
317 (substitute-command-keys
318 "\\<finder-mode-map>\\[finder-select] = select, \
319 \\[finder-mouse-select] = select, \\[finder-list-keywords] = to \
320 finder directory, \\[finder-exit] = quit, \\[finder-summary] = help")))
322 (defun finder-exit ()
323 "Exit Finder mode and kill the buffer."
324 (interactive)
325 (or (one-window-p t)
326 (delete-window))
327 ;; Can happen in either buffer -- kill each of the two that exists
328 (and (get-buffer "*Finder*")
329 (kill-buffer "*Finder*"))
330 (and (get-buffer "*Finder Category*")
331 (kill-buffer "*Finder Category*")))
333 (provide 'finder)
335 ;;; finder.el ends here