]> code.delx.au - gnu-emacs/blob - lisp/finder.el
Add finder unknown keywords.
[gnu-emacs] / lisp / finder.el
1 ;;; finder.el --- topic & keyword-based code finder
2
3 ;; Copyright (C) 1992, 1997, 1998, 1999, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
7 ;; Created: 16 Jun 1992
8 ;; Version: 1.0
9 ;; Keywords: help
10
11 ;; This file is part of GNU Emacs.
12
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.
17
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.
22
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/>.
25
26 ;;; Commentary:
27
28 ;; This mode uses the Keywords library header to provide code-finding
29 ;; services by keyword.
30 ;;
31 ;; Things to do:
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
37 ;;; Code:
38
39 (require 'lisp-mnt)
40 (require 'find-func) ;for find-library(-suffixes)
41 ;; Use `load' rather than `require' so that it doesn't get loaded
42 ;; during byte-compilation (at which point it might be missing).
43 (load "finder-inf" t t)
44
45 ;; These are supposed to correspond to top-level customization groups,
46 ;; says rms.
47 (defvar finder-known-keywords
48 '(
49 (abbrev . "abbreviation handling, typing shortcuts, macros")
50 ;; Too specific:
51 (bib . "code related to the `bib' bibliography processor")
52 (c . "support for the C language and related languages")
53 (calendar . "calendar and time management support")
54 (comm . "communications, networking, remote access to files")
55 (convenience . "convenience features for faster editing")
56 (data . "support for editing files of data")
57 (docs . "support for Emacs documentation")
58 (emulations . "emulations of other editors")
59 (extensions . "Emacs Lisp language extensions")
60 (faces . "support for multiple fonts")
61 (files . "support for editing and manipulating files")
62 (frames . "support for Emacs frames and window systems")
63 (games . "games, jokes and amusements")
64 (hardware . "support for interfacing with exotic hardware")
65 (help . "support for on-line help systems")
66 (hypermedia . "support for links between text or other media types")
67 (i18n . "internationalization and alternate character-set support")
68 (internal . "code for Emacs internals, build process, defaults")
69 (languages . "specialized modes for editing programming languages")
70 (lisp . "Lisp support, including Emacs Lisp")
71 (local . "code local to your site")
72 (maint . "maintenance aids for the Emacs development group")
73 (mail . "modes for electronic-mail handling")
74 (matching . "various sorts of searching and matching")
75 (mouse . "mouse support")
76 (multimedia . "images and sound support")
77 (news . "support for netnews reading and posting")
78 (oop . "support for object-oriented programming")
79 (outlines . "support for hierarchical outlining")
80 (processes . "process, subshell, compilation, and job control support")
81 (terminals . "support for terminal types")
82 (tex . "supporting code for the TeX formatter")
83 (tools . "programming tools")
84 (unix . "front-ends/assistants for, or emulators of, UNIX-like features")
85 (wp . "word processing")
86 ))
87
88 (defvar finder-mode-map
89 (let ((map (make-sparse-keymap))
90 (menu-map (make-sparse-keymap "Finder")))
91 (define-key map " " 'finder-select)
92 (define-key map "f" 'finder-select)
93 (define-key map [follow-link] 'mouse-face)
94 (define-key map [mouse-2] 'finder-mouse-select)
95 (define-key map "\C-m" 'finder-select)
96 (define-key map "?" 'finder-summary)
97 (define-key map "n" 'next-line)
98 (define-key map "p" 'previous-line)
99 (define-key map "q" 'finder-exit)
100 (define-key map "d" 'finder-list-keywords)
101
102 (define-key map [menu-bar finder-mode]
103 (cons "Finder" menu-map))
104 (define-key menu-map [finder-exit]
105 '(menu-item "Quit" finder-exit
106 :help "Exit Finder mode"))
107 (define-key menu-map [finder-summary]
108 '(menu-item "Summary" finder-summary
109 :help "Summary item on current line in a finder buffer"))
110 (define-key menu-map [finder-list-keywords]
111 '(menu-item "List keywords" finder-list-keywords
112 :help "Display descriptions of the keywords in the Finder buffer"))
113 (define-key menu-map [finder-select]
114 '(menu-item "Select" finder-select
115 :help "Select item on current line in a finder buffer"))
116 map))
117
118 (defvar finder-mode-syntax-table
119 (let ((st (make-syntax-table emacs-lisp-mode-syntax-table)))
120 (modify-syntax-entry ?\; ". " st)
121 st)
122 "Syntax table used while in `finder-mode'.")
123
124 (defvar finder-font-lock-keywords
125 '(("`\\([^'`]+\\)'" 1 font-lock-constant-face prepend))
126 "Font-lock keywords for Finder mode.")
127
128 (defvar finder-headmark nil
129 "Internal finder-mode variable, local in finder buffer.")
130
131 ;;; Code for regenerating the keyword list.
132
133 (defvar finder-package-info nil
134 "Assoc list mapping file names to description & keyword lists.")
135
136 (defvar generated-finder-keywords-file "finder-inf.el"
137 "The function `finder-compile-keywords' writes keywords into this file.")
138
139 ;; Skip autogenerated files, because they will never contain anything
140 ;; useful, and because in parallel builds of Emacs they may get
141 ;; modified while we are trying to read them.
142 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-01/msg00469.html
143 ;; ldefs-boot is not auto-generated, but has nothing useful.
144 (defvar finder-no-scan-regexp "\\(^\\.#\\|\\(loaddefs\\|ldefs-boot\\|\
145 cus-load\\|finder-inf\\|esh-groups\\|subdirs\\)\\.el$\\)"
146 "Regexp matching file names not to scan for keywords.")
147
148 (autoload 'autoload-rubric "autoload")
149
150 (defun finder-compile-keywords (&rest dirs)
151 "Regenerate the keywords association list into `generated-finder-keywords-file'.
152 Optional arguments DIRS are a list of Emacs Lisp directories to compile from;
153 no arguments compiles from `load-path'."
154 (save-excursion
155 (find-file generated-finder-keywords-file)
156 (setq buffer-undo-list t)
157 (erase-buffer)
158 (insert (autoload-rubric generated-finder-keywords-file
159 "keyword-to-package mapping" t))
160 (search-backward "\f")
161 (insert "(setq finder-package-info '(\n")
162 (let (processed summary keywords)
163 (mapc
164 (lambda (d)
165 (when (file-exists-p (directory-file-name d))
166 (message "Directory %s" d)
167 (mapc
168 (lambda (f)
169 ;; FIXME should this not be using (expand-file-name f d)?
170 (unless (or (member f processed)
171 (string-match finder-no-scan-regexp f))
172 (setq processed (cons f processed))
173 (with-temp-buffer
174 (insert-file-contents (expand-file-name f d))
175 (setq summary (lm-synopsis)
176 keywords (lm-keywords-list)))
177 (insert
178 (format " (\"%s\"\n "
179 (if (string-match "\\.\\(gz\\|Z\\)$" f)
180 (file-name-sans-extension f)
181 f)))
182 (prin1 summary (current-buffer))
183 (insert "\n ")
184 (prin1 (mapcar 'intern keywords) (current-buffer))
185 (insert ")\n")))
186 (directory-files d nil
187 ;; Allow compressed files also. FIXME:
188 ;; generalize this, especially for
189 ;; MS-DOG-type filenames.
190 "^[^=].*\\.el\\(\\.\\(gz\\|Z\\)\\)?$"
191 ))))
192 (or dirs load-path)))
193 (insert " ))\n")
194 (eval-buffer) ; so we get the new keyword list immediately
195 (basic-save-buffer)))
196
197 (defun finder-compile-keywords-make-dist ()
198 "Regenerate `finder-inf.el' for the Emacs distribution."
199 (apply 'finder-compile-keywords command-line-args-left)
200 (kill-emacs))
201
202 ;;; Now the retrieval code
203
204 (defun finder-insert-at-column (column &rest strings)
205 "Insert, at column COLUMN, other args STRINGS."
206 (if (>= (current-column) column) (insert "\n"))
207 (move-to-column column t)
208 (apply 'insert strings))
209
210 (defvar finder-help-echo nil)
211
212 (defun finder-mouse-face-on-line ()
213 "Put `mouse-face' and `help-echo' properties on the previous line."
214 (save-excursion
215 (forward-line -1)
216 ;; If finder-insert-at-column moved us to a new line, go back one more.
217 (if (looking-at "[ \t]") (forward-line -1))
218 (unless finder-help-echo
219 (setq finder-help-echo
220 (let* ((keys1 (where-is-internal 'finder-select
221 finder-mode-map))
222 (keys (nconc (where-is-internal
223 'finder-mouse-select finder-mode-map)
224 keys1)))
225 (concat (mapconcat 'key-description keys ", ")
226 ": select item"))))
227 (add-text-properties
228 (line-beginning-position) (line-end-position)
229 '(mouse-face highlight
230 help-echo finder-help-echo))))
231
232 (defun finder-unknown-keywords ()
233 "Return an alist of unknown keywords and number of their occurences.
234 Unknown are keywords that are present in `finder-package-info'
235 but absent in `finder-known-keywords'."
236 (let ((unknown-keywords-hash (make-hash-table)))
237 ;; Prepare a hash where key is a keyword
238 ;; and value is the number of keyword occurences.
239 (mapc (lambda (package)
240 (mapc (lambda (keyword)
241 (unless (assq keyword finder-known-keywords)
242 (puthash keyword
243 (1+ (gethash keyword unknown-keywords-hash 0))
244 unknown-keywords-hash)))
245 (nth 2 package)))
246 finder-package-info)
247 ;; Make an alist from the hash and sort by the keyword name.
248 (sort (let (unknown-keywords-list)
249 (maphash (lambda (key value)
250 (push (cons key value) unknown-keywords-list))
251 unknown-keywords-hash)
252 unknown-keywords-list)
253 (lambda (a b) (string< (car a) (car b))))))
254
255 ;;;###autoload
256 (defun finder-list-keywords ()
257 "Display descriptions of the keywords in the Finder buffer."
258 (interactive)
259 (if (get-buffer "*Finder*")
260 (pop-to-buffer "*Finder*")
261 (pop-to-buffer (get-buffer-create "*Finder*"))
262 (finder-mode)
263 (setq buffer-read-only nil
264 buffer-undo-list t)
265 (erase-buffer)
266 (mapc
267 (lambda (assoc)
268 (let ((keyword (car assoc)))
269 (insert (symbol-name keyword))
270 (finder-insert-at-column 14 (concat (cdr assoc) "\n"))
271 (finder-mouse-face-on-line)))
272 finder-known-keywords)
273 (goto-char (point-min))
274 (setq finder-headmark (point)
275 buffer-read-only t)
276 (set-buffer-modified-p nil)
277 (balance-windows)
278 (finder-summary)))
279
280 (defun finder-list-matches (key)
281 (pop-to-buffer (set-buffer (get-buffer-create "*Finder Category*")))
282 (finder-mode)
283 (setq buffer-read-only nil
284 buffer-undo-list t)
285 (erase-buffer)
286 (let ((id (intern key)))
287 (insert
288 "The following packages match the keyword `" key "':\n\n")
289 (setq finder-headmark (point))
290 (mapc
291 (lambda (x)
292 (when (memq id (cadr (cdr x)))
293 (insert (car x))
294 (finder-insert-at-column 16 (concat (cadr x) "\n"))
295 (finder-mouse-face-on-line)))
296 finder-package-info)
297 (goto-char (point-min))
298 (forward-line)
299 (setq buffer-read-only t)
300 (set-buffer-modified-p nil)
301 (shrink-window-if-larger-than-buffer)
302 (finder-summary)))
303
304 (define-button-type 'finder-xref 'action #'finder-goto-xref)
305
306 (defun finder-goto-xref (button)
307 "Jump to a lisp file for the BUTTON at point."
308 (let* ((file (button-get button 'xref))
309 (lib (locate-library file)))
310 (if lib (finder-commentary lib)
311 (message "Unable to locate `%s'" file))))
312
313 ;;;###autoload
314 (defun finder-commentary (file)
315 "Display FILE's commentary section.
316 FILE should be in a form suitable for passing to `locate-library'."
317 (interactive
318 (list
319 (completing-read "Library name: "
320 (apply-partially 'locate-file-completion-table
321 (or find-function-source-path load-path)
322 (find-library-suffixes)))))
323 (let ((str (lm-commentary (find-library-name file))))
324 (or str (error "Can't find any Commentary section"))
325 ;; This used to use *Finder* but that would clobber the
326 ;; directory of categories.
327 (pop-to-buffer "*Finder-package*")
328 (setq buffer-read-only nil
329 buffer-undo-list t)
330 (erase-buffer)
331 (insert str)
332 (goto-char (point-min))
333 (delete-blank-lines)
334 (goto-char (point-max))
335 (delete-blank-lines)
336 (goto-char (point-min))
337 (while (re-search-forward "^;+ ?" nil t)
338 (replace-match "" nil nil))
339 (goto-char (point-min))
340 (while (re-search-forward "\\<\\([-[:alnum:]]+\\.el\\)\\>" nil t)
341 (if (locate-library (match-string 1))
342 (make-text-button (match-beginning 1) (match-end 1)
343 'xref (match-string-no-properties 1)
344 'help-echo "Read this file's commentary"
345 :type 'finder-xref)))
346 (goto-char (point-min))
347 (setq buffer-read-only t)
348 (set-buffer-modified-p nil)
349 (shrink-window-if-larger-than-buffer)
350 (finder-mode)
351 (finder-summary)))
352
353 (defun finder-current-item ()
354 (let ((key (save-excursion
355 (beginning-of-line)
356 (current-word))))
357 (if (or (and finder-headmark (< (point) finder-headmark))
358 (zerop (length key)))
359 (error "No keyword or filename on this line")
360 key)))
361
362 (defun finder-select ()
363 "Select item on current line in a finder buffer."
364 (interactive)
365 (let ((key (finder-current-item)))
366 (if (string-match "\\.el$" key)
367 (finder-commentary key)
368 (finder-list-matches key))))
369
370 (defun finder-mouse-select (event)
371 "Select item in a finder buffer with the mouse."
372 (interactive "e")
373 (with-current-buffer (window-buffer (posn-window (event-start event)))
374 (goto-char (posn-point (event-start event)))
375 (finder-select)))
376
377 ;;;###autoload
378 (defun finder-by-keyword ()
379 "Find packages matching a given keyword."
380 (interactive)
381 (finder-list-keywords))
382
383 (define-derived-mode finder-mode nil "Finder"
384 "Major mode for browsing package documentation.
385 \\<finder-mode-map>
386 \\[finder-select] more help for the item on the current line
387 \\[finder-exit] exit Finder mode and kill the Finder buffer."
388 :syntax-table finder-mode-syntax-table
389 (setq font-lock-defaults '(finder-font-lock-keywords nil nil
390 (("+-*/.<>=!?$%_&~^:@" . "w")) nil))
391 (set (make-local-variable 'finder-headmark) nil))
392
393 (defun finder-summary ()
394 "Summarize basic Finder commands."
395 (interactive)
396 (message "%s"
397 (substitute-command-keys
398 "\\<finder-mode-map>\\[finder-select] = select, \
399 \\[finder-mouse-select] = select, \\[finder-list-keywords] = to \
400 finder directory, \\[finder-exit] = quit, \\[finder-summary] = help")))
401
402 (defun finder-exit ()
403 "Exit Finder mode.
404 Delete the window and kill all Finder-related buffers."
405 (interactive)
406 (ignore-errors (delete-window))
407 (dolist (buff '("*Finder*" "*Finder-package*" "*Finder Category*"))
408 (and (get-buffer buff) (kill-buffer buff))))
409
410 \f
411 (provide 'finder)
412
413 ;; arch-tag: ec85ff49-8cb8-41f5-a63f-9131d53ce2c5
414 ;;; finder.el ends here