1 ;;; finder.el --- topic & keyword-based code finder
3 ;; Copyright (C) 1992, 1997, 1998, 1999, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
7 ;; Created: 16 Jun 1992
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
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.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This mode uses the Keywords library header to provide code-finding
29 ;; services by keyword.
35 (require 'find-func
) ;for find-library(-suffixes)
36 (require 'finder-inf nil t
)
38 ;; These are supposed to correspond to top-level customization groups,
40 (defvar finder-known-keywords
41 '((abbrev .
"abbreviation handling, typing shortcuts, and macros")
42 (bib .
"bibliography processors")
43 (c .
"C and related programming languages")
44 (calendar .
"calendar and time management tools")
45 (comm .
"communications, networking, and remote file access")
46 (convenience .
"convenience features for faster editing")
47 (data .
"editing data (non-text) files")
48 (docs .
"Emacs documentation facilities")
49 (emulations .
"emulations of other editors")
50 (extensions .
"Emacs Lisp language extensions")
51 (faces .
"fonts and colors for text")
52 (files .
"file editing and manipulation")
53 (frames .
"Emacs frames and window systems")
54 (games .
"games, jokes and amusements")
55 (hardware .
"interfacing with system hardware")
56 (help .
"on-line help systems")
57 (hypermedia .
"links between text or other media types")
58 (i18n .
"internationalization and character-set support")
59 (internal .
"code for Emacs internals, build process, defaults")
60 (languages .
"specialized modes for editing programming languages")
61 (lisp .
"Lisp support, including Emacs Lisp")
62 (local .
"code local to your site")
63 (maint .
"Emacs development tools and aids")
64 (mail .
"email reading and posting")
65 (matching .
"searching, matching, and sorting")
66 (mouse .
"mouse support")
67 (multimedia .
"images and sound")
68 (news .
"USENET news reading and posting")
69 (outlines .
"hierarchical outlining and note taking")
70 (processes .
"processes, subshells, and compilation")
71 (terminals .
"text terminals (ttys)")
72 (tex .
"the TeX document formatter")
73 (tools .
"programming tools")
74 (unix .
"UNIX feature interfaces and emulators")
75 (vc .
"version control")
76 (wp .
"word processing")))
78 (defvar finder-mode-map
79 (let ((map (make-sparse-keymap))
80 (menu-map (make-sparse-keymap "Finder")))
81 (define-key map
" " 'finder-select
)
82 (define-key map
"f" 'finder-select
)
83 (define-key map
[follow-link
] 'mouse-face
)
84 (define-key map
[mouse-2
] 'finder-mouse-select
)
85 (define-key map
"\C-m" 'finder-select
)
86 (define-key map
"?" 'finder-summary
)
87 (define-key map
"n" 'next-line
)
88 (define-key map
"p" 'previous-line
)
89 (define-key map
"q" 'finder-exit
)
90 (define-key map
"d" 'finder-list-keywords
)
92 (define-key map
[menu-bar finder-mode
]
93 (cons "Finder" menu-map
))
94 (define-key menu-map
[finder-exit
]
95 '(menu-item "Quit" finder-exit
96 :help
"Exit Finder mode"))
97 (define-key menu-map
[finder-summary
]
98 '(menu-item "Summary" finder-summary
99 :help
"Summary item on current line in a finder buffer"))
100 (define-key menu-map
[finder-list-keywords
]
101 '(menu-item "List keywords" finder-list-keywords
102 :help
"Display descriptions of the keywords in the Finder buffer"))
103 (define-key menu-map
[finder-select
]
104 '(menu-item "Select" finder-select
105 :help
"Select item on current line in a finder buffer"))
108 (defvar finder-mode-syntax-table
109 (let ((st (make-syntax-table emacs-lisp-mode-syntax-table
)))
110 (modify-syntax-entry ?\
; ". " st)
112 "Syntax table used while in `finder-mode'.")
114 (defvar finder-font-lock-keywords
115 '(("`\\([^'`]+\\)'" 1 font-lock-constant-face prepend
))
116 "Font-lock keywords for Finder mode.")
118 (defvar finder-headmark nil
119 "Internal finder-mode variable, local in finder buffer.")
121 ;;; Code for regenerating the keyword list.
123 (defvar finder-keywords-hash nil
124 "Hash table mapping keywords to lists of package names.
125 Keywords and package names both should be symbols.")
127 (defvar generated-finder-keywords-file
"finder-inf.el"
128 "The function `finder-compile-keywords' writes keywords into this file.")
130 ;; Skip autogenerated files, because they will never contain anything
131 ;; useful, and because in parallel builds of Emacs they may get
132 ;; modified while we are trying to read them.
133 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-01/msg00469.html
134 ;; ldefs-boot is not auto-generated, but has nothing useful.
135 (defvar finder-no-scan-regexp
"\\(^\\.#\\|\\(loaddefs\\|ldefs-boot\\|\
136 cus-load\\|finder-inf\\|esh-groups\\|subdirs\\)\\.el$\\)"
137 "Regexp matching file names not to scan for keywords.")
139 (autoload 'autoload-rubric
"autoload")
141 (defvar finder--builtins-alist
147 ("international" . emacs
)
150 ("semantic" . semantic
)
151 ("analyze" . semantic
)
152 ("bovine" . semantic
)
153 ("decorate" . semantic
)
154 ("symref" . semantic
)
155 ("wisent" . semantic
)
158 ("srecode" . srecode
)
161 "Alist of built-in package directories.
162 Each element should have the form (DIR . PACKAGE), where DIR is a
163 directory name and PACKAGE is the name of a package (a symbol).
164 When generating `package--builtins', Emacs assumes any file in
165 DIR is part of the package PACKAGE.")
167 (defun finder-compile-keywords (&rest dirs
)
168 "Regenerate list of built-in Emacs packages.
169 This recomputes `package--builtins' and `finder-keywords-hash',
170 and prints them into the file `generated-finder-keywords-file'.
172 Optional DIRS is a list of Emacs Lisp directories to compile
173 from; the default is `load-path'."
174 ;; Allow compressed files also.
175 (setq package--builtins nil
)
176 (setq finder-keywords-hash
(make-hash-table :test
'eq
))
177 (let ((el-file-regexp "^\\([^=].*\\)\\.el\\(\\.\\(gz\\|Z\\)\\)?$")
178 package-override files base-name processed
179 summary keywords package version entry desc
)
180 (dolist (d (or dirs load-path
))
181 (when (file-exists-p (directory-file-name d
))
182 (message "Directory %s" d
)
183 (setq package-override
186 (assoc (file-name-nondirectory (directory-file-name d
))
187 finder--builtins-alist
))))
188 (setq files
(directory-files d nil el-file-regexp
))
190 (unless (or (string-match finder-no-scan-regexp f
)
191 (null (setq base-name
192 (and (string-match el-file-regexp f
)
193 (intern (match-string 1 f
)))))
194 (memq base-name processed
))
195 (push base-name processed
)
197 (insert-file-contents (expand-file-name f d
))
198 (setq summary
(lm-synopsis)
199 keywords
(mapcar 'intern
(lm-keywords-list))
200 package
(or package-override
201 (intern-soft (lm-header "package"))
203 version
(lm-header "version")))
205 (setq version
(ignore-errors (version-to-list version
)))
206 (setq entry
(assq package package--builtins
))
208 (push (cons package
(vector version nil summary
))
210 ((eq base-name package
)
211 (setq desc
(cdr entry
))
212 (aset desc
0 version
)
213 (aset desc
2 summary
)))
214 (dolist (kw keywords
)
218 (gethash kw finder-keywords-hash
)))
219 finder-keywords-hash
))))))))
221 (setq package--builtins
222 (sort package--builtins
223 (lambda (a b
) (string< (symbol-name (car a
))
224 (symbol-name (car b
))))))
227 (find-file generated-finder-keywords-file
)
228 (setq buffer-undo-list t
)
230 (insert (autoload-rubric generated-finder-keywords-file
231 "keyword-to-package mapping" t
))
232 (search-backward "\f")
233 (insert "(setq package--builtins '(\n")
234 (dolist (package package--builtins
)
236 (prin1 package
(current-buffer))
239 ;; Insert hash table.
240 (insert "(setq finder-keywords-hash\n ")
241 (prin1 finder-keywords-hash
(current-buffer))
243 (basic-save-buffer)))
245 (defun finder-compile-keywords-make-dist ()
246 "Regenerate `finder-inf.el' for the Emacs distribution."
247 (apply 'finder-compile-keywords command-line-args-left
)
250 ;;; Now the retrieval code
252 (defun finder-insert-at-column (column &rest strings
)
253 "Insert, at column COLUMN, other args STRINGS."
254 (if (>= (current-column) column
) (insert "\n"))
255 (move-to-column column t
)
256 (apply 'insert strings
))
258 (defvar finder-help-echo nil
)
260 (defun finder-mouse-face-on-line ()
261 "Put `mouse-face' and `help-echo' properties on the previous line."
264 ;; If finder-insert-at-column moved us to a new line, go back one more.
265 (if (looking-at "[ \t]") (forward-line -
1))
266 (unless finder-help-echo
267 (setq finder-help-echo
268 (let* ((keys1 (where-is-internal 'finder-select
270 (keys (nconc (where-is-internal
271 'finder-mouse-select finder-mode-map
)
273 (concat (mapconcat 'key-description keys
", ")
276 (line-beginning-position) (line-end-position)
277 '(mouse-face highlight
278 help-echo finder-help-echo
))))
280 (defun finder-unknown-keywords ()
281 "Return an alist of unknown keywords and number of their occurrences.
282 Unknown keywords are those present in `finder-keywords-hash' but
283 not `finder-known-keywords'."
285 (maphash (lambda (kw packages
)
286 (unless (assq kw finder-known-keywords
)
287 (push (cons kw
(length packages
)) alist
)))
288 finder-keywords-hash
)
289 (sort alist
(lambda (a b
) (string< (car a
) (car b
))))))
292 (defun finder-list-keywords ()
293 "Display descriptions of the keywords in the Finder buffer."
295 (if (get-buffer "*Finder*")
296 (pop-to-buffer "*Finder*")
297 (pop-to-buffer (get-buffer-create "*Finder*"))
299 (let ((inhibit-read-only t
))
301 (dolist (assoc finder-known-keywords
)
302 (let ((keyword (car assoc
)))
303 (insert (propertize (symbol-name keyword
)
304 'font-lock-face
'font-lock-constant-face
))
305 (finder-insert-at-column 14 (concat (cdr assoc
) "\n"))
306 (finder-mouse-face-on-line)))
307 (goto-char (point-min))
308 (setq finder-headmark
(point)
310 (set-buffer-modified-p nil
)
314 (defun finder-list-matches (key)
315 (let* ((id (intern key
))
316 (packages (gethash id finder-keywords-hash
)))
318 (error "No packages matching key `%s'" key
))
319 (package--list-packages packages
)))
321 (define-button-type 'finder-xref
'action
#'finder-goto-xref
)
323 (defun finder-goto-xref (button)
324 "Jump to a lisp file for the BUTTON at point."
325 (let* ((file (button-get button
'xref
))
326 (lib (locate-library file
)))
327 (if lib
(finder-commentary lib
)
328 (message "Unable to locate `%s'" file
))))
331 (defun finder-commentary (file)
332 "Display FILE's commentary section.
333 FILE should be in a form suitable for passing to `locate-library'."
336 (completing-read "Library name: "
337 (apply-partially 'locate-file-completion-table
338 (or find-function-source-path load-path
)
339 (find-library-suffixes)))))
340 (let ((str (lm-commentary (find-library-name file
))))
341 (or str
(error "Can't find any Commentary section"))
342 ;; This used to use *Finder* but that would clobber the
343 ;; directory of categories.
344 (pop-to-buffer "*Finder-package*")
345 (setq buffer-read-only nil
349 (goto-char (point-min))
351 (goto-char (point-max))
353 (goto-char (point-min))
354 (while (re-search-forward "^;+ ?" nil t
)
355 (replace-match "" nil nil
))
356 (goto-char (point-min))
357 (while (re-search-forward "\\<\\([-[:alnum:]]+\\.el\\)\\>" nil t
)
358 (if (locate-library (match-string 1))
359 (make-text-button (match-beginning 1) (match-end 1)
360 'xref
(match-string-no-properties 1)
361 'help-echo
"Read this file's commentary"
362 :type
'finder-xref
)))
363 (goto-char (point-min))
364 (setq buffer-read-only t
)
365 (set-buffer-modified-p nil
)
366 (shrink-window-if-larger-than-buffer)
370 (defun finder-current-item ()
371 (let ((key (save-excursion
374 (if (or (and finder-headmark
(< (point) finder-headmark
))
375 (zerop (length key
)))
376 (error "No keyword or filename on this line")
379 (defun finder-select ()
380 "Select item on current line in a finder buffer."
382 (let ((key (finder-current-item)))
383 (if (string-match "\\.el$" key
)
384 (finder-commentary key
)
385 (finder-list-matches key
))))
387 (defun finder-mouse-select (event)
388 "Select item in a finder buffer with the mouse."
390 (with-current-buffer (window-buffer (posn-window (event-start event
)))
391 (goto-char (posn-point (event-start event
)))
395 (defun finder-by-keyword ()
396 "Find packages matching a given keyword."
398 (finder-list-keywords))
400 (define-derived-mode finder-mode nil
"Finder"
401 "Major mode for browsing package documentation.
403 \\[finder-select] more help for the item on the current line
404 \\[finder-exit] exit Finder mode and kill the Finder buffer."
405 :syntax-table finder-mode-syntax-table
406 (setq buffer-read-only t
408 (set (make-local-variable 'finder-headmark
) nil
))
410 (defun finder-summary ()
411 "Summarize basic Finder commands."
414 (substitute-command-keys
415 "\\<finder-mode-map>\\[finder-select] = select, \
416 \\[finder-mouse-select] = select, \\[finder-list-keywords] = to \
417 finder directory, \\[finder-exit] = quit, \\[finder-summary] = help")))
419 (defun finder-exit ()
421 Delete the window and kill all Finder-related buffers."
423 (ignore-errors (delete-window))
424 (let ((buf "*Finder*"))
425 (and (get-buffer buf
) (kill-buffer buf
))))
430 ;; arch-tag: ec85ff49-8cb8-41f5-a63f-9131d53ce2c5
431 ;;; finder.el ends here