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