aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeter Breton1999-10-16 03:47:06 +0000
committerPeter Breton1999-10-16 03:47:06 +0000
commit83346ee87bc410e02d33b3bb45bc233bfc3a1b9c (patch)
tree4dfedfd2341b632030ad2f800c660ed2c86eb68d
parentd9823c3c1835b21e9550d06cc3db37f105a0cd45 (diff)
downloademacs-83346ee87bc410e02d33b3bb45bc233bfc3a1b9c.tar.gz
emacs-83346ee87bc410e02d33b3bb45bc233bfc3a1b9c.zip
(locate-in-alternate-database): Added this function
(locate): Added locate-post-command-hook. (locate-prompt-for-command): Added this variable. (locate): If locate-prompt-for-command is set, prompt for a command to run to populate the locate buffer as the default behavior. (locate-update): Add prefix arg to locate call. (locate-with-filter): Add prefix arg to locate call. (locate): Add prefix arg. If set, the function prompts the user (locate-mouse-face): No longer needed. (locate-mode): Setup `dired-subdir-alist' cleanly using `dired-alist-add-1'. (locate-set-properties): Set properties cleanly using `dired-insert-set-properties', giving dired like output. for a command to run instead of the default one. (locate-grep-history-list): Added this variable. (locate-with-filter): Use locate-grep-history-list instead of grep-history. (locate-filter-output): filter is not regexp-quoted. (locate-mode-map): Added keybinding for locate-find-directory. Changed keybinding for "U" from dired-unmark-all-files-no-query to dired-unmark-all-files. (locate-find-directory): Added this function. (locate-find-directory-other-window): Added this function. (locate-get-dirname): Added this function. (locate-mouse-view-file): Renamed mouse-locate-view-file to this name.
-rw-r--r--lisp/locate.el197
1 files changed, 146 insertions, 51 deletions
diff --git a/lisp/locate.el b/lisp/locate.el
index 0330a45453f..947f8aa7deb 100644
--- a/lisp/locate.el
+++ b/lisp/locate.el
@@ -24,11 +24,11 @@
24;;; Commentary: 24;;; Commentary:
25 25
26;; Search a database of files and use dired commands on 26;; Search a database of files and use dired commands on
27;; the result. 27;; the result.
28;; 28;;
29 29
30;;;;; Building a database of files ;;;;;;;;; 30;;;;; Building a database of files ;;;;;;;;;
31;; 31;;
32;; You can create a simple files database with a port of the Unix find command 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, 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 34;; for example the AT command from the NT Resource Kit, WinCron which is
@@ -36,18 +36,18 @@
36;; 36;;
37;; To set up a function which searches the files database, do something 37;; To set up a function which searches the files database, do something
38;; like this: 38;; like this:
39;; 39;;
40;; (defvar locate-fcodes-file "c:/users/peter/fcodes") 40;; (defvar locate-fcodes-file "c:/users/peter/fcodes")
41;; (defvar locate-make-command-line 'nt-locate-make-command-line) 41;; (defvar locate-make-command-line 'nt-locate-make-command-line)
42;; 42;;
43;; (defun nt-locate-make-command-line (arg) 43;; (defun nt-locate-make-command-line (arg)
44;; (list "grep" "-i" arg locate-fcodes-file)) 44;; (list "grep" "-i" arg locate-fcodes-file))
45;; 45;;
46;;;;;;;; ADVICE For dired-make-relative: ;;;;;;;;; 46;;;;;;;; ADVICE For dired-make-relative: ;;;;;;;;;
47;; 47;;
48;; For certain dired commands to work right, you should also include the 48;; For certain dired commands to work right, you should also include the
49;; following in your _emacs/.emacs: 49;; following in your _emacs/.emacs:
50;; 50;;
51;; (defadvice dired-make-relative (before set-no-error activate) 51;; (defadvice dired-make-relative (before set-no-error activate)
52;; "For locate mode and Windows, don't return errors" 52;; "For locate mode and Windows, don't return errors"
53;; (if (and (eq major-mode 'locate-mode) 53;; (if (and (eq major-mode 'locate-mode)
@@ -66,18 +66,18 @@
66;; user specified command. 66;; user specified command.
67;; 67;;
68;; To use the BSD-style "fast find", or any other shell command of the 68;; To use the BSD-style "fast find", or any other shell command of the
69;; form 69;; form
70;; 70;;
71;; SHELLPROGRAM Name-to-find 71;; SHELLPROGRAM Name-to-find
72;; 72;;
73;; set the variable `locate-command' in your .emacs file. 73;; set the variable `locate-command' in your .emacs file.
74;; 74;;
75;; To use a more complicated expression, create a function which 75;; To use a more complicated expression, create a function which
76;; takes a string (the name to find) as input and returns a list. 76;; takes a string (the name to find) as input and returns a list.
77;; The first element should be the command to be executed, the remaining 77;; The first element should be the command to be executed, the remaining
78;; elements should be the arguments (including the name to find). Then put 78;; elements should be the arguments (including the name to find). Then put
79;; 79;;
80;; (setq locate-make-command-line 'my-locate-command-line) 80;; (setq locate-make-command-line 'my-locate-command-line)
81;; 81;;
82;; in your .emacs, using the name of your function in place of 82;; in your .emacs, using the name of your function in place of
83;; my-locate-command-line. 83;; my-locate-command-line.
@@ -91,7 +91,7 @@
91;; Locate-mode assumes that each line output from the locate-command 91;; Locate-mode assumes that each line output from the locate-command
92;; consists exactly of a file name, possibly preceded or trailed by 92;; consists exactly of a file name, possibly preceded or trailed by
93;; whitespace. If your file database has other information on the line (for 93;; whitespace. If your file database has other information on the line (for
94;; example, the file size), you will need to redefine the function 94;; example, the file size), you will need to redefine the function
95;; `locate-get-file-positions' to return a list consisting of the first 95;; `locate-get-file-positions' to return a list consisting of the first
96;; character in the file name and the last character in the file name. 96;; character in the file name and the last character in the file name.
97;; 97;;
@@ -130,6 +130,9 @@
130(defvar locate-history-list nil 130(defvar locate-history-list nil
131 "The history list used by the \\[locate] command.") 131 "The history list used by the \\[locate] command.")
132 132
133(defvar locate-grep-history-list nil
134 "The history list used by the \\[locate-with-filter] command.")
135
133(defcustom locate-make-command-line 'locate-default-make-command-line 136(defcustom locate-make-command-line 'locate-default-make-command-line
134 "*Function used to create the locate command line." 137 "*Function used to create the locate command line."
135 :type 'function 138 :type 'function
@@ -145,12 +148,7 @@
145 :type '(choice file (const nil)) 148 :type '(choice file (const nil))
146 :group 'locate) 149 :group 'locate)
147 150
148(defcustom locate-mouse-face 'highlight 151(defcustom locate-header-face nil
149 "*Face used to highlight locate entries."
150 :type 'face
151 :group 'locate)
152
153(defcustom locate-header-face 'underline
154 "*Face used to highlight the locate header." 152 "*Face used to highlight the locate header."
155 :type 'face 153 :type 'face
156 :group 'locate) 154 :group 'locate)
@@ -160,31 +158,52 @@
160 :type 'string 158 :type 'string
161 :group 'locate) 159 :group 'locate)
162 160
161(defcustom locate-prompt-for-command nil
162 "If non-nil, the default behavior of the locate command is to prompt for a command to run.
163Otherwise, that behavior is invoked via a prefix argument."
164 :group 'locate
165 :type 'boolean
166 )
167
163;; Functions 168;; Functions
164 169
165(defun locate-default-make-command-line (search-string) 170(defun locate-default-make-command-line (search-string)
166 (list locate-command search-string)) 171 (list locate-command search-string))
167 172
168;;;###autoload 173;;;###autoload
169(defun locate (search-string &optional filter) 174(defun locate (arg search-string &optional filter)
170 "Run the program `locate', putting results in `*Locate*' buffer." 175 "Run the program `locate', putting results in `*Locate*' buffer.
176With prefix arg, prompt for the locate command to run."
171 (interactive 177 (interactive
172 (list (read-from-minibuffer "Locate: " nil nil 178 (list
173 nil 'locate-history-list))) 179 current-prefix-arg
180 (if (or (and current-prefix-arg (not locate-prompt-for-command))
181 (and (not current-prefix-arg) locate-prompt-for-command))
182 (read-from-minibuffer "Run locate command: "
183 nil nil nil 'locate-history-list)
184 (read-from-minibuffer "Locate: " nil nil
185 nil 'locate-history-list)
186 )))
174 (let* ((locate-cmd-list (funcall locate-make-command-line search-string)) 187 (let* ((locate-cmd-list (funcall locate-make-command-line search-string))
175 (locate-cmd (car locate-cmd-list)) 188 (locate-cmd (car locate-cmd-list))
176 (locate-cmd-args (cdr locate-cmd-list)) 189 (locate-cmd-args (cdr locate-cmd-list))
190 (run-locate-command
191 (or (and arg (not locate-prompt-for-command))
192 (and (not arg) locate-prompt-for-command)))
177 ) 193 )
178 194
179 ;; Find the Locate buffer 195 ;; Find the Locate buffer
180 (save-window-excursion 196 (save-window-excursion
181 (set-buffer (get-buffer-create locate-buffer-name)) 197 (set-buffer (get-buffer-create locate-buffer-name))
182 (locate-mode) 198 (locate-mode)
183 (erase-buffer) 199 (erase-buffer)
184 200
185 (setq locate-current-filter filter) 201 (setq locate-current-filter filter)
186 202
187 (apply 'call-process locate-cmd nil t nil locate-cmd-args) 203 (if run-locate-command
204 (shell-command search-string locate-buffer-name)
205 (apply 'call-process locate-cmd nil t nil locate-cmd-args))
206
188 (and filter 207 (and filter
189 (locate-filter-output filter)) 208 (locate-filter-output filter))
190 209
@@ -192,6 +211,8 @@
192 ) 211 )
193 (and (not (string-equal (buffer-name) locate-buffer-name)) 212 (and (not (string-equal (buffer-name) locate-buffer-name))
194 (switch-to-buffer-other-window locate-buffer-name)) 213 (switch-to-buffer-other-window locate-buffer-name))
214
215 (run-hooks 'locate-post-command-hook)
195 ) 216 )
196 ) 217 )
197 218
@@ -202,13 +223,13 @@
202 (list (read-from-minibuffer "Locate: " nil nil 223 (list (read-from-minibuffer "Locate: " nil nil
203 nil 'locate-history-list) 224 nil 'locate-history-list)
204 (read-from-minibuffer "Filter: " nil nil 225 (read-from-minibuffer "Filter: " nil nil
205 nil 'grep-history))) 226 nil 'locate-grep-history-list)))
206 (locate search-string filter)) 227 (locate nil search-string filter))
207 228
208(defun locate-filter-output (filter) 229(defun locate-filter-output (filter)
209 "Filter output from the locate command." 230 "Filter output from the locate command."
210 (goto-char (point-min)) 231 (goto-char (point-min))
211 (delete-non-matching-lines (regexp-quote filter))) 232 (delete-non-matching-lines filter))
212 233
213(defvar locate-mode-map nil 234(defvar locate-mode-map nil
214 "Local keymap for Locate mode buffers.") 235 "Local keymap for Locate mode buffers.")
@@ -228,10 +249,11 @@
228 (define-key locate-mode-map [menu-bar mark directories] 'undefined) 249 (define-key locate-mode-map [menu-bar mark directories] 'undefined)
229 (define-key locate-mode-map [menu-bar mark symlinks] 'undefined) 250 (define-key locate-mode-map [menu-bar mark symlinks] 'undefined)
230 251
231 (define-key locate-mode-map [mouse-2] 'mouse-locate-view-file) 252 (define-key locate-mode-map [mouse-2] 'locate-mouse-view-file)
232 (define-key locate-mode-map "\C-ct" 'locate-tags) 253 (define-key locate-mode-map "\C-c\C-t" 'locate-tags)
233 254
234 (define-key locate-mode-map "U" 'dired-unmark-all-files-no-query) 255 (define-key locate-mode-map "U" 'dired-unmark-all-files)
256 (define-key locate-mode-map "V" 'locate-find-directory)
235) 257)
236 258
237;; This variable is used to indent the lines and then to search for 259;; This variable is used to indent the lines and then to search for
@@ -244,7 +266,7 @@
244 (end-of-line) 266 (end-of-line)
245 (let ((eol (point))) 267 (let ((eol (point)))
246 (beginning-of-line) 268 (beginning-of-line)
247 269
248 ;; Assumes names end at the end of the line 270 ;; Assumes names end at the end of the line
249 (forward-char locate-filename-indentation) 271 (forward-char locate-filename-indentation)
250 (list (point) eol)))) 272 (list (point) eol))))
@@ -260,13 +282,13 @@
260(defun locate-get-filename () 282(defun locate-get-filename ()
261 (let ((pos (locate-get-file-positions)) 283 (let ((pos (locate-get-file-positions))
262 (lineno (locate-current-line-number))) 284 (lineno (locate-current-line-number)))
263 (and (not (eq lineno 1)) 285 (and (not (eq lineno 1))
264 (not (eq lineno 2)) 286 (not (eq lineno 2))
265 (buffer-substring (elt pos 0) (elt pos 1))))) 287 (buffer-substring (elt pos 0) (elt pos 1)))))
266 288
267(defun mouse-locate-view-file (event) 289(defun locate-mouse-view-file (event)
268 "In Locate mode, view a file, using the mouse." 290 "In Locate mode, view a file, using the mouse."
269 (interactive "@e") 291 (interactive "@e")
270 (save-excursion 292 (save-excursion
271 (goto-char (posn-point (event-start event))) 293 (goto-char (posn-point (event-start event)))
272 (view-file (locate-get-filename)))) 294 (view-file (locate-get-filename))))
@@ -277,27 +299,35 @@
277(defun locate-mode () 299(defun locate-mode ()
278 "Major mode for the `*Locate*' buffer made by \\[locate]." 300 "Major mode for the `*Locate*' buffer made by \\[locate]."
279 (kill-all-local-variables) 301 (kill-all-local-variables)
302 ;; Avoid clobbering this variables
303 (make-local-variable 'dired-subdir-alist)
280 (use-local-map locate-mode-map) 304 (use-local-map locate-mode-map)
281 (setq major-mode 'locate-mode 305 (setq major-mode 'locate-mode
282 mode-name "Locate" 306 mode-name "Locate"
283 default-directory "/" 307 default-directory "/")
284 dired-subdir-alist (list (cons "/" (point-min-marker)))) 308 (dired-alist-add-1 default-directory (point-min-marker))
285 (make-local-variable 'dired-move-to-filename-regexp) 309 (make-local-variable 'dired-move-to-filename-regexp)
310 ;; This should support both Unix and Windoze style names
286 (setq dired-move-to-filename-regexp 311 (setq dired-move-to-filename-regexp
287 (make-string locate-filename-indentation ?\ )) 312 (concat "."
313 (make-string (1- locate-filename-indentation) ?\ )
314 "\\(/\\|[A-Za-z]:\\)"))
288 (make-local-variable 'dired-actual-switches) 315 (make-local-variable 'dired-actual-switches)
289 (setq dired-actual-switches "") 316 (setq dired-actual-switches "")
290 (make-local-variable 'dired-permission-flags-regexp) 317 (make-local-variable 'dired-permission-flags-regexp)
291 (setq dired-permission-flags-regexp "^\\( \\)") 318 (setq dired-permission-flags-regexp
319 (concat "^.\\("
320 (make-string (1- locate-filename-indentation) ?\ )
321 "\\)"))
292 (make-local-variable 'revert-buffer-function) 322 (make-local-variable 'revert-buffer-function)
293 (setq revert-buffer-function 'locate-update) 323 (setq revert-buffer-function 'locate-update)
294 (run-hooks 'locate-mode-hook)) 324 (run-hooks 'locate-mode-hook))
295 325
296(defun locate-do-setup () 326(defun locate-do-setup ()
297 (let ((search-string (car locate-history-list))) 327 (let ((search-string (car locate-history-list)))
298 (goto-char (point-min)) 328 (goto-char (point-min))
299 (save-excursion 329 (save-excursion
300 330
301 ;; Nothing returned from locate command? 331 ;; Nothing returned from locate command?
302 (and (eobp) 332 (and (eobp)
303 (progn 333 (progn
@@ -306,9 +336,9 @@
306 (error "Locate: no match for %s in database using filter %s" 336 (error "Locate: no match for %s in database using filter %s"
307 search-string locate-current-filter) 337 search-string locate-current-filter)
308 (error "Locate: no match for %s in database" search-string)))) 338 (error "Locate: no match for %s in database" search-string))))
309 339
310 (locate-insert-header search-string) 340 (locate-insert-header search-string)
311 341
312 (while (not (eobp)) 342 (while (not (eobp))
313 (insert-char ?\ locate-filename-indentation t) 343 (insert-char ?\ locate-filename-indentation t)
314 (locate-set-properties) 344 (locate-set-properties)
@@ -317,8 +347,7 @@
317(defun locate-set-properties () 347(defun locate-set-properties ()
318 (save-excursion 348 (save-excursion
319 (let ((pos (locate-get-file-positions))) 349 (let ((pos (locate-get-file-positions)))
320 (add-text-properties (elt pos 0) (elt pos 1) 350 (dired-insert-set-properties (elt pos 0) (elt pos 1)))))
321 (list 'mouse-face locate-mouse-face)))))
322 351
323(defun locate-insert-header (search-string) 352(defun locate-insert-header (search-string)
324 (let ((locate-format-string "Matches for %s") 353 (let ((locate-format-string "Matches for %s")
@@ -326,7 +355,7 @@
326 (concat " *Matches for \\(" (regexp-quote search-string) "\\)")) 355 (concat " *Matches for \\(" (regexp-quote search-string) "\\)"))
327 (locate-format-args (list search-string)) 356 (locate-format-args (list search-string))
328 ) 357 )
329 358
330 (and locate-fcodes-file 359 (and locate-fcodes-file
331 (setq locate-format-string 360 (setq locate-format-string
332 (concat locate-format-string " in %s") 361 (concat locate-format-string " in %s")
@@ -349,14 +378,14 @@
349 "\\)") 378 "\\)")
350 locate-format-args 379 locate-format-args
351 (append (list locate-current-filter) locate-format-args))) 380 (append (list locate-current-filter) locate-format-args)))
352 381
353 (setq locate-format-string 382 (setq locate-format-string
354 (concat locate-format-string ": \n\n") 383 (concat locate-format-string ": \n\n")
355 locate-regexp-match 384 locate-regexp-match
356 (concat locate-regexp-match ": \n")) 385 (concat locate-regexp-match ": \n"))
357 386
358 (insert (apply 'format locate-format-string (reverse locate-format-args))) 387 (insert (apply 'format locate-format-string (reverse locate-format-args)))
359 388
360 (save-excursion 389 (save-excursion
361 (goto-char (point-min)) 390 (goto-char (point-min))
362 (if (not (looking-at locate-regexp-match)) 391 (if (not (looking-at locate-regexp-match))
@@ -375,7 +404,7 @@
375 "Visit a tags table in `*Locate*' mode." 404 "Visit a tags table in `*Locate*' mode."
376 (interactive) 405 (interactive)
377 (let ((tags-table (locate-get-filename))) 406 (let ((tags-table (locate-get-filename)))
378 (and (y-or-n-p (format "Visit tags table %s? " tags-table)) 407 (and (y-or-n-p (format "Visit tags table %s? " tags-table))
379 (visit-tags-table tags-table)))) 408 (visit-tags-table tags-table))))
380 409
381;; From Stephen Eglen <stephen@cns.ed.ac.uk> 410;; From Stephen Eglen <stephen@cns.ed.ac.uk>
@@ -385,7 +414,73 @@ Database is updated using the shell command in `locate-update-command'."
385 (let ((str (car locate-history-list))) 414 (let ((str (car locate-history-list)))
386 (cond ((yes-or-no-p "Update locate database (may take a few seconds)? ") 415 (cond ((yes-or-no-p "Update locate database (may take a few seconds)? ")
387 (shell-command locate-update-command) 416 (shell-command locate-update-command)
388 (locate str))))) 417 (locate nil str)))))
418
419;;; Modified three functions from `dired.el':
420;;; dired-find-directory,
421;;; dired-find-directory-other-window
422;;; dired-get-filename
423
424(defun locate-find-directory ()
425 "Visit the directory of the file mentioned on this line."
426 (interactive)
427 (let ((directory-name (locate-get-dirname)))
428 (if (file-directory-p directory-name)
429 (find-file directory-name)
430 (if (file-symlink-p directory-name)
431 (error "Directory is a symlink to a nonexistent target")
432 (error "Directory no longer exists; run `updatedb' to update database")))))
433
434(defun locate-find-directory-other-window ()
435 "Visit the directory of the file named on this line in other window."
436 (interactive)
437 (find-file-other-window (locate-get-dirname)))
438
439(defun locate-get-dirname ()
440 "Return the directory name of the file mentioned on this line."
441 (let (file (filepos (locate-get-file-positions)))
442 (if (setq file (buffer-substring (nth 0 filepos) (nth 1 filepos)))
443 (progn
444 ;; Get rid of the mouse-face property that file names have.
445 (set-text-properties 0 (length file) nil file)
446 (setq file (file-name-directory file))
447 ;; Unquote names quoted by ls or by dired-insert-directory.
448 ;; Using read to unquote is much faster than substituting
449 ;; \007 (4 chars) -> ^G (1 char) etc. in a lisp loop.
450 (setq file
451 (read
452 (concat "\""
453 ;; some ls -b don't escape quotes, argh!
454 ;; This is not needed for GNU ls, though.
455 (or (dired-string-replace-match
456 "\\([^\\]\\|\\`\\)\"" file "\\1\\\\\"" nil t)
457 file)
458 "\"")))))
459 (and file buffer-file-coding-system
460 (not file-name-coding-system)
461 (setq file (encode-coding-string file buffer-file-coding-system)))
462 file))
463
464;; Only for GNU locate
465(defun locate-in-alternate-database (search-string database)
466 "Run the GNU locate command, using an alternate database."
467 (interactive
468 (list
469 (progn
470 ;; (require 'locate)
471 (read-from-minibuffer "Locate: " nil nil
472 nil 'locate-history-list))
473 (read-file-name "Locate using Database: " )
474 ))
475 (or (file-exists-p database)
476 (error "Database file %s does not exist" database))
477 (let ((locate-make-command-line
478 (function (lambda (string)
479 (cons locate-command
480 (list (concat "--database="
481 (expand-file-name database))
482 string))))))
483 (locate nil search-string)))
389 484
390(provide 'locate) 485(provide 'locate)
391 486