Mercurial > emacs
annotate lisp/locate.el @ 20946:3dbeb1555fa7
(Fmessage): Check byte size (instead of char size) of
VAL against MESSAGE_LENGTH.
author | Kenichi Handa <handa@m17n.org> |
---|---|
date | Fri, 20 Feb 1998 11:14:32 +0000 |
parents | 2fd9f2883ad4 |
children | 124580b805a3 |
rev | line source |
---|---|
17517 | 1 ;;; locate.el --- interface to the locate command |
16781 | 2 |
3 ;; Copyright (C) 1996 Free Software Foundation, Inc. | |
4 | |
5 ;; Author: Peter Breton <pbreton@i-kinetics.com> | |
6 | |
7 ;; This file is part of GNU Emacs. | |
8 | |
9 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
10 ;; it under the terms of the GNU General Public License as published by | |
11 ;; the Free Software Foundation; either version 2, or (at your option) | |
12 ;; any later version. | |
13 | |
14 ;; GNU Emacs is distributed in the hope that it will be useful, | |
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 ;; GNU General Public License for more details. | |
18 | |
19 ;; You should have received a copy of the GNU General Public License | |
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
22 ;; Boston, MA 02111-1307, USA. | |
23 | |
24 ;;; Commentary: | |
25 | |
26 ;; Search a database of files and use dired commands on | |
27 ;; the result. | |
28 ;; | |
29 | |
30 ;;;;; Building a database of files ;;;;;;;;; | |
31 ;; | |
32 ;; You can create a simple files database with a port of the Unix find command | |
33 ;; and one of the various Windows NT various scheduling utilities, | |
34 ;; for example the AT command from the NT Resource Kit, WinCron which is | |
35 ;; included with Microsoft FrontPage, or the shareware NTCron program. | |
36 ;; | |
37 ;; To set up a function which searches the files database, do something | |
38 ;; like this: | |
39 ;; | |
40 ;; (defvar locate-fcodes-file (concat my-home "/fcodes")) | |
41 ;; (defvar locate-make-command-line 'nt-locate-make-command-line) | |
42 ;; | |
43 ;; (defun nt-locate-make-command-line (arg) | |
44 ;; (cons "grep" | |
45 ;; (mapconcat 'identity | |
46 ;; (list "-i" arg locate-fcodes-file) | |
47 ;; " "))) | |
48 ;; | |
49 ;;;;;;;; ADVICE For dired-make-relative: ;;;;;;;;; | |
50 ;; | |
51 ;; For certain dired commands to work right, you should also include the | |
52 ;; following in your _emacs/.emacs: | |
53 ;; | |
54 ;; (defadvice dired-make-relative (before set-no-error activate) | |
55 ;; "For locate mode and Windows, don't return errors" | |
56 ;; (if (and (eq major-mode 'locate-mode) | |
57 ;; (memq system-type (list 'windows-nt 'ms-dos))) | |
58 ;; (ad-set-arg 2 t) | |
59 ;; )) | |
60 ;; | |
61 ;; Otherwise, dired-make-relative will give error messages like | |
62 ;; "FILENAME: not in directory tree growing at /" | |
63 | |
64 ;;; Commentary: | |
65 ;; | |
66 ;; Locate.el provides an interface to a program which searches a | |
67 ;; database of file names. By default, this program is the GNU locate | |
68 ;; command, but it could also be the BSD-style find command, or even a | |
69 ;; user specified command. | |
70 ;; | |
71 ;; To use the BSD-style "fast find", or any other shell command of the | |
72 ;; form | |
73 ;; | |
74 ;; SHELLPROGRAM Name-to-find | |
75 ;; | |
76 ;; set the variable locate-command in your .emacs file. | |
77 ;; | |
78 ;; To use a more complicated expression, create a function which | |
79 ;; takes a string (the name to find) as input and returns a cons | |
80 ;; pair: the car should be the command to be executed, the cdr | |
81 ;; should be the arguments, concatenated into a string (including | |
82 ;; the name to find). Then do | |
83 ;; | |
84 ;; (setq locate-make-command-line 'my-locate-command-line) | |
85 ;; | |
86 ;; in your .emacs, using the name of your function in place of | |
87 ;; my-locate-command-line | |
88 ;; | |
89 ;; You should make sure that whichever command you use works correctly | |
90 ;; from a shell prompt. GNU locate and BSD find expect the file databases | |
91 ;; to either be in standard places or located via environment variables. | |
92 ;; If the latter, make sure these environment variables are set in | |
93 ;; your emacs process | |
94 ;; | |
95 ;; Locate-mode assumes that each line output from the locate-command | |
96 ;; consists exactly of a file name, possibly preceded or trailed by | |
97 ;; whitespace. If your file database has other information on the line (for | |
98 ;; example, the file size), you will need to redefine the function | |
99 ;; locate-get-file-positions to return a list consisting of the first | |
100 ;; character in the file name and the last character in the file name. | |
101 ;; | |
102 ;; To use locate-mode, simply type M-x locate and then the string | |
103 ;; you wish to find. You can use almost all of the dired commands in | |
104 ;; the resulting *Locate* buffer. It is worth noting that your commands | |
105 ;; do not, of course, affect the file database. For example, if you | |
106 ;; compress a file in the locate buffer, the actual file will be | |
107 ;; compressed, but the entry in the file database will not be | |
108 ;; affected. Consequently, the database and the filesystem will be out | |
109 ;; of sync until the next time the database is updated | |
110 ;; | |
111 ;; The command locate-with-filter keeps only lines matching a | |
112 ;; regular expression; this is often useful to constrain a big search. | |
113 ;; | |
114 | |
115 ;;; Code: | |
116 | |
117 (eval-when-compile | |
118 (require 'dired)) | |
119 | |
120 ;; Variables | |
20845 | 121 |
122 (defgroup locate nil | |
123 "Interface to the locate command." | |
124 :prefix "locate-" | |
125 :group 'external) | |
126 | |
127 (defcustom locate-command "locate" | |
128 "*The executable program used to search a database of files." | |
129 :type 'string | |
130 :group 'locate) | |
16781 | 131 |
132 (defvar locate-history-list nil | |
133 "The history list used by the \\[locate] command.") | |
134 | |
20845 | 135 (defcustom locate-make-command-line 'locate-default-make-command-line |
136 "*Function used to create the locate command line." | |
137 :type 'function | |
138 :group 'locate) | |
16781 | 139 |
20845 | 140 (defcustom locate-buffer-name "*Locate*" |
141 "*Name of the buffer to show results from the \\[locate] command." | |
142 :type 'string | |
143 :group 'locate) | |
16781 | 144 |
20845 | 145 (defcustom locate-fcodes-file nil |
146 "*Database of filenames." | |
147 :type 'file | |
148 :group 'locate) | |
16781 | 149 |
20845 | 150 (defcustom locate-mouse-face 'highlight |
151 "*Face used to highlight locate entries." | |
152 :type 'face | |
153 :group 'locate) | |
16781 | 154 |
20845 | 155 (defcustom locate-header-face 'region |
156 "*Face used to highlight the locate header." | |
157 :type 'face | |
158 :group 'locate) | |
16781 | 159 |
160 (defvar locate-current-filter nil) | |
161 | |
162 ;; Functions | |
163 | |
164 (defun locate-default-make-command-line (search-string) | |
165 (cons locate-command search-string)) | |
166 | |
167 ;;;### autoload | |
168 (defun locate (search-string &optional filter) | |
169 "Run the \\[locate] command, putting results in `*Locate*' buffer." | |
170 (interactive | |
171 (list (read-from-minibuffer "Locate: " nil nil | |
172 nil 'locate-history-list))) | |
173 (let* ((pop-up-windows 1) | |
174 (locate-cmd-list (funcall locate-make-command-line search-string)) | |
175 (locate-cmd (car locate-cmd-list)) | |
176 (locate-cmd-args (cdr locate-cmd-list)) | |
177 (locate-proc) | |
178 ) | |
179 | |
180 ;; Find the Locate buffer | |
181 (if (not (string-equal (buffer-name) locate-buffer-name)) | |
182 (switch-to-buffer-other-window locate-buffer-name)) | |
183 | |
184 (locate-mode) | |
185 (erase-buffer) | |
186 | |
187 (setq locate-current-filter filter) | |
188 | |
189 (call-process locate-cmd nil t nil locate-cmd-args) | |
190 (if filter | |
191 (locate-filter-output filter)) | |
192 | |
193 (locate-do-setup) | |
194 ) | |
195 ) | |
196 | |
197 ;;;### autoload | |
198 (defun locate-with-filter (search-string filter) | |
199 "Run the locate command with a filter." | |
200 (interactive | |
201 (list (read-from-minibuffer "Locate: " nil nil | |
202 nil 'locate-history-list) | |
203 (read-from-minibuffer "Filter: " nil nil | |
204 nil 'grep-history))) | |
205 (locate search-string filter)) | |
206 | |
207 (defun locate-filter-output (filter) | |
208 "Filter output from the locate command." | |
209 (goto-char (point-min)) | |
210 (delete-non-matching-lines (regexp-quote filter))) | |
211 | |
212 (defvar locate-mode-map nil | |
213 "Local keymap for Locate mode buffers.") | |
214 (if locate-mode-map | |
215 nil | |
216 | |
217 (require 'dired) | |
218 | |
219 (setq locate-mode-map (copy-keymap dired-mode-map)) | |
220 | |
221 ;; Undefine Useless Dired Menu bars | |
222 (define-key locate-mode-map [menu-bar Dired] 'undefined) | |
223 (define-key locate-mode-map [menu-bar subdir] 'undefined) | |
224 | |
225 (define-key locate-mode-map [menu-bar mark executables] 'undefined) | |
226 (define-key locate-mode-map [menu-bar mark directory] 'undefined) | |
227 (define-key locate-mode-map [menu-bar mark directories] 'undefined) | |
228 (define-key locate-mode-map [menu-bar mark symlinks] 'undefined) | |
229 | |
230 (define-key locate-mode-map [mouse-2] 'mouse-locate-view-file) | |
231 (define-key locate-mode-map "\C-ct" 'locate-tags) | |
232 | |
233 (define-key locate-mode-map "U" 'dired-unmark-all-files-no-query) | |
234 ) | |
235 | |
236 ;; This variable is used to indent the lines and then to search for | |
237 ;; the file name | |
238 (defconst locate-filename-indentation 4 | |
239 "The amount of indentation for each file.") | |
240 | |
241 (defun locate-get-file-positions () | |
242 (save-excursion | |
243 (end-of-line) | |
244 (let ((eol (point))) | |
245 (beginning-of-line) | |
246 | |
247 ;; Assumes names end at the end of the line | |
248 (forward-char locate-filename-indentation) | |
249 (list (point) eol)))) | |
250 | |
251 ;; From SQL-mode | |
252 (defun current-line () | |
253 "Return the current line number, as an integer." | |
254 (interactive) | |
255 (+ (count-lines (point-min) (point)) | |
256 (if (eq (current-column) 0) | |
257 1 | |
258 0))) | |
259 | |
260 (defun locate-get-filename () | |
261 (let ((pos (locate-get-file-positions)) | |
262 (lineno (current-line))) | |
263 (and (not (eq lineno 1)) | |
264 (not (eq lineno 2)) | |
265 (buffer-substring (elt pos 0) (elt pos 1))))) | |
266 | |
267 (defun mouse-locate-view-file (event) | |
268 "In Locate mode, view a file, using the mouse." | |
269 (interactive "@e") | |
270 (save-excursion | |
271 (goto-char (posn-point (event-start event))) | |
272 (view-file (locate-get-filename)))) | |
273 | |
274 ;; Define a mode for locate | |
275 ;; Default directory is set to "/" so that dired commands, which | |
276 ;; expect to be in a tree, will work properly | |
277 (defun locate-mode () | |
278 "Major mode for the `*Locate*' buffer made by \\[locate]." | |
279 (kill-all-local-variables) | |
280 (use-local-map locate-mode-map) | |
281 (setq major-mode 'locate-mode | |
282 mode-name "Locate" | |
283 default-directory "/" | |
284 dired-subdir-alist (list (cons "/" (point-min-marker)))) | |
16795
2a9ae2be61ae
(locate-mode): Locally set dired-move-to-filename-regexp,
Richard M. Stallman <rms@gnu.org>
parents:
16781
diff
changeset
|
285 (make-local-variable 'dired-move-to-filename-regexp) |
2a9ae2be61ae
(locate-mode): Locally set dired-move-to-filename-regexp,
Richard M. Stallman <rms@gnu.org>
parents:
16781
diff
changeset
|
286 (setq dired-move-to-filename-regexp |
2a9ae2be61ae
(locate-mode): Locally set dired-move-to-filename-regexp,
Richard M. Stallman <rms@gnu.org>
parents:
16781
diff
changeset
|
287 (make-string locate-filename-indentation ?\ )) |
2a9ae2be61ae
(locate-mode): Locally set dired-move-to-filename-regexp,
Richard M. Stallman <rms@gnu.org>
parents:
16781
diff
changeset
|
288 (make-local-variable 'dired-actual-switches) |
2a9ae2be61ae
(locate-mode): Locally set dired-move-to-filename-regexp,
Richard M. Stallman <rms@gnu.org>
parents:
16781
diff
changeset
|
289 (setq dired-actual-switches "") |
2a9ae2be61ae
(locate-mode): Locally set dired-move-to-filename-regexp,
Richard M. Stallman <rms@gnu.org>
parents:
16781
diff
changeset
|
290 (make-local-variable 'dired-permission-flags-regexp) |
2a9ae2be61ae
(locate-mode): Locally set dired-move-to-filename-regexp,
Richard M. Stallman <rms@gnu.org>
parents:
16781
diff
changeset
|
291 (setq dired-permission-flags-regexp "^\\( \\)") |
16781 | 292 (run-hooks 'locate-mode-hook)) |
293 | |
294 (defun locate-do-setup () | |
295 (let ((search-string (car locate-history-list))) | |
296 (goto-char (point-min)) | |
297 (save-excursion | |
298 | |
299 ;; Nothing returned from locate command? | |
300 (if (eobp) | |
301 (progn | |
302 (kill-buffer locate-buffer-name) | |
303 (delete-window) | |
304 (if locate-current-filter | |
305 (error "Locate: no match for %s in database using filter %s" | |
306 search-string locate-current-filter) | |
307 (error "Locate: no match for %s in database" search-string)))) | |
308 | |
309 (locate-insert-header search-string) | |
310 | |
16795
2a9ae2be61ae
(locate-mode): Locally set dired-move-to-filename-regexp,
Richard M. Stallman <rms@gnu.org>
parents:
16781
diff
changeset
|
311 (while (not (eobp)) |
2a9ae2be61ae
(locate-mode): Locally set dired-move-to-filename-regexp,
Richard M. Stallman <rms@gnu.org>
parents:
16781
diff
changeset
|
312 (insert-char ?\ locate-filename-indentation t) |
2a9ae2be61ae
(locate-mode): Locally set dired-move-to-filename-regexp,
Richard M. Stallman <rms@gnu.org>
parents:
16781
diff
changeset
|
313 (locate-set-properties) |
2a9ae2be61ae
(locate-mode): Locally set dired-move-to-filename-regexp,
Richard M. Stallman <rms@gnu.org>
parents:
16781
diff
changeset
|
314 (forward-line 1))))) |
16781 | 315 |
316 (defun locate-set-properties () | |
317 (save-excursion | |
318 (let ((pos (locate-get-file-positions))) | |
319 (add-text-properties (elt pos 0) (elt pos 1) | |
320 (list 'mouse-face locate-mouse-face))))) | |
321 | |
322 (defun locate-insert-header (search-string) | |
323 (let ((locate-format-string "Matches for %s") | |
324 (locate-regexp-match | |
325 (concat " *Matches for \\(" (regexp-quote search-string) "\\)")) | |
326 (locate-format-args (list search-string)) | |
327 ) | |
328 | |
329 (if locate-fcodes-file | |
330 (setq locate-format-string | |
331 (concat locate-format-string " in %s") | |
332 locate-regexp-match | |
333 (concat locate-regexp-match | |
334 " in \\(" | |
335 (regexp-quote locate-fcodes-file) | |
336 "\\)") | |
337 locate-format-args | |
338 (append (list locate-fcodes-file) locate-format-args))) | |
339 | |
340 (if locate-current-filter | |
341 (setq locate-format-string | |
342 (concat locate-format-string " using filter %s") | |
343 locate-regexp-match | |
344 (concat locate-regexp-match | |
345 " using filter " | |
346 "\\(" | |
347 (regexp-quote locate-current-filter) | |
348 "\\)") | |
349 locate-format-args | |
350 (append (list locate-current-filter) locate-format-args))) | |
351 | |
352 (setq locate-format-string | |
353 (concat locate-format-string ": \n\n") | |
354 locate-regexp-match | |
355 (concat locate-regexp-match ": \n")) | |
356 | |
16795
2a9ae2be61ae
(locate-mode): Locally set dired-move-to-filename-regexp,
Richard M. Stallman <rms@gnu.org>
parents:
16781
diff
changeset
|
357 (insert (apply 'format locate-format-string (reverse locate-format-args))) |
16781 | 358 |
359 (save-excursion | |
360 (goto-char (point-min)) | |
361 (if (not (looking-at locate-regexp-match)) | |
362 nil | |
363 (add-text-properties (match-beginning 1) (match-end 1) | |
364 (list 'face locate-header-face)) | |
365 (and (match-beginning 2) | |
366 (add-text-properties (match-beginning 2) (match-end 2) | |
367 (list 'face locate-header-face))) | |
368 (and (match-beginning 3) | |
369 (add-text-properties (match-beginning 3) (match-end 3) | |
370 (list 'face locate-header-face))) | |
371 )))) | |
372 | |
373 (defun locate-tags () | |
374 "Visit a tags table in `*Locate*' mode." | |
375 (interactive) | |
376 (let ((tags-table (locate-get-filename))) | |
377 (if (y-or-n-p (format "Visit tags table %s? " tags-table)) | |
378 (visit-tags-table tags-table) | |
379 nil))) | |
380 | |
381 (provide 'locate) | |
382 | |
383 ;;; locate.el ends here |