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, or (at your option)
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; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
30 ;; This mode uses the Keywords library header to provide code-finding
31 ;; services by keyword.
34 ;; 1. Support multiple keywords per search. This could be extremely hairy;
35 ;; there doesn't seem to be any way to get completing-read to exit on
36 ;; an EOL with no substring pending, which is what we'd want to end the loop.
37 ;; 2. Search by string in synopsis line?
38 ;; 3. Function to check finder-package-info for unknown keywords.
43 (require 'find-func
) ;for find-library(-suffixes)
44 ;; Use `load' rather than `require' so that it doesn't get loaded
45 ;; during byte-compilation (at which point it might be missing).
46 (load "finder-inf" t t
)
48 (defvar finder-mode-hook nil
49 "*Hook run when function `finder-mode' is called.")
51 ;; Local variable in finder buffer.
52 (defvar finder-headmark
)
54 ;; These are supposed to correspond to top-level customization groups,
56 (defvar finder-known-keywords
58 (abbrev .
"abbreviation handling, typing shortcuts, macros")
60 (bib .
"code related to the `bib' bibliography processor")
61 (c .
"support for the C language and related languages")
62 (calendar .
"calendar and time management support")
63 (comm .
"communications, networking, remote access to files")
64 (convenience .
"convenience features for faster editing")
65 (data .
"support for editing files of data")
66 (docs .
"support for Emacs documentation")
67 (emulations .
"emulations of other editors")
68 (extensions .
"Emacs Lisp language extensions")
69 (faces .
"support for multiple fonts")
70 (files .
"support for editing and manipulating files")
71 (frames .
"support for Emacs frames and window systems")
72 (games .
"games, jokes and amusements")
73 (hardware .
"support for interfacing with exotic hardware")
74 (help .
"support for on-line help systems")
75 (hypermedia .
"support for links between text or other media types")
76 (i18n .
"internationalization and alternate character-set support")
77 (internal .
"code for Emacs internals, build process, defaults")
78 (languages .
"specialized modes for editing programming languages")
79 (lisp .
"Lisp support, including Emacs Lisp")
80 (local .
"code local to your site")
81 (maint .
"maintenance aids for the Emacs development group")
82 (mail .
"modes for electronic-mail handling")
83 (matching .
"various sorts of searching and matching")
84 (mouse .
"mouse support")
85 (multimedia .
"images and sound support")
86 (news .
"support for netnews reading and posting")
87 (oop .
"support for object-oriented programming")
88 (outlines .
"support for hierarchical outlining")
89 (processes .
"process, subshell, compilation, and job control support")
90 (terminals .
"support for terminal types")
91 (tex .
"supporting code for the TeX formatter")
92 (tools .
"programming tools")
93 (unix .
"front-ends/assistants for, or emulators of, UNIX-like features")
94 ;; Not a custom group and not currently useful.
95 ;; (vms . "support code for vms")
96 (wp .
"word processing")
99 (defvar finder-mode-map
100 (let ((map (make-sparse-keymap)))
101 (define-key map
" " 'finder-select
)
102 (define-key map
"f" 'finder-select
)
103 (define-key map
[follow-link
] 'mouse-face
)
104 (define-key map
[mouse-2
] 'finder-mouse-select
)
105 (define-key map
"\C-m" 'finder-select
)
106 (define-key map
"?" 'finder-summary
)
107 (define-key map
"n" 'next-line
)
108 (define-key map
"p" 'previous-line
)
109 (define-key map
"q" 'finder-exit
)
110 (define-key map
"d" 'finder-list-keywords
)
113 (defvar finder-mode-syntax-table
114 (let ((st (make-syntax-table emacs-lisp-mode-syntax-table
)))
115 (modify-syntax-entry ?\
; ". " st)
117 "Syntax table used while in `finder-mode'.")
119 (defvar finder-font-lock-keywords
120 '(("`\\([^']+\\)'" 1 font-lock-constant-face prepend
))
121 "Font-lock keywords for Finder mode.")
124 ;;; Code for regenerating the keyword list.
126 (defvar finder-package-info nil
127 "Assoc list mapping file names to description & keyword lists.")
129 (defvar generated-finder-keywords-file
"finder-inf.el"
130 "File \\[finder-compile-keywords] puts finder keywords into.")
132 (defun finder-compile-keywords (&rest dirs
)
133 "Regenerate the keywords association list into `generated-finder-keywords-file'.
134 Optional arguments DIRS are a list of Emacs Lisp directories to compile from;
135 no arguments compiles from `load-path'."
137 (let ((processed nil
))
138 (find-file generated-finder-keywords-file
)
140 (insert ";;; " (file-name-nondirectory generated-finder-keywords-file
)
141 " --- keyword-to-package mapping\n")
142 (insert ";; This file is part of GNU Emacs.\n")
143 (insert ";;; Commentary:\n")
144 (insert ";; Don't edit this file. It's generated by finder.el\n\n")
145 (insert ";;; Code:\n")
146 (insert "\n(setq finder-package-info '(\n")
149 (when (file-exists-p (directory-file-name d
))
150 (message "Directory %s" d
)
153 (if (and (or (string-match "^[^=].*\\.el$" f
)
154 ;; Allow compressed files also. Fixme:
155 ;; generalize this, especially for
156 ;; MS-DOG-type filenames.
157 (and (string-match "^[^=].*\\.el\\.\\(gz\\|Z\\)$" f
)
158 (require 'jka-compr
)))
159 ;; Ignore lock files.
160 (not (string-match "^.#" f
))
161 (not (member f processed
)))
162 (let (summary keystart keywords
)
163 (setq processed
(cons f processed
))
165 (set-buffer (get-buffer-create "*finder-scratch*"))
166 (buffer-disable-undo (current-buffer))
168 (insert-file-contents
169 (concat (file-name-as-directory (or d
".")) f
))
170 (setq summary
(lm-synopsis))
171 (setq keywords
(lm-keywords)))
173 (format " (\"%s\"\n "
174 (if (string-match "\\.\\(gz\\|Z\\)$" f
)
175 (file-name-sans-extension f
)
177 (prin1 summary
(current-buffer))
180 (setq keystart
(point))
182 (if keywords
(format "(%s)" keywords
) "nil")
184 (subst-char-in-region keystart
(point) ?
, ?
)
186 (directory-files (or d
".")))))
189 \(provide '" (file-name-sans-extension
190 (file-name-nondirectory generated-finder-keywords-file
)) ")
193 ;; version-control: never
194 ;; no-byte-compile: t
195 ;; no-update-autoloads: t
197 \;;; " (file-name-nondirectory generated-finder-keywords-file
) " ends here\n")
198 (kill-buffer "*finder-scratch*")
199 (eval-buffer) ;; So we get the new keyword list immediately
200 (basic-save-buffer))))
202 (defun finder-compile-keywords-make-dist ()
203 "Regenerate `finder-inf.el' for the Emacs distribution."
204 (apply 'finder-compile-keywords command-line-args-left
)
207 ;;; Now the retrieval code
209 (defun finder-insert-at-column (column &rest strings
)
210 "Insert, at column COLUMN, other args STRINGS."
211 (if (>= (current-column) column
) (insert "\n"))
212 (move-to-column column t
)
213 (apply 'insert strings
))
215 (defvar finder-help-echo nil
)
217 (defun finder-mouse-face-on-line ()
218 "Put `mouse-face' and `help-echo' properties on the previous line."
221 (unless finder-help-echo
222 (setq finder-help-echo
223 (let* ((keys1 (where-is-internal 'finder-select
225 (keys (nconc (where-is-internal
226 'finder-mouse-select finder-mode-map
)
228 (concat (mapconcat 'key-description keys
", ")
231 (line-beginning-position) (line-end-position)
232 '(mouse-face highlight
233 help-echo finder-help-echo
))))
236 (defun finder-list-keywords ()
237 "Display descriptions of the keywords in the Finder buffer."
239 (if (get-buffer "*Finder*")
240 (pop-to-buffer "*Finder*")
241 (pop-to-buffer (set-buffer (get-buffer-create "*Finder*")))
243 (setq buffer-read-only nil
)
247 (let ((keyword (car assoc
)))
248 (insert (symbol-name keyword
))
249 (finder-insert-at-column 14 (concat (cdr assoc
) "\n"))
250 (finder-mouse-face-on-line)))
251 finder-known-keywords
)
252 (goto-char (point-min))
253 (setq finder-headmark
(point))
254 (setq buffer-read-only t
)
255 (set-buffer-modified-p nil
)
259 (defun finder-list-matches (key)
260 (pop-to-buffer (set-buffer (get-buffer-create "*Finder Category*")))
262 (setq buffer-read-only nil
)
264 (let ((id (intern key
)))
266 "The following packages match the keyword `" key
"':\n\n")
267 (setq finder-headmark
(point))
270 (if (memq id
(car (cdr (cdr x
))))
273 (finder-insert-at-column 16 (concat (nth 1 x
) "\n"))
274 (finder-mouse-face-on-line))))
276 (goto-char (point-min))
278 (setq buffer-read-only t
)
279 (set-buffer-modified-p nil
)
280 (shrink-window-if-larger-than-buffer)
284 (defun finder-commentary (file)
285 "Display FILE's commentary section.
286 FILE should be in a form suitable for passing to `locate-library'."
289 (completing-read "Library name: "
290 'locate-file-completion
291 (cons (or find-function-source-path load-path
)
292 (find-library-suffixes)))))
293 (let* ((str (lm-commentary (find-library-name file
))))
295 (error "Can't find any Commentary section"))
296 ;; This used to use *Finder* but that would clobber the
297 ;; directory of categories.
298 (delete-other-windows)
299 (pop-to-buffer "*Finder-package*")
300 (setq buffer-read-only nil
)
303 (goto-char (point-min))
305 (goto-char (point-max))
307 (goto-char (point-min))
308 (while (re-search-forward "^;+ ?" nil t
)
309 (replace-match "" nil nil
))
310 (goto-char (point-min))
311 (setq buffer-read-only t
)
312 (set-buffer-modified-p nil
)
313 (shrink-window-if-larger-than-buffer)
317 (defun finder-current-item ()
318 (let ((key (save-excursion
321 (if (or (and finder-headmark
(< (point) finder-headmark
))
323 (error "No keyword or filename on this line")
326 (defun finder-select ()
327 "Select item on current line in a finder buffer."
329 (let ((key (finder-current-item)))
330 (if (string-match "\\.el$" key
)
331 (finder-commentary key
)
332 (finder-list-matches key
))))
334 (defun finder-mouse-select (event)
335 "Select item in a finder buffer with the mouse."
338 (set-buffer (window-buffer (posn-window (event-start event
))))
339 (goto-char (posn-point (event-start event
)))
343 (defun finder-by-keyword ()
344 "Find packages matching a given keyword."
346 (finder-list-keywords))
348 (defun finder-mode ()
349 "Major mode for browsing package documentation.
351 \\[finder-select] more help for the item on the current line
352 \\[finder-exit] exit Finder mode and kill the Finder buffer."
354 (kill-all-local-variables)
355 (use-local-map finder-mode-map
)
356 (set-syntax-table finder-mode-syntax-table
)
357 (setq font-lock-defaults
'(finder-font-lock-keywords nil nil
358 (("+-*/.<>=!?$%_&~^:@" .
"w")) nil
))
359 (setq mode-name
"Finder")
360 (setq major-mode
'finder-mode
)
361 (set (make-local-variable 'finder-headmark
) nil
)
362 (run-mode-hooks 'finder-mode-hook
))
364 (defun finder-summary ()
365 "Summarize basic Finder commands."
368 (substitute-command-keys
369 "\\<finder-mode-map>\\[finder-select] = select, \
370 \\[finder-mouse-select] = select, \\[finder-list-keywords] = to \
371 finder directory, \\[finder-exit] = quit, \\[finder-summary] = help")))
373 (defun finder-exit ()
375 Delete the window and kill the buffer."
377 (condition-case nil
(delete-window) (error nil
))
378 (when (get-buffer "*Finder*") (kill-buffer "*Finder*"))
379 (when (get-buffer "*Finder-package*") (kill-buffer "*Finder-package*"))
380 (when (get-buffer "*Finder Category*") (kill-buffer "*Finder Category*")))
385 ;; arch-tag: ec85ff49-8cb8-41f5-a63f-9131d53ce2c5
386 ;;; finder.el ends here