Mark `last_thing_searched' and `saved_last_thing_searched' before free unused memory.
[emacs.git] / lisp / finder.el
blob2de8e2e01a725c60e16626557534cff728fa112a
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
8 ;; Version: 1.0
9 ;; Keywords: help
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/>.
26 ;;; Commentary:
28 ;; This mode uses the Keywords library header to provide code-finding
29 ;; services by keyword.
31 ;;; Code:
33 (require 'lisp-mnt)
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,
40 ;; says rms.
41 (defvar finder-known-keywords
43 (abbrev . "abbreviation handling, typing shortcuts, macros")
44 ;; Too specific:
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 (wp . "word processing")
82 (defvar finder-mode-map
83 (let ((map (make-sparse-keymap))
84 (menu-map (make-sparse-keymap "Finder")))
85 (define-key map " " 'finder-select)
86 (define-key map "f" 'finder-select)
87 (define-key map [follow-link] 'mouse-face)
88 (define-key map [mouse-2] 'finder-mouse-select)
89 (define-key map "\C-m" 'finder-select)
90 (define-key map "?" 'finder-summary)
91 (define-key map "n" 'next-line)
92 (define-key map "p" 'previous-line)
93 (define-key map "q" 'finder-exit)
94 (define-key map "d" 'finder-list-keywords)
96 (define-key map [menu-bar finder-mode]
97 (cons "Finder" menu-map))
98 (define-key menu-map [finder-exit]
99 '(menu-item "Quit" finder-exit
100 :help "Exit Finder mode"))
101 (define-key menu-map [finder-summary]
102 '(menu-item "Summary" finder-summary
103 :help "Summary item on current line in a finder buffer"))
104 (define-key menu-map [finder-list-keywords]
105 '(menu-item "List keywords" finder-list-keywords
106 :help "Display descriptions of the keywords in the Finder buffer"))
107 (define-key menu-map [finder-select]
108 '(menu-item "Select" finder-select
109 :help "Select item on current line in a finder buffer"))
110 map))
112 (defvar finder-mode-syntax-table
113 (let ((st (make-syntax-table emacs-lisp-mode-syntax-table)))
114 (modify-syntax-entry ?\; ". " st)
116 "Syntax table used while in `finder-mode'.")
118 (defvar finder-font-lock-keywords
119 '(("`\\([^'`]+\\)'" 1 font-lock-constant-face prepend))
120 "Font-lock keywords for Finder mode.")
122 (defvar finder-headmark nil
123 "Internal finder-mode variable, local in finder buffer.")
125 ;;; Code for regenerating the keyword list.
127 (defvar finder-package-info nil
128 "Assoc list mapping file names to description & keyword lists.")
130 (defvar generated-finder-keywords-file "finder-inf.el"
131 "The function `finder-compile-keywords' writes keywords into this file.")
133 ;; Skip autogenerated files, because they will never contain anything
134 ;; useful, and because in parallel builds of Emacs they may get
135 ;; modified while we are trying to read them.
136 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-01/msg00469.html
137 ;; ldefs-boot is not auto-generated, but has nothing useful.
138 (defvar finder-no-scan-regexp "\\(^\\.#\\|\\(loaddefs\\|ldefs-boot\\|\
139 cus-load\\|finder-inf\\|esh-groups\\|subdirs\\)\\.el$\\)"
140 "Regexp matching file names not to scan for keywords.")
142 (autoload 'autoload-rubric "autoload")
144 (defun finder-compile-keywords (&rest dirs)
145 "Regenerate the keywords association list into `generated-finder-keywords-file'.
146 Optional arguments DIRS are a list of Emacs Lisp directories to compile from;
147 no arguments compiles from `load-path'."
148 (save-excursion
149 (find-file generated-finder-keywords-file)
150 (setq buffer-undo-list t)
151 (erase-buffer)
152 (insert (autoload-rubric generated-finder-keywords-file
153 "keyword-to-package mapping" t))
154 (search-backward "\f")
155 (insert "(setq finder-package-info '(\n")
156 (let (processed summary keywords)
157 (mapc
158 (lambda (d)
159 (when (file-exists-p (directory-file-name d))
160 (message "Directory %s" d)
161 (mapc
162 (lambda (f)
163 ;; FIXME should this not be using (expand-file-name f d)?
164 (unless (or (member f processed)
165 (string-match finder-no-scan-regexp f))
166 (setq processed (cons f processed))
167 (with-temp-buffer
168 (insert-file-contents (expand-file-name f d))
169 (setq summary (lm-synopsis)
170 keywords (lm-keywords-list)))
171 (insert
172 (format " (\"%s\"\n "
173 (if (string-match "\\.\\(gz\\|Z\\)$" f)
174 (file-name-sans-extension f)
175 f)))
176 (prin1 summary (current-buffer))
177 (insert "\n ")
178 (prin1 (mapcar 'intern keywords) (current-buffer))
179 (insert ")\n")))
180 (directory-files d nil
181 ;; Allow compressed files also. FIXME:
182 ;; generalize this, especially for
183 ;; MS-DOG-type filenames.
184 "^[^=].*\\.el\\(\\.\\(gz\\|Z\\)\\)?$"
185 ))))
186 (or dirs load-path)))
187 (insert " ))\n")
188 (eval-buffer) ; so we get the new keyword list immediately
189 (basic-save-buffer)))
191 (defun finder-compile-keywords-make-dist ()
192 "Regenerate `finder-inf.el' for the Emacs distribution."
193 (apply 'finder-compile-keywords command-line-args-left)
194 (kill-emacs))
196 ;;; Now the retrieval code
198 (defun finder-insert-at-column (column &rest strings)
199 "Insert, at column COLUMN, other args STRINGS."
200 (if (>= (current-column) column) (insert "\n"))
201 (move-to-column column t)
202 (apply 'insert strings))
204 (defvar finder-help-echo nil)
206 (defun finder-mouse-face-on-line ()
207 "Put `mouse-face' and `help-echo' properties on the previous line."
208 (save-excursion
209 (forward-line -1)
210 ;; If finder-insert-at-column moved us to a new line, go back one more.
211 (if (looking-at "[ \t]") (forward-line -1))
212 (unless finder-help-echo
213 (setq finder-help-echo
214 (let* ((keys1 (where-is-internal 'finder-select
215 finder-mode-map))
216 (keys (nconc (where-is-internal
217 'finder-mouse-select finder-mode-map)
218 keys1)))
219 (concat (mapconcat 'key-description keys ", ")
220 ": select item"))))
221 (add-text-properties
222 (line-beginning-position) (line-end-position)
223 '(mouse-face highlight
224 help-echo finder-help-echo))))
226 (defun finder-unknown-keywords ()
227 "Return an alist of unknown keywords and number of their occurences.
228 Unknown are keywords that are present in `finder-package-info'
229 but absent in `finder-known-keywords'."
230 (let ((unknown-keywords-hash (make-hash-table)))
231 ;; Prepare a hash where key is a keyword
232 ;; and value is the number of keyword occurences.
233 (mapc (lambda (package)
234 (mapc (lambda (keyword)
235 (unless (assq keyword finder-known-keywords)
236 (puthash keyword
237 (1+ (gethash keyword unknown-keywords-hash 0))
238 unknown-keywords-hash)))
239 (nth 2 package)))
240 finder-package-info)
241 ;; Make an alist from the hash and sort by the keyword name.
242 (sort (let (unknown-keywords-list)
243 (maphash (lambda (key value)
244 (push (cons key value) unknown-keywords-list))
245 unknown-keywords-hash)
246 unknown-keywords-list)
247 (lambda (a b) (string< (car a) (car b))))))
249 ;;;###autoload
250 (defun finder-list-keywords ()
251 "Display descriptions of the keywords in the Finder buffer."
252 (interactive)
253 (if (get-buffer "*Finder*")
254 (pop-to-buffer "*Finder*")
255 (pop-to-buffer (get-buffer-create "*Finder*"))
256 (finder-mode)
257 (setq buffer-read-only nil
258 buffer-undo-list t)
259 (erase-buffer)
260 (mapc
261 (lambda (assoc)
262 (let ((keyword (car assoc)))
263 (insert (symbol-name keyword))
264 (finder-insert-at-column 14 (concat (cdr assoc) "\n"))
265 (finder-mouse-face-on-line)))
266 finder-known-keywords)
267 (goto-char (point-min))
268 (setq finder-headmark (point)
269 buffer-read-only t)
270 (set-buffer-modified-p nil)
271 (balance-windows)
272 (finder-summary)))
274 (defun finder-list-matches (key)
275 (pop-to-buffer (set-buffer (get-buffer-create "*Finder Category*")))
276 (finder-mode)
277 (setq buffer-read-only nil
278 buffer-undo-list t)
279 (erase-buffer)
280 (let ((id (intern key)))
281 (insert
282 "The following packages match the keyword `" key "':\n\n")
283 (setq finder-headmark (point))
284 (mapc
285 (lambda (x)
286 (when (memq id (cadr (cdr x)))
287 (insert (car x))
288 (finder-insert-at-column 16 (concat (cadr x) "\n"))
289 (finder-mouse-face-on-line)))
290 finder-package-info)
291 (goto-char (point-min))
292 (forward-line)
293 (setq buffer-read-only t)
294 (set-buffer-modified-p nil)
295 (shrink-window-if-larger-than-buffer)
296 (finder-summary)))
298 (define-button-type 'finder-xref 'action #'finder-goto-xref)
300 (defun finder-goto-xref (button)
301 "Jump to a lisp file for the BUTTON at point."
302 (let* ((file (button-get button 'xref))
303 (lib (locate-library file)))
304 (if lib (finder-commentary lib)
305 (message "Unable to locate `%s'" file))))
307 ;;;###autoload
308 (defun finder-commentary (file)
309 "Display FILE's commentary section.
310 FILE should be in a form suitable for passing to `locate-library'."
311 (interactive
312 (list
313 (completing-read "Library name: "
314 (apply-partially 'locate-file-completion-table
315 (or find-function-source-path load-path)
316 (find-library-suffixes)))))
317 (let ((str (lm-commentary (find-library-name file))))
318 (or str (error "Can't find any Commentary section"))
319 ;; This used to use *Finder* but that would clobber the
320 ;; directory of categories.
321 (pop-to-buffer "*Finder-package*")
322 (setq buffer-read-only nil
323 buffer-undo-list t)
324 (erase-buffer)
325 (insert str)
326 (goto-char (point-min))
327 (delete-blank-lines)
328 (goto-char (point-max))
329 (delete-blank-lines)
330 (goto-char (point-min))
331 (while (re-search-forward "^;+ ?" nil t)
332 (replace-match "" nil nil))
333 (goto-char (point-min))
334 (while (re-search-forward "\\<\\([-[:alnum:]]+\\.el\\)\\>" nil t)
335 (if (locate-library (match-string 1))
336 (make-text-button (match-beginning 1) (match-end 1)
337 'xref (match-string-no-properties 1)
338 'help-echo "Read this file's commentary"
339 :type 'finder-xref)))
340 (goto-char (point-min))
341 (setq buffer-read-only t)
342 (set-buffer-modified-p nil)
343 (shrink-window-if-larger-than-buffer)
344 (finder-mode)
345 (finder-summary)))
347 (defun finder-current-item ()
348 (let ((key (save-excursion
349 (beginning-of-line)
350 (current-word))))
351 (if (or (and finder-headmark (< (point) finder-headmark))
352 (zerop (length key)))
353 (error "No keyword or filename on this line")
354 key)))
356 (defun finder-select ()
357 "Select item on current line in a finder buffer."
358 (interactive)
359 (let ((key (finder-current-item)))
360 (if (string-match "\\.el$" key)
361 (finder-commentary key)
362 (finder-list-matches key))))
364 (defun finder-mouse-select (event)
365 "Select item in a finder buffer with the mouse."
366 (interactive "e")
367 (with-current-buffer (window-buffer (posn-window (event-start event)))
368 (goto-char (posn-point (event-start event)))
369 (finder-select)))
371 ;;;###autoload
372 (defun finder-by-keyword ()
373 "Find packages matching a given keyword."
374 (interactive)
375 (finder-list-keywords))
377 (define-derived-mode finder-mode nil "Finder"
378 "Major mode for browsing package documentation.
379 \\<finder-mode-map>
380 \\[finder-select] more help for the item on the current line
381 \\[finder-exit] exit Finder mode and kill the Finder buffer."
382 :syntax-table finder-mode-syntax-table
383 (setq font-lock-defaults '(finder-font-lock-keywords nil nil
384 (("+-*/.<>=!?$%_&~^:@" . "w")) nil))
385 (set (make-local-variable 'finder-headmark) nil))
387 (defun finder-summary ()
388 "Summarize basic Finder commands."
389 (interactive)
390 (message "%s"
391 (substitute-command-keys
392 "\\<finder-mode-map>\\[finder-select] = select, \
393 \\[finder-mouse-select] = select, \\[finder-list-keywords] = to \
394 finder directory, \\[finder-exit] = quit, \\[finder-summary] = help")))
396 (defun finder-exit ()
397 "Exit Finder mode.
398 Delete the window and kill all Finder-related buffers."
399 (interactive)
400 (ignore-errors (delete-window))
401 (dolist (buff '("*Finder*" "*Finder-package*" "*Finder Category*"))
402 (and (get-buffer buff) (kill-buffer buff))))
405 (provide 'finder)
407 ;; arch-tag: ec85ff49-8cb8-41f5-a63f-9131d53ce2c5
408 ;;; finder.el ends here