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