1 ;;; locate.el --- interface to the locate command
3 ;; Copyright (C) 1996, 1998, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Peter Breton <pbreton@cs.umb.edu>
7 ;; Keywords: unix files
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; Search a database of files and use dired commands on the result.
30 ;; Locate.el provides an interface to a program which searches a
31 ;; database of file names. By default, this program is the GNU locate
32 ;; command, but it could also be the BSD-style find command, or even a
33 ;; user specified command.
35 ;; To use the BSD-style "fast find", or any other shell command of the
38 ;; SHELLPROGRAM Name-to-find
40 ;; set the variable `locate-command' in your .emacs file.
42 ;; To use a more complicated expression, create a function which
43 ;; takes a string (the name to find) as input and returns a list.
44 ;; The first element should be the command to be executed, the remaining
45 ;; elements should be the arguments (including the name to find). Then put
47 ;; (setq locate-make-command-line 'my-locate-command-line)
49 ;; in your .emacs, using the name of your function in place of
50 ;; my-locate-command-line.
52 ;; You should make sure that whichever command you use works correctly
53 ;; from a shell prompt. GNU locate and BSD find expect the file databases
54 ;; to either be in standard places or located via environment variables.
55 ;; If the latter, make sure these environment variables are set in
56 ;; your emacs process.
58 ;; Locate-mode assumes that each line output from the locate-command
59 ;; consists exactly of a file name, possibly preceded or trailed by
60 ;; whitespace. If your file database has other information on the line (for
61 ;; example, the file size), you will need to redefine the function
62 ;; `locate-get-file-positions' to return a list consisting of the first
63 ;; character in the file name and the last character in the file name.
65 ;; To use locate-mode, simply type M-x locate and then the string
66 ;; you wish to find. You can use almost all of the dired commands in
67 ;; the resulting *Locate* buffer. It is worth noting that your commands
68 ;; do not, of course, affect the file database. For example, if you
69 ;; compress a file in the locate buffer, the actual file will be
70 ;; compressed, but the entry in the file database will not be
71 ;; affected. Consequently, the database and the filesystem will be out
72 ;; of sync until the next time the database is updated.
74 ;; The command `locate-with-filter' keeps only lines matching a
75 ;; regular expression; this is often useful to constrain a big search.
78 ;;;;; Building a database of files ;;;;;;;;;
80 ;; You can create a simple files database with a port of the Unix find command
81 ;; and one of the various Windows NT various scheduling utilities,
82 ;; for example the AT command from the NT Resource Kit, WinCron which is
83 ;; included with Microsoft FrontPage, or the shareware NTCron program.
85 ;; To set up a function which searches the files database, do something
88 ;; (defvar locate-fcodes-file "c:/users/peter/fcodes")
89 ;; (defvar locate-make-command-line 'nt-locate-make-command-line)
91 ;; (defun nt-locate-make-command-line (arg)
92 ;; (list "grep" "-i" arg locate-fcodes-file))
94 ;;;;;;;; ADVICE For dired-make-relative: ;;;;;;;;;
96 ;; For certain dired commands to work right, you should also include the
97 ;; following in your _emacs/.emacs:
99 ;; (defadvice dired-make-relative (before set-no-error activate)
100 ;; "For locate mode and Windows, don't return errors"
101 ;; (if (and (eq major-mode 'locate-mode)
102 ;; (memq system-type (list 'windows-nt 'ms-dos)))
106 ;; Otherwise, `dired-make-relative' will give error messages like
107 ;; "FILENAME: not in directory tree growing at /"
117 (defvar locate-current-search nil
)
118 (defvar locate-current-filter nil
)
121 "Interface to the locate command."
125 (defcustom locate-command
"locate"
126 "Executable program for searching a database of files.
127 The Emacs commands `locate' and `locate-with-filter' use this.
128 The value should be a program that can be called from a shell
129 with one argument, SEARCH-STRING. The program determines which
130 database it searches. The output of the program should consist
131 of those file names in the database that match SEARCH-STRING,
132 listed one per line, possibly with leading or trailing
133 whitespace. If the output is in another form, you may have to
134 redefine the function `locate-get-file-positions'.
136 The program may interpret SEARCH-STRING as a literal string, a
137 shell pattern or a regular expression. The exact rules of what
138 constitutes a match may also depend on the program.
140 The standard value of this variable is \"locate\".
141 This program normally searches a database of all files on your
142 system, or of all files that you have access to. Consult the
143 documentation of that program for the details about how it determines
144 which file names match SEARCH-STRING. (Those details vary highly with
149 (defvar locate-history-list nil
150 "The history list used by the \\[locate] command.")
152 (defvar locate-grep-history-list nil
153 "The history list used by the \\[locate-with-filter] command.")
155 (defcustom locate-make-command-line
'locate-default-make-command-line
156 "Function used to create the locate command line.
157 The Emacs commands `locate' and `locate-with-filter' use this.
158 This function should take one argument, a string (the name to find)
159 and return a list of strings. The first element of the list should be
160 the name of a command to be executed by a shell, the remaining elements
161 should be the arguments to that command (including the name to find)."
165 (defcustom locate-buffer-name
"*Locate*"
166 "Name of the buffer to show results from the \\[locate] command."
170 (defcustom locate-fcodes-file nil
171 "File name for the database of file names used by `locate'.
172 If non-nil, `locate' uses this name in the header of the `*Locate*'
173 buffer. If nil, it mentions no file name in that header.
175 Just setting this variable does not actually change the database
176 that `locate' searches. The executive program that the Emacs
177 function `locate' uses, as given by the variables `locate-command'
178 or `locate-make-command-line', determines the database."
179 :type
'(choice (const :tag
"None" nil
) file
)
182 (defcustom locate-header-face nil
183 "Face used to highlight the locate header."
184 :type
'(choice (const :tag
"None" nil
) face
)
188 (defcustom locate-ls-subdir-switches
"-al"
189 "`ls' switches for inserting subdirectories in `*Locate*' buffers.
190 This should contain the \"-l\" switch, but not the \"-F\" or \"-b\" switches."
195 (defcustom locate-update-when-revert nil
196 "This option affects how the *Locate* buffer gets reverted.
197 If non-nil, offer to update the locate database when reverting that buffer.
198 \(Normally, you need to have root privileges for this to work. See the
199 option `locate-update-path'.)
200 If nil, reverting does not update the locate database."
205 (defcustom locate-update-command
"updatedb"
206 "The executable program used to update the locate database."
210 (defcustom locate-update-path
"/"
211 "The default directory from where `locate-update-command' is called.
212 Usually, root permissions are required to run that command. This
213 can be achieved by setting this option to \"/su::\" or \"/sudo::\"
214 \(if you have the appropriate authority). If your current user
215 permissions are sufficient to run the command, you can set this
221 (defcustom locate-prompt-for-command nil
222 "If non-nil, the `locate' command prompts for a command to run.
223 Otherwise, that behavior is invoked via a prefix argument."
229 (defun locate-default-make-command-line (search-string)
230 (list locate-command search-string
))
232 (defun locate-word-at-point ()
234 (buffer-substring-no-properties
236 (skip-chars-backward "-a-zA-Z0-9.")
239 (skip-chars-forward "-a-zA-Z0-9.")
240 (skip-chars-backward "." pt
)
244 (defun locate (search-string &optional filter
)
245 "Run the program `locate', putting results in `*Locate*' buffer.
246 Pass it SEARCH-STRING as argument. Interactively, prompt for SEARCH-STRING.
247 With prefix arg, prompt for the exact shell command to run instead.
249 This program searches for those file names in a database that match
250 SEARCH-STRING and normally outputs all matching absolute file names,
251 one per line. The database normally consists of all files on your
252 system, or of all files that you have access to. Consult the
253 documentation of the program for the details about how it determines
254 which file names match SEARCH-STRING. (Those details vary highly with
257 You can specify another program for this command to run by customizing
258 the variables `locate-command' or `locate-make-command-line'.
260 The main use of FILTER is to implement `locate-with-filter'. See
261 the docstring of that function for its meaning."
264 (if (or (and current-prefix-arg
265 (not locate-prompt-for-command
))
266 (and (not current-prefix-arg
) locate-prompt-for-command
))
267 (let ((locate-cmd (funcall locate-make-command-line
"")))
268 (read-from-minibuffer
269 "Run locate (like this): "
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))
277 (read-from-minibuffer
278 (if (> (length default
) 0)
279 (format "Locate (default %s): " default
)
281 nil nil nil
'locate-history-list default t
)))
282 (and (equal input
"") default
283 (setq input default
))
285 (if (equal search-string
"")
286 (error "Please specify a filename to search for"))
287 (let* ((locate-cmd-list (funcall locate-make-command-line search-string
))
288 (locate-cmd (car locate-cmd-list
))
289 (locate-cmd-args (cdr locate-cmd-list
))
291 (or (and current-prefix-arg
(not locate-prompt-for-command
))
292 (and (not current-prefix-arg
) locate-prompt-for-command
)))
296 ;; Find the Locate buffer
297 (setq locate-buffer
(if (eq major-mode
'locate-mode
)
299 (get-buffer-create locate-buffer-name
)))
302 (set-buffer locate-buffer
)
305 (let ((inhibit-read-only t
)
306 (buffer-undo-list t
))
309 (set (make-local-variable 'locate-current-search
) search-string
)
310 (set (make-local-variable 'locate-current-filter
) filter
)
312 (if run-locate-command
313 (shell-command search-string
)
314 (apply 'call-process locate-cmd nil t nil locate-cmd-args
))
317 (locate-filter-output filter
))
319 (locate-do-setup search-string
)))
321 (unless (eq (current-buffer) locate-buffer
)
322 (switch-to-buffer-other-window locate-buffer
))
324 (run-hooks 'dired-mode-hook
)
325 (dired-next-line 3) ;move to first matching file.
326 (run-hooks 'locate-post-command-hook
)
331 (defun locate-with-filter (search-string filter
)
332 "Run the executable program `locate' with a filter.
333 This function is similar to the function `locate', which see.
334 The difference is that, when invoked interactively, the present function
335 prompts for both SEARCH-STRING and FILTER. It passes SEARCH-STRING
336 to the locate executable program. It produces a `*Locate*' buffer
337 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
339 to constrain a big search.
341 When called from Lisp, this function is identical with `locate',
342 except that FILTER is not optional."
344 (list (read-from-minibuffer "Locate: " nil nil
345 nil
'locate-history-list
)
346 (read-from-minibuffer "Filter: " nil nil
347 nil
'locate-grep-history-list
)))
348 (locate search-string filter
))
350 (defun locate-filter-output (filter)
351 "Filter output from the locate command."
352 (goto-char (point-min))
355 (defvar locate-mode-map nil
356 "Local keymap for Locate mode buffers.")
362 (setq locate-mode-map
(copy-keymap dired-mode-map
))
364 ;; Undefine Useless Dired Menu bars
365 (define-key locate-mode-map
[menu-bar Dired
] 'undefined
)
366 (define-key locate-mode-map
[menu-bar subdir
] 'undefined
)
368 (define-key locate-mode-map
[menu-bar mark executables
] 'undefined
)
369 (define-key locate-mode-map
[menu-bar mark directory
] 'undefined
)
370 (define-key locate-mode-map
[menu-bar mark directories
] 'undefined
)
371 (define-key locate-mode-map
[menu-bar mark symlinks
] 'undefined
)
373 (define-key locate-mode-map
[M-mouse-2
] 'locate-mouse-view-file
)
374 (define-key locate-mode-map
"\C-c\C-t" 'locate-tags
)
376 (define-key locate-mode-map
"l" 'locate-do-redisplay
)
377 (define-key locate-mode-map
"U" 'dired-unmark-all-files
)
378 (define-key locate-mode-map
"V" 'locate-find-directory
)
381 ;; This variable is used to indent the lines and then to search for
383 (defconst locate-filename-indentation
4
384 "The amount of indentation for each file.")
386 (defun locate-get-file-positions ()
387 "Return list of start and end of the file name on the current line.
388 This is a list of two buffer positions.
390 You should only call this function on lines that contain a file name
391 listed by the locate program. Inside inserted subdirectories, or if
392 there is no file name on the current line, the return value is
393 meaningless. You can check whether the current line contains a file
394 listed by the locate program, using the function
395 `locate-main-listing-line-p'."
401 ;; Assumes names end at the end of the line
402 (forward-char locate-filename-indentation
)
403 (list (point) eol
))))
406 (defun locate-current-line-number ()
407 "Return the current line number, as an integer."
408 (+ (count-lines (point-min) (point))
409 (if (eq (current-column) 0)
413 ;; You should only call this function on lines that contain a file name
414 ;; listed by the locate program. Inside inserted subdirectories, or if
415 ;; there is no file name on the current line, the return value is
416 ;; meaningless. You can check whether the current line contains a file
417 ;; listed by the locate program, using the function
418 ;; `locate-main-listing-line-p'.
419 (defun locate-get-filename ()
420 (let ((pos (locate-get-file-positions))
421 (lineno (locate-current-line-number)))
422 (and (not (eq lineno
1))
424 (buffer-substring (elt pos
0) (elt pos
1)))))
426 (defun locate-main-listing-line-p ()
427 "Return t if current line contains a file name listed by locate.
428 This function returns nil if the current line either contains no
429 file name or is inside a subdirectory."
432 (looking-at (concat "."
433 (make-string (1- locate-filename-indentation
) ?\
)
434 "\\(/\\|[A-Za-z]:\\)"))))
436 (defun locate-mouse-view-file (event)
437 "In Locate mode, view a file, using the mouse."
440 (goto-char (posn-point (event-start event
)))
441 (if (locate-main-listing-line-p)
442 (view-file (locate-get-filename))
443 (message "This command only works inside main listing."))))
445 ;; Define a mode for locate
446 ;; Default directory is set to "/" so that dired commands, which
447 ;; expect to be in a tree, will work properly
448 (defun locate-mode ()
449 "Major mode for the `*Locate*' buffer made by \\[locate].
451 In that buffer, you can use almost all the usual dired bindings.
452 \\[locate-find-directory] visits the directory of the file on the current line.
454 Operating on listed files works, but does not always
455 automatically update the buffer as in ordinary Dired.
456 This is true both for the main listing and for subdirectories.
457 Reverting the buffer using \\[revert-buffer] deletes all subdirectories.
458 Specific `locate-mode' commands, such as \\[locate-find-directory],
459 do not work in subdirectories.
462 ;; Not to be called interactively.
463 (kill-all-local-variables)
464 ;; Avoid clobbering this variable
465 (make-local-variable 'dired-subdir-alist
)
466 (use-local-map locate-mode-map
)
467 (setq major-mode
'locate-mode
469 default-directory
"/"
472 (buffer-disable-undo)
473 (dired-alist-add-1 default-directory
(point-min-marker))
474 (set (make-local-variable 'dired-directory
) "/")
475 (set (make-local-variable 'dired-subdir-switches
) locate-ls-subdir-switches
)
476 (setq dired-switches-alist nil
)
477 (make-local-variable 'directory-listing-before-filename-regexp
)
478 ;; This should support both Unix and Windoze style names
479 (setq directory-listing-before-filename-regexp
481 (make-string (1- locate-filename-indentation
) ?\
)
482 "\\(/\\|[A-Za-z]:\\)\\|"
483 (default-value 'directory-listing-before-filename-regexp
)))
484 (make-local-variable 'dired-actual-switches
)
485 (setq dired-actual-switches
"")
486 (make-local-variable 'dired-permission-flags-regexp
)
487 (setq dired-permission-flags-regexp
489 (make-string (1- locate-filename-indentation
) ?\
)
491 (default-value 'dired-permission-flags-regexp
)))
492 (make-local-variable 'revert-buffer-function
)
493 (setq revert-buffer-function
'locate-update
)
494 (set (make-local-variable 'page-delimiter
) "\n\n")
495 (run-mode-hooks 'locate-mode-hook
))
497 (defun locate-do-setup (search-string)
498 (goto-char (point-min))
501 ;; Nothing returned from locate command?
504 (let ((filter locate-current-filter
)) ; local
505 (kill-buffer (current-buffer))
507 (error "Locate: no match for %s in database using filter %s"
508 search-string filter
)
509 (error "Locate: no match for %s in database" search-string
)))))
511 (locate-insert-header search-string
)
514 (insert-char ?\ locate-filename-indentation t
)
515 (locate-set-properties)
517 (goto-char (point-min)))
519 (defun locate-set-properties ()
521 (let ((pos (locate-get-file-positions)))
522 (dired-insert-set-properties (elt pos
0) (elt pos
1)))))
524 (defun locate-insert-header (search-string)
525 ;; There needs to be a space before `Matches, because otherwise,
526 ;; `*!" would erase the `M'. We can not use two spaces, or the line
527 ;; would mistakenly fit `dired-subdir-regexp'.
528 (let ((locate-format-string " /:\n Matches for %s")
530 (concat " *Matches for \\(" (regexp-quote search-string
) "\\)"))
531 (locate-format-args (list search-string
))
534 (and locate-fcodes-file
535 (setq locate-format-string
536 (concat locate-format-string
" in %s")
538 (concat locate-regexp-match
540 (regexp-quote locate-fcodes-file
)
543 (append (list locate-fcodes-file
) locate-format-args
)))
545 (and locate-current-filter
546 (setq locate-format-string
547 (concat locate-format-string
" using filter %s")
549 (concat locate-regexp-match
552 (regexp-quote locate-current-filter
)
555 (append (list locate-current-filter
) locate-format-args
)))
557 (setq locate-format-string
558 (concat locate-format-string
":\n\n")
560 (concat locate-regexp-match
":\n"))
562 (insert (apply 'format locate-format-string
(reverse locate-format-args
)))
565 (goto-char (point-min))
567 (if (not (looking-at locate-regexp-match
))
569 (add-text-properties (match-beginning 1) (match-end 1)
570 (list 'face locate-header-face
))
571 (and (match-beginning 2)
572 (add-text-properties (match-beginning 2) (match-end 2)
573 (list 'face locate-header-face
)))
574 (and (match-beginning 3)
575 (add-text-properties (match-beginning 3) (match-end 3)
576 (list 'face locate-header-face
)))
579 (defun locate-tags ()
580 "Visit a tags table in `*Locate*' mode."
582 (if (locate-main-listing-line-p)
583 (let ((tags-table (locate-get-filename)))
584 (and (y-or-n-p (format "Visit tags table %s? " tags-table
))
585 (visit-tags-table tags-table
)))
586 (message "This command only works inside main listing.")))
588 ;; From Stephen Eglen <stephen@cns.ed.ac.uk>
589 (defun locate-update (ignore1 ignore2
)
590 "Revert the *Locate* buffer.
591 If `locate-update-when-revert' is non-nil, offer to update the
592 locate database using the shell command in `locate-update-command'."
593 (and locate-update-when-revert
594 (yes-or-no-p "Update locate database (may take a few seconds)? ")
595 ;; `expand-file-name' is used in order to autoload Tramp if
596 ;; necessary. It cannot be loaded when `default-directory'
598 (let ((default-directory (expand-file-name locate-update-path
)))
599 (shell-command locate-update-command
)))
600 (locate locate-current-search locate-current-filter
))
602 ;;; Modified three functions from `dired.el':
603 ;;; dired-find-directory,
604 ;;; dired-find-directory-other-window
605 ;;; dired-get-filename
607 (defun locate-find-directory ()
608 "Visit the directory of the file mentioned on this line."
610 (if (locate-main-listing-line-p)
611 (let ((directory-name (locate-get-dirname)))
612 (if (file-directory-p directory-name
)
613 (find-file directory-name
)
614 (if (file-symlink-p directory-name
)
615 (error "Directory is a symlink to a nonexistent target")
616 (error "Directory no longer exists; run `updatedb' to update database"))))
617 (message "This command only works inside main listing.")))
619 (defun locate-find-directory-other-window ()
620 "Visit the directory of the file named on this line in other window."
622 (if (locate-main-listing-line-p)
623 (find-file-other-window (locate-get-dirname))
624 (message "This command only works inside main listing.")))
626 ;; You should only call this function on lines that contain a file name
627 ;; listed by the locate program. Inside inserted subdirectories, or if
628 ;; there is no file name on the current line, the return value is
629 ;; meaningless. You can check whether the current line contains a file
630 ;; listed by the locate program, using the function
631 ;; `locate-main-listing-line-p'.
632 (defun locate-get-dirname ()
633 "Return the directory name of the file mentioned on this line."
634 (let (file (filepos (locate-get-file-positions)))
635 (if (setq file
(buffer-substring (nth 0 filepos
) (nth 1 filepos
)))
637 ;; Get rid of the mouse-face property that file names have.
638 (set-text-properties 0 (length file
) nil file
)
639 (setq file
(file-name-directory file
))
640 ;; Unquote names quoted by ls or by dired-insert-directory.
641 ;; Using read to unquote is much faster than substituting
642 ;; \007 (4 chars) -> ^G (1 char) etc. in a lisp loop.
646 ;; some ls -b don't escape quotes, argh!
647 ;; This is not needed for GNU ls, though.
648 (or (dired-string-replace-match
649 "\\([^\\]\\|\\`\\)\"" file
"\\1\\\\\"" nil t
)
652 (and file buffer-file-coding-system
653 (not file-name-coding-system
)
654 (setq file
(encode-coding-string file buffer-file-coding-system
)))
657 ;; Only for GNU locate
658 (defun locate-in-alternate-database (search-string database
)
659 "Run the GNU locate command, using an alternate database."
664 (read-from-minibuffer "Locate: " nil nil
665 nil
'locate-history-list
))
666 (read-file-name "Locate using Database: " )
668 (or (file-exists-p database
)
669 (error "Database file %s does not exist" database
))
670 (let ((locate-make-command-line
671 (function (lambda (string)
673 (list (concat "--database="
674 (expand-file-name database
))
676 (locate search-string
)))
678 (defun locate-do-redisplay (&optional arg test-for-subdir
)
679 "Like `dired-do-redisplay', but adapted for `*Locate*' buffers."
681 (if (string= (dired-current-directory) "/")
682 (message "This command only works in subdirectories.")
683 (let ((dired-actual-switches locate-ls-subdir-switches
))
684 (dired-do-redisplay arg test-for-subdir
))))
688 ;;; arch-tag: 60c4d098-b5d5-4b3c-a3e0-51a2e9f43898
689 ;;; locate.el ends here