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