Commit | Line | Data |
---|---|---|
1164bae9 ER |
1 | ;;; finder.el --- topic & keyword-based code finder |
2 | ||
0d30b337 TTN |
3 | ;; Copyright (C) 1992, 1997, 1998, 1999, 2001, 2002, 2003, |
4 | ;; 2004, 2005 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) | |
103 | (define-key map [mouse-2] 'finder-mouse-select) | |
104 | (define-key map "\C-m" 'finder-select) | |
105 | (define-key map "?" 'finder-summary) | |
106 | (define-key map "q" 'finder-exit) | |
107 | (define-key map "d" 'finder-list-keywords) | |
108 | map)) | |
e141acb3 | 109 | |
101dad14 | 110 | |
1164bae9 ER |
111 | ;;; Code for regenerating the keyword list. |
112 | ||
113 | (defvar finder-package-info nil | |
114 | "Assoc list mapping file names to description & keyword lists.") | |
115 | ||
9bd0d71a AS |
116 | (defvar generated-finder-keywords-file "finder-inf.el" |
117 | "File \\[finder-compile-keywords] puts finder keywords into.") | |
118 | ||
1164bae9 | 119 | (defun finder-compile-keywords (&rest dirs) |
9bd0d71a | 120 | "Regenerate the keywords association list into `generated-finder-keywords-file'. |
2d212d87 DL |
121 | Optional arguments DIRS are a list of Emacs Lisp directories to compile from; |
122 | no arguments compiles from `load-path'." | |
1164bae9 | 123 | (save-excursion |
c5d87135 | 124 | (let ((processed nil)) |
9bd0d71a | 125 | (find-file generated-finder-keywords-file) |
c5d87135 | 126 | (erase-buffer) |
9bd0d71a AS |
127 | (insert ";;; " (file-name-nondirectory generated-finder-keywords-file) |
128 | " --- keyword-to-package mapping\n") | |
e8af40ee | 129 | (insert ";; This file is part of GNU Emacs.\n") |
0cbe967f ER |
130 | (insert ";;; Commentary:\n") |
131 | (insert ";; Don't edit this file. It's generated by finder.el\n\n") | |
132 | (insert ";;; Code:\n") | |
c5d87135 ER |
133 | (insert "\n(setq finder-package-info '(\n") |
134 | (mapcar | |
e6fb4d11 | 135 | (lambda (d) |
5b64ebe8 RS |
136 | (when (file-exists-p (directory-file-name d)) |
137 | (message "Directory %s" d) | |
138 | (mapcar | |
2d212d87 | 139 | (lambda (f) |
5b64ebe8 RS |
140 | (if (and (or (string-match "^[^=].*\\.el$" f) |
141 | ;; Allow compressed files also. Fixme: | |
142 | ;; generalize this, especially for | |
143 | ;; MS-DOG-type filenames. | |
144 | (and (string-match "^[^=].*\\.el\\.\\(gz\\|Z\\)$" f) | |
145 | (require 'jka-compr))) | |
146 | ;; Ignore lock files. | |
147 | (not (string-match "^.#" f)) | |
148 | (not (member f processed))) | |
149 | (let (summary keystart keywords) | |
150 | (setq processed (cons f processed)) | |
151 | (save-excursion | |
152 | (set-buffer (get-buffer-create "*finder-scratch*")) | |
153 | (buffer-disable-undo (current-buffer)) | |
154 | (erase-buffer) | |
155 | (insert-file-contents | |
156 | (concat (file-name-as-directory (or d ".")) f)) | |
157 | (setq summary (lm-synopsis)) | |
158 | (setq keywords (lm-keywords))) | |
159 | (insert | |
160 | (format " (\"%s\"\n " | |
161 | (if (string-match "\\.\\(gz\\|Z\\)$" f) | |
2d212d87 | 162 | (file-name-sans-extension f) |
5b64ebe8 RS |
163 | f))) |
164 | (prin1 summary (current-buffer)) | |
165 | (insert | |
166 | "\n ") | |
167 | (setq keystart (point)) | |
168 | (insert | |
169 | (if keywords (format "(%s)" keywords) "nil") | |
170 | ")\n") | |
171 | (subst-char-in-region keystart (point) ?, ? ) | |
172 | ))) | |
173 | (directory-files (or d "."))))) | |
c5d87135 | 174 | (or dirs load-path)) |
6a7ceddc | 175 | (insert "))\n |
9bd0d71a AS |
176 | \(provide '" (file-name-sans-extension |
177 | (file-name-nondirectory generated-finder-keywords-file)) ") | |
6a7ceddc SM |
178 | |
179 | ;;; Local Variables: | |
180 | ;;; version-control: never | |
181 | ;;; no-byte-compile: t | |
182 | ;;; no-update-autoloads: t | |
183 | ;;; End: | |
9bd0d71a | 184 | ;;; " (file-name-nondirectory generated-finder-keywords-file) " ends here\n") |
c5d87135 | 185 | (kill-buffer "*finder-scratch*") |
887195ac | 186 | (eval-buffer) ;; So we get the new keyword list immediately |
e6fb4d11 | 187 | (basic-save-buffer)))) |
1164bae9 | 188 | |
1c6425ea RS |
189 | (defun finder-compile-keywords-make-dist () |
190 | "Regenerate `finder-inf.el' for the Emacs distribution." | |
644f58eb RS |
191 | (apply 'finder-compile-keywords command-line-args-left) |
192 | (kill-emacs)) | |
1c6425ea | 193 | |
1164bae9 ER |
194 | ;;; Now the retrieval code |
195 | ||
e6fb4d11 | 196 | (defun finder-insert-at-column (column &rest strings) |
eb4df0c3 | 197 | "Insert, at column COLUMN, other args STRINGS." |
ff524b84 | 198 | (if (>= (current-column) column) (insert "\n")) |
eb4df0c3 | 199 | (move-to-column column t) |
e6fb4d11 EN |
200 | (apply 'insert strings)) |
201 | ||
f06f7416 DL |
202 | (defvar finder-help-echo nil) |
203 | ||
eb4df0c3 | 204 | (defun finder-mouse-face-on-line () |
f06f7416 | 205 | "Put `mouse-face' and `help-echo' properties on the previous line." |
eb4df0c3 DL |
206 | (save-excursion |
207 | (previous-line 1) | |
f06f7416 DL |
208 | (unless finder-help-echo |
209 | (setq finder-help-echo | |
210 | (let* ((keys1 (where-is-internal 'finder-select | |
211 | finder-mode-map)) | |
212 | (keys (nconc (where-is-internal | |
213 | 'finder-mouse-select finder-mode-map) | |
214 | keys1))) | |
215 | (concat (mapconcat 'key-description keys ", ") | |
216 | ": select item")))) | |
217 | (add-text-properties | |
218 | (line-beginning-position) (line-end-position) | |
219 | '(mouse-face highlight | |
220 | help-echo finder-help-echo)))) | |
eb4df0c3 | 221 | |
706239c7 | 222 | ;;;###autoload |
101dad14 ER |
223 | (defun finder-list-keywords () |
224 | "Display descriptions of the keywords in the Finder buffer." | |
225 | (interactive) | |
5acc847d RS |
226 | (if (get-buffer "*Finder*") |
227 | (pop-to-buffer "*Finder*") | |
b3241066 | 228 | (pop-to-buffer (set-buffer (get-buffer-create "*Finder*"))) |
5acc847d RS |
229 | (finder-mode) |
230 | (setq buffer-read-only nil) | |
231 | (erase-buffer) | |
f06f7416 | 232 | (mapc |
5acc847d RS |
233 | (lambda (assoc) |
234 | (let ((keyword (car assoc))) | |
235 | (insert (symbol-name keyword)) | |
236 | (finder-insert-at-column 14 (concat (cdr assoc) "\n")) | |
eb4df0c3 | 237 | (finder-mouse-face-on-line))) |
5acc847d RS |
238 | finder-known-keywords) |
239 | (goto-char (point-min)) | |
240 | (setq finder-headmark (point)) | |
241 | (setq buffer-read-only t) | |
242 | (set-buffer-modified-p nil) | |
243 | (balance-windows) | |
244 | (finder-summary))) | |
101dad14 ER |
245 | |
246 | (defun finder-list-matches (key) | |
2d7b0db7 | 247 | (pop-to-buffer (set-buffer (get-buffer-create "*Finder Category*"))) |
5acc847d | 248 | (finder-mode) |
101dad14 ER |
249 | (setq buffer-read-only nil) |
250 | (erase-buffer) | |
251 | (let ((id (intern key))) | |
252 | (insert | |
253 | "The following packages match the keyword `" key "':\n\n") | |
14931973 | 254 | (setq finder-headmark (point)) |
f06f7416 | 255 | (mapc |
e6fb4d11 EN |
256 | (lambda (x) |
257 | (if (memq id (car (cdr (cdr x)))) | |
258 | (progn | |
259 | (insert (car x)) | |
eb4df0c3 DL |
260 | (finder-insert-at-column 16 (concat (nth 1 x) "\n")) |
261 | (finder-mouse-face-on-line)))) | |
101dad14 ER |
262 | finder-package-info) |
263 | (goto-char (point-min)) | |
264 | (forward-line) | |
265 | (setq buffer-read-only t) | |
266 | (set-buffer-modified-p nil) | |
267 | (shrink-window-if-larger-than-buffer) | |
268 | (finder-summary))) | |
269 | ||
706239c7 | 270 | ;;;###autoload |
101dad14 | 271 | (defun finder-commentary (file) |
2d212d87 DL |
272 | "Display FILE's commentary section. |
273 | FILE should be in a form suitable for passing to `locate-library'." | |
24d30c03 SM |
274 | (interactive |
275 | (list | |
276 | (completing-read "Library name: " | |
277 | 'locate-file-completion | |
278 | (cons (or find-function-source-path load-path) | |
279 | (find-library-suffixes))))) | |
280 | (let* ((str (lm-commentary (find-library-name file)))) | |
101dad14 | 281 | (if (null str) |
67a69ba6 | 282 | (error "Can't find any Commentary section")) |
63fe0a98 RS |
283 | ;; This used to use *Finder* but that would clobber the |
284 | ;; directory of categories. | |
61e938eb | 285 | (delete-other-windows) |
63fe0a98 | 286 | (pop-to-buffer "*Finder-package*") |
101dad14 ER |
287 | (setq buffer-read-only nil) |
288 | (erase-buffer) | |
289 | (insert str) | |
290 | (goto-char (point-min)) | |
291 | (delete-blank-lines) | |
292 | (goto-char (point-max)) | |
293 | (delete-blank-lines) | |
294 | (goto-char (point-min)) | |
295 | (while (re-search-forward "^;+ ?" nil t) | |
296 | (replace-match "" nil nil)) | |
297 | (goto-char (point-min)) | |
298 | (setq buffer-read-only t) | |
299 | (set-buffer-modified-p nil) | |
300 | (shrink-window-if-larger-than-buffer) | |
8f713f53 | 301 | (finder-mode) |
e6fb4d11 | 302 | (finder-summary))) |
101dad14 ER |
303 | |
304 | (defun finder-current-item () | |
62832105 EZ |
305 | (let ((key (save-excursion |
306 | (beginning-of-line) | |
307 | (current-word)))) | |
308 | (if (or (and finder-headmark (< (point) finder-headmark)) | |
309 | (= (length key) 0)) | |
310 | (error "No keyword or filename on this line") | |
311 | key))) | |
101dad14 ER |
312 | |
313 | (defun finder-select () | |
eb4df0c3 | 314 | "Select item on current line in a finder buffer." |
101dad14 ER |
315 | (interactive) |
316 | (let ((key (finder-current-item))) | |
f3b330d6 KH |
317 | (if (string-match "\\.el$" key) |
318 | (finder-commentary key) | |
319 | (finder-list-matches key)))) | |
320 | ||
321 | (defun finder-mouse-select (event) | |
eb4df0c3 | 322 | "Select item in a finder buffer with the mouse." |
f3b330d6 KH |
323 | (interactive "e") |
324 | (save-excursion | |
eb4df0c3 DL |
325 | (set-buffer (window-buffer (posn-window (event-start event)))) |
326 | (goto-char (posn-point (event-start event))) | |
327 | (finder-select))) | |
101dad14 | 328 | |
706239c7 | 329 | ;;;###autoload |
1164bae9 ER |
330 | (defun finder-by-keyword () |
331 | "Find packages matching a given keyword." | |
332 | (interactive) | |
101dad14 ER |
333 | (finder-list-keywords)) |
334 | ||
335 | (defun finder-mode () | |
336 | "Major mode for browsing package documentation. | |
527da106 | 337 | \\<finder-mode-map> |
101dad14 | 338 | \\[finder-select] more help for the item on the current line |
2d212d87 | 339 | \\[finder-exit] exit Finder mode and kill the Finder buffer." |
101dad14 | 340 | (interactive) |
79760fb2 | 341 | (kill-all-local-variables) |
101dad14 ER |
342 | (use-local-map finder-mode-map) |
343 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
344 | (setq mode-name "Finder") | |
345 | (setq major-mode 'finder-mode) | |
79760fb2 SM |
346 | (set (make-local-variable 'finder-headmark) nil) |
347 | (run-mode-hooks 'finder-mode-hook)) | |
101dad14 ER |
348 | |
349 | (defun finder-summary () | |
350 | "Summarize basic Finder commands." | |
351 | (interactive) | |
90f061c7 | 352 | (message "%s" |
527da106 | 353 | (substitute-command-keys |
eb4df0c3 DL |
354 | "\\<finder-mode-map>\\[finder-select] = select, \ |
355 | \\[finder-mouse-select] = select, \\[finder-list-keywords] = to \ | |
356 | finder directory, \\[finder-exit] = quit, \\[finder-summary] = help"))) | |
f5a16823 | 357 | |
101dad14 | 358 | (defun finder-exit () |
35863d77 | 359 | "Exit Finder mode and kill the buffer." |
101dad14 | 360 | (interactive) |
b26f4fbd KH |
361 | (or (one-window-p t) |
362 | (delete-window)) | |
f3b330d6 | 363 | ;; Can happen in either buffer -- kill each of the two that exists |
b26f4fbd KH |
364 | (and (get-buffer "*Finder*") |
365 | (kill-buffer "*Finder*")) | |
366 | (and (get-buffer "*Finder Category*") | |
367 | (kill-buffer "*Finder Category*"))) | |
1164bae9 | 368 | |
6a7ceddc | 369 | \f |
1164bae9 ER |
370 | (provide 'finder) |
371 | ||
ab5796a9 | 372 | ;;; arch-tag: ec85ff49-8cb8-41f5-a63f-9131d53ce2c5 |
1164bae9 | 373 | ;;; finder.el ends here |