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