1 ;;; finder.el --- topic & keyword-based code finder
3 ;; Copyright (C) 1992 Free Software Foundation, Inc.
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
6 ;; Created: 16 Jun 1992
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.
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.
20 ;; This file is part of XEmacs.
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)
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.
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, Inc., 59 Temple Place - Suite 330, Boston, MA
37 ;;; Synched up with: FSF 19.34.
41 ;; This mode uses the Keywords library header to provide code-finding
42 ;; services by keyword.
45 ;; 1. Support multiple keywords per search. This could be extremely hairy;
46 ;; there doesn't seem to be any way to get completing-read to exit on
47 ;; an EOL with no substring pending, which is what we'd want to end the loop.
48 ;; 2. Search by string in synopsis line?
49 ;; 3. Function to check finder-package-info for unknown keywords.
59 (require 'mode-motion)
61 (defvar finder-emacs-root-directory
62 (file-name-directory (directory-file-name data-directory))
63 "Root directory of current emacs tree.")
65 (defvar finder-abbreviate-directory-list
66 (list finder-emacs-root-directory)
67 "*List of directory roots to remove from finder-package-info directory entries.
68 The first element in the list is used when expanding relative package
69 directories to view or extract information from package source code.")
71 (defvar finder-file-regexp "\\.el$"
72 "Regexp which matches file names but not Emacs Lisp finder keywords.")
74 ;; Local variable in finder buffer.
75 (defvar finder-headmark)
77 (defvar finder-known-keywords
79 (abbrev . "abbreviation handling, typing shortcuts, macros")
80 (bib . "code related to the `bib' bibliography processor")
81 (build . "code used to build XEmacs")
82 (c . "C, C++, and Objective-C language support")
83 (calendar . "calendar and time management support")
84 (comm . "communications, networking, remote access to files")
85 (content . "contains content (menu/dialog box descs, text, images, &c)")
86 (data . "support for editing files of data")
87 (docs . "support for XEmacs documentation")
88 (dumped . "files preloaded into XEmacs")
89 (emulations . "emulations of other editors")
90 (extensions . "Emacs Lisp language extensions")
91 (faces . "support for multiple fonts")
92 (frames . "support for XEmacs frames and window systems")
93 (games . "games, jokes and amusements")
94 (gui . "support for menubars, dialog boxes, and other GUI features")
95 (hardware . "support for interfacing with exotic hardware")
96 (help . "support for on-line help systems")
97 (hypermedia . "support for links between text or other media types")
98 (i18n . "internationalization and alternate character-set support")
99 (internal . "code implementing core functionality in XEmacs")
100 (languages . "specialized modes for editing programming languages")
101 (lisp . "Lisp support, including Emacs Lisp")
102 (local . "code local to your site")
103 (mail . "modes for electronic-mail handling")
104 (maint . "maintenance aids for the Emacs development group")
105 (matching . "various sorts of searching and matching")
106 (mouse . "mouse support")
107 (mswin . "support for anything running on MS Windows")
108 ,(when (featurep 'mule)
109 (cons 'mule "multi-language extensions"))
110 (news . "support for netnews reading and posting")
111 (oop . "support for object-oriented programming")
112 (outlines . "support for hierarchical outlining")
113 (processes . "process, subshell, compilation, and job control support")
114 (services . "provides services for use by other programs (cf `user')")
115 (terminals . "support for terminal types")
116 (tex . "code related to the TeX formatter")
117 (tools . "programming tools")
118 (unix . "front-ends/assistants for, or emulators of, UNIX features")
119 (user . "program interacts directly with the user (cf `services'")
120 (vms . "support code for vms")
121 (wp . "word processing")
122 (www . "support for the Web (WWW, the World Wide Web)")
125 (defvar finder-mode-map nil)
127 (let ((map (make-sparse-keymap)))
128 (define-key map " " 'finder-select)
129 (define-key map "f" 'finder-select)
130 (define-key map "\C-m" 'finder-select)
132 (define-key map "e" 'finder-edit)
133 (define-key map "v" 'finder-view)
134 (define-key map "?" 'finder-summary)
135 (define-key map "q" 'finder-exit)
136 (define-key map "d" 'finder-list-keywords)
138 (define-key map [button2] 'finder-mouse-select)
139 (setq finder-mode-map map)))
142 ;;; Code for regenerating the keyword list.
144 (defvar finder-package-info nil
145 "Assoc list mapping file names to description & keyword lists.")
147 (defvar finder-compile-keywords-quiet nil
148 "If non-nil finder-compile-keywords will not print any messages.")
150 (defun finder-compile-keywords (&rest dirs)
151 "Regenerate the keywords association list into the file `finder-inf.el'.
152 Optional arguments are a list of Emacs Lisp directories to compile from; no
153 arguments compiles from `load-path'."
156 (find-file "finder-inf.el")
157 (let ((processed nil)
158 (directory-abbrev-alist
160 (mapcar (function (lambda (dir)
161 (cons (concat "^" (regexp-quote dir))
163 finder-abbreviate-directory-list)
164 directory-abbrev-alist))
166 (or dirs (setq dirs load-path))
167 (setq using-load-path (equal dirs load-path))
169 (insert ";;; finder-inf.el --- keyword-to-package mapping\n")
170 (insert ";; Keywords: help\n")
171 (insert ";;; Commentary:\n")
172 (insert ";; Don't edit this file. It's generated by finder.el\n\n")
173 (insert ";;; Code:\n")
174 (insert "\n(defconst finder-package-info '(\n")
179 (when (and (not (member f processed)) (file-readable-p f))
180 (let (summary keystart keywords)
181 (setq processed (cons f processed))
182 (if (not finder-compile-keywords-quiet)
183 (message "Processing %s ..." f))
185 (set-buffer (get-buffer-create "*finder-scratch*"))
186 (buffer-disable-undo (current-buffer))
188 (insert-file-contents (expand-file-name f d))
190 (setq summary (lm-synopsis)
191 keywords (lm-keywords))
192 (t (message "finder: error processing %s %S" f err))))
194 (insert (format " (\"%s\"\n " f))
195 (prin1 summary (current-buffer))
197 (setq keystart (point))
198 (insert (if keywords (format "(%s)" keywords) "nil"))
199 (subst-char-in-region keystart (point) ?, ? )
201 (prin1 (abbreviate-file-name d) (current-buffer))
204 ;; Skip null, non-existent or relative pathnames, e.g. "./", if
205 ;; using load-path, so that they do not interfere with a scan of
206 ;; library directories only.
207 (if (and using-load-path
208 (not (and d (file-name-absolute-p d) (file-exists-p d))))
210 (setq d (file-name-as-directory (or d ".")))
211 (directory-files d nil "^[^=].*\\.el$"))))
213 (insert "))\n\n(provide 'finder-inf)\n\n;;; finder-inf.el ends here\n")
214 (kill-buffer "*finder-scratch*")
215 (unless noninteractive
216 (eval-current-buffer)) ; So we get the new keyword list immediately
217 (basic-save-buffer))))
219 (defun finder-compile-keywords-make-dist ()
220 "Regenerate `finder-inf.el' for the Emacs distribution."
221 (finder-compile-keywords default-directory))
223 ;;; Now the retrieval code
225 (defun finder-insert-at-column (column &rest strings)
226 "Insert list of STRINGS, at column COLUMN."
227 (if (>= (current-column) column) (insert "\n"))
228 (move-to-column column)
229 (let ((col (current-column)))
232 (if (and (/= col column)
233 (= (preceding-char) ?\t))
234 (let (indent-tabs-mode)
237 (move-to-column column)))))
238 (apply 'insert strings))
240 (defun finder-list-keywords ()
241 "Display descriptions of the keywords in the Finder buffer."
243 (setq buffer-read-only nil)
247 (let ((keyword (car assoc)))
248 (insert (symbol-name keyword))
249 (finder-insert-at-column 14 (concat (cdr assoc) "\n"))
250 (cons (symbol-name keyword) keyword)))
251 finder-known-keywords)
252 (goto-char (point-min))
253 (setq finder-headmark (point))
254 (setq buffer-read-only t)
255 (set-buffer-modified-p nil)
257 (if (not (one-window-p))
261 (defun finder-list-matches (key)
262 (setq buffer-read-only nil)
264 (let ((id (intern key)))
266 "The following packages match the keyword `" key "':\n\n")
267 (setq finder-headmark (point))
270 (if (memq id (car (cdr (cdr x))))
273 (finder-insert-at-column 16 (concat (car (cdr x)) "\n")))))
275 (goto-char (point-min))
277 (setq buffer-read-only t)
278 (set-buffer-modified-p nil)
279 (shrink-window-if-larger-than-buffer)
282 ;; Search for a file named FILE the same way `load' would search.
283 (defun finder-find-library (file)
284 (if (file-name-absolute-p file)
286 (let ((dirs load-path)
288 (while (and dirs (not found))
289 (if (file-exists-p (expand-file-name (concat file ".el") (car dirs)))
290 (setq found (expand-file-name (concat file ".el") (car dirs)))
291 (if (file-exists-p (expand-file-name file (car dirs)))
292 (setq found (expand-file-name file (car dirs)))))
293 (setq dirs (cdr dirs)))
297 (defun finder-commentary (file)
298 "Display FILE's commentary section.
299 FILE should be in a form suitable for passing to `locate-library'."
300 (interactive "sLibrary name: ")
301 (let* ((str (lm-commentary (or (finder-find-library file)
302 (finder-find-library (concat file ".el"))
303 (error "Can't find library %s" file)))))
305 (error "Can't find any Commentary section"))
306 (pop-to-buffer "*Finder*")
308 (setq buffer-read-only nil
309 mode-motion-hook 'mode-motion-highlight-line)
312 (goto-char (point-min))
314 (goto-char (point-max))
316 (goto-char (point-min))
317 (while (re-search-forward "^;+ ?" nil t)
318 (replace-match "" nil nil))
319 (goto-char (point-min))
320 (setq buffer-read-only t)
321 (set-buffer-modified-p nil)
322 (shrink-window-if-larger-than-buffer)
325 (defun finder-current-item ()
326 (if (and finder-headmark (< (point) finder-headmark))
327 (error "No keyword or filename on this line")
333 (defun finder-edit ()
335 (let ((entry (finder-current-item)))
336 (if (string-match finder-file-regexp entry)
337 (let ((path (finder-find-library entry)))
339 (find-file-other-window path)
340 (error "Can't find Emacs Lisp library: '%s'" entry)))
342 (error "Finder-edit works on Emacs Lisp libraries only"))))
345 (defun finder-view ()
347 (let ((entry (finder-current-item)))
348 (if (string-match finder-file-regexp entry)
349 (let ((path (finder-find-library entry)))
351 (view-file-other-window path)
352 (error "Can't find Emacs Lisp library: '%s'" entry)))
354 (error "Finder-view works on Emacs Lisp libraries only"))))
356 (defun finder-select ()
358 (let ((key (finder-current-item)))
360 (if (string-match finder-file-regexp key)
361 (finder-commentary key)
362 (finder-list-matches key))))
365 (defun finder-mouse-select (ev)
367 (goto-char (event-point ev))
372 (defun finder-by-keyword ()
373 "Find packages matching a given keyword."
376 (finder-list-keywords))
378 (defun finder-mode ()
379 "Major mode for browsing package documentation.
381 \\[finder-select] more help for the item on the current line
382 \\[finder-edit] edit Lisp library in another window
383 \\[finder-view] view Lisp library in another window
384 \\[finder-exit] exit Finder mode and kill the Finder buffer.
387 (pop-to-buffer "*Finder*")
389 (setq buffer-read-only nil
390 mode-motion-hook 'mode-motion-highlight-line)
392 (use-local-map finder-mode-map)
393 (set-syntax-table emacs-lisp-mode-syntax-table)
394 (setq mode-name "Finder")
395 (setq major-mode 'finder-mode)
396 (make-local-variable 'finder-headmark)
397 (setq finder-headmark nil))
399 (defun finder-summary ()
400 "Summarize basic Finder commands."
403 (substitute-command-keys
405 "\\<finder-mode-map>\\[finder-select] = select, \\[finder-list-keywords] = keywords, \\[finder-edit] = edit, \\[finder-view] = view, \\[finder-exit] = quit, \\[finder-summary] = help")))
407 (defun finder-exit ()
408 "Exit Finder mode and kill the buffer."
411 (or (one-window-p t 0)
413 (kill-buffer "*Finder*"))
417 ;;; finder.el ends here