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 (require 'find-func) ;for find-library(-suffixes)
43 ;; Use `load' rather than `require' so that it doesn't get loaded
44 ;; during byte-compilation (at which point it might be missing).
45 (load "finder-inf" t t)
47 ;; Local variable in finder buffer.
48 (defvar finder-headmark)
50 ;; These are supposed to correspond to top-level customization groups,
52 (defvar finder-known-keywords
54 (abbrev . "abbreviation handling, typing shortcuts, macros")
56 (bib . "code related to the `bib' bibliography processor")
57 (c . "support for the C language and related languages")
58 (calendar . "calendar and time management support")
59 (comm . "communications, networking, remote access to files")
60 (convenience . "convenience features for faster editing")
61 (data . "support editing files of data")
62 (docs . "support for Emacs documentation")
63 (emulations . "emulations of other editors")
64 (extensions . "Emacs Lisp language extensions")
65 (faces . "support for multiple fonts")
66 (files . "support for editing and manipulating files")
67 (frames . "support for Emacs frames and window systems")
68 (games . "games, jokes and amusements")
69 (hardware . "support for interfacing with exotic hardware")
70 (help . "support for on-line help systems")
71 (hypermedia . "support for links between text or other media types")
72 (i18n . "internationalization and alternate character-set support")
73 (internal . "code for Emacs internals, build process, defaults")
74 (languages . "specialized modes for editing programming languages")
75 (lisp . "Lisp support, including Emacs Lisp")
76 (local . "code local to your site")
77 (maint . "maintenance aids for the Emacs development group")
78 (mail . "modes for electronic-mail handling")
79 (matching . "various sorts of searching and matching")
80 (mouse . "mouse support")
81 (multimedia . "images and sound support")
82 (news . "support for netnews reading and posting")
83 (oop . "support for object-oriented programming")
84 (outlines . "support for hierarchical outlining")
85 (processes . "process, subshell, compilation, and job control support")
86 (terminals . "support for terminal types")
87 (tex . "code related to the TeX formatter")
88 (tools . "programming tools")
89 (unix . "front-ends/assistants for, or emulators of, UNIX features")
90 ;; Not a custom group and not currently useful.
91 ;; (vms . "support code for vms")
92 (wp . "word processing")
95 (defvar finder-mode-map nil)
97 (let ((map (make-sparse-keymap)))
98 (define-key map " " 'finder-select)
99 (define-key map "f" 'finder-select)
100 (define-key map [mouse-2] 'finder-mouse-select)
101 (define-key map "\C-m" 'finder-select)
102 (define-key map "?" 'finder-summary)
103 (define-key map "q" 'finder-exit)
104 (define-key map "d" 'finder-list-keywords)
105 (setq finder-mode-map map)))
108 ;;; Code for regenerating the keyword list.
110 (defvar finder-package-info nil
111 "Assoc list mapping file names to description & keyword lists.")
113 (defvar generated-finder-keywords-file "finder-inf.el"
114 "File \\[finder-compile-keywords] puts finder keywords into.")
116 (defun finder-compile-keywords (&rest dirs)
117 "Regenerate the keywords association list into `generated-finder-keywords-file'.
118 Optional arguments DIRS are a list of Emacs Lisp directories to compile from;
119 no arguments compiles from `load-path'."
121 (let ((processed nil))
122 (find-file generated-finder-keywords-file)
124 (insert ";;; " (file-name-nondirectory generated-finder-keywords-file)
125 " --- keyword-to-package mapping\n")
126 (insert ";; This file is part of GNU Emacs.\n")
127 (insert ";;; Commentary:\n")
128 (insert ";; Don't edit this file. It's generated by finder.el\n\n")
129 (insert ";;; Code:\n")
130 (insert "\n(setq finder-package-info '(\n")
133 (when (file-exists-p (directory-file-name d))
134 (message "Directory %s" d)
137 (if (and (or (string-match "^[^=].*\\.el$" f)
138 ;; Allow compressed files also. Fixme:
139 ;; generalize this, especially for
140 ;; MS-DOG-type filenames.
141 (and (string-match "^[^=].*\\.el\\.\\(gz\\|Z\\)$" f)
142 (require 'jka-compr)))
143 ;; Ignore lock files.
144 (not (string-match "^.#" f))
145 (not (member f processed)))
146 (let (summary keystart keywords)
147 (setq processed (cons f processed))
149 (set-buffer (get-buffer-create "*finder-scratch*"))
150 (buffer-disable-undo (current-buffer))
152 (insert-file-contents
153 (concat (file-name-as-directory (or d ".")) f))
154 (setq summary (lm-synopsis))
155 (setq keywords (lm-keywords)))
157 (format " (\"%s\"\n "
158 (if (string-match "\\.\\(gz\\|Z\\)$" f)
159 (file-name-sans-extension f)
161 (prin1 summary (current-buffer))
164 (setq keystart (point))
166 (if keywords (format "(%s)" keywords) "nil")
168 (subst-char-in-region keystart (point) ?, ? )
170 (directory-files (or d ".")))))
173 \(provide '" (file-name-sans-extension
174 (file-name-nondirectory generated-finder-keywords-file)) ")
177 ;;; version-control: never
178 ;;; no-byte-compile: t
179 ;;; no-update-autoloads: t
181 ;;; " (file-name-nondirectory generated-finder-keywords-file) " ends here\n")
182 (kill-buffer "*finder-scratch*")
183 (eval-current-buffer) ;; So we get the new keyword list immediately
184 (basic-save-buffer))))
186 (defun finder-compile-keywords-make-dist ()
187 "Regenerate `finder-inf.el' for the Emacs distribution."
188 (apply 'finder-compile-keywords command-line-args-left)
191 ;;; Now the retrieval code
193 (defun finder-insert-at-column (column &rest strings)
194 "Insert, at column COLUMN, other args STRINGS."
195 (if (>= (current-column) column) (insert "\n"))
196 (move-to-column column t)
197 (apply 'insert strings))
199 (defvar finder-help-echo nil)
201 (defun finder-mouse-face-on-line ()
202 "Put `mouse-face' and `help-echo' properties on the previous line."
205 (unless finder-help-echo
206 (setq finder-help-echo
207 (let* ((keys1 (where-is-internal 'finder-select
209 (keys (nconc (where-is-internal
210 'finder-mouse-select finder-mode-map)
212 (concat (mapconcat 'key-description keys ", ")
215 (line-beginning-position) (line-end-position)
216 '(mouse-face highlight
217 help-echo finder-help-echo))))
220 (defun finder-list-keywords ()
221 "Display descriptions of the keywords in the Finder buffer."
223 (if (get-buffer "*Finder*")
224 (pop-to-buffer "*Finder*")
225 (pop-to-buffer (set-buffer (get-buffer-create "*Finder*")))
227 (setq buffer-read-only nil)
231 (let ((keyword (car assoc)))
232 (insert (symbol-name keyword))
233 (finder-insert-at-column 14 (concat (cdr assoc) "\n"))
234 (finder-mouse-face-on-line)))
235 finder-known-keywords)
236 (goto-char (point-min))
237 (setq finder-headmark (point))
238 (setq buffer-read-only t)
239 (set-buffer-modified-p nil)
243 (defun finder-list-matches (key)
244 (pop-to-buffer (set-buffer (get-buffer-create "*Finder Category*")))
246 (setq buffer-read-only nil)
248 (let ((id (intern key)))
250 "The following packages match the keyword `" key "':\n\n")
251 (setq finder-headmark (point))
254 (if (memq id (car (cdr (cdr x))))
257 (finder-insert-at-column 16 (concat (nth 1 x) "\n"))
258 (finder-mouse-face-on-line))))
260 (goto-char (point-min))
262 (setq buffer-read-only t)
263 (set-buffer-modified-p nil)
264 (shrink-window-if-larger-than-buffer)
268 (defun finder-commentary (file)
269 "Display FILE's commentary section.
270 FILE should be in a form suitable for passing to `locate-library'."
273 (completing-read "Library name: "
274 'locate-file-completion
275 (cons (or find-function-source-path load-path)
276 (find-library-suffixes)))))
277 (let* ((str (lm-commentary (find-library-name file))))
279 (error "Can't find any Commentary section"))
280 ;; This used to use *Finder* but that would clobber the
281 ;; directory of categories.
282 (pop-to-buffer "*Finder-package*")
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