Commit | Line | Data |
---|---|---|
1164bae9 ER |
1 | ;;; finder.el --- topic & keyword-based code finder |
2 | ||
228b7396 | 3 | ;; Copyright (C) 1992, 1997, 1998, 1999, 2001, 2002, 2003, 2004, 2005, |
114f9c96 | 4 | ;; 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
1164bae9 ER |
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 | ||
eb3fa2cf | 13 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
1164bae9 | 14 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
15 | ;; the Free Software Foundation, either version 3 of the License, or |
16 | ;; (at your option) any later version. | |
1164bae9 ER |
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 | |
eb3fa2cf | 24 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
1164bae9 | 25 | |
101dad14 | 26 | ;;; Commentary: |
1164bae9 ER |
27 | |
28 | ;; This mode uses the Keywords library header to provide code-finding | |
29 | ;; services by keyword. | |
1164bae9 ER |
30 | |
31 | ;;; Code: | |
32 | ||
33 | (require 'lisp-mnt) | |
24d30c03 | 34 | (require 'find-func) ;for find-library(-suffixes) |
f0c4433f SM |
35 | ;; Use `load' rather than `require' so that it doesn't get loaded |
36 | ;; during byte-compilation (at which point it might be missing). | |
2e819a08 | 37 | (load "finder-inf" t t) |
1164bae9 | 38 | |
4f451c3f DL |
39 | ;; These are supposed to correspond to top-level customization groups, |
40 | ;; says rms. | |
1164bae9 ER |
41 | (defvar finder-known-keywords |
42 | '( | |
dea24479 | 43 | (abbrev . "abbreviation handling, typing shortcuts, macros") |
f06f7416 | 44 | ;; Too specific: |
be2c35e4 | 45 | (bib . "code related to the `bib' bibliography processor") |
68f8f37f | 46 | (c . "support for the C language and related languages") |
1164bae9 | 47 | (calendar . "calendar and time management support") |
e9571d2a | 48 | (comm . "communications, networking, remote access to files") |
35e9c644 | 49 | (convenience . "convenience features for faster editing") |
dde4a5bb | 50 | (data . "support for editing files of data") |
1164bae9 ER |
51 | (docs . "support for Emacs documentation") |
52 | (emulations . "emulations of other editors") | |
53 | (extensions . "Emacs Lisp language extensions") | |
be2c35e4 | 54 | (faces . "support for multiple fonts") |
f06f7416 | 55 | (files . "support for editing and manipulating files") |
f1aabce0 | 56 | (frames . "support for Emacs frames and window systems") |
1164bae9 ER |
57 | (games . "games, jokes and amusements") |
58 | (hardware . "support for interfacing with exotic hardware") | |
59 | (help . "support for on-line help systems") | |
f1aabce0 | 60 | (hypermedia . "support for links between text or other media types") |
14931973 | 61 | (i18n . "internationalization and alternate character-set support") |
1164bae9 ER |
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") | |
be2c35e4 RS |
68 | (matching . "various sorts of searching and matching") |
69 | (mouse . "mouse support") | |
1226dd0b | 70 | (multimedia . "images and sound support") |
1164bae9 | 71 | (news . "support for netnews reading and posting") |
f1aabce0 RS |
72 | (oop . "support for object-oriented programming") |
73 | (outlines . "support for hierarchical outlining") | |
1164bae9 ER |
74 | (processes . "process, subshell, compilation, and job control support") |
75 | (terminals . "support for terminal types") | |
dde4a5bb | 76 | (tex . "supporting code for the TeX formatter") |
1164bae9 | 77 | (tools . "programming tools") |
dde4a5bb | 78 | (unix . "front-ends/assistants for, or emulators of, UNIX-like features") |
1164bae9 ER |
79 | (wp . "word processing") |
80 | )) | |
81 | ||
79760fb2 | 82 | (defvar finder-mode-map |
2643c7aa DN |
83 | (let ((map (make-sparse-keymap)) |
84 | (menu-map (make-sparse-keymap "Finder"))) | |
79760fb2 SM |
85 | (define-key map " " 'finder-select) |
86 | (define-key map "f" 'finder-select) | |
24bdbffe | 87 | (define-key map [follow-link] 'mouse-face) |
79760fb2 SM |
88 | (define-key map [mouse-2] 'finder-mouse-select) |
89 | (define-key map "\C-m" 'finder-select) | |
90 | (define-key map "?" 'finder-summary) | |
c9fdebdf RS |
91 | (define-key map "n" 'next-line) |
92 | (define-key map "p" 'previous-line) | |
79760fb2 SM |
93 | (define-key map "q" 'finder-exit) |
94 | (define-key map "d" 'finder-list-keywords) | |
2643c7aa DN |
95 | |
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")) | |
79760fb2 | 110 | map)) |
e141acb3 | 111 | |
89e2d476 SM |
112 | (defvar finder-mode-syntax-table |
113 | (let ((st (make-syntax-table emacs-lisp-mode-syntax-table))) | |
114 | (modify-syntax-entry ?\; ". " st) | |
115 | st) | |
116 | "Syntax table used while in `finder-mode'.") | |
117 | ||
118 | (defvar finder-font-lock-keywords | |
54e05660 | 119 | '(("`\\([^'`]+\\)'" 1 font-lock-constant-face prepend)) |
89e2d476 SM |
120 | "Font-lock keywords for Finder mode.") |
121 | ||
228b7396 GM |
122 | (defvar finder-headmark nil |
123 | "Internal finder-mode variable, local in finder buffer.") | |
101dad14 | 124 | |
1164bae9 ER |
125 | ;;; Code for regenerating the keyword list. |
126 | ||
127 | (defvar finder-package-info nil | |
128 | "Assoc list mapping file names to description & keyword lists.") | |
129 | ||
9bd0d71a | 130 | (defvar generated-finder-keywords-file "finder-inf.el" |
228b7396 GM |
131 | "The function `finder-compile-keywords' writes keywords into this file.") |
132 | ||
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 | |
276f10b7 GM |
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$\\)" | |
228b7396 GM |
140 | "Regexp matching file names not to scan for keywords.") |
141 | ||
142 | (autoload 'autoload-rubric "autoload") | |
9bd0d71a | 143 | |
1164bae9 | 144 | (defun finder-compile-keywords (&rest dirs) |
9bd0d71a | 145 | "Regenerate the keywords association list into `generated-finder-keywords-file'. |
2d212d87 DL |
146 | Optional arguments DIRS are a list of Emacs Lisp directories to compile from; |
147 | no arguments compiles from `load-path'." | |
1164bae9 | 148 | (save-excursion |
54e05660 GM |
149 | (find-file generated-finder-keywords-file) |
150 | (setq buffer-undo-list t) | |
151 | (erase-buffer) | |
152 | (insert (autoload-rubric generated-finder-keywords-file | |
60878f2d | 153 | "keyword-to-package mapping" t)) |
54e05660 GM |
154 | (search-backward "\f") |
155 | (insert "(setq finder-package-info '(\n") | |
67f63a7a | 156 | (let (processed summary keywords) |
3731290f | 157 | (mapc |
e6fb4d11 | 158 | (lambda (d) |
5b64ebe8 RS |
159 | (when (file-exists-p (directory-file-name d)) |
160 | (message "Directory %s" d) | |
228b7396 | 161 | (mapc |
2d212d87 | 162 | (lambda (f) |
228b7396 GM |
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) | |
67f63a7a | 170 | keywords (lm-keywords-list))) |
228b7396 GM |
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 ") | |
88f4758e | 178 | (prin1 (mapcar 'intern keywords) (current-buffer)) |
67f63a7a | 179 | (insert ")\n"))) |
228b7396 GM |
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 | )))) | |
54e05660 GM |
186 | (or dirs load-path))) |
187 | (insert " ))\n") | |
188 | (eval-buffer) ; so we get the new keyword list immediately | |
189 | (basic-save-buffer))) | |
1164bae9 | 190 | |
1c6425ea RS |
191 | (defun finder-compile-keywords-make-dist () |
192 | "Regenerate `finder-inf.el' for the Emacs distribution." | |
644f58eb RS |
193 | (apply 'finder-compile-keywords command-line-args-left) |
194 | (kill-emacs)) | |
1c6425ea | 195 | |
1164bae9 ER |
196 | ;;; Now the retrieval code |
197 | ||
e6fb4d11 | 198 | (defun finder-insert-at-column (column &rest strings) |
eb4df0c3 | 199 | "Insert, at column COLUMN, other args STRINGS." |
ff524b84 | 200 | (if (>= (current-column) column) (insert "\n")) |
eb4df0c3 | 201 | (move-to-column column t) |
e6fb4d11 EN |
202 | (apply 'insert strings)) |
203 | ||
f06f7416 DL |
204 | (defvar finder-help-echo nil) |
205 | ||
eb4df0c3 | 206 | (defun finder-mouse-face-on-line () |
f06f7416 | 207 | "Put `mouse-face' and `help-echo' properties on the previous line." |
eb4df0c3 | 208 | (save-excursion |
bf8aa901 | 209 | (forward-line -1) |
54e05660 GM |
210 | ;; If finder-insert-at-column moved us to a new line, go back one more. |
211 | (if (looking-at "[ \t]") (forward-line -1)) | |
f06f7416 DL |
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)))) | |
eb4df0c3 | 225 | |
57938a79 JL |
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)))))) | |
248 | ||
706239c7 | 249 | ;;;###autoload |
101dad14 ER |
250 | (defun finder-list-keywords () |
251 | "Display descriptions of the keywords in the Finder buffer." | |
252 | (interactive) | |
5acc847d RS |
253 | (if (get-buffer "*Finder*") |
254 | (pop-to-buffer "*Finder*") | |
228b7396 | 255 | (pop-to-buffer (get-buffer-create "*Finder*")) |
5acc847d | 256 | (finder-mode) |
228b7396 GM |
257 | (setq buffer-read-only nil |
258 | buffer-undo-list t) | |
5acc847d | 259 | (erase-buffer) |
f06f7416 | 260 | (mapc |
5acc847d RS |
261 | (lambda (assoc) |
262 | (let ((keyword (car assoc))) | |
263 | (insert (symbol-name keyword)) | |
264 | (finder-insert-at-column 14 (concat (cdr assoc) "\n")) | |
eb4df0c3 | 265 | (finder-mouse-face-on-line))) |
5acc847d RS |
266 | finder-known-keywords) |
267 | (goto-char (point-min)) | |
228b7396 GM |
268 | (setq finder-headmark (point) |
269 | buffer-read-only t) | |
5acc847d RS |
270 | (set-buffer-modified-p nil) |
271 | (balance-windows) | |
272 | (finder-summary))) | |
101dad14 ER |
273 | |
274 | (defun finder-list-matches (key) | |
2d7b0db7 | 275 | (pop-to-buffer (set-buffer (get-buffer-create "*Finder Category*"))) |
5acc847d | 276 | (finder-mode) |
228b7396 GM |
277 | (setq buffer-read-only nil |
278 | buffer-undo-list t) | |
101dad14 ER |
279 | (erase-buffer) |
280 | (let ((id (intern key))) | |
281 | (insert | |
282 | "The following packages match the keyword `" key "':\n\n") | |
14931973 | 283 | (setq finder-headmark (point)) |
f06f7416 | 284 | (mapc |
e6fb4d11 | 285 | (lambda (x) |
54e05660 GM |
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))) | |
101dad14 ER |
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))) | |
297 | ||
54e05660 GM |
298 | (define-button-type 'finder-xref 'action #'finder-goto-xref) |
299 | ||
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)))) | |
306 | ||
706239c7 | 307 | ;;;###autoload |
101dad14 | 308 | (defun finder-commentary (file) |
2d212d87 DL |
309 | "Display FILE's commentary section. |
310 | FILE should be in a form suitable for passing to `locate-library'." | |
24d30c03 SM |
311 | (interactive |
312 | (list | |
313 | (completing-read "Library name: " | |
6a021917 SM |
314 | (apply-partially 'locate-file-completion-table |
315 | (or find-function-source-path load-path) | |
316 | (find-library-suffixes))))) | |
228b7396 GM |
317 | (let ((str (lm-commentary (find-library-name file)))) |
318 | (or str (error "Can't find any Commentary section")) | |
63fe0a98 RS |
319 | ;; This used to use *Finder* but that would clobber the |
320 | ;; directory of categories. | |
321 | (pop-to-buffer "*Finder-package*") | |
228b7396 GM |
322 | (setq buffer-read-only nil |
323 | buffer-undo-list t) | |
101dad14 ER |
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)) | |
54e05660 GM |
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)) | |
101dad14 ER |
341 | (setq buffer-read-only t) |
342 | (set-buffer-modified-p nil) | |
343 | (shrink-window-if-larger-than-buffer) | |
8f713f53 | 344 | (finder-mode) |
e6fb4d11 | 345 | (finder-summary))) |
101dad14 ER |
346 | |
347 | (defun finder-current-item () | |
62832105 EZ |
348 | (let ((key (save-excursion |
349 | (beginning-of-line) | |
350 | (current-word)))) | |
351 | (if (or (and finder-headmark (< (point) finder-headmark)) | |
228b7396 | 352 | (zerop (length key))) |
62832105 EZ |
353 | (error "No keyword or filename on this line") |
354 | key))) | |
101dad14 ER |
355 | |
356 | (defun finder-select () | |
eb4df0c3 | 357 | "Select item on current line in a finder buffer." |
101dad14 ER |
358 | (interactive) |
359 | (let ((key (finder-current-item))) | |
f3b330d6 KH |
360 | (if (string-match "\\.el$" key) |
361 | (finder-commentary key) | |
362 | (finder-list-matches key)))) | |
363 | ||
364 | (defun finder-mouse-select (event) | |
eb4df0c3 | 365 | "Select item in a finder buffer with the mouse." |
f3b330d6 | 366 | (interactive "e") |
7fdbcd83 | 367 | (with-current-buffer (window-buffer (posn-window (event-start event))) |
eb4df0c3 DL |
368 | (goto-char (posn-point (event-start event))) |
369 | (finder-select))) | |
101dad14 | 370 | |
706239c7 | 371 | ;;;###autoload |
1164bae9 ER |
372 | (defun finder-by-keyword () |
373 | "Find packages matching a given keyword." | |
374 | (interactive) | |
101dad14 ER |
375 | (finder-list-keywords)) |
376 | ||
228b7396 | 377 | (define-derived-mode finder-mode nil "Finder" |
101dad14 | 378 | "Major mode for browsing package documentation. |
527da106 | 379 | \\<finder-mode-map> |
101dad14 | 380 | \\[finder-select] more help for the item on the current line |
2d212d87 | 381 | \\[finder-exit] exit Finder mode and kill the Finder buffer." |
228b7396 | 382 | :syntax-table finder-mode-syntax-table |
89e2d476 SM |
383 | (setq font-lock-defaults '(finder-font-lock-keywords nil nil |
384 | (("+-*/.<>=!?$%_&~^:@" . "w")) nil)) | |
228b7396 | 385 | (set (make-local-variable 'finder-headmark) nil)) |
101dad14 ER |
386 | |
387 | (defun finder-summary () | |
388 | "Summarize basic Finder commands." | |
389 | (interactive) | |
90f061c7 | 390 | (message "%s" |
527da106 | 391 | (substitute-command-keys |
eb4df0c3 DL |
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"))) | |
f5a16823 | 395 | |
101dad14 | 396 | (defun finder-exit () |
89e2d476 | 397 | "Exit Finder mode. |
228b7396 | 398 | Delete the window and kill all Finder-related buffers." |
101dad14 | 399 | (interactive) |
228b7396 GM |
400 | (ignore-errors (delete-window)) |
401 | (dolist (buff '("*Finder*" "*Finder-package*" "*Finder Category*")) | |
402 | (and (get-buffer buff) (kill-buffer buff)))) | |
1164bae9 | 403 | |
6a7ceddc | 404 | \f |
1164bae9 ER |
405 | (provide 'finder) |
406 | ||
bf8aa901 | 407 | ;; arch-tag: ec85ff49-8cb8-41f5-a63f-9131d53ce2c5 |
1164bae9 | 408 | ;;; finder.el ends here |