1 ;;; finder.el --- topic & keyword-based code finder
3 ;; Copyright (C) 1992, 1997 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")
121 (if (and (or (string-match "^[^=].*\\.el$" f
)
122 ;; Allow compressed files also. Fixme:
123 ;; generalize this, especially for
124 ;; MS-DOG-type filenames.
125 (and (string-match "^[^=].*\\.el\\.\\(gz\\|Z\\)$" f
)
126 (require 'jka-compr
)))
127 (not (member f processed
)))
128 (let (summary keystart keywords
)
129 (setq processed
(cons f processed
))
131 (set-buffer (get-buffer-create "*finder-scratch*"))
132 (buffer-disable-undo (current-buffer))
134 (insert-file-contents
135 (concat (file-name-as-directory (or d
".")) f
))
136 (setq summary
(lm-synopsis))
137 (setq keywords
(lm-keywords)))
139 (format " (\"%s\"\n "
140 (if (string-match "\\.\\(gz\\|Z\\)$" f
)
141 (file-name-sans-extension f
)
143 (prin1 summary
(current-buffer))
146 (setq keystart
(point))
148 (if keywords
(format "(%s)" keywords
) "nil")
150 (subst-char-in-region keystart
(point) ?
, ?
)
152 (directory-files (or d
"."))))
154 (insert "))\n\n(provide 'finder-inf)\n\n;;; finder-inf.el ends here\n")
155 (kill-buffer "*finder-scratch*")
156 (eval-current-buffer) ;; So we get the new keyword list immediately
157 (basic-save-buffer))))
159 (defun finder-compile-keywords-make-dist ()
160 "Regenerate `finder-inf.el' for the Emacs distribution."
161 (apply 'finder-compile-keywords command-line-args-left
)
164 ;;; Now the retrieval code
166 (defun finder-insert-at-column (column &rest strings
)
167 "Insert list of STRINGS, at column COLUMN."
168 (if (> (current-column) column
) (insert "\n"))
169 (move-to-column column
)
170 (let ((col (current-column)))
173 (if (and (/= col column
)
174 (= (preceding-char) ?
\t))
175 (let (indent-tabs-mode)
178 (move-to-column column
)))))
179 (apply 'insert strings
))
181 (defun finder-list-keywords ()
182 "Display descriptions of the keywords in the Finder buffer."
184 (if (get-buffer "*Finder*")
185 (pop-to-buffer "*Finder*")
186 (pop-to-buffer (set-buffer (get-buffer-create "*Finder*")))
188 (setq buffer-read-only nil
)
192 (let ((keyword (car assoc
)))
193 (insert (symbol-name keyword
))
194 (finder-insert-at-column 14 (concat (cdr assoc
) "\n"))
195 (cons (symbol-name keyword
) keyword
)))
196 finder-known-keywords
)
197 (goto-char (point-min))
198 (setq finder-headmark
(point))
199 (setq buffer-read-only t
)
200 (set-buffer-modified-p nil
)
204 (defun finder-list-matches (key)
205 (pop-to-buffer (set-buffer (get-buffer-create "*Finder Category*")))
207 (setq buffer-read-only nil
)
209 (let ((id (intern key
)))
211 "The following packages match the keyword `" key
"':\n\n")
212 (setq finder-headmark
(point))
215 (if (memq id
(car (cdr (cdr x
))))
218 (finder-insert-at-column 16 (concat (car (cdr x
)) "\n")))))
220 (goto-char (point-min))
222 (setq buffer-read-only t
)
223 (set-buffer-modified-p nil
)
224 (shrink-window-if-larger-than-buffer)
227 ;; Search for a file named FILE on `load-path', also trying compressed
228 ;; versions if jka-compr is in use.
229 (defun finder-find-library (library)
230 (or (locate-library library t
)
231 (if (rassq 'jka-compr-handler file-name-handler-alist
)
232 (or (locate-library (concat library
".gz") t
)
233 (locate-library (concat library
".Z") t
)
234 ;; last resort for MS-DOG et al
235 (locate-library (concat library
"z"))))))
237 (defun finder-commentary (file)
239 (let* ((str (lm-commentary (finder-find-library file
))))
241 (error "Can't find any Commentary section"))
242 (pop-to-buffer "*Finder*")
243 (setq buffer-read-only nil
)
246 (goto-char (point-min))
248 (goto-char (point-max))
250 (goto-char (point-min))
251 (while (re-search-forward "^;+ ?" nil t
)
252 (replace-match "" nil nil
))
253 (goto-char (point-min))
254 (setq buffer-read-only t
)
255 (set-buffer-modified-p nil
)
256 (shrink-window-if-larger-than-buffer)
259 (defun finder-current-item ()
260 (if (and finder-headmark
(< (point) finder-headmark
))
261 (error "No keyword or filename on this line")
266 (defun finder-select ()
268 (let ((key (finder-current-item)))
269 (if (string-match "\\.el$" key
)
270 (finder-commentary key
)
271 (finder-list-matches key
))))
273 (defun finder-mouse-select (event)
276 (set-buffer (window-buffer (posn-window (event-start event
))))
277 (goto-char (posn-point (event-start event
)))
278 (let ((key (finder-current-item)))
281 (defun finder-by-keyword ()
282 "Find packages matching a given keyword."
284 (finder-list-keywords))
286 (defun finder-mode ()
287 "Major mode for browsing package documentation.
289 \\[finder-select] more help for the item on the current line
290 \\[finder-exit] exit Finder mode and kill the Finder buffer.
293 (use-local-map finder-mode-map
)
294 (set-syntax-table emacs-lisp-mode-syntax-table
)
295 (setq mode-name
"Finder")
296 (setq major-mode
'finder-mode
)
297 (make-local-variable 'finder-headmark
)
298 (setq finder-headmark nil
))
300 (defun finder-summary ()
301 "Summarize basic Finder commands."
304 (substitute-command-keys
305 "\\<finder-mode-map>\\[finder-select] = select, \\[finder-mouse-select] = select, \\[finder-list-keywords] = to finder directory, \\[finder-exit] = quit, \\[finder-summary] = help")))
307 (defun finder-exit ()
308 "Exit Finder mode and kill the buffer."
312 ;; Can happen in either buffer -- kill each of the two that exists
313 (and (get-buffer "*Finder*")
314 (kill-buffer "*Finder*"))
315 (and (get-buffer "*Finder Category*")
316 (kill-buffer "*Finder Category*")))
320 ;;; finder.el ends here