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