1 ;;; finder.el --- topic & keyword-based code finder
3 ;; Copyright (C) 1992, 1997, 1998, 1999, 2001 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.
42 ;; Use `load' rather than `require' so that it doesn't get loaded
43 ;; during byte-compilation (at which point it might be missing).
44 (load "finder-inf" nil t
)
46 ;; Local variable in finder buffer.
47 (defvar finder-headmark
)
49 ;; These are supposed to correspond to top-level customization groups,
51 (defvar finder-known-keywords
53 (abbrev .
"abbreviation handling, typing shortcuts, macros")
55 (bib .
"code related to the `bib' bibliography processor")
56 (c .
"support for the C language and related languages")
57 (calendar .
"calendar and time management support")
58 (comm .
"communications, networking, remote access to files")
59 (convenience .
"convenience features for faster editing")
60 (data .
"support editing files of data")
61 (docs .
"support for Emacs documentation")
62 (emulations .
"emulations of other editors")
63 (extensions .
"Emacs Lisp language extensions")
64 (faces .
"support for multiple fonts")
65 (files .
"support for editing and manipulating files")
66 (frames .
"support for Emacs frames and window systems")
67 (games .
"games, jokes and amusements")
68 (hardware .
"support for interfacing with exotic hardware")
69 (help .
"support for on-line help systems")
70 (hypermedia .
"support for links between text or other media types")
71 (i18n .
"internationalization and alternate character-set support")
72 (internal .
"code for Emacs internals, build process, defaults")
73 (languages .
"specialized modes for editing programming languages")
74 (lisp .
"Lisp support, including Emacs Lisp")
75 (local .
"code local to your site")
76 (maint .
"maintenance aids for the Emacs development group")
77 (mail .
"modes for electronic-mail handling")
78 (matching .
"various sorts of searching and matching")
79 (mouse .
"mouse support")
80 (multimedia .
"images and sound support")
81 (news .
"support for netnews reading and posting")
82 (oop .
"support for object-oriented programming")
83 (outlines .
"support for hierarchical outlining")
84 (processes .
"process, subshell, compilation, and job control support")
85 (terminals .
"support for terminal types")
86 (tex .
"code related to the TeX formatter")
87 (tools .
"programming tools")
88 (unix .
"front-ends/assistants for, or emulators of, UNIX features")
89 ;; Not a custom group and not currently useful.
90 ;; (vms . "support code for vms")
91 (wp .
"word processing")
94 (defvar finder-mode-map nil
)
96 (let ((map (make-sparse-keymap)))
97 (define-key map
" " 'finder-select
)
98 (define-key map
"f" 'finder-select
)
99 (define-key map
[mouse-2
] 'finder-mouse-select
)
100 (define-key map
"\C-m" 'finder-select
)
101 (define-key map
"?" 'finder-summary
)
102 (define-key map
"q" 'finder-exit
)
103 (define-key map
"d" 'finder-list-keywords
)
104 (setq finder-mode-map map
)))
107 ;;; Code for regenerating the keyword list.
109 (defvar finder-package-info nil
110 "Assoc list mapping file names to description & keyword lists.")
112 (defun finder-compile-keywords (&rest dirs
)
113 "Regenerate the keywords association list into the file `finder-inf.el'.
114 Optional arguments DIRS are a list of Emacs Lisp directories to compile from;
115 no arguments compiles from `load-path'."
117 (let ((processed nil
))
118 (find-file "finder-inf.el")
120 (insert ";;; finder-inf.el --- keyword-to-package mapping\n")
121 (insert ";; This file is part of GNU Emacs.\n")
122 (insert ";; Keywords: help\n")
123 (insert ";;; Commentary:\n")
124 (insert ";; Don't edit this file. It's generated by finder.el\n\n")
125 (insert ";;; Code:\n")
126 (insert "\n(setq finder-package-info '(\n")
129 (when (file-exists-p (directory-file-name d
))
130 (message "Directory %s" d
)
133 (if (and (or (string-match "^[^=].*\\.el$" f
)
134 ;; Allow compressed files also. Fixme:
135 ;; generalize this, especially for
136 ;; MS-DOG-type filenames.
137 (and (string-match "^[^=].*\\.el\\.\\(gz\\|Z\\)$" f
)
138 (require 'jka-compr
)))
139 ;; Ignore lock files.
140 (not (string-match "^.#" f
))
141 (not (member f processed
)))
142 (let (summary keystart keywords
)
143 (setq processed
(cons f processed
))
145 (set-buffer (get-buffer-create "*finder-scratch*"))
146 (buffer-disable-undo (current-buffer))
148 (insert-file-contents
149 (concat (file-name-as-directory (or d
".")) f
))
150 (setq summary
(lm-synopsis))
151 (setq keywords
(lm-keywords)))
153 (format " (\"%s\"\n "
154 (if (string-match "\\.\\(gz\\|Z\\)$" f
)
155 (file-name-sans-extension f
)
157 (prin1 summary
(current-buffer))
160 (setq keystart
(point))
162 (if keywords
(format "(%s)" keywords
) "nil")
164 (subst-char-in-region keystart
(point) ?
, ?
)
166 (directory-files (or d
".")))))
169 \(provide 'finder-inf)
172 ;;; version-control: never
173 ;;; no-byte-compile: t
174 ;;; no-update-autoloads: t
176 ;;; finder-inf.el ends here\n")
177 (kill-buffer "*finder-scratch*")
178 (eval-current-buffer) ;; So we get the new keyword list immediately
179 (basic-save-buffer))))
181 (defun finder-compile-keywords-make-dist ()
182 "Regenerate `finder-inf.el' for the Emacs distribution."
183 (apply 'finder-compile-keywords command-line-args-left
)
186 ;;; Now the retrieval code
188 (defun finder-insert-at-column (column &rest strings
)
189 "Insert, at column COLUMN, other args STRINGS."
190 (if (>= (current-column) column
) (insert "\n"))
191 (move-to-column column t
)
192 (apply 'insert strings
))
194 (defvar finder-help-echo nil
)
196 (defun finder-mouse-face-on-line ()
197 "Put `mouse-face' and `help-echo' properties on the previous line."
200 (unless finder-help-echo
201 (setq finder-help-echo
202 (let* ((keys1 (where-is-internal 'finder-select
204 (keys (nconc (where-is-internal
205 'finder-mouse-select finder-mode-map
)
207 (concat (mapconcat 'key-description keys
", ")
210 (line-beginning-position) (line-end-position)
211 '(mouse-face highlight
212 help-echo finder-help-echo
))))
215 (defun finder-list-keywords ()
216 "Display descriptions of the keywords in the Finder buffer."
218 (if (get-buffer "*Finder*")
219 (pop-to-buffer "*Finder*")
220 (pop-to-buffer (set-buffer (get-buffer-create "*Finder*")))
222 (setq buffer-read-only nil
)
226 (let ((keyword (car assoc
)))
227 (insert (symbol-name keyword
))
228 (finder-insert-at-column 14 (concat (cdr assoc
) "\n"))
229 (finder-mouse-face-on-line)))
230 finder-known-keywords
)
231 (goto-char (point-min))
232 (setq finder-headmark
(point))
233 (setq buffer-read-only t
)
234 (set-buffer-modified-p nil
)
238 (defun finder-list-matches (key)
239 (pop-to-buffer (set-buffer (get-buffer-create "*Finder Category*")))
241 (setq buffer-read-only nil
)
243 (let ((id (intern key
)))
245 "The following packages match the keyword `" key
"':\n\n")
246 (setq finder-headmark
(point))
249 (if (memq id
(car (cdr (cdr x
))))
252 (finder-insert-at-column 16 (concat (nth 1 x
) "\n"))
253 (finder-mouse-face-on-line))))
255 (goto-char (point-min))
257 (setq buffer-read-only t
)
258 (set-buffer-modified-p nil
)
259 (shrink-window-if-larger-than-buffer)
262 (defun finder-find-library (library)
263 "Search for file LIBRARY on `load-path'.
264 Try compressed versions if jka-compr is in use."
265 (or (locate-library library t
)
266 (if (rassq 'jka-compr-handler file-name-handler-alist
)
267 (or (locate-library (concat library
".gz") t
)
268 (locate-library (concat library
".Z") t
)
269 ;; last resort for MS-DOG et al
270 (locate-library (concat library
"z"))))))
273 (defun finder-commentary (file)
274 "Display FILE's commentary section.
275 FILE should be in a form suitable for passing to `locate-library'."
276 (interactive "sLibrary name: ")
277 (let* ((str (lm-commentary (or (finder-find-library file
)
278 (finder-find-library (concat file
".el"))
279 (error "Can't find library %s" file
)))))
281 (error "Can't find any Commentary section"))
282 (pop-to-buffer "*Finder*")
283 (setq buffer-read-only nil
)
286 (goto-char (point-min))
288 (goto-char (point-max))
290 (goto-char (point-min))
291 (while (re-search-forward "^;+ ?" nil t
)
292 (replace-match "" nil nil
))
293 (goto-char (point-min))
294 (setq buffer-read-only t
)
295 (set-buffer-modified-p nil
)
296 (shrink-window-if-larger-than-buffer)
300 (defun finder-current-item ()
301 (if (and finder-headmark
(< (point) finder-headmark
))
302 (error "No keyword or filename on this line")
307 (defun finder-select ()
308 "Select item on current line in a finder buffer."
310 (let ((key (finder-current-item)))
311 (if (string-match "\\.el$" key
)
312 (finder-commentary key
)
313 (finder-list-matches key
))))
315 (defun finder-mouse-select (event)
316 "Select item in a finder buffer with the mouse."
319 (set-buffer (window-buffer (posn-window (event-start event
))))
320 (goto-char (posn-point (event-start event
)))
324 (defun finder-by-keyword ()
325 "Find packages matching a given keyword."
327 (finder-list-keywords))
329 (defun finder-mode ()
330 "Major mode for browsing package documentation.
332 \\[finder-select] more help for the item on the current line
333 \\[finder-exit] exit Finder mode and kill the Finder buffer."
335 (use-local-map finder-mode-map
)
336 (set-syntax-table emacs-lisp-mode-syntax-table
)
337 (setq mode-name
"Finder")
338 (setq major-mode
'finder-mode
)
339 (make-local-variable 'finder-headmark
)
340 (setq finder-headmark nil
))
342 (defun finder-summary ()
343 "Summarize basic Finder commands."
346 (substitute-command-keys
347 "\\<finder-mode-map>\\[finder-select] = select, \
348 \\[finder-mouse-select] = select, \\[finder-list-keywords] = to \
349 finder directory, \\[finder-exit] = quit, \\[finder-summary] = help")))
351 (defun finder-exit ()
352 "Exit Finder mode and kill the buffer."
356 ;; Can happen in either buffer -- kill each of the two that exists
357 (and (get-buffer "*Finder*")
358 (kill-buffer "*Finder*"))
359 (and (get-buffer "*Finder Category*")
360 (kill-buffer "*Finder Category*")))
365 ;;; finder.el ends here