1 ;;; finder.el --- topic & keyword-based code finder
3 ;; Copyright (C) 1992, 1997, 1998, 1999, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
7 ;; Created: 16 Jun 1992
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This mode uses the Keywords library header to provide code-finding
29 ;; services by keyword.
32 ;; 1. Support multiple keywords per search. This could be extremely hairy;
33 ;; there doesn't seem to be any way to get completing-read to exit on
34 ;; an EOL with no substring pending, which is what we'd want to end the loop.
35 ;; 2. Search by string in synopsis line?
36 ;; 3. Function to check finder-package-info for unknown keywords.
41 (require 'find-func
) ;for find-library(-suffixes)
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" t t
)
46 (defvar finder-mode-hook nil
47 "*Hook run when function `finder-mode' is called.")
49 ;; Local variable in finder buffer.
50 (defvar finder-headmark
)
52 ;; These are supposed to correspond to top-level customization groups,
54 (defvar finder-known-keywords
56 (abbrev .
"abbreviation handling, typing shortcuts, macros")
58 (bib .
"code related to the `bib' bibliography processor")
59 (c .
"support for the C language and related languages")
60 (calendar .
"calendar and time management support")
61 (comm .
"communications, networking, remote access to files")
62 (convenience .
"convenience features for faster editing")
63 (data .
"support for editing files of data")
64 (docs .
"support for Emacs documentation")
65 (emulations .
"emulations of other editors")
66 (extensions .
"Emacs Lisp language extensions")
67 (faces .
"support for multiple fonts")
68 (files .
"support for editing and manipulating files")
69 (frames .
"support for Emacs frames and window systems")
70 (games .
"games, jokes and amusements")
71 (hardware .
"support for interfacing with exotic hardware")
72 (help .
"support for on-line help systems")
73 (hypermedia .
"support for links between text or other media types")
74 (i18n .
"internationalization and alternate character-set support")
75 (internal .
"code for Emacs internals, build process, defaults")
76 (languages .
"specialized modes for editing programming languages")
77 (lisp .
"Lisp support, including Emacs Lisp")
78 (local .
"code local to your site")
79 (maint .
"maintenance aids for the Emacs development group")
80 (mail .
"modes for electronic-mail handling")
81 (matching .
"various sorts of searching and matching")
82 (mouse .
"mouse support")
83 (multimedia .
"images and sound support")
84 (news .
"support for netnews reading and posting")
85 (oop .
"support for object-oriented programming")
86 (outlines .
"support for hierarchical outlining")
87 (processes .
"process, subshell, compilation, and job control support")
88 (terminals .
"support for terminal types")
89 (tex .
"supporting code for the TeX formatter")
90 (tools .
"programming tools")
91 (unix .
"front-ends/assistants for, or emulators of, UNIX-like features")
92 ;; Not a custom group and not currently useful.
93 ;; (vms . "support code for vms")
94 (wp .
"word processing")
97 (defvar finder-mode-map
98 (let ((map (make-sparse-keymap)))
99 (define-key map
" " 'finder-select
)
100 (define-key map
"f" 'finder-select
)
101 (define-key map
[follow-link
] 'mouse-face
)
102 (define-key map
[mouse-2
] 'finder-mouse-select
)
103 (define-key map
"\C-m" 'finder-select
)
104 (define-key map
"?" 'finder-summary
)
105 (define-key map
"n" 'next-line
)
106 (define-key map
"p" 'previous-line
)
107 (define-key map
"q" 'finder-exit
)
108 (define-key map
"d" 'finder-list-keywords
)
111 (defvar finder-mode-syntax-table
112 (let ((st (make-syntax-table emacs-lisp-mode-syntax-table
)))
113 (modify-syntax-entry ?\
; ". " st)
115 "Syntax table used while in `finder-mode'.")
117 (defvar finder-font-lock-keywords
118 '(("`\\([^']+\\)'" 1 font-lock-constant-face prepend
))
119 "Font-lock keywords for Finder mode.")
122 ;;; Code for regenerating the keyword list.
124 (defvar finder-package-info nil
125 "Assoc list mapping file names to description & keyword lists.")
127 (defvar generated-finder-keywords-file
"finder-inf.el"
128 "File \\[finder-compile-keywords] puts finder keywords into.")
130 (defun finder-compile-keywords (&rest dirs
)
131 "Regenerate the keywords association list into `generated-finder-keywords-file'.
132 Optional arguments DIRS are a list of Emacs Lisp directories to compile from;
133 no arguments compiles from `load-path'."
135 (let ((processed nil
))
136 (find-file generated-finder-keywords-file
)
138 (insert ";;; " (file-name-nondirectory generated-finder-keywords-file
)
139 " --- keyword-to-package mapping\n")
140 (insert ";; This file is part of GNU Emacs.\n")
141 (insert ";;; Commentary:\n")
142 (insert ";; Don't edit this file. It's generated by finder.el\n\n")
143 (insert ";;; Code:\n")
144 (insert "\n(setq finder-package-info '(\n")
147 (when (file-exists-p (directory-file-name d
))
148 (message "Directory %s" d
)
151 (if (and (or (string-match "^[^=].*\\.el$" f
)
152 ;; Allow compressed files also. Fixme:
153 ;; generalize this, especially for
154 ;; MS-DOG-type filenames.
155 (and (string-match "^[^=].*\\.el\\.\\(gz\\|Z\\)$" f
)
156 (require 'jka-compr
)))
157 ;; Ignore lock files.
158 (not (string-match "^.#" f
))
159 (not (member f processed
)))
160 (let (summary keystart keywords
)
161 (setq processed
(cons f processed
))
163 (set-buffer (get-buffer-create "*finder-scratch*"))
164 (buffer-disable-undo (current-buffer))
166 (insert-file-contents
167 (concat (file-name-as-directory (or d
".")) f
))
168 (setq summary
(lm-synopsis))
169 (setq keywords
(lm-keywords)))
171 (format " (\"%s\"\n "
172 (if (string-match "\\.\\(gz\\|Z\\)$" f
)
173 (file-name-sans-extension f
)
175 (prin1 summary
(current-buffer))
178 (setq keystart
(point))
180 (if keywords
(format "(%s)" keywords
) "nil")
182 (subst-char-in-region keystart
(point) ?
, ?
)
184 (directory-files (or d
".")))))
187 \(provide '" (file-name-sans-extension
188 (file-name-nondirectory generated-finder-keywords-file
)) ")
191 ;; version-control: never
192 ;; no-byte-compile: t
193 ;; no-update-autoloads: t
195 \;;; " (file-name-nondirectory generated-finder-keywords-file
) " ends here\n")
196 (kill-buffer "*finder-scratch*")
197 (eval-buffer) ;; So we get the new keyword list immediately
198 (basic-save-buffer))))
200 (defun finder-compile-keywords-make-dist ()
201 "Regenerate `finder-inf.el' for the Emacs distribution."
202 (apply 'finder-compile-keywords command-line-args-left
)
205 ;;; Now the retrieval code
207 (defun finder-insert-at-column (column &rest strings
)
208 "Insert, at column COLUMN, other args STRINGS."
209 (if (>= (current-column) column
) (insert "\n"))
210 (move-to-column column t
)
211 (apply 'insert strings
))
213 (defvar finder-help-echo nil
)
215 (defun finder-mouse-face-on-line ()
216 "Put `mouse-face' and `help-echo' properties on the previous line."
219 (unless finder-help-echo
220 (setq finder-help-echo
221 (let* ((keys1 (where-is-internal 'finder-select
223 (keys (nconc (where-is-internal
224 'finder-mouse-select finder-mode-map
)
226 (concat (mapconcat 'key-description keys
", ")
229 (line-beginning-position) (line-end-position)
230 '(mouse-face highlight
231 help-echo finder-help-echo
))))
234 (defun finder-list-keywords ()
235 "Display descriptions of the keywords in the Finder buffer."
237 (if (get-buffer "*Finder*")
238 (pop-to-buffer "*Finder*")
239 (pop-to-buffer (set-buffer (get-buffer-create "*Finder*")))
241 (setq buffer-read-only nil
)
245 (let ((keyword (car assoc
)))
246 (insert (symbol-name keyword
))
247 (finder-insert-at-column 14 (concat (cdr assoc
) "\n"))
248 (finder-mouse-face-on-line)))
249 finder-known-keywords
)
250 (goto-char (point-min))
251 (setq finder-headmark
(point))
252 (setq buffer-read-only t
)
253 (set-buffer-modified-p nil
)
257 (defun finder-list-matches (key)
258 (pop-to-buffer (set-buffer (get-buffer-create "*Finder Category*")))
260 (setq buffer-read-only nil
)
262 (let ((id (intern key
)))
264 "The following packages match the keyword `" key
"':\n\n")
265 (setq finder-headmark
(point))
268 (if (memq id
(car (cdr (cdr x
))))
271 (finder-insert-at-column 16 (concat (nth 1 x
) "\n"))
272 (finder-mouse-face-on-line))))
274 (goto-char (point-min))
276 (setq buffer-read-only t
)
277 (set-buffer-modified-p nil
)
278 (shrink-window-if-larger-than-buffer)
282 (defun finder-commentary (file)
283 "Display FILE's commentary section.
284 FILE should be in a form suitable for passing to `locate-library'."
287 (completing-read "Library name: "
288 (apply-partially 'locate-file-completion-table
289 (or find-function-source-path load-path
)
290 (find-library-suffixes)))))
291 (let* ((str (lm-commentary (find-library-name file
))))
293 (error "Can't find any Commentary section"))
294 ;; This used to use *Finder* but that would clobber the
295 ;; directory of categories.
296 (delete-other-windows)
297 (pop-to-buffer "*Finder-package*")
298 (setq buffer-read-only nil
)
301 (goto-char (point-min))
303 (goto-char (point-max))
305 (goto-char (point-min))
306 (while (re-search-forward "^;+ ?" nil t
)
307 (replace-match "" nil nil
))
308 (goto-char (point-min))
309 (setq buffer-read-only t
)
310 (set-buffer-modified-p nil
)
311 (shrink-window-if-larger-than-buffer)
315 (defun finder-current-item ()
316 (let ((key (save-excursion
319 (if (or (and finder-headmark
(< (point) finder-headmark
))
321 (error "No keyword or filename on this line")
324 (defun finder-select ()
325 "Select item on current line in a finder buffer."
327 (let ((key (finder-current-item)))
328 (if (string-match "\\.el$" key
)
329 (finder-commentary key
)
330 (finder-list-matches key
))))
332 (defun finder-mouse-select (event)
333 "Select item in a finder buffer with the mouse."
336 (set-buffer (window-buffer (posn-window (event-start event
))))
337 (goto-char (posn-point (event-start event
)))
341 (defun finder-by-keyword ()
342 "Find packages matching a given keyword."
344 (finder-list-keywords))
346 (defun finder-mode ()
347 "Major mode for browsing package documentation.
349 \\[finder-select] more help for the item on the current line
350 \\[finder-exit] exit Finder mode and kill the Finder buffer."
352 (kill-all-local-variables)
353 (use-local-map finder-mode-map
)
354 (set-syntax-table finder-mode-syntax-table
)
355 (setq font-lock-defaults
'(finder-font-lock-keywords nil nil
356 (("+-*/.<>=!?$%_&~^:@" .
"w")) nil
))
357 (setq mode-name
"Finder")
358 (setq major-mode
'finder-mode
)
359 (set (make-local-variable 'finder-headmark
) nil
)
360 (run-mode-hooks 'finder-mode-hook
))
362 (defun finder-summary ()
363 "Summarize basic Finder commands."
366 (substitute-command-keys
367 "\\<finder-mode-map>\\[finder-select] = select, \
368 \\[finder-mouse-select] = select, \\[finder-list-keywords] = to \
369 finder directory, \\[finder-exit] = quit, \\[finder-summary] = help")))
371 (defun finder-exit ()
373 Delete the window and kill the buffer."
375 (condition-case nil
(delete-window) (error nil
))
376 (when (get-buffer "*Finder*") (kill-buffer "*Finder*"))
377 (when (get-buffer "*Finder-package*") (kill-buffer "*Finder-package*"))
378 (when (get-buffer "*Finder Category*") (kill-buffer "*Finder Category*")))
383 ;; arch-tag: ec85ff49-8cb8-41f5-a63f-9131d53ce2c5
384 ;;; finder.el ends here