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) | |
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") |
be2c35e4 | 54 | (data . "support editing files of data") |
1164bae9 ER |
55 | (docs . "support for Emacs documentation") |
56 | (emulations . "emulations of other editors") | |
57 | (extensions . "Emacs Lisp language extensions") | |
be2c35e4 | 58 | (faces . "support for multiple fonts") |
f1aabce0 | 59 | (frames . "support for Emacs frames and window systems") |
1164bae9 ER |
60 | (games . "games, jokes and amusements") |
61 | (hardware . "support for interfacing with exotic hardware") | |
62 | (help . "support for on-line help systems") | |
f1aabce0 | 63 | (hypermedia . "support for links between text or other media types") |
14931973 | 64 | (i18n . "internationalization and alternate character-set support") |
1164bae9 ER |
65 | (internal . "code for Emacs internals, build process, defaults") |
66 | (languages . "specialized modes for editing programming languages") | |
67 | (lisp . "Lisp support, including Emacs Lisp") | |
68 | (local . "code local to your site") | |
69 | (maint . "maintenance aids for the Emacs development group") | |
70 | (mail . "modes for electronic-mail handling") | |
be2c35e4 RS |
71 | (matching . "various sorts of searching and matching") |
72 | (mouse . "mouse support") | |
1164bae9 | 73 | (news . "support for netnews reading and posting") |
f1aabce0 RS |
74 | (oop . "support for object-oriented programming") |
75 | (outlines . "support for hierarchical outlining") | |
1164bae9 ER |
76 | (processes . "process, subshell, compilation, and job control support") |
77 | (terminals . "support for terminal types") | |
78 | (tex . "code related to the TeX formatter") | |
79 | (tools . "programming tools") | |
80 | (unix . "front-ends/assistants for, or emulators of, UNIX features") | |
81 | (vms . "support code for vms") | |
82 | (wp . "word processing") | |
83 | )) | |
84 | ||
101dad14 | 85 | (defvar finder-mode-map nil) |
e141acb3 RS |
86 | (or finder-mode-map |
87 | (let ((map (make-sparse-keymap))) | |
88 | (define-key map " " 'finder-select) | |
89 | (define-key map "f" 'finder-select) | |
90 | (define-key map "\C-m" 'finder-select) | |
91 | (define-key map "?" 'finder-summary) | |
92 | (define-key map "q" 'finder-exit) | |
93 | (define-key map "d" 'finder-list-keywords) | |
94 | (setq finder-mode-map map))) | |
95 | ||
101dad14 | 96 | |
1164bae9 ER |
97 | ;;; Code for regenerating the keyword list. |
98 | ||
99 | (defvar finder-package-info nil | |
100 | "Assoc list mapping file names to description & keyword lists.") | |
101 | ||
102 | (defun finder-compile-keywords (&rest dirs) | |
14931973 | 103 | "Regenerate the keywords association list into the file `finder-inf.el'. |
1164bae9 | 104 | Optional arguments are a list of Emacs Lisp directories to compile from; no |
fe668515 | 105 | arguments compiles from `load-path'." |
1164bae9 | 106 | (save-excursion |
c5d87135 ER |
107 | (let ((processed nil)) |
108 | (find-file "finder-inf.el") | |
109 | (erase-buffer) | |
e9571d2a ER |
110 | (insert ";;; finder-inf.el --- keyword-to-package mapping\n") |
111 | (insert ";; Keywords: help\n") | |
0cbe967f ER |
112 | (insert ";;; Commentary:\n") |
113 | (insert ";; Don't edit this file. It's generated by finder.el\n\n") | |
114 | (insert ";;; Code:\n") | |
c5d87135 ER |
115 | (insert "\n(setq finder-package-info '(\n") |
116 | (mapcar | |
e6fb4d11 EN |
117 | (lambda (d) |
118 | (mapcar | |
119 | (lambda (f) | |
120 | (if (and (string-match "^[^=].*\\.el$" f) | |
121 | (not (member f processed))) | |
122 | (let (summary keystart keywords) | |
123 | (setq processed (cons f processed)) | |
124 | (save-excursion | |
125 | (set-buffer (get-buffer-create "*finder-scratch*")) | |
126 | (buffer-disable-undo (current-buffer)) | |
127 | (erase-buffer) | |
128 | (insert-file-contents | |
129 | (concat (file-name-as-directory (or d ".")) f)) | |
130 | (setq summary (lm-synopsis)) | |
131 | (setq keywords (lm-keywords))) | |
132 | (insert | |
133 | (format " (\"%s\"\n " f)) | |
134 | (prin1 summary (current-buffer)) | |
135 | (insert | |
136 | "\n ") | |
137 | (setq keystart (point)) | |
138 | (insert | |
139 | (if keywords (format "(%s)" keywords) "nil") | |
140 | ")\n") | |
141 | (subst-char-in-region keystart (point) ?, ? ) | |
142 | ))) | |
143 | (directory-files (or d ".")))) | |
c5d87135 | 144 | (or dirs load-path)) |
0cbe967f | 145 | (insert "))\n\n(provide 'finder-inf)\n\n;;; finder-inf.el ends here\n") |
c5d87135 | 146 | (kill-buffer "*finder-scratch*") |
3688bf22 | 147 | (eval-current-buffer) ;; So we get the new keyword list immediately |
e6fb4d11 | 148 | (basic-save-buffer)))) |
1164bae9 | 149 | |
1c6425ea RS |
150 | (defun finder-compile-keywords-make-dist () |
151 | "Regenerate `finder-inf.el' for the Emacs distribution." | |
644f58eb RS |
152 | (apply 'finder-compile-keywords command-line-args-left) |
153 | (kill-emacs)) | |
1c6425ea | 154 | |
1164bae9 ER |
155 | ;;; Now the retrieval code |
156 | ||
e6fb4d11 EN |
157 | (defun finder-insert-at-column (column &rest strings) |
158 | "Insert list of STRINGS, at column COLUMN." | |
159 | (if (> (current-column) column) (insert "\n")) | |
160 | (move-to-column column) | |
161 | (let ((col (current-column))) | |
162 | (if (< col column) | |
163 | (indent-to column) | |
164 | (if (and (/= col column) | |
165 | (= (preceding-char) ?\t)) | |
166 | (let (indent-tabs-mode) | |
167 | (delete-char -1) | |
168 | (indent-to col) | |
169 | (move-to-column column))))) | |
170 | (apply 'insert strings)) | |
171 | ||
101dad14 ER |
172 | (defun finder-list-keywords () |
173 | "Display descriptions of the keywords in the Finder buffer." | |
174 | (interactive) | |
175 | (setq buffer-read-only nil) | |
176 | (erase-buffer) | |
177 | (mapcar | |
e6fb4d11 EN |
178 | (lambda (assoc) |
179 | (let ((keyword (car assoc))) | |
180 | (insert (symbol-name keyword)) | |
181 | (finder-insert-at-column 14 (concat (cdr assoc) "\n")) | |
182 | (cons (symbol-name keyword) keyword))) | |
101dad14 ER |
183 | finder-known-keywords) |
184 | (goto-char (point-min)) | |
14931973 | 185 | (setq finder-headmark (point)) |
101dad14 ER |
186 | (setq buffer-read-only t) |
187 | (set-buffer-modified-p nil) | |
188 | (balance-windows) | |
189 | (finder-summary)) | |
190 | ||
191 | (defun finder-list-matches (key) | |
192 | (setq buffer-read-only nil) | |
193 | (erase-buffer) | |
194 | (let ((id (intern key))) | |
195 | (insert | |
196 | "The following packages match the keyword `" key "':\n\n") | |
14931973 | 197 | (setq finder-headmark (point)) |
101dad14 | 198 | (mapcar |
e6fb4d11 EN |
199 | (lambda (x) |
200 | (if (memq id (car (cdr (cdr x)))) | |
201 | (progn | |
202 | (insert (car x)) | |
203 | (finder-insert-at-column 16 (concat (car (cdr x)) "\n"))))) | |
101dad14 ER |
204 | finder-package-info) |
205 | (goto-char (point-min)) | |
206 | (forward-line) | |
207 | (setq buffer-read-only t) | |
208 | (set-buffer-modified-p nil) | |
209 | (shrink-window-if-larger-than-buffer) | |
210 | (finder-summary))) | |
211 | ||
d90f825c RS |
212 | ;; Search for a file named FILE the same way `load' would search. |
213 | (defun finder-find-library (file) | |
214 | (if (file-name-absolute-p file) | |
215 | file | |
216 | (let ((dirs load-path) | |
217 | found) | |
218 | (while (and dirs (not found)) | |
219 | (if (file-exists-p (expand-file-name (concat file ".el") (car dirs))) | |
220 | (setq found (expand-file-name file (car dirs))) | |
221 | (if (file-exists-p (expand-file-name file (car dirs))) | |
222 | (setq found (expand-file-name file (car dirs))))) | |
223 | (setq dirs (cdr dirs))) | |
224 | found))) | |
225 | ||
101dad14 ER |
226 | (defun finder-commentary (file) |
227 | (interactive) | |
d90f825c | 228 | (let* ((str (lm-commentary (finder-find-library file)))) |
101dad14 | 229 | (if (null str) |
67a69ba6 | 230 | (error "Can't find any Commentary section")) |
101dad14 ER |
231 | (pop-to-buffer "*Finder*") |
232 | (setq buffer-read-only nil) | |
233 | (erase-buffer) | |
234 | (insert str) | |
235 | (goto-char (point-min)) | |
236 | (delete-blank-lines) | |
237 | (goto-char (point-max)) | |
238 | (delete-blank-lines) | |
239 | (goto-char (point-min)) | |
240 | (while (re-search-forward "^;+ ?" nil t) | |
241 | (replace-match "" nil nil)) | |
242 | (goto-char (point-min)) | |
243 | (setq buffer-read-only t) | |
244 | (set-buffer-modified-p nil) | |
245 | (shrink-window-if-larger-than-buffer) | |
e6fb4d11 | 246 | (finder-summary))) |
101dad14 ER |
247 | |
248 | (defun finder-current-item () | |
14931973 | 249 | (if (and finder-headmark (< (point) finder-headmark)) |
101dad14 ER |
250 | (error "No keyword or filename on this line") |
251 | (save-excursion | |
252 | (beginning-of-line) | |
253 | (current-word)))) | |
254 | ||
255 | (defun finder-select () | |
256 | (interactive) | |
257 | (let ((key (finder-current-item))) | |
258 | (if (string-match "\\.el$" key) | |
259 | (finder-commentary key) | |
260 | (finder-list-matches key)))) | |
261 | ||
1164bae9 ER |
262 | (defun finder-by-keyword () |
263 | "Find packages matching a given keyword." | |
264 | (interactive) | |
101dad14 ER |
265 | (finder-mode) |
266 | (finder-list-keywords)) | |
267 | ||
268 | (defun finder-mode () | |
269 | "Major mode for browsing package documentation. | |
527da106 | 270 | \\<finder-mode-map> |
101dad14 | 271 | \\[finder-select] more help for the item on the current line |
a7a0dbf5 | 272 | \\[finder-exit] exit Finder mode and kill the Finder buffer. |
101dad14 ER |
273 | " |
274 | (interactive) | |
275 | (pop-to-buffer "*Finder*") | |
276 | (setq buffer-read-only nil) | |
1164bae9 | 277 | (erase-buffer) |
101dad14 ER |
278 | (use-local-map finder-mode-map) |
279 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
280 | (setq mode-name "Finder") | |
281 | (setq major-mode 'finder-mode) | |
14931973 | 282 | (make-local-variable 'finder-headmark) |
e6fb4d11 | 283 | (setq finder-headmark nil)) |
101dad14 ER |
284 | |
285 | (defun finder-summary () | |
286 | "Summarize basic Finder commands." | |
287 | (interactive) | |
90f061c7 | 288 | (message "%s" |
527da106 | 289 | (substitute-command-keys |
e141acb3 | 290 | "\\<finder-mode-map>\\[finder-select] = select, \\[finder-list-keywords] = to finder directory, \\[finder-exit] = quit, \\[finder-summary] = help"))) |
f5a16823 | 291 | |
101dad14 ER |
292 | (defun finder-exit () |
293 | "Exit Finder mode and kill the buffer" | |
294 | (interactive) | |
295 | (delete-window) | |
296 | (kill-buffer "*Finder*")) | |
1164bae9 ER |
297 | |
298 | (provide 'finder) | |
299 | ||
300 | ;;; finder.el ends here |