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