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