Mercurial > hg > xemacs-beta
comparison lisp/utils/finder.el @ 0:376386a54a3c r19-14
Import from CVS: tag r19-14
author | cvs |
---|---|
date | Mon, 13 Aug 2007 08:45:50 +0200 |
parents | |
children | ac2d302a0011 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:376386a54a3c |
---|---|
1 ;;; finder.el --- topic & keyword-based code finder | |
2 | |
3 ;; Copyright (C) 1992 Free Software Foundation, Inc. | |
4 | |
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com> | |
6 ;; Created: 16 Jun 1992 | |
7 ;; Version: 1.0 | |
8 ;; Keywords: help | |
9 ;; X-Modified-by: Bob Weiner <weiner@mot.com>, 4/18/95, to include Lisp | |
10 ;; library directory names in finder-program-info, for fast display of | |
11 ;; Lisp libraries and associated commentaries. Added {v}, finder-view, | |
12 ;; and {e}, finder-edit commands for displaying libraries. | |
13 ;; | |
14 ;; Added user variable, 'finder-abbreviate-directory-list', used to | |
15 ;; abbreviate directories before they are saved to finder-program-info. | |
16 ;; Such relative directories can be portable from one Emacs installation | |
17 ;; to another. Default value is based upon the value of Emacs' | |
18 ;; data-directory variable. | |
19 | |
20 ;; This file is part of XEmacs. | |
21 | |
22 ;; XEmacs is free software; you can redistribute it and/or modify it | |
23 ;; under the terms of the GNU General Public License as published by | |
24 ;; the Free Software Foundation; either version 2, or (at your option) | |
25 ;; any later version. | |
26 | |
27 ;; XEmacs is distributed in the hope that it will be useful, but | |
28 ;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
29 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
30 ;; General Public License for more details. | |
31 | |
32 ;; You should have received a copy of the GNU General Public License | |
33 ;; along with XEmacs; see the file COPYING. If not, write to the Free | |
34 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
35 | |
36 ;;; Synched up with: FSF 19.30. | |
37 | |
38 ;;; Commentary: | |
39 | |
40 ;; This mode uses the Keywords library header to provide code-finding | |
41 ;; services by keyword. | |
42 ;; | |
43 ;; Things to do: | |
44 ;; 1. Support multiple keywords per search. This could be extremely hairy; | |
45 ;; there doesn't seem to be any way to get completing-read to exit on | |
46 ;; an EOL with no substring pending, which is what we'd want to end the loop. | |
47 ;; 2. Search by string in synopsis line? | |
48 ;; 3. Function to check finder-package-info for unknown keywords. | |
49 | |
50 ;;; Code: | |
51 | |
52 (require 'lisp-mnt) | |
53 (require 'finder-inf) | |
54 (require 'picture) | |
55 (require 'mode-motion) | |
56 | |
57 (defvar finder-emacs-root-directory | |
58 (file-name-directory (directory-file-name data-directory)) | |
59 "Root directory of current emacs tree.") | |
60 | |
61 (defvar finder-abbreviate-directory-list | |
62 (list finder-emacs-root-directory) | |
63 "*List of directory roots to remove from finder-package-info directory entries. | |
64 The first element in the list is used when expanding relative package | |
65 directories to view or extract information from package source code.") | |
66 | |
67 (defvar finder-file-regexp "\\.el$" | |
68 "Regexp which matches file names but not Emacs Lisp finder keywords.") | |
69 | |
70 ;; Local variable in finder buffer. | |
71 (defvar finder-headmark) | |
72 | |
73 (defvar finder-known-keywords | |
74 '( | |
75 (abbrev . "abbreviation handling, typing shortcuts, macros") | |
76 (bib . "code related to the `bib' bibliography processor") | |
77 (c . "C, C++, and Objective-C language support") | |
78 (calendar . "calendar and time management support") | |
79 (comm . "communications, networking, remote access to files") | |
80 (data . "support editing files of data") | |
81 (docs . "support for Emacs documentation") | |
82 (emulations . "emulations of other editors") | |
83 (extensions . "Emacs Lisp language extensions") | |
84 (faces . "support for multiple fonts") | |
85 (frames . "support for Emacs frames and windows") | |
86 (games . "games, jokes and amusements") | |
87 (hardware . "support for interfacing with exotic hardware") | |
88 (help . "support for on-line help systems") | |
89 (hypermedia . "support for links between text or other media types") | |
90 (i18n . "internationalization and alternate character-set support") | |
91 (internal . "code for Emacs internals, build process, defaults") | |
92 (languages . "specialized modes for editing programming languages") | |
93 (lisp . "Lisp support, including Emacs Lisp") | |
94 (local . "code local to your site") | |
95 (maint . "maintenance aids for the Emacs development group") | |
96 (mail . "modes for electronic-mail handling") | |
97 (matching . "various sorts of searching and matching") | |
98 (mouse . "mouse support") | |
99 (news . "support for netnews reading and posting") | |
100 (oop . "support for object-oriented programming") | |
101 (outlines . "support for hierarchical outlining") | |
102 (processes . "process, subshell, compilation, and job control support") | |
103 (terminals . "support for terminal types") | |
104 (tex . "code related to the TeX formatter") | |
105 (tools . "programming tools") | |
106 (unix . "front-ends/assistants for, or emulators of, UNIX features") | |
107 (vms . "support code for vms") | |
108 (wp . "word processing") | |
109 )) | |
110 | |
111 (defvar finder-mode-map nil) | |
112 (or finder-mode-map | |
113 (let ((map (make-sparse-keymap))) | |
114 (define-key map " " 'finder-select) | |
115 (define-key map "f" 'finder-select) | |
116 (define-key map "\C-m" 'finder-select) | |
117 (define-key map "e" 'finder-edit) | |
118 (define-key map "v" 'finder-view) | |
119 (define-key map "?" 'finder-summary) | |
120 (define-key map "q" 'finder-exit) | |
121 (define-key map "d" 'finder-list-keywords) | |
122 (define-key map [button2] 'finder-mouse-select) | |
123 (setq finder-mode-map map))) | |
124 | |
125 | |
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 | |
131 (defvar finder-compile-keywords-quiet nil | |
132 "If non-nil finder-compile-keywords will not print any messages.") | |
133 | |
134 (defun finder-compile-keywords (&rest dirs) | |
135 "Regenerate the keywords association list into the file `finder-inf.el'. | |
136 Optional arguments are a list of Emacs Lisp directories to compile from; no | |
137 arguments compiles from `load-path'." | |
138 (save-excursion | |
139 (find-file "finder-inf.el") | |
140 (let ((processed nil) | |
141 (directory-abbrev-alist | |
142 (append | |
143 (mapcar (function (lambda (dir) (cons dir ""))) | |
144 finder-abbreviate-directory-list) | |
145 directory-abbrev-alist)) | |
146 (using-load-path)) | |
147 (or dirs (setq dirs load-path)) | |
148 (setq using-load-path (equal dirs load-path)) | |
149 (erase-buffer) | |
150 (insert ";;; finder-inf.el --- keyword-to-package mapping\n") | |
151 (insert ";; Keywords: help\n") | |
152 (insert ";;; Commentary:\n") | |
153 (insert ";; Don't edit this file. It's generated by finder.el\n\n") | |
154 (insert ";;; Code:\n") | |
155 (insert "\n(setq finder-package-info '(\n") | |
156 (mapcar | |
157 (function | |
158 (lambda (d) | |
159 (mapcar | |
160 (function | |
161 (lambda (f) | |
162 (if (not (member f processed)) | |
163 (let (summary keystart keywords) | |
164 (setq processed (cons f processed)) | |
165 (if (not finder-compile-keywords-quiet) | |
166 (message "Processing %s ..." f)) | |
167 (save-excursion | |
168 (set-buffer (get-buffer-create "*finder-scratch*")) | |
169 (buffer-disable-undo (current-buffer)) | |
170 (erase-buffer) | |
171 (insert-file-contents (expand-file-name f d)) | |
172 (setq summary (lm-synopsis) | |
173 keywords (lm-keywords))) | |
174 (if (not summary) | |
175 nil | |
176 (insert (format " (\"%s\"\n " f)) | |
177 (prin1 summary (current-buffer)) | |
178 (insert "\n ") | |
179 (setq keystart (point)) | |
180 (insert (if keywords (format "(%s)" keywords) "nil")) | |
181 (subst-char-in-region keystart (point) ?, ? ) | |
182 (insert "\n ") | |
183 (prin1 (abbreviate-file-name d) (current-buffer)) | |
184 (insert ")\n")))))) | |
185 ;; | |
186 ;; Skip null, non-existent or relative pathnames, e.g. "./", if | |
187 ;; using load-path, so that they do not interfere with a scan of | |
188 ;; library directories only. | |
189 (if (and using-load-path | |
190 (not (and d (file-name-absolute-p d) (file-exists-p d)))) | |
191 nil | |
192 (setq d (file-name-as-directory (or d "."))) | |
193 (directory-files d nil "^[^=].*\\.el$"))))) | |
194 dirs) | |
195 (insert "))\n\n(provide 'finder-inf)\n\n;;; finder-inf.el ends here\n") | |
196 (kill-buffer "*finder-scratch*") | |
197 (eval-current-buffer) ;; So we get the new keyword list immediately | |
198 (basic-save-buffer)))) | |
199 | |
200 ;;; Now the retrieval code | |
201 | |
202 (defun finder-list-keywords () | |
203 "Display descriptions of the keywords in the Finder buffer." | |
204 (interactive) | |
205 (setq buffer-read-only nil) | |
206 (erase-buffer) | |
207 (mapcar | |
208 (function (lambda (assoc) | |
209 (let ((keyword (car assoc))) | |
210 (insert (symbol-name keyword)) | |
211 (insert-at-column 14 (concat (cdr assoc) "\n")) | |
212 (cons (symbol-name keyword) keyword)))) | |
213 finder-known-keywords) | |
214 (goto-char (point-min)) | |
215 (setq finder-headmark (point)) | |
216 (setq buffer-read-only t) | |
217 (set-buffer-modified-p nil) | |
218 (if (not (one-window-p)) | |
219 (balance-windows)) | |
220 (finder-summary)) | |
221 | |
222 (defun finder-list-matches (key) | |
223 (setq buffer-read-only nil) | |
224 (erase-buffer) | |
225 (let ((id (intern key))) | |
226 (insert | |
227 "The following packages match the keyword `" key "':\n\n") | |
228 (setq finder-headmark (point)) | |
229 (mapcar | |
230 (function (lambda (x) | |
231 (if (memq id (car (cdr (cdr x)))) | |
232 (progn | |
233 (insert (car x)) | |
234 (insert-at-column 16 | |
235 (concat (car (cdr x)) "\n")) | |
236 )) | |
237 )) | |
238 finder-package-info) | |
239 (goto-char (point-min)) | |
240 (forward-line) | |
241 (setq buffer-read-only t) | |
242 (set-buffer-modified-p nil) | |
243 (shrink-window-if-larger-than-buffer) | |
244 (finder-summary))) | |
245 | |
246 ;; Return full pathname for FILE from finder-package-info | |
247 ;; or by searching for library in load-path. | |
248 (defun finder-find-library (file) | |
249 (let ((dir (nth 3 (assoc file finder-package-info))) | |
250 (path)) | |
251 (if dir | |
252 (setq path (expand-file-name | |
253 file | |
254 ;; Dir may be relative, in which case, we first expand it | |
255 ;; relative to the first element of | |
256 ;; finder-abbreviate-directory-list or to the local emacs | |
257 ;; root directory. | |
258 (expand-file-name | |
259 dir (or (car finder-abbreviate-directory-list) | |
260 finder-emacs-root-directory))))) | |
261 (if (and path (file-exists-p path)) | |
262 path | |
263 (finder-find-library-in-load-path file)))) | |
264 | |
265 ;; Search for a file named FILE the same way `load' would search. | |
266 (defun finder-find-library-in-load-path (file) | |
267 (if (file-name-absolute-p file) | |
268 file | |
269 (let ((dirs load-path) | |
270 found) | |
271 (while (and dirs (not found)) | |
272 (if (file-exists-p (expand-file-name (concat file ".el") (car dirs))) | |
273 (setq found (expand-file-name file (car dirs))) | |
274 (if (file-exists-p (expand-file-name file (car dirs))) | |
275 (setq found (expand-file-name file (car dirs))))) | |
276 (setq dirs (cdr dirs))) | |
277 found))) | |
278 | |
279 (defun finder-commentary (file) | |
280 (interactive) | |
281 (let* ((str (lm-commentary (finder-find-library file)))) | |
282 (if (null str) | |
283 (error "Can't find any Commentary section")) | |
284 (pop-to-buffer "*Finder*") | |
285 (setq buffer-read-only nil | |
286 mode-motion-hook 'mode-motion-highlight-line) | |
287 (erase-buffer) | |
288 (insert str) | |
289 (goto-char (point-min)) | |
290 (delete-blank-lines) | |
291 (goto-char (point-max)) | |
292 (delete-blank-lines) | |
293 (goto-char (point-min)) | |
294 (while (re-search-forward "^;+ ?" nil t) | |
295 (replace-match "" nil nil)) | |
296 (goto-char (point-min)) | |
297 (setq buffer-read-only t) | |
298 (set-buffer-modified-p nil) | |
299 (shrink-window-if-larger-than-buffer) | |
300 (finder-summary) | |
301 )) | |
302 | |
303 (defun finder-current-item () | |
304 (if (and finder-headmark (< (point) finder-headmark)) | |
305 (error "No keyword or filename on this line") | |
306 (save-excursion | |
307 (beginning-of-line) | |
308 (current-word)))) | |
309 | |
310 (defun finder-edit () | |
311 (interactive) | |
312 (let ((entry (finder-current-item))) | |
313 (if (string-match finder-file-regexp entry) | |
314 (let ((path (finder-find-library entry))) | |
315 (if path | |
316 (find-file-other-window path) | |
317 (error "Can't find Emacs Lisp library: '%s'" entry))) | |
318 ;; a finder keyword | |
319 (error "Finder-edit works on Emacs Lisp libraries only")))) | |
320 | |
321 (defun finder-view () | |
322 (interactive) | |
323 (let ((entry (finder-current-item))) | |
324 (if (string-match finder-file-regexp entry) | |
325 (let ((path (finder-find-library entry))) | |
326 (if path | |
327 (view-file-other-window path) | |
328 (error "Can't find Emacs Lisp library: '%s'" entry))) | |
329 ;; a finder keyword | |
330 (error "Finder-view works on Emacs Lisp libraries only")))) | |
331 | |
332 (defun finder-select () | |
333 (interactive) | |
334 (let ((key (finder-current-item))) | |
335 (if (string-match finder-file-regexp key) | |
336 (finder-commentary key) | |
337 (finder-list-matches key)))) | |
338 | |
339 (defun finder-mouse-select (ev) | |
340 (interactive "e") | |
341 (goto-char (event-point ev)) | |
342 (finder-select)) | |
343 | |
344 (defun finder-by-keyword () | |
345 "Find packages matching a given keyword." | |
346 (interactive) | |
347 (finder-mode) | |
348 (finder-list-keywords)) | |
349 | |
350 (defun finder-mode () | |
351 "Major mode for browsing package documentation. | |
352 \\<finder-mode-map> | |
353 \\[finder-select] more help for the item on the current line | |
354 \\[finder-edit] edit Lisp library in another window | |
355 \\[finder-view] view Lisp library in another window | |
356 \\[finder-exit] exit Finder mode and kill the Finder buffer. | |
357 " | |
358 (interactive) | |
359 (pop-to-buffer "*Finder*") | |
360 (setq buffer-read-only nil | |
361 mode-motion-hook 'mode-motion-highlight-line) | |
362 (erase-buffer) | |
363 (use-local-map finder-mode-map) | |
364 (set-syntax-table emacs-lisp-mode-syntax-table) | |
365 (setq mode-name "Finder") | |
366 (setq major-mode 'finder-mode) | |
367 (make-local-variable 'finder-headmark) | |
368 (setq finder-headmark nil) | |
369 ) | |
370 | |
371 (defun finder-summary () | |
372 "Summarize basic Finder commands." | |
373 (interactive) | |
374 (message | |
375 (substitute-command-keys | |
376 "\\<finder-mode-map>\\[finder-select] = select, \\[finder-list-keywords] = keywords, \\[finder-edit] = edit, \\[finder-view] = view, \\[finder-exit] = quit, \\[finder-summary] = help"))) | |
377 | |
378 (defun finder-exit () | |
379 "Exit Finder mode and kill the buffer" | |
380 (interactive) | |
381 (or (one-window-p t 0) | |
382 (delete-window)) | |
383 (kill-buffer "*Finder*")) | |
384 | |
385 (provide 'finder) | |
386 | |
387 ;;; finder.el ends here |