1 ;;; finder.el --- topic & keyword-based code finder
3 ;; Copyright (C) 1992, 1997-1999, 2001-2013 Free Software Foundation,
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 (defun set-generated-finder-keywords-file (file)
142 "Set value of `generated-finder-keywords-file' from FILE.
144 On systems other than MS-Windows, just sets the value
145 of `generated-finder-keywords-file'. On MS-Windows, converts
146 /d/foo/bar form passed by MSYS Make into d:/foo/bar that Emacs
147 can grok. This function is called from lisp/Makefile."
148 (when (and (eq system-type
'windows-nt
)
149 (string-match "\\`/[a-zA-Z]/" file
))
150 (setq file
(concat (substring file
1 2) ":" (substring file
2))))
151 (setq generated-finder-keywords-file file
))
153 (defvar finder--builtins-alist
159 ("international" . emacs
)
162 ("semantic" . semantic
)
163 ("analyze" . semantic
)
164 ("bovine" . semantic
)
165 ("decorate" . semantic
)
166 ("symref" . semantic
)
167 ("wisent" . semantic
)
170 ("srecode" . srecode
)
173 "Alist of built-in package directories.
174 Each element should have the form (DIR . PACKAGE), where DIR is a
175 directory name and PACKAGE is the name of a package (a symbol).
176 When generating `package--builtins', Emacs assumes any file in
177 DIR is part of the package PACKAGE.")
179 (defun finder-compile-keywords (&rest dirs
)
180 "Regenerate list of built-in Emacs packages.
181 This recomputes `package--builtins' and `finder-keywords-hash',
182 and prints them into the file `generated-finder-keywords-file'.
184 Optional DIRS is a list of Emacs Lisp directories to compile
185 from; the default is `load-path'."
186 ;; Allow compressed files also.
187 (setq package--builtins nil
)
188 (setq finder-keywords-hash
(make-hash-table :test
'eq
))
189 (let ((el-file-regexp "^\\([^=].*\\)\\.el\\(\\.\\(gz\\|Z\\)\\)?$")
190 package-override files base-name processed
191 summary keywords package version entry desc
)
192 (dolist (d (or dirs load-path
))
193 (when (file-exists-p (directory-file-name d
))
194 (message "Directory %s" d
)
195 (setq package-override
198 (assoc (file-name-nondirectory (directory-file-name d
))
199 finder--builtins-alist
))))
200 (setq files
(directory-files d nil el-file-regexp
))
202 (unless (or (string-match finder-no-scan-regexp f
)
203 (null (setq base-name
204 (and (string-match el-file-regexp f
)
205 (intern (match-string 1 f
)))))
206 (memq base-name processed
))
207 (push base-name processed
)
209 (insert-file-contents (expand-file-name f d
))
210 (setq summary
(lm-synopsis)
211 keywords
(mapcar 'intern
(lm-keywords-list))
212 package
(or package-override
213 (let ((str (lm-header "package")))
214 (if str
(intern str
)))
216 version
(lm-header "version")))
218 (setq version
(ignore-errors (version-to-list version
)))
219 (setq entry
(assq package package--builtins
))
221 (push (cons package
(vector version nil summary
))
223 ((eq base-name package
)
224 (setq desc
(cdr entry
))
225 (aset desc
0 version
)
226 (aset desc
2 summary
)))
227 (dolist (kw keywords
)
231 (gethash kw finder-keywords-hash
)))
232 finder-keywords-hash
))))))))
234 (setq package--builtins
235 (sort package--builtins
236 (lambda (a b
) (string< (symbol-name (car a
))
237 (symbol-name (car b
))))))
240 (find-file generated-finder-keywords-file
)
241 (setq buffer-undo-list t
)
243 (insert (autoload-rubric generated-finder-keywords-file
244 "keyword-to-package mapping" t
))
245 (search-backward "\f")
246 (insert "(setq package--builtins '(\n")
247 (dolist (package package--builtins
)
249 (prin1 package
(current-buffer))
252 ;; Insert hash table.
253 (insert "(setq finder-keywords-hash\n ")
254 (prin1 finder-keywords-hash
(current-buffer))
256 (basic-save-buffer)))
258 (defun finder-compile-keywords-make-dist ()
259 "Regenerate `finder-inf.el' for the Emacs distribution."
260 (apply 'finder-compile-keywords command-line-args-left
)
263 ;;; Now the retrieval code
265 (defun finder-insert-at-column (column &rest strings
)
266 "Insert, at column COLUMN, other args STRINGS."
267 (if (>= (current-column) column
) (insert "\n"))
268 (move-to-column column t
)
269 (apply 'insert strings
))
271 (defvar finder-help-echo nil
)
273 (defun finder-mouse-face-on-line ()
274 "Put `mouse-face' and `help-echo' properties on the previous line."
277 ;; If finder-insert-at-column moved us to a new line, go back one more.
278 (if (looking-at "[ \t]") (forward-line -
1))
279 (unless finder-help-echo
280 (setq finder-help-echo
281 (let* ((keys1 (where-is-internal 'finder-select
283 (keys (nconc (where-is-internal
284 'finder-mouse-select finder-mode-map
)
286 (concat (mapconcat 'key-description keys
", ")
289 (line-beginning-position) (line-end-position)
290 '(mouse-face highlight
291 help-echo finder-help-echo
))))
293 (defun finder-unknown-keywords ()
294 "Return an alist of unknown keywords and number of their occurrences.
295 Unknown keywords are those present in `finder-keywords-hash' but
296 not `finder-known-keywords'."
298 (maphash (lambda (kw packages
)
299 (unless (assq kw finder-known-keywords
)
300 (push (cons kw
(length packages
)) alist
)))
301 finder-keywords-hash
)
302 (sort alist
(lambda (a b
) (string< (car a
) (car b
))))))
305 (defun finder-list-keywords ()
306 "Display descriptions of the keywords in the Finder buffer."
308 (if (get-buffer "*Finder*")
309 (pop-to-buffer "*Finder*")
310 (pop-to-buffer (get-buffer-create "*Finder*"))
312 (let ((inhibit-read-only t
))
314 (dolist (assoc finder-known-keywords
)
315 (let ((keyword (car assoc
)))
316 (insert (propertize (symbol-name keyword
)
317 'font-lock-face
'font-lock-constant-face
))
318 (finder-insert-at-column 14 (concat (cdr assoc
) "\n"))
319 (finder-mouse-face-on-line)))
320 (goto-char (point-min))
321 (setq finder-headmark
(point)
323 (set-buffer-modified-p nil
)
327 (defun finder-list-matches (key)
328 (let* ((id (intern key
))
329 (packages (gethash id finder-keywords-hash
)))
331 (error "No packages matching key `%s'" key
))
332 (package-show-package-list packages
)))
334 (define-button-type 'finder-xref
'action
#'finder-goto-xref
)
336 (defun finder-goto-xref (button)
337 "Jump to a lisp file for the BUTTON at point."
338 (let* ((file (button-get button
'xref
))
339 (lib (locate-library file
)))
340 (if lib
(finder-commentary lib
)
341 (message "Unable to locate `%s'" file
))))
344 (defun finder-commentary (file)
345 "Display FILE's commentary section.
346 FILE should be in a form suitable for passing to `locate-library'."
349 (completing-read "Library name: "
350 (apply-partially 'locate-file-completion-table
351 (or find-function-source-path load-path
)
352 (find-library-suffixes)))))
353 (let ((str (lm-commentary (find-library-name file
))))
354 (or str
(error "Can't find any Commentary section"))
355 ;; This used to use *Finder* but that would clobber the
356 ;; directory of categories.
357 (pop-to-buffer "*Finder-package*")
358 (setq buffer-read-only nil
362 (goto-char (point-min))
364 (goto-char (point-max))
366 (goto-char (point-min))
367 (while (re-search-forward "^;+ ?" nil t
)
368 (replace-match "" nil nil
))
369 (goto-char (point-min))
370 (while (re-search-forward "\\<\\([-[:alnum:]]+\\.el\\)\\>" nil t
)
371 (if (locate-library (match-string 1))
372 (make-text-button (match-beginning 1) (match-end 1)
373 'xref
(match-string-no-properties 1)
374 'help-echo
"Read this file's commentary"
375 :type
'finder-xref
)))
376 (goto-char (point-min))
377 (setq buffer-read-only t
)
378 (set-buffer-modified-p nil
)
379 (shrink-window-if-larger-than-buffer)
383 (defun finder-current-item ()
384 (let ((key (save-excursion
387 (if (or (and finder-headmark
(< (point) finder-headmark
))
388 (zerop (length key
)))
389 (error "No keyword or filename on this line")
392 (defun finder-select ()
393 "Select item on current line in a finder buffer."
395 (let ((key (finder-current-item)))
396 (if (string-match "\\.el$" key
)
397 (finder-commentary key
)
398 (finder-list-matches key
))))
400 (defun finder-mouse-select (event)
401 "Select item in a finder buffer with the mouse."
403 (with-current-buffer (window-buffer (posn-window (event-start event
)))
404 (goto-char (posn-point (event-start event
)))
408 (defun finder-by-keyword ()
409 "Find packages matching a given keyword."
411 (finder-list-keywords))
413 (define-derived-mode finder-mode nil
"Finder"
414 "Major mode for browsing package documentation.
416 \\[finder-select] more help for the item on the current line
417 \\[finder-exit] exit Finder mode and kill the Finder buffer."
418 :syntax-table finder-mode-syntax-table
419 (setq buffer-read-only t
421 (set (make-local-variable 'finder-headmark
) nil
))
423 (defun finder-summary ()
424 "Summarize basic Finder commands."
427 (substitute-command-keys
428 "\\<finder-mode-map>\\[finder-select] = select, \
429 \\[finder-mouse-select] = select, \\[finder-list-keywords] = to \
430 finder directory, \\[finder-exit] = quit, \\[finder-summary] = help")))
432 (defun finder-exit ()
434 Delete the window and kill all Finder-related buffers."
436 (ignore-errors (delete-window))
437 (let ((buf "*Finder*"))
438 (and (get-buffer buf
) (kill-buffer buf
))))
443 ;;; finder.el ends here