1 ;;; finder.el --- topic & keyword-based code finder
3 ;; Copyright (C) 1992 Free Software Foundation, Inc.
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
6 ;; Created: 16 Jun 1992
10 ;; This file is part of GNU Emacs.
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)
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.
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.
28 ;; This mode uses the Keywords library header to provide code-finding
29 ;; services by keyword.
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.
44 ;; Local variable in finder buffer.
45 (defvar finder-headmark
)
47 (defvar finder-known-keywords
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")
81 (defvar finder-mode-map 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
)
91 ;;; Code for regenerating the keyword list.
93 (defvar finder-package-info nil
94 "Assoc list mapping file names to description & keyword lists.")
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'."
101 (let ((processed nil
))
102 (find-file "finder-inf.el")
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")
116 (if (and (string-match "^[^=].*\\.el$" f
)
117 (not (member f processed
)))
118 (let (summary keystart keywords
)
119 (setq processed
(cons f processed
))
121 (set-buffer (get-buffer-create "*finder-scratch*"))
122 (buffer-disable-undo (current-buffer))
124 (insert-file-contents
125 (concat (file-name-as-directory (or d
".")) f
))
126 (setq summary
(lm-synopsis))
127 (setq keywords
(lm-keywords)))
129 (format " (\"%s\"\n " f
))
130 (prin1 summary
(current-buffer))
133 (setq keystart
(point))
135 (if keywords
(format "(%s)" keywords
) "nil")
137 (subst-char-in-region keystart
(point) ?
, ?
)
140 (directory-files (or d
".")))
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
149 ;;; Now the retrieval code
151 (defun finder-list-keywords ()
152 "Display descriptions of the keywords in the Finder buffer."
154 (setq buffer-read-only nil
)
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
)
170 (defun finder-list-matches (key)
171 (setq buffer-read-only nil
)
173 (let ((id (intern key
)))
175 "The following packages match the keyword `" key
"':\n\n")
176 (setq finder-headmark
(point))
178 (function (lambda (x)
179 (if (memq id
(car (cdr (cdr x
))))
183 (concat (car (cdr x
)) "\n"))
187 (goto-char (point-min))
189 (setq buffer-read-only t
)
190 (set-buffer-modified-p nil
)
191 (shrink-window-if-larger-than-buffer)
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
)
198 (let ((dirs load-path
)
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
)))
208 (defun finder-commentary (file)
210 (let* ((str (lm-commentary (finder-find-library file
))))
212 (error "Can't find any Commentary section."))
213 (pop-to-buffer "*Finder*")
214 (setq buffer-read-only nil
)
217 (goto-char (point-min))
219 (goto-char (point-max))
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)
231 (defun finder-current-item ()
232 (if (and finder-headmark
(< (point) finder-headmark
))
233 (error "No keyword or filename on this line")
238 (defun finder-select ()
240 (let ((key (finder-current-item)))
241 (if (string-match "\\.el$" key
)
242 (finder-commentary key
)
243 (finder-list-matches key
))))
245 (defun finder-by-keyword ()
246 "Find packages matching a given keyword."
249 (finder-list-keywords))
251 (defun finder-mode ()
252 "Major mode for browsing package documentation.
254 \\[finder-select] more help for the item on the current line
255 \\[finder-exit] exit Finder mode and kill the Finder buffer.
258 (pop-to-buffer "*Finder*")
259 (setq buffer-read-only nil
)
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
)
269 (defun finder-summary ()
270 "Summarize basic Finder commands."
273 (substitute-command-keys
274 "\\<finder-mode-map>\\[finder-select] = select, \\[finder-list-keywords] = back to finder, \\[finder-exit] = quit, \\[finder-summary] = help")))
276 (defun finder-exit ()
277 "Exit Finder mode and kill the buffer"
280 (kill-buffer "*Finder*"))
284 ;;; finder.el ends here