(mh-make-local-vars):
[bpt/emacs.git] / lisp / finder.el
CommitLineData
1164bae9
ER
1;;; finder.el --- topic & keyword-based code finder
2
2d212d87 3;; Copyright (C) 1992, 1997, 1998, 1999 Free Software Foundation, Inc.
1164bae9
ER
4
5;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
6;; Created: 16 Jun 1992
7;; Version: 1.0
8;; Keywords: help
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
7c938215 14;; the Free Software Foundation; either version 2, or (at your option)
1164bae9
ER
15;; any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
b578f267
EN
23;; along with GNU Emacs; see the file COPYING. If not, write to the
24;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25;; Boston, MA 02111-1307, USA.
1164bae9 26
101dad14 27;;; Commentary:
1164bae9
ER
28
29;; This mode uses the Keywords library header to provide code-finding
30;; services by keyword.
31;;
32;; Things to do:
33;; 1. Support multiple keywords per search. This could be extremely hairy;
34;; there doesn't seem to be any way to get completing-read to exit on
35;; an EOL with no substring pending, which is what we'd want to end the loop.
36;; 2. Search by string in synopsis line?
37;; 3. Function to check finder-package-info for unknown keywords.
38
39;;; Code:
40
41(require 'lisp-mnt)
42(require 'finder-inf)
1164bae9 43
14931973
RS
44;; Local variable in finder buffer.
45(defvar finder-headmark)
46
4f451c3f
DL
47;; These are supposed to correspond to top-level customization groups,
48;; says rms.
1164bae9
ER
49(defvar finder-known-keywords
50 '(
dea24479 51 (abbrev . "abbreviation handling, typing shortcuts, macros")
be2c35e4 52 (bib . "code related to the `bib' bibliography processor")
68f8f37f 53 (c . "support for the C language and related languages")
1164bae9 54 (calendar . "calendar and time management support")
e9571d2a 55 (comm . "communications, networking, remote access to files")
35e9c644 56 (convenience . "convenience features for faster editing")
be2c35e4 57 (data . "support editing files of data")
1164bae9
ER
58 (docs . "support for Emacs documentation")
59 (emulations . "emulations of other editors")
60 (extensions . "Emacs Lisp language extensions")
be2c35e4 61 (faces . "support for multiple fonts")
f1aabce0 62 (frames . "support for Emacs frames and window systems")
1164bae9
ER
63 (games . "games, jokes and amusements")
64 (hardware . "support for interfacing with exotic hardware")
65 (help . "support for on-line help systems")
f1aabce0 66 (hypermedia . "support for links between text or other media types")
14931973 67 (i18n . "internationalization and alternate character-set support")
1164bae9
ER
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")
be2c35e4
RS
74 (matching . "various sorts of searching and matching")
75 (mouse . "mouse support")
1226dd0b 76 (multimedia . "images and sound support")
1164bae9 77 (news . "support for netnews reading and posting")
f1aabce0
RS
78 (oop . "support for object-oriented programming")
79 (outlines . "support for hierarchical outlining")
1164bae9
ER
80 (processes . "process, subshell, compilation, and job control support")
81 (terminals . "support for terminal types")
82 (tex . "code related to the TeX formatter")
83 (tools . "programming tools")
84 (unix . "front-ends/assistants for, or emulators of, UNIX features")
85 (vms . "support code for vms")
86 (wp . "word processing")
87 ))
88
101dad14 89(defvar finder-mode-map nil)
e141acb3
RS
90(or finder-mode-map
91 (let ((map (make-sparse-keymap)))
92 (define-key map " " 'finder-select)
93 (define-key map "f" 'finder-select)
f3b330d6 94 (define-key map [mouse-2] 'finder-mouse-select)
e141acb3
RS
95 (define-key map "\C-m" 'finder-select)
96 (define-key map "?" 'finder-summary)
97 (define-key map "q" 'finder-exit)
98 (define-key map "d" 'finder-list-keywords)
99 (setq finder-mode-map map)))
100
101dad14 101
1164bae9
ER
102;;; Code for regenerating the keyword list.
103
104(defvar finder-package-info nil
105 "Assoc list mapping file names to description & keyword lists.")
106
107(defun finder-compile-keywords (&rest dirs)
14931973 108 "Regenerate the keywords association list into the file `finder-inf.el'.
2d212d87
DL
109Optional arguments DIRS are a list of Emacs Lisp directories to compile from;
110no arguments compiles from `load-path'."
1164bae9 111 (save-excursion
c5d87135
ER
112 (let ((processed nil))
113 (find-file "finder-inf.el")
114 (erase-buffer)
e9571d2a
ER
115 (insert ";;; finder-inf.el --- keyword-to-package mapping\n")
116 (insert ";; Keywords: help\n")
0cbe967f
ER
117 (insert ";;; Commentary:\n")
118 (insert ";; Don't edit this file. It's generated by finder.el\n\n")
119 (insert ";;; Code:\n")
c5d87135
ER
120 (insert "\n(setq finder-package-info '(\n")
121 (mapcar
e6fb4d11 122 (lambda (d)
5b64ebe8
RS
123 (when (file-exists-p (directory-file-name d))
124 (message "Directory %s" d)
125 (mapcar
2d212d87 126 (lambda (f)
5b64ebe8
RS
127 (if (and (or (string-match "^[^=].*\\.el$" f)
128 ;; Allow compressed files also. Fixme:
129 ;; generalize this, especially for
130 ;; MS-DOG-type filenames.
131 (and (string-match "^[^=].*\\.el\\.\\(gz\\|Z\\)$" f)
132 (require 'jka-compr)))
133 ;; Ignore lock files.
134 (not (string-match "^.#" f))
135 (not (member f processed)))
136 (let (summary keystart keywords)
137 (setq processed (cons f processed))
138 (save-excursion
139 (set-buffer (get-buffer-create "*finder-scratch*"))
140 (buffer-disable-undo (current-buffer))
141 (erase-buffer)
142 (insert-file-contents
143 (concat (file-name-as-directory (or d ".")) f))
144 (setq summary (lm-synopsis))
145 (setq keywords (lm-keywords)))
146 (insert
147 (format " (\"%s\"\n "
148 (if (string-match "\\.\\(gz\\|Z\\)$" f)
2d212d87 149 (file-name-sans-extension f)
5b64ebe8
RS
150 f)))
151 (prin1 summary (current-buffer))
152 (insert
153 "\n ")
154 (setq keystart (point))
155 (insert
156 (if keywords (format "(%s)" keywords) "nil")
157 ")\n")
158 (subst-char-in-region keystart (point) ?, ? )
159 )))
160 (directory-files (or d ".")))))
c5d87135 161 (or dirs load-path))
0cbe967f 162 (insert "))\n\n(provide 'finder-inf)\n\n;;; finder-inf.el ends here\n")
c5d87135 163 (kill-buffer "*finder-scratch*")
3688bf22 164 (eval-current-buffer) ;; So we get the new keyword list immediately
e6fb4d11 165 (basic-save-buffer))))
1164bae9 166
1c6425ea
RS
167(defun finder-compile-keywords-make-dist ()
168 "Regenerate `finder-inf.el' for the Emacs distribution."
644f58eb
RS
169 (apply 'finder-compile-keywords command-line-args-left)
170 (kill-emacs))
1c6425ea 171
1164bae9
ER
172;;; Now the retrieval code
173
e6fb4d11 174(defun finder-insert-at-column (column &rest strings)
eb4df0c3 175 "Insert, at column COLUMN, other args STRINGS."
e6fb4d11 176 (if (> (current-column) column) (insert "\n"))
eb4df0c3 177 (move-to-column column t)
e6fb4d11
EN
178 (apply 'insert strings))
179
eb4df0c3
DL
180(defun finder-mouse-face-on-line ()
181 "Put a `mouse-face' property on the previous line."
182 (save-excursion
183 (previous-line 1)
184 (put-text-property (save-excursion (beginning-of-line) (point))
185 (progn (end-of-line) (point))
186 'mouse-face 'highlight)))
187
706239c7 188;;;###autoload
101dad14
ER
189(defun finder-list-keywords ()
190 "Display descriptions of the keywords in the Finder buffer."
191 (interactive)
5acc847d
RS
192 (if (get-buffer "*Finder*")
193 (pop-to-buffer "*Finder*")
b3241066 194 (pop-to-buffer (set-buffer (get-buffer-create "*Finder*")))
5acc847d
RS
195 (finder-mode)
196 (setq buffer-read-only nil)
197 (erase-buffer)
198 (mapcar
199 (lambda (assoc)
200 (let ((keyword (car assoc)))
201 (insert (symbol-name keyword))
202 (finder-insert-at-column 14 (concat (cdr assoc) "\n"))
eb4df0c3 203 (finder-mouse-face-on-line)))
5acc847d
RS
204 finder-known-keywords)
205 (goto-char (point-min))
206 (setq finder-headmark (point))
207 (setq buffer-read-only t)
208 (set-buffer-modified-p nil)
209 (balance-windows)
210 (finder-summary)))
101dad14
ER
211
212(defun finder-list-matches (key)
2d7b0db7 213 (pop-to-buffer (set-buffer (get-buffer-create "*Finder Category*")))
5acc847d 214 (finder-mode)
101dad14
ER
215 (setq buffer-read-only nil)
216 (erase-buffer)
217 (let ((id (intern key)))
218 (insert
219 "The following packages match the keyword `" key "':\n\n")
14931973 220 (setq finder-headmark (point))
101dad14 221 (mapcar
e6fb4d11
EN
222 (lambda (x)
223 (if (memq id (car (cdr (cdr x))))
224 (progn
225 (insert (car x))
eb4df0c3
DL
226 (finder-insert-at-column 16 (concat (nth 1 x) "\n"))
227 (finder-mouse-face-on-line))))
101dad14
ER
228 finder-package-info)
229 (goto-char (point-min))
230 (forward-line)
231 (setq buffer-read-only t)
232 (set-buffer-modified-p nil)
233 (shrink-window-if-larger-than-buffer)
234 (finder-summary)))
235
35863d77 236(defun finder-find-library (library)
2d212d87
DL
237 "Search for file LIBRARY on `load-path'.
238Try compressed versions if jka-compr is in use."
35863d77
KH
239 (or (locate-library library t)
240 (if (rassq 'jka-compr-handler file-name-handler-alist)
241 (or (locate-library (concat library ".gz") t)
242 (locate-library (concat library ".Z") t)
243 ;; last resort for MS-DOG et al
244 (locate-library (concat library "z"))))))
245
706239c7 246;;;###autoload
101dad14 247(defun finder-commentary (file)
2d212d87
DL
248 "Display FILE's commentary section.
249FILE should be in a form suitable for passing to `locate-library'."
250 (interactive "sLibrary name: ")
251 (let* ((str (lm-commentary (or (finder-find-library file)
252 (finder-find-library (concat file ".el"))
253 (error "Can't find library %s" file)))))
101dad14 254 (if (null str)
67a69ba6 255 (error "Can't find any Commentary section"))
101dad14
ER
256 (pop-to-buffer "*Finder*")
257 (setq buffer-read-only nil)
258 (erase-buffer)
259 (insert str)
260 (goto-char (point-min))
261 (delete-blank-lines)
262 (goto-char (point-max))
263 (delete-blank-lines)
264 (goto-char (point-min))
265 (while (re-search-forward "^;+ ?" nil t)
266 (replace-match "" nil nil))
267 (goto-char (point-min))
268 (setq buffer-read-only t)
269 (set-buffer-modified-p nil)
270 (shrink-window-if-larger-than-buffer)
8f713f53 271 (finder-mode)
e6fb4d11 272 (finder-summary)))
101dad14
ER
273
274(defun finder-current-item ()
14931973 275 (if (and finder-headmark (< (point) finder-headmark))
101dad14
ER
276 (error "No keyword or filename on this line")
277 (save-excursion
278 (beginning-of-line)
279 (current-word))))
280
281(defun finder-select ()
eb4df0c3 282 "Select item on current line in a finder buffer."
101dad14
ER
283 (interactive)
284 (let ((key (finder-current-item)))
f3b330d6
KH
285 (if (string-match "\\.el$" key)
286 (finder-commentary key)
287 (finder-list-matches key))))
288
289(defun finder-mouse-select (event)
eb4df0c3 290 "Select item in a finder buffer with the mouse."
f3b330d6
KH
291 (interactive "e")
292 (save-excursion
eb4df0c3
DL
293 (set-buffer (window-buffer (posn-window (event-start event))))
294 (goto-char (posn-point (event-start event)))
295 (finder-select)))
101dad14 296
706239c7 297;;;###autoload
1164bae9
ER
298(defun finder-by-keyword ()
299 "Find packages matching a given keyword."
300 (interactive)
101dad14
ER
301 (finder-list-keywords))
302
303(defun finder-mode ()
304 "Major mode for browsing package documentation.
527da106 305\\<finder-mode-map>
101dad14 306\\[finder-select] more help for the item on the current line
2d212d87 307\\[finder-exit] exit Finder mode and kill the Finder buffer."
101dad14 308 (interactive)
101dad14
ER
309 (use-local-map finder-mode-map)
310 (set-syntax-table emacs-lisp-mode-syntax-table)
311 (setq mode-name "Finder")
312 (setq major-mode 'finder-mode)
14931973 313 (make-local-variable 'finder-headmark)
e6fb4d11 314 (setq finder-headmark nil))
101dad14
ER
315
316(defun finder-summary ()
317 "Summarize basic Finder commands."
318 (interactive)
90f061c7 319 (message "%s"
527da106 320 (substitute-command-keys
eb4df0c3
DL
321 "\\<finder-mode-map>\\[finder-select] = select, \
322\\[finder-mouse-select] = select, \\[finder-list-keywords] = to \
323finder directory, \\[finder-exit] = quit, \\[finder-summary] = help")))
f5a16823 324
101dad14 325(defun finder-exit ()
35863d77 326 "Exit Finder mode and kill the buffer."
101dad14 327 (interactive)
b26f4fbd
KH
328 (or (one-window-p t)
329 (delete-window))
f3b330d6 330 ;; Can happen in either buffer -- kill each of the two that exists
b26f4fbd
KH
331 (and (get-buffer "*Finder*")
332 (kill-buffer "*Finder*"))
333 (and (get-buffer "*Finder Category*")
334 (kill-buffer "*Finder Category*")))
1164bae9
ER
335
336(provide 'finder)
337
338;;; finder.el ends here