Update years in copyright notice; nfc.
[bpt/emacs.git] / lisp / finder.el
CommitLineData
1164bae9
ER
1;;; finder.el --- topic & keyword-based code finder
2
0d30b337 3;; Copyright (C) 1992, 1997, 1998, 1999, 2001, 2002, 2003,
aaef169d 4;; 2004, 2005, 2006 Free Software Foundation, Inc.
1164bae9
ER
5
6;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
7;; Created: 16 Jun 1992
8;; Version: 1.0
9;; Keywords: help
10
11;; This file is part of GNU Emacs.
12
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
7c938215 15;; the Free Software Foundation; either version 2, or (at your option)
1164bae9
ER
16;; any later version.
17
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.
22
23;; You should have received a copy of the GNU General Public License
b578f267 24;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
25;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26;; Boston, MA 02110-1301, USA.
1164bae9 27
101dad14 28;;; Commentary:
1164bae9
ER
29
30;; This mode uses the Keywords library header to provide code-finding
31;; services by keyword.
32;;
33;; Things to do:
34;; 1. Support multiple keywords per search. This could be extremely hairy;
35;; there doesn't seem to be any way to get completing-read to exit on
36;; an EOL with no substring pending, which is what we'd want to end the loop.
37;; 2. Search by string in synopsis line?
38;; 3. Function to check finder-package-info for unknown keywords.
39
40;;; Code:
41
42(require 'lisp-mnt)
24d30c03 43(require 'find-func) ;for find-library(-suffixes)
f0c4433f
SM
44;; Use `load' rather than `require' so that it doesn't get loaded
45;; during byte-compilation (at which point it might be missing).
2e819a08 46(load "finder-inf" t t)
1164bae9 47
dcd70a81
RS
48(defvar finder-mode-hook nil
49 "*Hook run when function `finder-mode' is called.")
50
14931973
RS
51;; Local variable in finder buffer.
52(defvar finder-headmark)
53
4f451c3f
DL
54;; These are supposed to correspond to top-level customization groups,
55;; says rms.
1164bae9
ER
56(defvar finder-known-keywords
57 '(
dea24479 58 (abbrev . "abbreviation handling, typing shortcuts, macros")
f06f7416 59 ;; Too specific:
be2c35e4 60 (bib . "code related to the `bib' bibliography processor")
68f8f37f 61 (c . "support for the C language and related languages")
1164bae9 62 (calendar . "calendar and time management support")
e9571d2a 63 (comm . "communications, networking, remote access to files")
35e9c644 64 (convenience . "convenience features for faster editing")
dde4a5bb 65 (data . "support for editing files of data")
1164bae9
ER
66 (docs . "support for Emacs documentation")
67 (emulations . "emulations of other editors")
68 (extensions . "Emacs Lisp language extensions")
be2c35e4 69 (faces . "support for multiple fonts")
f06f7416 70 (files . "support for editing and manipulating files")
f1aabce0 71 (frames . "support for Emacs frames and window systems")
1164bae9
ER
72 (games . "games, jokes and amusements")
73 (hardware . "support for interfacing with exotic hardware")
74 (help . "support for on-line help systems")
f1aabce0 75 (hypermedia . "support for links between text or other media types")
14931973 76 (i18n . "internationalization and alternate character-set support")
1164bae9
ER
77 (internal . "code for Emacs internals, build process, defaults")
78 (languages . "specialized modes for editing programming languages")
79 (lisp . "Lisp support, including Emacs Lisp")
80 (local . "code local to your site")
81 (maint . "maintenance aids for the Emacs development group")
82 (mail . "modes for electronic-mail handling")
be2c35e4
RS
83 (matching . "various sorts of searching and matching")
84 (mouse . "mouse support")
1226dd0b 85 (multimedia . "images and sound support")
1164bae9 86 (news . "support for netnews reading and posting")
f1aabce0
RS
87 (oop . "support for object-oriented programming")
88 (outlines . "support for hierarchical outlining")
1164bae9
ER
89 (processes . "process, subshell, compilation, and job control support")
90 (terminals . "support for terminal types")
dde4a5bb 91 (tex . "supporting code for the TeX formatter")
1164bae9 92 (tools . "programming tools")
dde4a5bb 93 (unix . "front-ends/assistants for, or emulators of, UNIX-like features")
f06f7416
DL
94;; Not a custom group and not currently useful.
95;; (vms . "support code for vms")
1164bae9
ER
96 (wp . "word processing")
97 ))
98
79760fb2
SM
99(defvar finder-mode-map
100 (let ((map (make-sparse-keymap)))
101 (define-key map " " 'finder-select)
102 (define-key map "f" 'finder-select)
24bdbffe 103 (define-key map [follow-link] 'mouse-face)
79760fb2
SM
104 (define-key map [mouse-2] 'finder-mouse-select)
105 (define-key map "\C-m" 'finder-select)
106 (define-key map "?" 'finder-summary)
107 (define-key map "q" 'finder-exit)
108 (define-key map "d" 'finder-list-keywords)
109 map))
e141acb3 110
101dad14 111
1164bae9
ER
112;;; Code for regenerating the keyword list.
113
114(defvar finder-package-info nil
115 "Assoc list mapping file names to description & keyword lists.")
116
9bd0d71a
AS
117(defvar generated-finder-keywords-file "finder-inf.el"
118 "File \\[finder-compile-keywords] puts finder keywords into.")
119
1164bae9 120(defun finder-compile-keywords (&rest dirs)
9bd0d71a 121 "Regenerate the keywords association list into `generated-finder-keywords-file'.
2d212d87
DL
122Optional arguments DIRS are a list of Emacs Lisp directories to compile from;
123no arguments compiles from `load-path'."
1164bae9 124 (save-excursion
c5d87135 125 (let ((processed nil))
9bd0d71a 126 (find-file generated-finder-keywords-file)
c5d87135 127 (erase-buffer)
9bd0d71a
AS
128 (insert ";;; " (file-name-nondirectory generated-finder-keywords-file)
129 " --- keyword-to-package mapping\n")
e8af40ee 130 (insert ";; This file is part of GNU Emacs.\n")
0cbe967f
ER
131 (insert ";;; Commentary:\n")
132 (insert ";; Don't edit this file. It's generated by finder.el\n\n")
133 (insert ";;; Code:\n")
c5d87135
ER
134 (insert "\n(setq finder-package-info '(\n")
135 (mapcar
e6fb4d11 136 (lambda (d)
5b64ebe8
RS
137 (when (file-exists-p (directory-file-name d))
138 (message "Directory %s" d)
139 (mapcar
2d212d87 140 (lambda (f)
5b64ebe8
RS
141 (if (and (or (string-match "^[^=].*\\.el$" f)
142 ;; Allow compressed files also. Fixme:
143 ;; generalize this, especially for
144 ;; MS-DOG-type filenames.
145 (and (string-match "^[^=].*\\.el\\.\\(gz\\|Z\\)$" f)
146 (require 'jka-compr)))
147 ;; Ignore lock files.
148 (not (string-match "^.#" f))
149 (not (member f processed)))
150 (let (summary keystart keywords)
151 (setq processed (cons f processed))
152 (save-excursion
153 (set-buffer (get-buffer-create "*finder-scratch*"))
154 (buffer-disable-undo (current-buffer))
155 (erase-buffer)
156 (insert-file-contents
157 (concat (file-name-as-directory (or d ".")) f))
158 (setq summary (lm-synopsis))
159 (setq keywords (lm-keywords)))
160 (insert
161 (format " (\"%s\"\n "
162 (if (string-match "\\.\\(gz\\|Z\\)$" f)
2d212d87 163 (file-name-sans-extension f)
5b64ebe8
RS
164 f)))
165 (prin1 summary (current-buffer))
166 (insert
167 "\n ")
168 (setq keystart (point))
169 (insert
170 (if keywords (format "(%s)" keywords) "nil")
171 ")\n")
172 (subst-char-in-region keystart (point) ?, ? )
173 )))
174 (directory-files (or d ".")))))
c5d87135 175 (or dirs load-path))
6a7ceddc 176 (insert "))\n
9bd0d71a
AS
177\(provide '" (file-name-sans-extension
178 (file-name-nondirectory generated-finder-keywords-file)) ")
6a7ceddc
SM
179
180;;; Local Variables:
181;;; version-control: never
182;;; no-byte-compile: t
183;;; no-update-autoloads: t
184;;; End:
9bd0d71a 185;;; " (file-name-nondirectory generated-finder-keywords-file) " ends here\n")
c5d87135 186 (kill-buffer "*finder-scratch*")
887195ac 187 (eval-buffer) ;; So we get the new keyword list immediately
e6fb4d11 188 (basic-save-buffer))))
1164bae9 189
1c6425ea
RS
190(defun finder-compile-keywords-make-dist ()
191 "Regenerate `finder-inf.el' for the Emacs distribution."
644f58eb
RS
192 (apply 'finder-compile-keywords command-line-args-left)
193 (kill-emacs))
1c6425ea 194
1164bae9
ER
195;;; Now the retrieval code
196
e6fb4d11 197(defun finder-insert-at-column (column &rest strings)
eb4df0c3 198 "Insert, at column COLUMN, other args STRINGS."
ff524b84 199 (if (>= (current-column) column) (insert "\n"))
eb4df0c3 200 (move-to-column column t)
e6fb4d11
EN
201 (apply 'insert strings))
202
f06f7416
DL
203(defvar finder-help-echo nil)
204
eb4df0c3 205(defun finder-mouse-face-on-line ()
f06f7416 206 "Put `mouse-face' and `help-echo' properties on the previous line."
eb4df0c3
DL
207 (save-excursion
208 (previous-line 1)
f06f7416
DL
209 (unless finder-help-echo
210 (setq finder-help-echo
211 (let* ((keys1 (where-is-internal 'finder-select
212 finder-mode-map))
213 (keys (nconc (where-is-internal
214 'finder-mouse-select finder-mode-map)
215 keys1)))
216 (concat (mapconcat 'key-description keys ", ")
217 ": select item"))))
218 (add-text-properties
219 (line-beginning-position) (line-end-position)
220 '(mouse-face highlight
221 help-echo finder-help-echo))))
eb4df0c3 222
706239c7 223;;;###autoload
101dad14
ER
224(defun finder-list-keywords ()
225 "Display descriptions of the keywords in the Finder buffer."
226 (interactive)
5acc847d
RS
227 (if (get-buffer "*Finder*")
228 (pop-to-buffer "*Finder*")
b3241066 229 (pop-to-buffer (set-buffer (get-buffer-create "*Finder*")))
5acc847d
RS
230 (finder-mode)
231 (setq buffer-read-only nil)
232 (erase-buffer)
f06f7416 233 (mapc
5acc847d
RS
234 (lambda (assoc)
235 (let ((keyword (car assoc)))
236 (insert (symbol-name keyword))
237 (finder-insert-at-column 14 (concat (cdr assoc) "\n"))
eb4df0c3 238 (finder-mouse-face-on-line)))
5acc847d
RS
239 finder-known-keywords)
240 (goto-char (point-min))
241 (setq finder-headmark (point))
242 (setq buffer-read-only t)
243 (set-buffer-modified-p nil)
244 (balance-windows)
245 (finder-summary)))
101dad14
ER
246
247(defun finder-list-matches (key)
2d7b0db7 248 (pop-to-buffer (set-buffer (get-buffer-create "*Finder Category*")))
5acc847d 249 (finder-mode)
101dad14
ER
250 (setq buffer-read-only nil)
251 (erase-buffer)
252 (let ((id (intern key)))
253 (insert
254 "The following packages match the keyword `" key "':\n\n")
14931973 255 (setq finder-headmark (point))
f06f7416 256 (mapc
e6fb4d11
EN
257 (lambda (x)
258 (if (memq id (car (cdr (cdr x))))
259 (progn
260 (insert (car x))
eb4df0c3
DL
261 (finder-insert-at-column 16 (concat (nth 1 x) "\n"))
262 (finder-mouse-face-on-line))))
101dad14
ER
263 finder-package-info)
264 (goto-char (point-min))
265 (forward-line)
266 (setq buffer-read-only t)
267 (set-buffer-modified-p nil)
268 (shrink-window-if-larger-than-buffer)
269 (finder-summary)))
270
706239c7 271;;;###autoload
101dad14 272(defun finder-commentary (file)
2d212d87
DL
273 "Display FILE's commentary section.
274FILE should be in a form suitable for passing to `locate-library'."
24d30c03
SM
275 (interactive
276 (list
277 (completing-read "Library name: "
278 'locate-file-completion
279 (cons (or find-function-source-path load-path)
280 (find-library-suffixes)))))
281 (let* ((str (lm-commentary (find-library-name file))))
101dad14 282 (if (null str)
67a69ba6 283 (error "Can't find any Commentary section"))
63fe0a98
RS
284 ;; This used to use *Finder* but that would clobber the
285 ;; directory of categories.
61e938eb 286 (delete-other-windows)
63fe0a98 287 (pop-to-buffer "*Finder-package*")
101dad14
ER
288 (setq buffer-read-only nil)
289 (erase-buffer)
290 (insert str)
291 (goto-char (point-min))
292 (delete-blank-lines)
293 (goto-char (point-max))
294 (delete-blank-lines)
295 (goto-char (point-min))
296 (while (re-search-forward "^;+ ?" nil t)
297 (replace-match "" nil nil))
298 (goto-char (point-min))
299 (setq buffer-read-only t)
300 (set-buffer-modified-p nil)
301 (shrink-window-if-larger-than-buffer)
8f713f53 302 (finder-mode)
e6fb4d11 303 (finder-summary)))
101dad14
ER
304
305(defun finder-current-item ()
62832105
EZ
306 (let ((key (save-excursion
307 (beginning-of-line)
308 (current-word))))
309 (if (or (and finder-headmark (< (point) finder-headmark))
310 (= (length key) 0))
311 (error "No keyword or filename on this line")
312 key)))
101dad14
ER
313
314(defun finder-select ()
eb4df0c3 315 "Select item on current line in a finder buffer."
101dad14
ER
316 (interactive)
317 (let ((key (finder-current-item)))
f3b330d6
KH
318 (if (string-match "\\.el$" key)
319 (finder-commentary key)
320 (finder-list-matches key))))
321
322(defun finder-mouse-select (event)
eb4df0c3 323 "Select item in a finder buffer with the mouse."
f3b330d6
KH
324 (interactive "e")
325 (save-excursion
eb4df0c3
DL
326 (set-buffer (window-buffer (posn-window (event-start event))))
327 (goto-char (posn-point (event-start event)))
328 (finder-select)))
101dad14 329
706239c7 330;;;###autoload
1164bae9
ER
331(defun finder-by-keyword ()
332 "Find packages matching a given keyword."
333 (interactive)
101dad14
ER
334 (finder-list-keywords))
335
336(defun finder-mode ()
337 "Major mode for browsing package documentation.
527da106 338\\<finder-mode-map>
101dad14 339\\[finder-select] more help for the item on the current line
2d212d87 340\\[finder-exit] exit Finder mode and kill the Finder buffer."
101dad14 341 (interactive)
79760fb2 342 (kill-all-local-variables)
101dad14
ER
343 (use-local-map finder-mode-map)
344 (set-syntax-table emacs-lisp-mode-syntax-table)
345 (setq mode-name "Finder")
346 (setq major-mode 'finder-mode)
79760fb2
SM
347 (set (make-local-variable 'finder-headmark) nil)
348 (run-mode-hooks 'finder-mode-hook))
101dad14
ER
349
350(defun finder-summary ()
351 "Summarize basic Finder commands."
352 (interactive)
90f061c7 353 (message "%s"
527da106 354 (substitute-command-keys
eb4df0c3
DL
355 "\\<finder-mode-map>\\[finder-select] = select, \
356\\[finder-mouse-select] = select, \\[finder-list-keywords] = to \
357finder directory, \\[finder-exit] = quit, \\[finder-summary] = help")))
f5a16823 358
101dad14 359(defun finder-exit ()
35863d77 360 "Exit Finder mode and kill the buffer."
101dad14 361 (interactive)
b26f4fbd
KH
362 (or (one-window-p t)
363 (delete-window))
f3b330d6 364 ;; Can happen in either buffer -- kill each of the two that exists
b26f4fbd
KH
365 (and (get-buffer "*Finder*")
366 (kill-buffer "*Finder*"))
367 (and (get-buffer "*Finder Category*")
368 (kill-buffer "*Finder Category*")))
1164bae9 369
6a7ceddc 370\f
1164bae9
ER
371(provide 'finder)
372
ab5796a9 373;;; arch-tag: ec85ff49-8cb8-41f5-a63f-9131d53ce2c5
1164bae9 374;;; finder.el ends here