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 (defvar finder-mode-hook nil
48 "*Hook run when function `finder-mode' is called.")
50 ;; Local variable in finder buffer.
51 (defvar finder-headmark
)
53 ;; These are supposed to correspond to top-level customization groups,
55 (defvar finder-known-keywords
57 (abbrev .
"abbreviation handling, typing shortcuts, macros")
59 (bib .
"code related to the `bib' bibliography processor")
60 (c .
"support for the C language and related languages")
61 (calendar .
"calendar and time management support")
62 (comm .
"communications, networking, remote access to files")
63 (convenience .
"convenience features for faster editing")
64 (data .
"support editing files of data")
65 (docs .
"support for Emacs documentation")
66 (emulations .
"emulations of other editors")
67 (extensions .
"Emacs Lisp language extensions")
68 (faces .
"support for multiple fonts")
69 (files .
"support for editing and manipulating files")
70 (frames .
"support for Emacs frames and window systems")
71 (games .
"games, jokes and amusements")
72 (hardware .
"support for interfacing with exotic hardware")
73 (help .
"support for on-line help systems")
74 (hypermedia .
"support for links between text or other media types")
75 (i18n .
"internationalization and alternate character-set support")
76 (internal .
"code for Emacs internals, build process, defaults")
77 (languages .
"specialized modes for editing programming languages")
78 (lisp .
"Lisp support, including Emacs Lisp")
79 (local .
"code local to your site")
80 (maint .
"maintenance aids for the Emacs development group")
81 (mail .
"modes for electronic-mail handling")
82 (matching .
"various sorts of searching and matching")
83 (mouse .
"mouse support")
84 (multimedia .
"images and sound support")
85 (news .
"support for netnews reading and posting")
86 (oop .
"support for object-oriented programming")
87 (outlines .
"support for hierarchical outlining")
88 (processes .
"process, subshell, compilation, and job control support")
89 (terminals .
"support for terminal types")
90 (tex .
"code related to the TeX formatter")
91 (tools .
"programming tools")
92 (unix .
"front-ends/assistants for, or emulators of, UNIX features")
93 ;; Not a custom group and not currently useful.
94 ;; (vms . "support code for vms")
95 (wp .
"word processing")
98 (defvar finder-mode-map nil
)
100 (let ((map (make-sparse-keymap)))
101 (define-key map
" " 'finder-select
)
102 (define-key map
"f" 'finder-select
)
103 (define-key map
[mouse-2
] 'finder-mouse-select
)
104 (define-key map
"\C-m" 'finder-select
)
105 (define-key map
"?" 'finder-summary
)
106 (define-key map
"q" 'finder-exit
)
107 (define-key map
"d" 'finder-list-keywords
)
108 (setq finder-mode-map map
)))
111 ;;; Code for regenerating the keyword list.
113 (defvar finder-package-info nil
114 "Assoc list mapping file names to description & keyword lists.")
116 (defvar generated-finder-keywords-file
"finder-inf.el"
117 "File \\[finder-compile-keywords] puts finder keywords into.")
119 (defun finder-compile-keywords (&rest dirs
)
120 "Regenerate the keywords association list into `generated-finder-keywords-file'.
121 Optional arguments DIRS are a list of Emacs Lisp directories to compile from;
122 no arguments compiles from `load-path'."
124 (let ((processed nil
))
125 (find-file generated-finder-keywords-file
)
127 (insert ";;; " (file-name-nondirectory generated-finder-keywords-file
)
128 " --- keyword-to-package mapping\n")
129 (insert ";; This file is part of GNU Emacs.\n")
130 (insert ";;; Commentary:\n")
131 (insert ";; Don't edit this file. It's generated by finder.el\n\n")
132 (insert ";;; Code:\n")
133 (insert "\n(setq finder-package-info '(\n")
136 (when (file-exists-p (directory-file-name d
))
137 (message "Directory %s" d
)
140 (if (and (or (string-match "^[^=].*\\.el$" f
)
141 ;; Allow compressed files also. Fixme:
142 ;; generalize this, especially for
143 ;; MS-DOG-type filenames.
144 (and (string-match "^[^=].*\\.el\\.\\(gz\\|Z\\)$" f
)
145 (require 'jka-compr
)))
146 ;; Ignore lock files.
147 (not (string-match "^.#" f
))
148 (not (member f processed
)))
149 (let (summary keystart keywords
)
150 (setq processed
(cons f processed
))
152 (set-buffer (get-buffer-create "*finder-scratch*"))
153 (buffer-disable-undo (current-buffer))
155 (insert-file-contents
156 (concat (file-name-as-directory (or d
".")) f
))
157 (setq summary
(lm-synopsis))
158 (setq keywords
(lm-keywords)))
160 (format " (\"%s\"\n "
161 (if (string-match "\\.\\(gz\\|Z\\)$" f
)
162 (file-name-sans-extension f
)
164 (prin1 summary
(current-buffer))
167 (setq keystart
(point))
169 (if keywords
(format "(%s)" keywords
) "nil")
171 (subst-char-in-region keystart
(point) ?
, ?
)
173 (directory-files (or d
".")))))
176 \(provide '" (file-name-sans-extension
177 (file-name-nondirectory generated-finder-keywords-file
)) ")
180 ;;; version-control: never
181 ;;; no-byte-compile: t
182 ;;; no-update-autoloads: t
184 ;;; " (file-name-nondirectory generated-finder-keywords-file
) " ends here\n")
185 (kill-buffer "*finder-scratch*")
186 (eval-current-buffer) ;; So we get the new keyword list immediately
187 (basic-save-buffer))))
189 (defun finder-compile-keywords-make-dist ()
190 "Regenerate `finder-inf.el' for the Emacs distribution."
191 (apply 'finder-compile-keywords command-line-args-left
)
194 ;;; Now the retrieval code
196 (defun finder-insert-at-column (column &rest strings
)
197 "Insert, at column COLUMN, other args STRINGS."
198 (if (>= (current-column) column
) (insert "\n"))
199 (move-to-column column t
)
200 (apply 'insert strings
))
202 (defvar finder-help-echo nil
)
204 (defun finder-mouse-face-on-line ()
205 "Put `mouse-face' and `help-echo' properties on the previous line."
208 (unless finder-help-echo
209 (setq finder-help-echo
210 (let* ((keys1 (where-is-internal 'finder-select
212 (keys (nconc (where-is-internal
213 'finder-mouse-select finder-mode-map
)
215 (concat (mapconcat 'key-description keys
", ")
218 (line-beginning-position) (line-end-position)
219 '(mouse-face highlight
220 help-echo finder-help-echo
))))
223 (defun finder-list-keywords ()
224 "Display descriptions of the keywords in the Finder buffer."
226 (if (get-buffer "*Finder*")
227 (pop-to-buffer "*Finder*")
228 (pop-to-buffer (set-buffer (get-buffer-create "*Finder*")))
230 (setq buffer-read-only nil
)
234 (let ((keyword (car assoc
)))
235 (insert (symbol-name keyword
))
236 (finder-insert-at-column 14 (concat (cdr assoc
) "\n"))
237 (finder-mouse-face-on-line)))
238 finder-known-keywords
)
239 (goto-char (point-min))
240 (setq finder-headmark
(point))
241 (setq buffer-read-only t
)
242 (set-buffer-modified-p nil
)
246 (defun finder-list-matches (key)
247 (pop-to-buffer (set-buffer (get-buffer-create "*Finder Category*")))
249 (setq buffer-read-only nil
)
251 (let ((id (intern key
)))
253 "The following packages match the keyword `" key
"':\n\n")
254 (setq finder-headmark
(point))
257 (if (memq id
(car (cdr (cdr x
))))
260 (finder-insert-at-column 16 (concat (nth 1 x
) "\n"))
261 (finder-mouse-face-on-line))))
263 (goto-char (point-min))
265 (setq buffer-read-only t
)
266 (set-buffer-modified-p nil
)
267 (shrink-window-if-larger-than-buffer)
271 (defun finder-commentary (file)
272 "Display FILE's commentary section.
273 FILE should be in a form suitable for passing to `locate-library'."
276 (completing-read "Library name: "
277 'locate-file-completion
278 (cons (or find-function-source-path load-path
)
279 (find-library-suffixes)))))
280 (let* ((str (lm-commentary (find-library-name file
))))
282 (error "Can't find any Commentary section"))
283 ;; This used to use *Finder* but that would clobber the
284 ;; directory of categories.
285 (pop-to-buffer "*Finder-package*")
286 (setq buffer-read-only nil
)
289 (goto-char (point-min))
291 (goto-char (point-max))
293 (goto-char (point-min))
294 (while (re-search-forward "^;+ ?" nil t
)
295 (replace-match "" nil nil
))
296 (goto-char (point-min))
297 (setq buffer-read-only t
)
298 (set-buffer-modified-p nil
)
299 (shrink-window-if-larger-than-buffer)
303 (defun finder-current-item ()
304 (if (and finder-headmark
(< (point) finder-headmark
))
305 (error "No keyword or filename on this line")
310 (defun finder-select ()
311 "Select item on current line in a finder buffer."
313 (let ((key (finder-current-item)))
314 (if (string-match "\\.el$" key
)
315 (finder-commentary key
)
316 (finder-list-matches key
))))
318 (defun finder-mouse-select (event)
319 "Select item in a finder buffer with the mouse."
322 (set-buffer (window-buffer (posn-window (event-start event
))))
323 (goto-char (posn-point (event-start event
)))
327 (defun finder-by-keyword ()
328 "Find packages matching a given keyword."
330 (finder-list-keywords))
332 (defun finder-mode ()
333 "Major mode for browsing package documentation.
335 \\[finder-select] more help for the item on the current line
336 \\[finder-exit] exit Finder mode and kill the Finder buffer."
338 (use-local-map finder-mode-map
)
339 (set-syntax-table emacs-lisp-mode-syntax-table
)
340 (setq mode-name
"Finder")
341 (setq major-mode
'finder-mode
)
342 (make-local-variable 'finder-headmark
)
343 (setq finder-headmark nil
)
344 (run-hooks 'finder-mode-hook
))
346 (defun finder-summary ()
347 "Summarize basic Finder commands."
350 (substitute-command-keys
351 "\\<finder-mode-map>\\[finder-select] = select, \
352 \\[finder-mouse-select] = select, \\[finder-list-keywords] = to \
353 finder directory, \\[finder-exit] = quit, \\[finder-summary] = help")))
355 (defun finder-exit ()
356 "Exit Finder mode and kill the buffer."
360 ;; Can happen in either buffer -- kill each of the two that exists
361 (and (get-buffer "*Finder*")
362 (kill-buffer "*Finder*"))
363 (and (get-buffer "*Finder Category*")
364 (kill-buffer "*Finder Category*")))
369 ;;; arch-tag: ec85ff49-8cb8-41f5-a63f-9131d53ce2c5
370 ;;; finder.el ends here