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.
34 (require 'find-func
) ;for find-library(-suffixes)
35 ;; Use `load' rather than `require' so that it doesn't get loaded
36 ;; during byte-compilation (at which point it might be missing).
37 (load "finder-inf" t t
)
39 ;; These are supposed to correspond to top-level customization groups,
41 (defvar finder-known-keywords
43 (abbrev .
"abbreviation handling, typing shortcuts, macros")
45 (bib .
"code related to the `bib' bibliography processor")
46 (c .
"support for the C language and related languages")
47 (calendar .
"calendar and time management support")
48 (comm .
"communications, networking, remote access to files")
49 (convenience .
"convenience features for faster editing")
50 (data .
"support for editing files of data")
51 (docs .
"support for Emacs documentation")
52 (emulations .
"emulations of other editors")
53 (extensions .
"Emacs Lisp language extensions")
54 (faces .
"support for multiple fonts")
55 (files .
"support for editing and manipulating files")
56 (frames .
"support for Emacs frames and window systems")
57 (games .
"games, jokes and amusements")
58 (hardware .
"support for interfacing with exotic hardware")
59 (help .
"support for on-line help systems")
60 (hypermedia .
"support for links between text or other media types")
61 (i18n .
"internationalization and alternate character-set support")
62 (internal .
"code for Emacs internals, build process, defaults")
63 (languages .
"specialized modes for editing programming languages")
64 (lisp .
"Lisp support, including Emacs Lisp")
65 (local .
"code local to your site")
66 (maint .
"maintenance aids for the Emacs development group")
67 (mail .
"modes for electronic-mail handling")
68 (matching .
"various sorts of searching and matching")
69 (mouse .
"mouse support")
70 (multimedia .
"images and sound support")
71 (news .
"support for netnews reading and posting")
72 (oop .
"support for object-oriented programming")
73 (outlines .
"support for hierarchical outlining")
74 (processes .
"process, subshell, compilation, and job control support")
75 (terminals .
"support for terminal types")
76 (tex .
"supporting code for the TeX formatter")
77 (tools .
"programming tools")
78 (unix .
"front-ends/assistants for, or emulators of, UNIX-like features")
79 (vc .
"version control")
80 (wp .
"word processing")
83 (defvar finder-mode-map
84 (let ((map (make-sparse-keymap))
85 (menu-map (make-sparse-keymap "Finder")))
86 (define-key map
" " 'finder-select
)
87 (define-key map
"f" 'finder-select
)
88 (define-key map
[follow-link
] 'mouse-face
)
89 (define-key map
[mouse-2
] 'finder-mouse-select
)
90 (define-key map
"\C-m" 'finder-select
)
91 (define-key map
"?" 'finder-summary
)
92 (define-key map
"n" 'next-line
)
93 (define-key map
"p" 'previous-line
)
94 (define-key map
"q" 'finder-exit
)
95 (define-key map
"d" 'finder-list-keywords
)
97 (define-key map
[menu-bar finder-mode
]
98 (cons "Finder" menu-map
))
99 (define-key menu-map
[finder-exit
]
100 '(menu-item "Quit" finder-exit
101 :help
"Exit Finder mode"))
102 (define-key menu-map
[finder-summary
]
103 '(menu-item "Summary" finder-summary
104 :help
"Summary item on current line in a finder buffer"))
105 (define-key menu-map
[finder-list-keywords
]
106 '(menu-item "List keywords" finder-list-keywords
107 :help
"Display descriptions of the keywords in the Finder buffer"))
108 (define-key menu-map
[finder-select
]
109 '(menu-item "Select" finder-select
110 :help
"Select item on current line in a finder buffer"))
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.")
123 (defvar finder-headmark nil
124 "Internal finder-mode variable, local in finder buffer.")
126 ;;; Code for regenerating the keyword list.
128 (defvar finder-package-info nil
129 "Assoc list mapping file names to description & keyword lists.")
131 (defvar generated-finder-keywords-file
"finder-inf.el"
132 "The function `finder-compile-keywords' writes keywords into this file.")
134 ;; Skip autogenerated files, because they will never contain anything
135 ;; useful, and because in parallel builds of Emacs they may get
136 ;; modified while we are trying to read them.
137 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-01/msg00469.html
138 ;; ldefs-boot is not auto-generated, but has nothing useful.
139 (defvar finder-no-scan-regexp
"\\(^\\.#\\|\\(loaddefs\\|ldefs-boot\\|\
140 cus-load\\|finder-inf\\|esh-groups\\|subdirs\\)\\.el$\\)"
141 "Regexp matching file names not to scan for keywords.")
143 (autoload 'autoload-rubric
"autoload")
145 (defun finder-compile-keywords (&rest dirs
)
146 "Regenerate the keywords association list into `generated-finder-keywords-file'.
147 Optional arguments DIRS are a list of Emacs Lisp directories to compile from;
148 no arguments compiles from `load-path'."
150 (find-file generated-finder-keywords-file
)
151 (setq buffer-undo-list t
)
153 (insert (autoload-rubric generated-finder-keywords-file
154 "keyword-to-package mapping" t
))
155 (search-backward "\f")
156 (insert "(setq finder-package-info '(\n")
157 (let (processed summary keywords
)
160 (when (file-exists-p (directory-file-name d
))
161 (message "Directory %s" d
)
164 ;; FIXME should this not be using (expand-file-name f d)?
165 (unless (or (member f processed
)
166 (string-match finder-no-scan-regexp f
))
167 (setq processed
(cons f processed
))
169 (insert-file-contents (expand-file-name f d
))
170 (setq summary
(lm-synopsis)
171 keywords
(lm-keywords-list)))
173 (format " (\"%s\"\n "
174 (if (string-match "\\.\\(gz\\|Z\\)$" f
)
175 (file-name-sans-extension f
)
177 (prin1 summary
(current-buffer))
179 (prin1 (mapcar 'intern keywords
) (current-buffer))
181 (directory-files d nil
182 ;; Allow compressed files also. FIXME:
183 ;; generalize this, especially for
184 ;; MS-DOG-type filenames.
185 "^[^=].*\\.el\\(\\.\\(gz\\|Z\\)\\)?$"
187 (or dirs load-path
)))
189 (eval-buffer) ; so we get the new keyword list immediately
190 (basic-save-buffer)))
192 (defun finder-compile-keywords-make-dist ()
193 "Regenerate `finder-inf.el' for the Emacs distribution."
194 (apply 'finder-compile-keywords command-line-args-left
)
197 ;;; Now the retrieval code
199 (defun finder-insert-at-column (column &rest strings
)
200 "Insert, at column COLUMN, other args STRINGS."
201 (if (>= (current-column) column
) (insert "\n"))
202 (move-to-column column t
)
203 (apply 'insert strings
))
205 (defvar finder-help-echo nil
)
207 (defun finder-mouse-face-on-line ()
208 "Put `mouse-face' and `help-echo' properties on the previous line."
211 ;; If finder-insert-at-column moved us to a new line, go back one more.
212 (if (looking-at "[ \t]") (forward-line -
1))
213 (unless finder-help-echo
214 (setq finder-help-echo
215 (let* ((keys1 (where-is-internal 'finder-select
217 (keys (nconc (where-is-internal
218 'finder-mouse-select finder-mode-map
)
220 (concat (mapconcat 'key-description keys
", ")
223 (line-beginning-position) (line-end-position)
224 '(mouse-face highlight
225 help-echo finder-help-echo
))))
227 (defun finder-unknown-keywords ()
228 "Return an alist of unknown keywords and number of their occurences.
229 Unknown are keywords that are present in `finder-package-info'
230 but absent in `finder-known-keywords'."
231 (let ((unknown-keywords-hash (make-hash-table)))
232 ;; Prepare a hash where key is a keyword
233 ;; and value is the number of keyword occurences.
234 (mapc (lambda (package)
235 (mapc (lambda (keyword)
236 (unless (assq keyword finder-known-keywords
)
238 (1+ (gethash keyword unknown-keywords-hash
0))
239 unknown-keywords-hash
)))
242 ;; Make an alist from the hash and sort by the keyword name.
243 (sort (let (unknown-keywords-list)
244 (maphash (lambda (key value
)
245 (push (cons key value
) unknown-keywords-list
))
246 unknown-keywords-hash
)
247 unknown-keywords-list
)
248 (lambda (a b
) (string< (car a
) (car b
))))))
251 (defun finder-list-keywords ()
252 "Display descriptions of the keywords in the Finder buffer."
254 (if (get-buffer "*Finder*")
255 (pop-to-buffer "*Finder*")
256 (pop-to-buffer (get-buffer-create "*Finder*"))
258 (setq buffer-read-only nil
263 (let ((keyword (car assoc
)))
264 (insert (symbol-name keyword
))
265 (finder-insert-at-column 14 (concat (cdr assoc
) "\n"))
266 (finder-mouse-face-on-line)))
267 finder-known-keywords
)
268 (goto-char (point-min))
269 (setq finder-headmark
(point)
271 (set-buffer-modified-p nil
)
275 (defun finder-list-matches (key)
276 (pop-to-buffer (set-buffer (get-buffer-create "*Finder Category*")))
278 (setq buffer-read-only nil
281 (let ((id (intern key
)))
283 "The following packages match the keyword `" key
"':\n\n")
284 (setq finder-headmark
(point))
287 (when (memq id
(cadr (cdr x
)))
289 (finder-insert-at-column 16 (concat (cadr x
) "\n"))
290 (finder-mouse-face-on-line)))
292 (goto-char (point-min))
294 (setq buffer-read-only t
)
295 (set-buffer-modified-p nil
)
296 (shrink-window-if-larger-than-buffer)
299 (define-button-type 'finder-xref
'action
#'finder-goto-xref
)
301 (defun finder-goto-xref (button)
302 "Jump to a lisp file for the BUTTON at point."
303 (let* ((file (button-get button
'xref
))
304 (lib (locate-library file
)))
305 (if lib
(finder-commentary lib
)
306 (message "Unable to locate `%s'" file
))))
309 (defun finder-commentary (file)
310 "Display FILE's commentary section.
311 FILE should be in a form suitable for passing to `locate-library'."
314 (completing-read "Library name: "
315 (apply-partially 'locate-file-completion-table
316 (or find-function-source-path load-path
)
317 (find-library-suffixes)))))
318 (let ((str (lm-commentary (find-library-name file
))))
319 (or str
(error "Can't find any Commentary section"))
320 ;; This used to use *Finder* but that would clobber the
321 ;; directory of categories.
322 (pop-to-buffer "*Finder-package*")
323 (setq buffer-read-only nil
327 (goto-char (point-min))
329 (goto-char (point-max))
331 (goto-char (point-min))
332 (while (re-search-forward "^;+ ?" nil t
)
333 (replace-match "" nil nil
))
334 (goto-char (point-min))
335 (while (re-search-forward "\\<\\([-[:alnum:]]+\\.el\\)\\>" nil t
)
336 (if (locate-library (match-string 1))
337 (make-text-button (match-beginning 1) (match-end 1)
338 'xref
(match-string-no-properties 1)
339 'help-echo
"Read this file's commentary"
340 :type
'finder-xref
)))
341 (goto-char (point-min))
342 (setq buffer-read-only t
)
343 (set-buffer-modified-p nil
)
344 (shrink-window-if-larger-than-buffer)
348 (defun finder-current-item ()
349 (let ((key (save-excursion
352 (if (or (and finder-headmark
(< (point) finder-headmark
))
353 (zerop (length key
)))
354 (error "No keyword or filename on this line")
357 (defun finder-select ()
358 "Select item on current line in a finder buffer."
360 (let ((key (finder-current-item)))
361 (if (string-match "\\.el$" key
)
362 (finder-commentary key
)
363 (finder-list-matches key
))))
365 (defun finder-mouse-select (event)
366 "Select item in a finder buffer with the mouse."
368 (with-current-buffer (window-buffer (posn-window (event-start event
)))
369 (goto-char (posn-point (event-start event
)))
373 (defun finder-by-keyword ()
374 "Find packages matching a given keyword."
376 (finder-list-keywords))
378 (define-derived-mode finder-mode nil
"Finder"
379 "Major mode for browsing package documentation.
381 \\[finder-select] more help for the item on the current line
382 \\[finder-exit] exit Finder mode and kill the Finder buffer."
383 :syntax-table finder-mode-syntax-table
384 (setq font-lock-defaults
'(finder-font-lock-keywords nil nil
385 (("+-*/.<>=!?$%_&~^:@" .
"w")) nil
))
386 (set (make-local-variable 'finder-headmark
) nil
))
388 (defun finder-summary ()
389 "Summarize basic Finder commands."
392 (substitute-command-keys
393 "\\<finder-mode-map>\\[finder-select] = select, \
394 \\[finder-mouse-select] = select, \\[finder-list-keywords] = to \
395 finder directory, \\[finder-exit] = quit, \\[finder-summary] = help")))
397 (defun finder-exit ()
399 Delete the window and kill all Finder-related buffers."
401 (ignore-errors (delete-window))
402 (dolist (buff '("*Finder*" "*Finder-package*" "*Finder Category*"))
403 (and (get-buffer buff
) (kill-buffer buff
))))
408 ;; arch-tag: ec85ff49-8cb8-41f5-a63f-9131d53ce2c5
409 ;;; finder.el ends here