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