]> code.delx.au - gnu-emacs/blob - lisp/finder.el
(finder-known-keywords):
[gnu-emacs] / lisp / finder.el
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
14 ;; the Free Software Foundation; either version 1, or (at your option)
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
23 ;; along with GNU Emacs; see the file COPYING. If not, write to
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25
26 ;;; Commentary:
27
28 ;; This mode uses the Keywords library header to provide code-finding
29 ;; services by keyword.
30 ;;
31 ;; Things to do:
32 ;; 1. Support multiple keywords per search. This could be extremely hairy;
33 ;; there doesn't seem to be any way to get completing-read to exit on
34 ;; an EOL with no substring pending, which is what we'd want to end the loop.
35 ;; 2. Search by string in synopsis line?
36 ;; 3. Function to check finder-package-info for unknown keywords.
37
38 ;;; Code:
39
40 (require 'lisp-mnt)
41 (require 'finder-inf)
42 (require 'picture)
43
44 ;; Local variable in finder buffer.
45 (defvar finder-headmark)
46
47 (defvar finder-known-keywords
48 '(
49 (abbrev . "abbreviation handling, typing shortcuts, macros")
50 (bib . "code related to the `bib' bibliography processor")
51 (c . "C and C++ language support")
52 (calendar . "calendar and time management support")
53 (comm . "communications, networking, remote access to files")
54 (data . "support editing files of data")
55 (docs . "support for Emacs documentation")
56 (emulations . "emulations of other editors")
57 (extensions . "Emacs Lisp language extensions")
58 (faces . "support for multiple fonts")
59 (frames . "support for Emacs frames and window systems")
60 (games . "games, jokes and amusements")
61 (hardware . "support for interfacing with exotic hardware")
62 (help . "support for on-line help systems")
63 (hypermedia . "support for links between text or other media types")
64 (i18n . "internationalization and alternate character-set support")
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")
71 (matching . "various sorts of searching and matching")
72 (mouse . "mouse support")
73 (news . "support for netnews reading and posting")
74 (oop . "support for object-oriented programming")
75 (outlines . "support for hierarchical outlining")
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
85 (defvar finder-mode-map nil)
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
96
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)
103 "Regenerate the keywords association list into the file `finder-inf.el'.
104 Optional arguments are a list of Emacs Lisp directories to compile from; no
105 arguments compiles from `load-path'."
106 (save-excursion
107 (let ((processed nil))
108 (find-file "finder-inf.el")
109 (erase-buffer)
110 (insert ";;; finder-inf.el --- keyword-to-package mapping\n")
111 (insert ";; Keywords: help\n")
112 (insert ";;; Commentary:\n")
113 (insert ";; Don't edit this file. It's generated by finder.el\n\n")
114 (insert ";;; Code:\n")
115 (insert "\n(setq finder-package-info '(\n")
116 (mapcar
117 (function
118 (lambda (d)
119 (mapcar
120 (function
121 (lambda (f)
122 (if (and (string-match "^[^=].*\\.el$" f)
123 (not (member f processed)))
124 (let (summary keystart keywords)
125 (setq processed (cons f processed))
126 (save-excursion
127 (set-buffer (get-buffer-create "*finder-scratch*"))
128 (buffer-disable-undo (current-buffer))
129 (erase-buffer)
130 (insert-file-contents
131 (concat (file-name-as-directory (or d ".")) f))
132 (setq summary (lm-synopsis))
133 (setq keywords (lm-keywords)))
134 (insert
135 (format " (\"%s\"\n " f))
136 (prin1 summary (current-buffer))
137 (insert
138 "\n ")
139 (setq keystart (point))
140 (insert
141 (if keywords (format "(%s)" keywords) "nil")
142 ")\n")
143 (subst-char-in-region keystart (point) ?, ? )
144 )
145 )))
146 (directory-files (or d ".")))
147 ))
148 (or dirs load-path))
149 (insert "))\n\n(provide 'finder-inf)\n\n;;; finder-inf.el ends here\n")
150 (kill-buffer "*finder-scratch*")
151 (eval-current-buffer) ;; So we get the new keyword list immediately
152 (basic-save-buffer)
153 )))
154
155 ;;; Now the retrieval code
156
157 (defun finder-list-keywords ()
158 "Display descriptions of the keywords in the Finder buffer."
159 (interactive)
160 (setq buffer-read-only nil)
161 (erase-buffer)
162 (mapcar
163 (function (lambda (assoc)
164 (let ((keyword (car assoc)))
165 (insert (symbol-name keyword))
166 (insert-at-column 14 (concat (cdr assoc) "\n"))
167 (cons (symbol-name keyword) keyword))))
168 finder-known-keywords)
169 (goto-char (point-min))
170 (setq finder-headmark (point))
171 (setq buffer-read-only t)
172 (set-buffer-modified-p nil)
173 (balance-windows)
174 (finder-summary))
175
176 (defun finder-list-matches (key)
177 (setq buffer-read-only nil)
178 (erase-buffer)
179 (let ((id (intern key)))
180 (insert
181 "The following packages match the keyword `" key "':\n\n")
182 (setq finder-headmark (point))
183 (mapcar
184 (function (lambda (x)
185 (if (memq id (car (cdr (cdr x))))
186 (progn
187 (insert (car x))
188 (insert-at-column 16
189 (concat (car (cdr x)) "\n"))
190 ))
191 ))
192 finder-package-info)
193 (goto-char (point-min))
194 (forward-line)
195 (setq buffer-read-only t)
196 (set-buffer-modified-p nil)
197 (shrink-window-if-larger-than-buffer)
198 (finder-summary)))
199
200 ;; Search for a file named FILE the same way `load' would search.
201 (defun finder-find-library (file)
202 (if (file-name-absolute-p file)
203 file
204 (let ((dirs load-path)
205 found)
206 (while (and dirs (not found))
207 (if (file-exists-p (expand-file-name (concat file ".el") (car dirs)))
208 (setq found (expand-file-name file (car dirs)))
209 (if (file-exists-p (expand-file-name file (car dirs)))
210 (setq found (expand-file-name file (car dirs)))))
211 (setq dirs (cdr dirs)))
212 found)))
213
214 (defun finder-commentary (file)
215 (interactive)
216 (let* ((str (lm-commentary (finder-find-library file))))
217 (if (null str)
218 (error "Can't find any Commentary section"))
219 (pop-to-buffer "*Finder*")
220 (setq buffer-read-only nil)
221 (erase-buffer)
222 (insert str)
223 (goto-char (point-min))
224 (delete-blank-lines)
225 (goto-char (point-max))
226 (delete-blank-lines)
227 (goto-char (point-min))
228 (while (re-search-forward "^;+ ?" nil t)
229 (replace-match "" nil nil))
230 (goto-char (point-min))
231 (setq buffer-read-only t)
232 (set-buffer-modified-p nil)
233 (shrink-window-if-larger-than-buffer)
234 (finder-summary)
235 ))
236
237 (defun finder-current-item ()
238 (if (and finder-headmark (< (point) finder-headmark))
239 (error "No keyword or filename on this line")
240 (save-excursion
241 (beginning-of-line)
242 (current-word))))
243
244 (defun finder-select ()
245 (interactive)
246 (let ((key (finder-current-item)))
247 (if (string-match "\\.el$" key)
248 (finder-commentary key)
249 (finder-list-matches key))))
250
251 (defun finder-by-keyword ()
252 "Find packages matching a given keyword."
253 (interactive)
254 (finder-mode)
255 (finder-list-keywords))
256
257 (defun finder-mode ()
258 "Major mode for browsing package documentation.
259 \\<finder-mode-map>
260 \\[finder-select] more help for the item on the current line
261 \\[finder-exit] exit Finder mode and kill the Finder buffer.
262 "
263 (interactive)
264 (pop-to-buffer "*Finder*")
265 (setq buffer-read-only nil)
266 (erase-buffer)
267 (use-local-map finder-mode-map)
268 (set-syntax-table emacs-lisp-mode-syntax-table)
269 (setq mode-name "Finder")
270 (setq major-mode 'finder-mode)
271 (make-local-variable 'finder-headmark)
272 (setq finder-headmark nil)
273 )
274
275 (defun finder-summary ()
276 "Summarize basic Finder commands."
277 (interactive)
278 (message
279 (substitute-command-keys
280 "\\<finder-mode-map>\\[finder-select] = select, \\[finder-list-keywords] = to finder directory, \\[finder-exit] = quit, \\[finder-summary] = help")))
281
282 (defun finder-exit ()
283 "Exit Finder mode and kill the buffer"
284 (interactive)
285 (delete-window)
286 (kill-buffer "*Finder*"))
287
288 (provide 'finder)
289
290 ;;; finder.el ends here