1 ;;; finder.el --- topic & keyword-based code finder
3 ;; Copyright (C) 1992, 1997, 1998, 1999 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 ;; These are supposed to correspond to top-level customization groups,
49 (defvar finder-known-keywords
51 (abbrev . "abbreviation handling, typing shortcuts, macros")
53 (bib . "code related to the `bib' bibliography processor")
54 (c . "support for the C language and related languages")
55 (calendar . "calendar and time management support")
56 (comm . "communications, networking, remote access to files")
57 (convenience . "convenience features for faster editing")
58 (data . "support editing files of data")
59 (docs . "support for Emacs documentation")
60 (emulations . "emulations of other editors")
61 (extensions . "Emacs Lisp language extensions")
62 (faces . "support for multiple fonts")
63 (files . "support for editing and manipulating files")
64 (frames . "support for Emacs frames and window systems")
65 (games . "games, jokes and amusements")
66 (hardware . "support for interfacing with exotic hardware")
67 (help . "support for on-line help systems")
68 (hypermedia . "support for links between text or other media types")
69 (i18n . "internationalization and alternate character-set support")
70 (internal . "code for Emacs internals, build process, defaults")
71 (languages . "specialized modes for editing programming languages")
72 (lisp . "Lisp support, including Emacs Lisp")
73 (local . "code local to your site")
74 (maint . "maintenance aids for the Emacs development group")
75 (mail . "modes for electronic-mail handling")
76 (matching . "various sorts of searching and matching")
77 (mouse . "mouse support")
78 (multimedia . "images and sound support")
79 (news . "support for netnews reading and posting")
80 (oop . "support for object-oriented programming")
81 (outlines . "support for hierarchical outlining")
82 (processes . "process, subshell, compilation, and job control support")
83 (terminals . "support for terminal types")
84 (tex . "code related to the TeX formatter")
85 (tools . "programming tools")
86 (unix . "front-ends/assistants for, or emulators of, UNIX features")
87 ;; Not a custom group and not currently useful.
88 ;; (vms . "support code for vms")
89 (wp . "word processing")
92 (defvar finder-mode-map nil)
94 (let ((map (make-sparse-keymap)))
95 (define-key map " " 'finder-select)
96 (define-key map "f" 'finder-select)
97 (define-key map [mouse-2] 'finder-mouse-select)
98 (define-key map "\C-m" 'finder-select)
99 (define-key map "?" 'finder-summary)
100 (define-key map "q" 'finder-exit)
101 (define-key map "d" 'finder-list-keywords)
102 (setq finder-mode-map map)))
105 ;;; Code for regenerating the keyword list.
107 (defvar finder-package-info nil
108 "Assoc list mapping file names to description & keyword lists.")
110 (defun finder-compile-keywords (&rest dirs)
111 "Regenerate the keywords association list into the file `finder-inf.el'.
112 Optional arguments DIRS are a list of Emacs Lisp directories to compile from;
113 no arguments compiles from `load-path'."
115 (let ((processed nil))
116 (find-file "finder-inf.el")
118 (insert ";;; finder-inf.el --- keyword-to-package mapping\n")
119 (insert ";; Keywords: help\n")
120 (insert ";;; Commentary:\n")
121 (insert ";; Don't edit this file. It's generated by finder.el\n\n")
122 (insert ";;; Code:\n")
123 (insert "\n(setq finder-package-info '(\n")
126 (when (file-exists-p (directory-file-name d))
127 (message "Directory %s" d)
130 (if (and (or (string-match "^[^=].*\\.el$" f)
131 ;; Allow compressed files also. Fixme:
132 ;; generalize this, especially for
133 ;; MS-DOG-type filenames.
134 (and (string-match "^[^=].*\\.el\\.\\(gz\\|Z\\)$" f)
135 (require 'jka-compr)))
136 ;; Ignore lock files.
137 (not (string-match "^.#" f))
138 (not (member f processed)))
139 (let (summary keystart keywords)
140 (setq processed (cons f processed))
142 (set-buffer (get-buffer-create "*finder-scratch*"))
143 (buffer-disable-undo (current-buffer))
145 (insert-file-contents
146 (concat (file-name-as-directory (or d ".")) f))
147 (setq summary (lm-synopsis))
148 (setq keywords (lm-keywords)))
150 (format " (\"%s\"\n "
151 (if (string-match "\\.\\(gz\\|Z\\)$" f)
152 (file-name-sans-extension f)
154 (prin1 summary (current-buffer))
157 (setq keystart (point))
159 (if keywords (format "(%s)" keywords) "nil")
161 (subst-char-in-region keystart (point) ?, ? )
163 (directory-files (or d ".")))))
166 \(provide 'finder-inf)
169 ;;; version-control: never
170 ;;; no-byte-compile: t
171 ;;; no-update-autoloads: t
173 ;;; finder-inf.el ends here\n")
174 (kill-buffer "*finder-scratch*")
175 (eval-current-buffer) ;; So we get the new keyword list immediately
176 (basic-save-buffer))))
178 (defun finder-compile-keywords-make-dist ()
179 "Regenerate `finder-inf.el' for the Emacs distribution."
180 (apply 'finder-compile-keywords command-line-args-left)
183 ;;; Now the retrieval code
185 (defun finder-insert-at-column (column &rest strings)
186 "Insert, at column COLUMN, other args STRINGS."
187 (if (> (current-column) column) (insert "\n"))
188 (move-to-column column t)
189 (apply 'insert strings))
191 (defvar finder-help-echo nil)
193 (defun finder-mouse-face-on-line ()
194 "Put `mouse-face' and `help-echo' properties on the previous line."
197 (unless finder-help-echo
198 (setq finder-help-echo
199 (let* ((keys1 (where-is-internal 'finder-select
201 (keys (nconc (where-is-internal
202 'finder-mouse-select finder-mode-map)
204 (concat (mapconcat 'key-description keys ", ")
207 (line-beginning-position) (line-end-position)
208 '(mouse-face highlight
209 help-echo finder-help-echo))))
212 (defun finder-list-keywords ()
213 "Display descriptions of the keywords in the Finder buffer."
215 (if (get-buffer "*Finder*")
216 (pop-to-buffer "*Finder*")
217 (pop-to-buffer (set-buffer (get-buffer-create "*Finder*")))
219 (setq buffer-read-only nil)
223 (let ((keyword (car assoc)))
224 (insert (symbol-name keyword))
225 (finder-insert-at-column 14 (concat (cdr assoc) "\n"))
226 (finder-mouse-face-on-line)))
227 finder-known-keywords)
228 (goto-char (point-min))
229 (setq finder-headmark (point))
230 (setq buffer-read-only t)
231 (set-buffer-modified-p nil)
235 (defun finder-list-matches (key)
236 (pop-to-buffer (set-buffer (get-buffer-create "*Finder Category*")))
238 (setq buffer-read-only nil)
240 (let ((id (intern key)))
242 "The following packages match the keyword `" key "':\n\n")
243 (setq finder-headmark (point))
246 (if (memq id (car (cdr (cdr x))))
249 (finder-insert-at-column 16 (concat (nth 1 x) "\n"))
250 (finder-mouse-face-on-line))))
252 (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-find-library (library)
260 "Search for file LIBRARY on `load-path'.
261 Try compressed versions if jka-compr is in use."
262 (or (locate-library library t)
263 (if (rassq 'jka-compr-handler file-name-handler-alist)
264 (or (locate-library (concat library ".gz") t)
265 (locate-library (concat library ".Z") t)
266 ;; last resort for MS-DOG et al
267 (locate-library (concat library "z"))))))
270 (defun finder-commentary (file)
271 "Display FILE's commentary section.
272 FILE should be in a form suitable for passing to `locate-library'."
273 (interactive "sLibrary name: ")
274 (let* ((str (lm-commentary (or (finder-find-library file)
275 (finder-find-library (concat file ".el"))
276 (error "Can't find library %s" file)))))
278 (error "Can't find any Commentary section"))
279 (pop-to-buffer "*Finder*")
280 (setq buffer-read-only nil)
283 (goto-char (point-min))
285 (goto-char (point-max))
287 (goto-char (point-min))
288 (while (re-search-forward "^;+ ?" nil t)
289 (replace-match "" nil nil))
290 (goto-char (point-min))
291 (setq buffer-read-only t)
292 (set-buffer-modified-p nil)
293 (shrink-window-if-larger-than-buffer)
297 (defun finder-current-item ()
298 (if (and finder-headmark (< (point) finder-headmark))
299 (error "No keyword or filename on this line")
304 (defun finder-select ()
305 "Select item on current line in a finder buffer."
307 (let ((key (finder-current-item)))
308 (if (string-match "\\.el$" key)
309 (finder-commentary key)
310 (finder-list-matches key))))
312 (defun finder-mouse-select (event)
313 "Select item in a finder buffer with the mouse."
316 (set-buffer (window-buffer (posn-window (event-start event))))
317 (goto-char (posn-point (event-start event)))
321 (defun finder-by-keyword ()
322 "Find packages matching a given keyword."
324 (finder-list-keywords))
326 (defun finder-mode ()
327 "Major mode for browsing package documentation.
329 \\[finder-select] more help for the item on the current line
330 \\[finder-exit] exit Finder mode and kill the Finder buffer."
332 (use-local-map finder-mode-map)
333 (set-syntax-table emacs-lisp-mode-syntax-table)
334 (setq mode-name "Finder")
335 (setq major-mode 'finder-mode)
336 (make-local-variable 'finder-headmark)
337 (setq finder-headmark nil))
339 (defun finder-summary ()
340 "Summarize basic Finder commands."
343 (substitute-command-keys
344 "\\<finder-mode-map>\\[finder-select] = select, \
345 \\[finder-mouse-select] = select, \\[finder-list-keywords] = to \
346 finder directory, \\[finder-exit] = quit, \\[finder-summary] = help")))
348 (defun finder-exit ()
349 "Exit Finder mode and kill the buffer."
353 ;; Can happen in either buffer -- kill each of the two that exists
354 (and (get-buffer "*Finder*")
355 (kill-buffer "*Finder*"))
356 (and (get-buffer "*Finder Category*")
357 (kill-buffer "*Finder Category*")))
362 ;;; finder.el ends here