1 ;;; finder.el --- topic & keyword-based code finder
3 ;; Copyright (C) 1992, 1997, 1998 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 2, 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 the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; This mode uses the Keywords library header to provide code-finding
30 ;; services by keyword.
33 ;; 1. Support multiple keywords per search. This could be extremely hairy;
34 ;; there doesn't seem to be any way to get completing-read to exit on
35 ;; an EOL with no substring pending, which is what we'd want to end the loop.
36 ;; 2. Search by string in synopsis line?
37 ;; 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 .
"support for the C language and related languages")
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")
85 (defvar finder-mode-map nil
)
87 (let ((map (make-sparse-keymap)))
88 (define-key map
" " 'finder-select
)
89 (define-key map
"f" 'finder-select
)
90 (define-key map
[mouse-2
] 'finder-mouse-select
)
91 (define-key map
"\C-m" 'finder-select
)
92 (define-key map
"?" 'finder-summary
)
93 (define-key map
"q" 'finder-exit
)
94 (define-key map
"d" 'finder-list-keywords
)
95 (setq finder-mode-map map
)))
98 ;;; Code for regenerating the keyword list.
100 (defvar finder-package-info nil
101 "Assoc list mapping file names to description & keyword lists.")
103 (defun finder-compile-keywords (&rest dirs
)
104 "Regenerate the keywords association list into the file `finder-inf.el'.
105 Optional arguments are a list of Emacs Lisp directories to compile from; no
106 arguments compiles from `load-path'."
108 (let ((processed nil
))
109 (find-file "finder-inf.el")
111 (insert ";;; finder-inf.el --- keyword-to-package mapping\n")
112 (insert ";; Keywords: help\n")
113 (insert ";;; Commentary:\n")
114 (insert ";; Don't edit this file. It's generated by finder.el\n\n")
115 (insert ";;; Code:\n")
116 (insert "\n(setq finder-package-info '(\n")
119 (when (file-exists-p (directory-file-name d
))
120 (message "Directory %s" d
)
123 (if (and (or (string-match "^[^=].*\\.el$" f
)
124 ;; Allow compressed files also. Fixme:
125 ;; generalize this, especially for
126 ;; MS-DOG-type filenames.
127 (and (string-match "^[^=].*\\.el\\.\\(gz\\|Z\\)$" f
)
128 (require 'jka-compr
)))
129 ;; Ignore lock files.
130 (not (string-match "^.#" f
))
131 (not (member f processed
)))
132 (let (summary keystart keywords
)
133 (setq processed
(cons f processed
))
135 (set-buffer (get-buffer-create "*finder-scratch*"))
136 (buffer-disable-undo (current-buffer))
138 (insert-file-contents
139 (concat (file-name-as-directory (or d
".")) f
))
140 (setq summary
(lm-synopsis))
141 (setq keywords
(lm-keywords)))
143 (format " (\"%s\"\n "
144 (if (string-match "\\.\\(gz\\|Z\\)$" f
)
145 (file-name-sans-extension f
)
147 (prin1 summary
(current-buffer))
150 (setq keystart
(point))
152 (if keywords
(format "(%s)" keywords
) "nil")
154 (subst-char-in-region keystart
(point) ?
, ?
)
156 (directory-files (or d
".")))))
158 (insert "))\n\n(provide 'finder-inf)\n\n;;; finder-inf.el ends here\n")
159 (kill-buffer "*finder-scratch*")
160 (eval-current-buffer) ;; So we get the new keyword list immediately
161 (basic-save-buffer))))
163 (defun finder-compile-keywords-make-dist ()
164 "Regenerate `finder-inf.el' for the Emacs distribution."
165 (apply 'finder-compile-keywords command-line-args-left
)
168 ;;; Now the retrieval code
170 (defun finder-insert-at-column (column &rest strings
)
171 "Insert, at column COLUMN, other args STRINGS."
172 (if (> (current-column) column
) (insert "\n"))
173 (move-to-column column t
)
174 (apply 'insert strings
))
176 (defun finder-mouse-face-on-line ()
177 "Put a `mouse-face' property on the previous line."
180 (put-text-property (save-excursion (beginning-of-line) (point))
181 (progn (end-of-line) (point))
182 'mouse-face
'highlight
)))
184 (defun finder-list-keywords ()
185 "Display descriptions of the keywords in the Finder buffer."
187 (if (get-buffer "*Finder*")
188 (pop-to-buffer "*Finder*")
189 (pop-to-buffer (set-buffer (get-buffer-create "*Finder*")))
191 (setq buffer-read-only nil
)
195 (let ((keyword (car assoc
)))
196 (insert (symbol-name keyword
))
197 (finder-insert-at-column 14 (concat (cdr assoc
) "\n"))
198 (finder-mouse-face-on-line)))
199 finder-known-keywords
)
200 (goto-char (point-min))
201 (setq finder-headmark
(point))
202 (setq buffer-read-only t
)
203 (set-buffer-modified-p nil
)
207 (defun finder-list-matches (key)
208 (pop-to-buffer (set-buffer (get-buffer-create "*Finder Category*")))
210 (setq buffer-read-only nil
)
212 (let ((id (intern key
)))
214 "The following packages match the keyword `" key
"':\n\n")
215 (setq finder-headmark
(point))
218 (if (memq id
(car (cdr (cdr x
))))
221 (finder-insert-at-column 16 (concat (nth 1 x
) "\n"))
222 (finder-mouse-face-on-line))))
224 (goto-char (point-min))
226 (setq buffer-read-only t
)
227 (set-buffer-modified-p nil
)
228 (shrink-window-if-larger-than-buffer)
231 ;; Search for a file named FILE on `load-path', also trying compressed
232 ;; versions if jka-compr is in use.
233 (defun finder-find-library (library)
234 (or (locate-library library t
)
235 (if (rassq 'jka-compr-handler file-name-handler-alist
)
236 (or (locate-library (concat library
".gz") t
)
237 (locate-library (concat library
".Z") t
)
238 ;; last resort for MS-DOG et al
239 (locate-library (concat library
"z"))))))
241 (defun finder-commentary (file)
242 "Display FILE's commentary section."
244 (let* ((str (lm-commentary (finder-find-library file
))))
246 (error "Can't find any Commentary section"))
247 (pop-to-buffer "*Finder*")
248 (setq buffer-read-only nil
)
251 (goto-char (point-min))
253 (goto-char (point-max))
255 (goto-char (point-min))
256 (while (re-search-forward "^;+ ?" nil t
)
257 (replace-match "" nil nil
))
258 (goto-char (point-min))
259 (setq buffer-read-only t
)
260 (set-buffer-modified-p nil
)
261 (shrink-window-if-larger-than-buffer)
264 (defun finder-current-item ()
265 (if (and finder-headmark
(< (point) finder-headmark
))
266 (error "No keyword or filename on this line")
271 (defun finder-select ()
272 "Select item on current line in a finder buffer."
274 (let ((key (finder-current-item)))
275 (if (string-match "\\.el$" key
)
276 (finder-commentary key
)
277 (finder-list-matches key
))))
279 (defun finder-mouse-select (event)
280 "Select item in a finder buffer with the mouse."
283 (set-buffer (window-buffer (posn-window (event-start event
))))
284 (goto-char (posn-point (event-start event
)))
287 (defun finder-by-keyword ()
288 "Find packages matching a given keyword."
290 (finder-list-keywords))
292 (defun finder-mode ()
293 "Major mode for browsing package documentation.
295 \\[finder-select] more help for the item on the current line
296 \\[finder-exit] exit Finder mode and kill the Finder buffer.
299 (use-local-map finder-mode-map
)
300 (set-syntax-table emacs-lisp-mode-syntax-table
)
301 (setq mode-name
"Finder")
302 (setq major-mode
'finder-mode
)
303 (make-local-variable 'finder-headmark
)
304 (setq finder-headmark nil
))
306 (defun finder-summary ()
307 "Summarize basic Finder commands."
310 (substitute-command-keys
311 "\\<finder-mode-map>\\[finder-select] = select, \
312 \\[finder-mouse-select] = select, \\[finder-list-keywords] = to \
313 finder directory, \\[finder-exit] = quit, \\[finder-summary] = help")))
315 (defun finder-exit ()
316 "Exit Finder mode and kill the buffer."
320 ;; Can happen in either buffer -- kill each of the two that exists
321 (and (get-buffer "*Finder*")
322 (kill-buffer "*Finder*"))
323 (and (get-buffer "*Finder Category*")
324 (kill-buffer "*Finder Category*")))
328 ;;; finder.el ends here