comparison lisp/locate.el @ 90813:e6fdae9180d4

Merge from emacs--devo--0 Patches applied: * emacs--devo--0 (patch 698-710) - Update from CVS - Merge from gnus--rel--5.10 * gnus--rel--5.10 (patch 216) - Update from CVS Revision: emacs@sv.gnu.org/emacs--unicode--0--patch-196
author Miles Bader <miles@gnu.org>
date Tue, 24 Apr 2007 21:56:25 +0000
parents 95d0cdf160ea 84802af12767
children 92c344270c8a
comparison
equal deleted inserted replaced
90812:6137cc8ddf90 90813:e6fdae9180d4
112 (eval-when-compile 112 (eval-when-compile
113 (require 'dired)) 113 (require 'dired))
114 114
115 ;; Variables 115 ;; Variables
116 116
117 (defvar locate-current-search nil)
118 (defvar locate-current-filter nil) 117 (defvar locate-current-filter nil)
118 (defvar locate-local-filter nil)
119 (defvar locate-local-search nil)
120 (defvar locate-local-prompt nil)
119 121
120 (defgroup locate nil 122 (defgroup locate nil
121 "Interface to the locate command." 123 "Interface to the locate command."
122 :prefix "locate-" 124 :prefix "locate-"
123 :group 'external) 125 :group 'external)
218 :group 'locate 220 :group 'locate
219 :version "22.1") 221 :version "22.1")
220 222
221 (defcustom locate-prompt-for-command nil 223 (defcustom locate-prompt-for-command nil
222 "If non-nil, the `locate' command prompts for a command to run. 224 "If non-nil, the `locate' command prompts for a command to run.
223 Otherwise, that behavior is invoked via a prefix argument." 225 Otherwise, that behavior is invoked via a prefix argument.
226
227 Setting this option non-nil actually inverts the meaning of a prefix arg;
228 that is, with a prefix arg, you get the default behavior."
224 :group 'locate 229 :group 'locate
225 :type 'boolean) 230 :type 'boolean)
226 231
227 ;; Functions 232 ;; Functions
228 233
238 (save-excursion 243 (save-excursion
239 (skip-chars-forward "-a-zA-Z0-9.") 244 (skip-chars-forward "-a-zA-Z0-9.")
240 (skip-chars-backward "." pt) 245 (skip-chars-backward "." pt)
241 (point))))) 246 (point)))))
242 247
248 ;; Function for use in interactive declarations.
249 (defun locate-prompt-for-search-string ()
250 (if (or (and current-prefix-arg
251 (not locate-prompt-for-command))
252 (and (not current-prefix-arg) locate-prompt-for-command))
253 (let ((locate-cmd (funcall locate-make-command-line "")))
254 (read-from-minibuffer
255 "Run locate (like this): "
256 (cons
257 (concat (car locate-cmd) " "
258 (mapconcat 'identity (cdr locate-cmd) " "))
259 (+ 2 (length (car locate-cmd))))
260 nil nil 'locate-history-list))
261 (let* ((default (locate-word-at-point))
262 (input
263 (read-from-minibuffer
264 (if (> (length default) 0)
265 (format "Locate (default %s): " default)
266 (format "Locate: "))
267 nil nil nil 'locate-history-list default t)))
268 (and (equal input "") default
269 (setq input default))
270 input)))
271
243 ;;;###autoload 272 ;;;###autoload
244 (defun locate (search-string &optional filter) 273 (defun locate (search-string &optional filter arg)
245 "Run the program `locate', putting results in `*Locate*' buffer. 274 "Run the program `locate', putting results in `*Locate*' buffer.
246 Pass it SEARCH-STRING as argument. Interactively, prompt for SEARCH-STRING. 275 Pass it SEARCH-STRING as argument. Interactively, prompt for SEARCH-STRING.
247 With prefix arg, prompt for the exact shell command to run instead. 276 With prefix arg, prompt for the exact shell command to run instead.
248 277
249 This program searches for those file names in a database that match 278 This program searches for those file names in a database that match
256 285
257 You can specify another program for this command to run by customizing 286 You can specify another program for this command to run by customizing
258 the variables `locate-command' or `locate-make-command-line'. 287 the variables `locate-command' or `locate-make-command-line'.
259 288
260 The main use of FILTER is to implement `locate-with-filter'. See 289 The main use of FILTER is to implement `locate-with-filter'. See
261 the docstring of that function for its meaning." 290 the docstring of that function for its meaning.
291
292 ARG is the interactive prefix arg."
262 (interactive 293 (interactive
263 (list 294 (list
264 (if (or (and current-prefix-arg 295 (locate-prompt-for-search-string)
265 (not locate-prompt-for-command)) 296 nil
266 (and (not current-prefix-arg) locate-prompt-for-command)) 297 current-prefix-arg))
267 (let ((locate-cmd (funcall locate-make-command-line ""))) 298
268 (read-from-minibuffer
269 "Run locate (like this): "
270 (cons
271 (concat (car locate-cmd) " "
272 (mapconcat 'identity (cdr locate-cmd) " "))
273 (+ 2 (length (car locate-cmd))))
274 nil nil 'locate-history-list))
275 (let* ((default (locate-word-at-point))
276 (input
277 (read-from-minibuffer
278 (if (> (length default) 0)
279 (format "Locate (default %s): " default)
280 (format "Locate: "))
281 nil nil nil 'locate-history-list default t)))
282 (and (equal input "") default
283 (setq input default))
284 input))))
285 (if (equal search-string "") 299 (if (equal search-string "")
286 (error "Please specify a filename to search for")) 300 (error "Please specify a filename to search for"))
287 (let* ((locate-cmd-list (funcall locate-make-command-line search-string)) 301 (let* ((locate-cmd-list (funcall locate-make-command-line search-string))
288 (locate-cmd (car locate-cmd-list)) 302 (locate-cmd (car locate-cmd-list))
289 (locate-cmd-args (cdr locate-cmd-list)) 303 (locate-cmd-args (cdr locate-cmd-list))
290 (run-locate-command 304 (run-locate-command
291 (or (and current-prefix-arg (not locate-prompt-for-command)) 305 (or (and arg (not locate-prompt-for-command))
292 (and (not current-prefix-arg) locate-prompt-for-command))) 306 (and (not arg) locate-prompt-for-command)))
293 locate-buffer
294 ) 307 )
295 308
296 ;; Find the Locate buffer 309 ;; Find the Locate buffer
297 (setq locate-buffer (if (eq major-mode 'locate-mode) 310 (save-window-excursion
298 (current-buffer) 311 (set-buffer (get-buffer-create locate-buffer-name))
299 (get-buffer-create locate-buffer-name)))
300
301 (save-excursion
302 (set-buffer locate-buffer)
303 (locate-mode) 312 (locate-mode)
304
305 (let ((inhibit-read-only t) 313 (let ((inhibit-read-only t)
306 (buffer-undo-list t)) 314 (buffer-undo-list t))
307 (erase-buffer) 315 (erase-buffer)
308 316
309 (set (make-local-variable 'locate-current-search) search-string) 317 (setq locate-current-filter filter)
310 (set (make-local-variable 'locate-current-filter) filter) 318 (set (make-local-variable 'locate-local-search) search-string)
311 319 (set (make-local-variable 'locate-local-filter) filter)
312 (if run-locate-command 320 (set (make-local-variable 'locate-local-prompt) run-locate-command)
313 (shell-command search-string) 321
314 (apply 'call-process locate-cmd nil t nil locate-cmd-args)) 322 (if run-locate-command
315 323 (shell-command search-string locate-buffer-name)
316 (and filter 324 (apply 'call-process locate-cmd nil t nil locate-cmd-args))
317 (locate-filter-output filter)) 325
318 326 (and filter
319 (locate-do-setup search-string))) 327 (locate-filter-output filter))
320 328
321 (unless (eq (current-buffer) locate-buffer) 329 (locate-do-setup search-string)
322 (switch-to-buffer-other-window locate-buffer)) 330 ))
331 (and (not (string-equal (buffer-name) locate-buffer-name))
332 (switch-to-buffer-other-window locate-buffer-name))
323 333
324 (run-hooks 'dired-mode-hook) 334 (run-hooks 'dired-mode-hook)
325 (dired-next-line 3) ;move to first matching file. 335 (dired-next-line 3) ;move to first matching file.
326 (run-hooks 'locate-post-command-hook) 336 (run-hooks 'locate-post-command-hook)
327 ) 337 )
328 ) 338 )
329 339
330 ;;;###autoload 340 ;;;###autoload
331 (defun locate-with-filter (search-string filter) 341 (defun locate-with-filter (search-string filter &optional arg)
332 "Run the executable program `locate' with a filter. 342 "Run the executable program `locate' with a filter.
333 This function is similar to the function `locate', which see. 343 This function is similar to the function `locate', which see.
334 The difference is that, when invoked interactively, the present function 344 The difference is that, when invoked interactively, the present function
335 prompts for both SEARCH-STRING and FILTER. It passes SEARCH-STRING 345 prompts for both SEARCH-STRING and FILTER. It passes SEARCH-STRING
336 to the locate executable program. It produces a `*Locate*' buffer 346 to the locate executable program. It produces a `*Locate*' buffer
337 that lists only those lines in the output of the locate program that 347 that lists only those lines in the output of the locate program that
338 contain a match for the regular expression FILTER; this is often useful 348 contain a match for the regular expression FILTER; this is often useful
339 to constrain a big search. 349 to constrain a big search.
340 350
351 ARG is the interactive prefix arg, which has the same effect as in `locate'.
352
341 When called from Lisp, this function is identical with `locate', 353 When called from Lisp, this function is identical with `locate',
342 except that FILTER is not optional." 354 except that FILTER is not optional."
343 (interactive 355 (interactive
344 (list (read-from-minibuffer "Locate: " nil nil 356 (list
345 nil 'locate-history-list) 357 (locate-prompt-for-search-string)
346 (read-from-minibuffer "Filter: " nil nil 358 (read-from-minibuffer "Filter: " nil nil
347 nil 'locate-grep-history-list))) 359 nil 'locate-grep-history-list)
348 (locate search-string filter)) 360 current-prefix-arg))
361 (locate search-string filter arg))
349 362
350 (defun locate-filter-output (filter) 363 (defun locate-filter-output (filter)
351 "Filter output from the locate command." 364 "Filter output from the locate command."
352 (goto-char (point-min)) 365 (goto-char (point-min))
353 (keep-lines filter)) 366 (keep-lines filter))
467 (setq major-mode 'locate-mode 480 (setq major-mode 'locate-mode
468 mode-name "Locate" 481 mode-name "Locate"
469 default-directory "/" 482 default-directory "/"
470 buffer-read-only t 483 buffer-read-only t
471 selective-display t) 484 selective-display t)
472 (buffer-disable-undo)
473 (dired-alist-add-1 default-directory (point-min-marker)) 485 (dired-alist-add-1 default-directory (point-min-marker))
474 (set (make-local-variable 'dired-directory) "/") 486 (set (make-local-variable 'dired-directory) "/")
475 (set (make-local-variable 'dired-subdir-switches) locate-ls-subdir-switches) 487 (set (make-local-variable 'dired-subdir-switches) locate-ls-subdir-switches)
476 (setq dired-switches-alist nil) 488 (setq dired-switches-alist nil)
477 (make-local-variable 'directory-listing-before-filename-regexp) 489 (make-local-variable 'directory-listing-before-filename-regexp)
499 (save-excursion 511 (save-excursion
500 512
501 ;; Nothing returned from locate command? 513 ;; Nothing returned from locate command?
502 (and (eobp) 514 (and (eobp)
503 (progn 515 (progn
504 (let ((filter locate-current-filter)) ; local 516 (kill-buffer locate-buffer-name)
505 (kill-buffer (current-buffer)) 517 (if locate-current-filter
506 (if filter 518 (error "Locate: no match for %s in database using filter %s"
507 (error "Locate: no match for %s in database using filter %s" 519 search-string locate-current-filter)
508 search-string filter) 520 (error "Locate: no match for %s in database" search-string))))
509 (error "Locate: no match for %s in database" search-string)))))
510 521
511 (locate-insert-header search-string) 522 (locate-insert-header search-string)
512 523
513 (while (not (eobp)) 524 (while (not (eobp))
514 (insert-char ?\s locate-filename-indentation t) 525 (insert-char ?\s locate-filename-indentation t)
588 ;; From Stephen Eglen <stephen@cns.ed.ac.uk> 599 ;; From Stephen Eglen <stephen@cns.ed.ac.uk>
589 (defun locate-update (ignore1 ignore2) 600 (defun locate-update (ignore1 ignore2)
590 "Revert the *Locate* buffer. 601 "Revert the *Locate* buffer.
591 If `locate-update-when-revert' is non-nil, offer to update the 602 If `locate-update-when-revert' is non-nil, offer to update the
592 locate database using the shell command in `locate-update-command'." 603 locate database using the shell command in `locate-update-command'."
593 (and locate-update-when-revert 604 (let ((locate-buffer-name (buffer-name))
594 (yes-or-no-p "Update locate database (may take a few seconds)? ") 605 (locate-prompt-for-command locate-local-prompt))
595 ;; `expand-file-name' is used in order to autoload Tramp if 606 (and locate-update-when-revert
596 ;; necessary. It cannot be loaded when `default-directory' 607 (yes-or-no-p "Update locate database (may take a few seconds)? ")
597 ;; is remote. 608 ;; `expand-file-name' is used in order to autoload Tramp if
598 (let ((default-directory (expand-file-name locate-update-path))) 609 ;; necessary. It cannot be loaded when `default-directory'
599 (shell-command locate-update-command))) 610 ;; is remote.
600 (locate locate-current-search locate-current-filter)) 611 (let ((default-directory (expand-file-name locate-update-path)))
612 (shell-command locate-update-command)))
613 (locate locate-local-search locate-local-filter)))
601 614
602 ;;; Modified three functions from `dired.el': 615 ;;; Modified three functions from `dired.el':
603 ;;; dired-find-directory, 616 ;;; dired-find-directory,
604 ;;; dired-find-directory-other-window 617 ;;; dired-find-directory-other-window
605 ;;; dired-get-filename 618 ;;; dired-get-filename