* net/tramp.el (tramp-open-shell): New defun.
[bpt/emacs.git] / lisp / finder.el
CommitLineData
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")
9766adfb 79 (vc . "version control")
1164bae9
ER
80 (wp . "word processing")
81 ))
82
79760fb2 83(defvar finder-mode-map
2643c7aa
DN
84 (let ((map (make-sparse-keymap))
85 (menu-map (make-sparse-keymap "Finder")))
79760fb2
SM
86 (define-key map " " 'finder-select)
87 (define-key map "f" 'finder-select)
24bdbffe 88 (define-key map [follow-link] 'mouse-face)
79760fb2
SM
89 (define-key map [mouse-2] 'finder-mouse-select)
90 (define-key map "\C-m" 'finder-select)
91 (define-key map "?" 'finder-summary)
c9fdebdf
RS
92 (define-key map "n" 'next-line)
93 (define-key map "p" 'previous-line)
79760fb2
SM
94 (define-key map "q" 'finder-exit)
95 (define-key map "d" 'finder-list-keywords)
2643c7aa
DN
96
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"))
79760fb2 111 map))
e141acb3 112
89e2d476
SM
113(defvar finder-mode-syntax-table
114 (let ((st (make-syntax-table emacs-lisp-mode-syntax-table)))
115 (modify-syntax-entry ?\; ". " st)
116 st)
117 "Syntax table used while in `finder-mode'.")
118
119(defvar finder-font-lock-keywords
54e05660 120 '(("`\\([^'`]+\\)'" 1 font-lock-constant-face prepend))
89e2d476
SM
121 "Font-lock keywords for Finder mode.")
122
228b7396
GM
123(defvar finder-headmark nil
124 "Internal finder-mode variable, local in finder buffer.")
101dad14 125
1164bae9
ER
126;;; Code for regenerating the keyword list.
127
128(defvar finder-package-info nil
129 "Assoc list mapping file names to description & keyword lists.")
130
9bd0d71a 131(defvar generated-finder-keywords-file "finder-inf.el"
228b7396
GM
132 "The function `finder-compile-keywords' writes keywords into this file.")
133
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
276f10b7
GM
138;; ldefs-boot is not auto-generated, but has nothing useful.
139(defvar finder-no-scan-regexp "\\(^\\.#\\|\\(loaddefs\\|ldefs-boot\\|\
140cus-load\\|finder-inf\\|esh-groups\\|subdirs\\)\\.el$\\)"
228b7396
GM
141 "Regexp matching file names not to scan for keywords.")
142
143(autoload 'autoload-rubric "autoload")
9bd0d71a 144
1164bae9 145(defun finder-compile-keywords (&rest dirs)
9bd0d71a 146 "Regenerate the keywords association list into `generated-finder-keywords-file'.
2d212d87
DL
147Optional arguments DIRS are a list of Emacs Lisp directories to compile from;
148no arguments compiles from `load-path'."
1164bae9 149 (save-excursion
54e05660
GM
150 (find-file generated-finder-keywords-file)
151 (setq buffer-undo-list t)
152 (erase-buffer)
153 (insert (autoload-rubric generated-finder-keywords-file
60878f2d 154 "keyword-to-package mapping" t))
54e05660
GM
155 (search-backward "\f")
156 (insert "(setq finder-package-info '(\n")
67f63a7a 157 (let (processed summary keywords)
3731290f 158 (mapc
e6fb4d11 159 (lambda (d)
5b64ebe8
RS
160 (when (file-exists-p (directory-file-name d))
161 (message "Directory %s" d)
228b7396 162 (mapc
2d212d87 163 (lambda (f)
228b7396
GM
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))
168 (with-temp-buffer
169 (insert-file-contents (expand-file-name f d))
170 (setq summary (lm-synopsis)
67f63a7a 171 keywords (lm-keywords-list)))
228b7396
GM
172 (insert
173 (format " (\"%s\"\n "
174 (if (string-match "\\.\\(gz\\|Z\\)$" f)
175 (file-name-sans-extension f)
176 f)))
177 (prin1 summary (current-buffer))
178 (insert "\n ")
88f4758e 179 (prin1 (mapcar 'intern keywords) (current-buffer))
67f63a7a 180 (insert ")\n")))
228b7396
GM
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\\)\\)?$"
186 ))))
54e05660
GM
187 (or dirs load-path)))
188 (insert " ))\n")
189 (eval-buffer) ; so we get the new keyword list immediately
190 (basic-save-buffer)))
1164bae9 191
1c6425ea
RS
192(defun finder-compile-keywords-make-dist ()
193 "Regenerate `finder-inf.el' for the Emacs distribution."
644f58eb
RS
194 (apply 'finder-compile-keywords command-line-args-left)
195 (kill-emacs))
1c6425ea 196
1164bae9
ER
197;;; Now the retrieval code
198
e6fb4d11 199(defun finder-insert-at-column (column &rest strings)
eb4df0c3 200 "Insert, at column COLUMN, other args STRINGS."
ff524b84 201 (if (>= (current-column) column) (insert "\n"))
eb4df0c3 202 (move-to-column column t)
e6fb4d11
EN
203 (apply 'insert strings))
204
f06f7416
DL
205(defvar finder-help-echo nil)
206
eb4df0c3 207(defun finder-mouse-face-on-line ()
f06f7416 208 "Put `mouse-face' and `help-echo' properties on the previous line."
eb4df0c3 209 (save-excursion
bf8aa901 210 (forward-line -1)
54e05660
GM
211 ;; If finder-insert-at-column moved us to a new line, go back one more.
212 (if (looking-at "[ \t]") (forward-line -1))
f06f7416
DL
213 (unless finder-help-echo
214 (setq finder-help-echo
215 (let* ((keys1 (where-is-internal 'finder-select
216 finder-mode-map))
217 (keys (nconc (where-is-internal
218 'finder-mouse-select finder-mode-map)
219 keys1)))
220 (concat (mapconcat 'key-description keys ", ")
221 ": select item"))))
222 (add-text-properties
223 (line-beginning-position) (line-end-position)
224 '(mouse-face highlight
225 help-echo finder-help-echo))))
eb4df0c3 226
57938a79
JL
227(defun finder-unknown-keywords ()
228 "Return an alist of unknown keywords and number of their occurences.
229Unknown are keywords that are present in `finder-package-info'
230but 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)
237 (puthash keyword
238 (1+ (gethash keyword unknown-keywords-hash 0))
239 unknown-keywords-hash)))
240 (nth 2 package)))
241 finder-package-info)
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))))))
249
706239c7 250;;;###autoload
101dad14
ER
251(defun finder-list-keywords ()
252 "Display descriptions of the keywords in the Finder buffer."
253 (interactive)
5acc847d
RS
254 (if (get-buffer "*Finder*")
255 (pop-to-buffer "*Finder*")
228b7396 256 (pop-to-buffer (get-buffer-create "*Finder*"))
5acc847d 257 (finder-mode)
228b7396
GM
258 (setq buffer-read-only nil
259 buffer-undo-list t)
5acc847d 260 (erase-buffer)
f06f7416 261 (mapc
5acc847d
RS
262 (lambda (assoc)
263 (let ((keyword (car assoc)))
264 (insert (symbol-name keyword))
265 (finder-insert-at-column 14 (concat (cdr assoc) "\n"))
eb4df0c3 266 (finder-mouse-face-on-line)))
5acc847d
RS
267 finder-known-keywords)
268 (goto-char (point-min))
228b7396
GM
269 (setq finder-headmark (point)
270 buffer-read-only t)
5acc847d
RS
271 (set-buffer-modified-p nil)
272 (balance-windows)
273 (finder-summary)))
101dad14
ER
274
275(defun finder-list-matches (key)
2d7b0db7 276 (pop-to-buffer (set-buffer (get-buffer-create "*Finder Category*")))
5acc847d 277 (finder-mode)
228b7396
GM
278 (setq buffer-read-only nil
279 buffer-undo-list t)
101dad14
ER
280 (erase-buffer)
281 (let ((id (intern key)))
282 (insert
283 "The following packages match the keyword `" key "':\n\n")
14931973 284 (setq finder-headmark (point))
f06f7416 285 (mapc
e6fb4d11 286 (lambda (x)
54e05660
GM
287 (when (memq id (cadr (cdr x)))
288 (insert (car x))
289 (finder-insert-at-column 16 (concat (cadr x) "\n"))
290 (finder-mouse-face-on-line)))
101dad14
ER
291 finder-package-info)
292 (goto-char (point-min))
293 (forward-line)
294 (setq buffer-read-only t)
295 (set-buffer-modified-p nil)
296 (shrink-window-if-larger-than-buffer)
297 (finder-summary)))
298
54e05660
GM
299(define-button-type 'finder-xref 'action #'finder-goto-xref)
300
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))))
307
706239c7 308;;;###autoload
101dad14 309(defun finder-commentary (file)
2d212d87
DL
310 "Display FILE's commentary section.
311FILE should be in a form suitable for passing to `locate-library'."
24d30c03
SM
312 (interactive
313 (list
314 (completing-read "Library name: "
6a021917
SM
315 (apply-partially 'locate-file-completion-table
316 (or find-function-source-path load-path)
317 (find-library-suffixes)))))
228b7396
GM
318 (let ((str (lm-commentary (find-library-name file))))
319 (or str (error "Can't find any Commentary section"))
63fe0a98
RS
320 ;; This used to use *Finder* but that would clobber the
321 ;; directory of categories.
322 (pop-to-buffer "*Finder-package*")
228b7396
GM
323 (setq buffer-read-only nil
324 buffer-undo-list t)
101dad14
ER
325 (erase-buffer)
326 (insert str)
327 (goto-char (point-min))
328 (delete-blank-lines)
329 (goto-char (point-max))
330 (delete-blank-lines)
331 (goto-char (point-min))
332 (while (re-search-forward "^;+ ?" nil t)
333 (replace-match "" nil nil))
334 (goto-char (point-min))
54e05660
GM
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))
101dad14
ER
342 (setq buffer-read-only t)
343 (set-buffer-modified-p nil)
344 (shrink-window-if-larger-than-buffer)
8f713f53 345 (finder-mode)
e6fb4d11 346 (finder-summary)))
101dad14
ER
347
348(defun finder-current-item ()
62832105
EZ
349 (let ((key (save-excursion
350 (beginning-of-line)
351 (current-word))))
352 (if (or (and finder-headmark (< (point) finder-headmark))
228b7396 353 (zerop (length key)))
62832105
EZ
354 (error "No keyword or filename on this line")
355 key)))
101dad14
ER
356
357(defun finder-select ()
eb4df0c3 358 "Select item on current line in a finder buffer."
101dad14
ER
359 (interactive)
360 (let ((key (finder-current-item)))
f3b330d6
KH
361 (if (string-match "\\.el$" key)
362 (finder-commentary key)
363 (finder-list-matches key))))
364
365(defun finder-mouse-select (event)
eb4df0c3 366 "Select item in a finder buffer with the mouse."
f3b330d6 367 (interactive "e")
7fdbcd83 368 (with-current-buffer (window-buffer (posn-window (event-start event)))
eb4df0c3
DL
369 (goto-char (posn-point (event-start event)))
370 (finder-select)))
101dad14 371
706239c7 372;;;###autoload
1164bae9
ER
373(defun finder-by-keyword ()
374 "Find packages matching a given keyword."
375 (interactive)
101dad14
ER
376 (finder-list-keywords))
377
228b7396 378(define-derived-mode finder-mode nil "Finder"
101dad14 379 "Major mode for browsing package documentation.
527da106 380\\<finder-mode-map>
101dad14 381\\[finder-select] more help for the item on the current line
2d212d87 382\\[finder-exit] exit Finder mode and kill the Finder buffer."
228b7396 383 :syntax-table finder-mode-syntax-table
89e2d476
SM
384 (setq font-lock-defaults '(finder-font-lock-keywords nil nil
385 (("+-*/.<>=!?$%_&~^:@" . "w")) nil))
228b7396 386 (set (make-local-variable 'finder-headmark) nil))
101dad14
ER
387
388(defun finder-summary ()
389 "Summarize basic Finder commands."
390 (interactive)
90f061c7 391 (message "%s"
527da106 392 (substitute-command-keys
eb4df0c3
DL
393 "\\<finder-mode-map>\\[finder-select] = select, \
394\\[finder-mouse-select] = select, \\[finder-list-keywords] = to \
395finder directory, \\[finder-exit] = quit, \\[finder-summary] = help")))
f5a16823 396
101dad14 397(defun finder-exit ()
89e2d476 398 "Exit Finder mode.
228b7396 399Delete the window and kill all Finder-related buffers."
101dad14 400 (interactive)
228b7396
GM
401 (ignore-errors (delete-window))
402 (dolist (buff '("*Finder*" "*Finder-package*" "*Finder Category*"))
403 (and (get-buffer buff) (kill-buffer buff))))
1164bae9 404
6a7ceddc 405\f
1164bae9
ER
406(provide 'finder)
407
bf8aa901 408;; arch-tag: ec85ff49-8cb8-41f5-a63f-9131d53ce2c5
1164bae9 409;;; finder.el ends here