1 ;;; finder.el --- topic & keyword-based code finder
3 ;; Copyright (C) 1992, 1997, 1998, 1999, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008, 2009, 2010 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 ;; These are supposed to correspond to top-level customization groups,
48 (defvar finder-known-keywords
50 (abbrev .
"abbreviation handling, typing shortcuts, macros")
52 (bib .
"code related to the `bib' bibliography processor")
53 (c .
"support for the C language and related languages")
54 (calendar .
"calendar and time management support")
55 (comm .
"communications, networking, remote access to files")
56 (convenience .
"convenience features for faster editing")
57 (data .
"support for editing files of data")
58 (docs .
"support for Emacs documentation")
59 (emulations .
"emulations of other editors")
60 (extensions .
"Emacs Lisp language extensions")
61 (faces .
"support for multiple fonts")
62 (files .
"support for editing and manipulating files")
63 (frames .
"support for Emacs frames and window systems")
64 (games .
"games, jokes and amusements")
65 (hardware .
"support for interfacing with exotic hardware")
66 (help .
"support for on-line help systems")
67 (hypermedia .
"support for links between text or other media types")
68 (i18n .
"internationalization and alternate character-set support")
69 (internal .
"code for Emacs internals, build process, defaults")
70 (languages .
"specialized modes for editing programming languages")
71 (lisp .
"Lisp support, including Emacs Lisp")
72 (local .
"code local to your site")
73 (maint .
"maintenance aids for the Emacs development group")
74 (mail .
"modes for electronic-mail handling")
75 (matching .
"various sorts of searching and matching")
76 (mouse .
"mouse support")
77 (multimedia .
"images and sound support")
78 (news .
"support for netnews reading and posting")
79 (oop .
"support for object-oriented programming")
80 (outlines .
"support for hierarchical outlining")
81 (processes .
"process, subshell, compilation, and job control support")
82 (terminals .
"support for terminal types")
83 (tex .
"supporting code for the TeX formatter")
84 (tools .
"programming tools")
85 (unix .
"front-ends/assistants for, or emulators of, UNIX-like features")
86 (wp .
"word processing")
89 (defvar finder-mode-map
90 (let ((map (make-sparse-keymap))
91 (menu-map (make-sparse-keymap "Finder")))
92 (define-key map
" " 'finder-select
)
93 (define-key map
"f" 'finder-select
)
94 (define-key map
[follow-link
] 'mouse-face
)
95 (define-key map
[mouse-2
] 'finder-mouse-select
)
96 (define-key map
"\C-m" 'finder-select
)
97 (define-key map
"?" 'finder-summary
)
98 (define-key map
"n" 'next-line
)
99 (define-key map
"p" 'previous-line
)
100 (define-key map
"q" 'finder-exit
)
101 (define-key map
"d" 'finder-list-keywords
)
103 (define-key map
[menu-bar finder-mode
]
104 (cons "Finder" menu-map
))
105 (define-key menu-map
[finder-exit
]
106 '(menu-item "Quit" finder-exit
107 :help
"Exit Finder mode"))
108 (define-key menu-map
[finder-summary
]
109 '(menu-item "Summary" finder-summary
110 :help
"Summary item on current line in a finder buffer"))
111 (define-key menu-map
[finder-list-keywords
]
112 '(menu-item "List keywords" finder-list-keywords
113 :help
"Display descriptions of the keywords in the Finder buffer"))
114 (define-key menu-map
[finder-select
]
115 '(menu-item "Select" finder-select
116 :help
"Select item on current line in a finder buffer"))
119 (defvar finder-mode-syntax-table
120 (let ((st (make-syntax-table emacs-lisp-mode-syntax-table
)))
121 (modify-syntax-entry ?\
; ". " st)
123 "Syntax table used while in `finder-mode'.")
125 (defvar finder-font-lock-keywords
126 '(("`\\([^'`]+\\)'" 1 font-lock-constant-face prepend
))
127 "Font-lock keywords for Finder mode.")
129 (defvar finder-headmark nil
130 "Internal finder-mode variable, local in finder buffer.")
132 ;;; Code for regenerating the keyword list.
134 (defvar finder-package-info nil
135 "Assoc list mapping file names to description & keyword lists.")
137 (defvar generated-finder-keywords-file
"finder-inf.el"
138 "The function `finder-compile-keywords' writes keywords into this file.")
140 ;; Skip autogenerated files, because they will never contain anything
141 ;; useful, and because in parallel builds of Emacs they may get
142 ;; modified while we are trying to read them.
143 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-01/msg00469.html
144 ;; ldefs-boot is not auto-generated, but has nothing useful.
145 (defvar finder-no-scan-regexp
"\\(^\\.#\\|\\(loaddefs\\|ldefs-boot\\|\
146 cus-load\\|finder-inf\\|esh-groups\\|subdirs\\)\\.el$\\)"
147 "Regexp matching file names not to scan for keywords.")
149 (autoload 'autoload-rubric
"autoload")
151 (defun finder-compile-keywords (&rest dirs
)
152 "Regenerate the keywords association list into `generated-finder-keywords-file'.
153 Optional arguments DIRS are a list of Emacs Lisp directories to compile from;
154 no arguments compiles from `load-path'."
156 (find-file generated-finder-keywords-file
)
157 (setq buffer-undo-list t
)
159 (insert (autoload-rubric generated-finder-keywords-file
160 "keyword-to-package mapping" t
))
161 (search-backward "\f")
162 (insert "(setq finder-package-info '(\n")
163 (let (processed summary keywords
)
166 (when (file-exists-p (directory-file-name d
))
167 (message "Directory %s" d
)
170 ;; FIXME should this not be using (expand-file-name f d)?
171 (unless (or (member f processed
)
172 (string-match finder-no-scan-regexp f
))
173 (setq processed
(cons f processed
))
175 (insert-file-contents (expand-file-name f d
))
176 (setq summary
(lm-synopsis)
177 keywords
(lm-keywords-list)))
179 (format " (\"%s\"\n "
180 (if (string-match "\\.\\(gz\\|Z\\)$" f
)
181 (file-name-sans-extension f
)
183 (prin1 summary
(current-buffer))
185 (princ keywords
(current-buffer))
187 (directory-files d nil
188 ;; Allow compressed files also. FIXME:
189 ;; generalize this, especially for
190 ;; MS-DOG-type filenames.
191 "^[^=].*\\.el\\(\\.\\(gz\\|Z\\)\\)?$"
193 (or dirs load-path
)))
195 (eval-buffer) ; so we get the new keyword list immediately
196 (basic-save-buffer)))
198 (defun finder-compile-keywords-make-dist ()
199 "Regenerate `finder-inf.el' for the Emacs distribution."
200 (apply 'finder-compile-keywords command-line-args-left
)
203 ;;; Now the retrieval code
205 (defun finder-insert-at-column (column &rest strings
)
206 "Insert, at column COLUMN, other args STRINGS."
207 (if (>= (current-column) column
) (insert "\n"))
208 (move-to-column column t
)
209 (apply 'insert strings
))
211 (defvar finder-help-echo nil
)
213 (defun finder-mouse-face-on-line ()
214 "Put `mouse-face' and `help-echo' properties on the previous line."
217 ;; If finder-insert-at-column moved us to a new line, go back one more.
218 (if (looking-at "[ \t]") (forward-line -
1))
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 (get-buffer-create "*Finder*"))
241 (setq buffer-read-only nil
246 (let ((keyword (car assoc
)))
247 (insert (symbol-name keyword
))
248 (finder-insert-at-column 14 (concat (cdr assoc
) "\n"))
249 (finder-mouse-face-on-line)))
250 finder-known-keywords
)
251 (goto-char (point-min))
252 (setq finder-headmark
(point)
254 (set-buffer-modified-p nil
)
258 (defun finder-list-matches (key)
259 (pop-to-buffer (set-buffer (get-buffer-create "*Finder Category*")))
261 (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 (when (memq id
(cadr (cdr x
)))
272 (finder-insert-at-column 16 (concat (cadr x
) "\n"))
273 (finder-mouse-face-on-line)))
275 (goto-char (point-min))
277 (setq buffer-read-only t
)
278 (set-buffer-modified-p nil
)
279 (shrink-window-if-larger-than-buffer)
282 (define-button-type 'finder-xref
'action
#'finder-goto-xref
)
284 (defun finder-goto-xref (button)
285 "Jump to a lisp file for the BUTTON at point."
286 (let* ((file (button-get button
'xref
))
287 (lib (locate-library file
)))
288 (if lib
(finder-commentary lib
)
289 (message "Unable to locate `%s'" file
))))
292 (defun finder-commentary (file)
293 "Display FILE's commentary section.
294 FILE should be in a form suitable for passing to `locate-library'."
297 (completing-read "Library name: "
298 (apply-partially 'locate-file-completion-table
299 (or find-function-source-path load-path
)
300 (find-library-suffixes)))))
301 (let ((str (lm-commentary (find-library-name file
))))
302 (or str
(error "Can't find any Commentary section"))
303 ;; This used to use *Finder* but that would clobber the
304 ;; directory of categories.
305 (pop-to-buffer "*Finder-package*")
306 (setq buffer-read-only nil
310 (goto-char (point-min))
312 (goto-char (point-max))
314 (goto-char (point-min))
315 (while (re-search-forward "^;+ ?" nil t
)
316 (replace-match "" nil nil
))
317 (goto-char (point-min))
318 (while (re-search-forward "\\<\\([-[:alnum:]]+\\.el\\)\\>" nil t
)
319 (if (locate-library (match-string 1))
320 (make-text-button (match-beginning 1) (match-end 1)
321 'xref
(match-string-no-properties 1)
322 'help-echo
"Read this file's commentary"
323 :type
'finder-xref
)))
324 (goto-char (point-min))
325 (setq buffer-read-only t
)
326 (set-buffer-modified-p nil
)
327 (shrink-window-if-larger-than-buffer)
331 (defun finder-current-item ()
332 (let ((key (save-excursion
335 (if (or (and finder-headmark
(< (point) finder-headmark
))
336 (zerop (length key
)))
337 (error "No keyword or filename on this line")
340 (defun finder-select ()
341 "Select item on current line in a finder buffer."
343 (let ((key (finder-current-item)))
344 (if (string-match "\\.el$" key
)
345 (finder-commentary key
)
346 (finder-list-matches key
))))
348 (defun finder-mouse-select (event)
349 "Select item in a finder buffer with the mouse."
351 (with-current-buffer (window-buffer (posn-window (event-start event
)))
352 (goto-char (posn-point (event-start event
)))
356 (defun finder-by-keyword ()
357 "Find packages matching a given keyword."
359 (finder-list-keywords))
361 (define-derived-mode finder-mode nil
"Finder"
362 "Major mode for browsing package documentation.
364 \\[finder-select] more help for the item on the current line
365 \\[finder-exit] exit Finder mode and kill the Finder buffer."
366 :syntax-table finder-mode-syntax-table
367 (setq font-lock-defaults
'(finder-font-lock-keywords nil nil
368 (("+-*/.<>=!?$%_&~^:@" .
"w")) nil
))
369 (set (make-local-variable 'finder-headmark
) nil
))
371 (defun finder-summary ()
372 "Summarize basic Finder commands."
375 (substitute-command-keys
376 "\\<finder-mode-map>\\[finder-select] = select, \
377 \\[finder-mouse-select] = select, \\[finder-list-keywords] = to \
378 finder directory, \\[finder-exit] = quit, \\[finder-summary] = help")))
380 (defun finder-exit ()
382 Delete the window and kill all Finder-related buffers."
384 (ignore-errors (delete-window))
385 (dolist (buff '("*Finder*" "*Finder-package*" "*Finder Category*"))
386 (and (get-buffer buff
) (kill-buffer buff
))))
391 ;; arch-tag: ec85ff49-8cb8-41f5-a63f-9131d53ce2c5
392 ;;; finder.el ends here