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