Commit | Line | Data |
---|---|---|
1164bae9 ER |
1 | ;;; finder.el --- topic & keyword-based code finder |
2 | ||
3 | ;; Copyright (C) 1992 Free Software Foundation, Inc. | |
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) | |
43 | (require 'picture) | |
44 | ||
14931973 RS |
45 | ;; Local variable in finder buffer. |
46 | (defvar finder-headmark) | |
47 | ||
1164bae9 ER |
48 | (defvar finder-known-keywords |
49 | '( | |
dea24479 | 50 | (abbrev . "abbreviation handling, typing shortcuts, macros") |
be2c35e4 | 51 | (bib . "code related to the `bib' bibliography processor") |
68f8f37f | 52 | (c . "support for the C language and related languages") |
1164bae9 | 53 | (calendar . "calendar and time management support") |
e9571d2a | 54 | (comm . "communications, networking, remote access to files") |
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) | |
91 | (define-key map "\C-m" 'finder-select) | |
92 | (define-key map "?" 'finder-summary) | |
93 | (define-key map "q" 'finder-exit) | |
94 | (define-key map "d" 'finder-list-keywords) | |
95 | (setq finder-mode-map map))) | |
96 | ||
101dad14 | 97 | |
1164bae9 ER |
98 | ;;; Code for regenerating the keyword list. |
99 | ||
100 | (defvar finder-package-info nil | |
101 | "Assoc list mapping file names to description & keyword lists.") | |
102 | ||
103 | (defun finder-compile-keywords (&rest dirs) | |
14931973 | 104 | "Regenerate the keywords association list into the file `finder-inf.el'. |
1164bae9 | 105 | Optional arguments are a list of Emacs Lisp directories to compile from; no |
fe668515 | 106 | arguments compiles from `load-path'." |
1164bae9 | 107 | (save-excursion |
c5d87135 ER |
108 | (let ((processed nil)) |
109 | (find-file "finder-inf.el") | |
110 | (erase-buffer) | |
e9571d2a ER |
111 | (insert ";;; finder-inf.el --- keyword-to-package mapping\n") |
112 | (insert ";; Keywords: help\n") | |
0cbe967f ER |
113 | (insert ";;; Commentary:\n") |
114 | (insert ";; Don't edit this file. It's generated by finder.el\n\n") | |
115 | (insert ";;; Code:\n") | |
c5d87135 ER |
116 | (insert "\n(setq finder-package-info '(\n") |
117 | (mapcar | |
118 | (function | |
119 | (lambda (d) | |
120 | (mapcar | |
121 | (function | |
122 | (lambda (f) | |
14931973 RS |
123 | (if (and (string-match "^[^=].*\\.el$" f) |
124 | (not (member f processed))) | |
125 | (let (summary keystart keywords) | |
c5d87135 ER |
126 | (setq processed (cons f processed)) |
127 | (save-excursion | |
128 | (set-buffer (get-buffer-create "*finder-scratch*")) | |
14931973 | 129 | (buffer-disable-undo (current-buffer)) |
c5d87135 ER |
130 | (erase-buffer) |
131 | (insert-file-contents | |
132 | (concat (file-name-as-directory (or d ".")) f)) | |
133 | (setq summary (lm-synopsis)) | |
134 | (setq keywords (lm-keywords))) | |
135 | (insert | |
136 | (format " (\"%s\"\n " f)) | |
137 | (prin1 summary (current-buffer)) | |
138 | (insert | |
139 | "\n ") | |
140 | (setq keystart (point)) | |
141 | (insert | |
142 | (if keywords (format "(%s)" keywords) "nil") | |
143 | ")\n") | |
144 | (subst-char-in-region keystart (point) ?, ? ) | |
71e683dd | 145 | ) |
c5d87135 ER |
146 | ))) |
147 | (directory-files (or d "."))) | |
148 | )) | |
149 | (or dirs load-path)) | |
0cbe967f | 150 | (insert "))\n\n(provide 'finder-inf)\n\n;;; finder-inf.el ends here\n") |
c5d87135 | 151 | (kill-buffer "*finder-scratch*") |
3688bf22 | 152 | (eval-current-buffer) ;; So we get the new keyword list immediately |
c5d87135 ER |
153 | (basic-save-buffer) |
154 | ))) | |
1164bae9 ER |
155 | |
156 | ;;; Now the retrieval code | |
157 | ||
101dad14 ER |
158 | (defun finder-list-keywords () |
159 | "Display descriptions of the keywords in the Finder buffer." | |
160 | (interactive) | |
161 | (setq buffer-read-only nil) | |
162 | (erase-buffer) | |
163 | (mapcar | |
164 | (function (lambda (assoc) | |
165 | (let ((keyword (car assoc))) | |
166 | (insert (symbol-name keyword)) | |
167 | (insert-at-column 14 (concat (cdr assoc) "\n")) | |
168 | (cons (symbol-name keyword) keyword)))) | |
169 | finder-known-keywords) | |
170 | (goto-char (point-min)) | |
14931973 | 171 | (setq finder-headmark (point)) |
101dad14 ER |
172 | (setq buffer-read-only t) |
173 | (set-buffer-modified-p nil) | |
174 | (balance-windows) | |
175 | (finder-summary)) | |
176 | ||
177 | (defun finder-list-matches (key) | |
178 | (setq buffer-read-only nil) | |
179 | (erase-buffer) | |
180 | (let ((id (intern key))) | |
181 | (insert | |
182 | "The following packages match the keyword `" key "':\n\n") | |
14931973 | 183 | (setq finder-headmark (point)) |
101dad14 ER |
184 | (mapcar |
185 | (function (lambda (x) | |
186 | (if (memq id (car (cdr (cdr x)))) | |
187 | (progn | |
188 | (insert (car x)) | |
189 | (insert-at-column 16 | |
190 | (concat (car (cdr x)) "\n")) | |
191 | )) | |
192 | )) | |
193 | finder-package-info) | |
194 | (goto-char (point-min)) | |
195 | (forward-line) | |
196 | (setq buffer-read-only t) | |
197 | (set-buffer-modified-p nil) | |
198 | (shrink-window-if-larger-than-buffer) | |
199 | (finder-summary))) | |
200 | ||
d90f825c RS |
201 | ;; Search for a file named FILE the same way `load' would search. |
202 | (defun finder-find-library (file) | |
203 | (if (file-name-absolute-p file) | |
204 | file | |
205 | (let ((dirs load-path) | |
206 | found) | |
207 | (while (and dirs (not found)) | |
208 | (if (file-exists-p (expand-file-name (concat file ".el") (car dirs))) | |
209 | (setq found (expand-file-name file (car dirs))) | |
210 | (if (file-exists-p (expand-file-name file (car dirs))) | |
211 | (setq found (expand-file-name file (car dirs))))) | |
212 | (setq dirs (cdr dirs))) | |
213 | found))) | |
214 | ||
101dad14 ER |
215 | (defun finder-commentary (file) |
216 | (interactive) | |
d90f825c | 217 | (let* ((str (lm-commentary (finder-find-library file)))) |
101dad14 | 218 | (if (null str) |
67a69ba6 | 219 | (error "Can't find any Commentary section")) |
101dad14 ER |
220 | (pop-to-buffer "*Finder*") |
221 | (setq buffer-read-only nil) | |
222 | (erase-buffer) | |
223 | (insert str) | |
224 | (goto-char (point-min)) | |
225 | (delete-blank-lines) | |
226 | (goto-char (point-max)) | |
227 | (delete-blank-lines) | |
228 | (goto-char (point-min)) | |
229 | (while (re-search-forward "^;+ ?" nil t) | |
230 | (replace-match "" nil nil)) | |
231 | (goto-char (point-min)) | |
232 | (setq buffer-read-only t) | |
233 | (set-buffer-modified-p nil) | |
234 | (shrink-window-if-larger-than-buffer) | |
235 | (finder-summary) | |
236 | )) | |
237 | ||
238 | (defun finder-current-item () | |
14931973 | 239 | (if (and finder-headmark (< (point) finder-headmark)) |
101dad14 ER |
240 | (error "No keyword or filename on this line") |
241 | (save-excursion | |
242 | (beginning-of-line) | |
243 | (current-word)))) | |
244 | ||
245 | (defun finder-select () | |
246 | (interactive) | |
247 | (let ((key (finder-current-item))) | |
248 | (if (string-match "\\.el$" key) | |
249 | (finder-commentary key) | |
250 | (finder-list-matches key)))) | |
251 | ||
1164bae9 ER |
252 | (defun finder-by-keyword () |
253 | "Find packages matching a given keyword." | |
254 | (interactive) | |
101dad14 ER |
255 | (finder-mode) |
256 | (finder-list-keywords)) | |
257 | ||
258 | (defun finder-mode () | |
259 | "Major mode for browsing package documentation. | |
527da106 | 260 | \\<finder-mode-map> |
101dad14 | 261 | \\[finder-select] more help for the item on the current line |
a7a0dbf5 | 262 | \\[finder-exit] exit Finder mode and kill the Finder buffer. |
101dad14 ER |
263 | " |
264 | (interactive) | |
265 | (pop-to-buffer "*Finder*") | |
266 | (setq buffer-read-only nil) | |
1164bae9 | 267 | (erase-buffer) |
101dad14 ER |
268 | (use-local-map finder-mode-map) |
269 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
270 | (setq mode-name "Finder") | |
271 | (setq major-mode 'finder-mode) | |
14931973 RS |
272 | (make-local-variable 'finder-headmark) |
273 | (setq finder-headmark nil) | |
101dad14 ER |
274 | ) |
275 | ||
276 | (defun finder-summary () | |
277 | "Summarize basic Finder commands." | |
278 | (interactive) | |
279 | (message | |
527da106 | 280 | (substitute-command-keys |
e141acb3 | 281 | "\\<finder-mode-map>\\[finder-select] = select, \\[finder-list-keywords] = to finder directory, \\[finder-exit] = quit, \\[finder-summary] = help"))) |
f5a16823 | 282 | |
101dad14 ER |
283 | (defun finder-exit () |
284 | "Exit Finder mode and kill the buffer" | |
285 | (interactive) | |
286 | (delete-window) | |
287 | (kill-buffer "*Finder*")) | |
1164bae9 ER |
288 | |
289 | (provide 'finder) | |
290 | ||
291 | ;;; finder.el ends here |