File: locate.el.html
Search a database of files and use dired commands on the result.
Locate.el provides an interface to a program which searches a database of file names. By default, this program is the GNU locate command, but it could also be the BSD-style find command, or even a user specified command.
To use the BSD-style "fast find", or any other shell command of the form
SHELLPROGRAM Name-to-find
set the variable locate-command in your init file.
To use a more complicated expression, create a function which
takes a string (the name to find) as input and returns a list.
The first element should be the command to be executed, the remaining
elements should be the arguments (including the name to find). Then put
(setq locate-make-command-line 'my-locate-command-line)
in your .emacs, using the name of your function in place of my-locate-command-line.
You should make sure that whichever command you use works correctly from a shell prompt. GNU locate and BSD find expect the file databases to either be in standard places or located via environment variables. If the latter, make sure these environment variables are set in your Emacs process.
Locate-mode assumes that each line output from the locate-command
consists exactly of a file name, possibly preceded or trailed by
whitespace. If your file database has other information on the line (for
example, the file size), you will need to redefine the function
locate-get-file-positions to return a list consisting of the first
character in the file name and the last character in the file name.
To use locate-mode, simply type M-x locate and then the string you wish to find. You can use almost all of the dired commands in the resulting *Locate* buffer. It is worth noting that your commands do not, of course, affect the file database. For example, if you compress a file in the locate buffer, the actual file will be compressed, but the entry in the file database will not be affected. Consequently, the database and the filesystem will be out of sync until the next time the database is updated.
The command locate-with-filter keeps only lines matching a
regular expression; this is often useful to constrain a big search.
;;; Building a database of files ;;;;;;;;;
You can create a simple files database with a port of the Unix find command and one of the various Windows NT various scheduling utilities, for example the AT command from the NT Resource Kit, WinCron which is included with Microsoft FrontPage, or the shareware NTCron program.
To set up a function which searches the files database, do something like this:
(defvar locate-fcodes-file "c:/users/peter/fcodes")
(defvar locate-make-command-line 'nt-locate-make-command-line)
(defun nt-locate-make-command-line (arg)
(list "grep" "-i" arg locate-fcodes-file))
;;;;;; ADVICE For dired-make-relative: ;;;;;;;;;
For certain dired commands to work right, you should also include the following in your _emacs/.emacs:
(defadvice dired-make-relative (before set-no-error activate)
"For locate mode and Windows, don't return errors"
(if (and (derived-mode-p 'locate-mode)
(memq system-type '(windows-nt ms-dos)))
(ad-set-arg 2 t)
))
Otherwise, dired-make-relative will give error messages like
"FILENAME: not in directory tree growing at /"
Defined variables (18)
locate-buffer-name | Name of the buffer to show results from the M-x locate command. |
locate-command | Executable program for searching a database of files. |
locate-fcodes-file | File name for the database of file names used by ‘locate’. |
locate-filename-indentation | The amount of indentation for each file. |
locate-grep-history-list | The history list used by the M-x locate-with-filter command. |
locate-header-face | Face used to highlight the locate header. |
locate-history-list | The history list used by the M-x locate command. |
locate-ls-subdir-switches | ‘ls’ switches for inserting subdirectories in ‘*Locate*’ buffers. |
locate-make-command-line | Function used to create the locate command line. |
locate-mode-abbrev-table | Abbrev table for ‘locate-mode’. |
locate-mode-hook | List of hook functions run by ‘locate-mode’ (see ‘run-mode-hooks’). |
locate-mode-map | Local keymap for Locate mode buffers. |
locate-mode-syntax-table | Syntax table for ‘locate-mode’. |
locate-post-command-hook | List of hook functions run after ‘locate’ (see ‘run-hooks’). |
locate-prompt-for-command | If non-nil, the ‘locate’ command prompts for a command to run. |
locate-update-command | The executable program used to update the locate database. |
locate-update-path | The default directory from where ‘locate-update-command’ is called. |
locate-update-when-revert | This option affects how the *Locate* buffer gets reverted. |
Defined functions (18)
locate | (SEARCH-STRING &optional FILTER ARG) |
locate-current-line-number | () |
locate-default-make-command-line | (SEARCH-STRING) |
locate-do-redisplay | (&optional ARG TEST-FOR-SUBDIR) |
locate-do-setup | (SEARCH-STRING) |
locate-filter-output | (FILTER) |
locate-find-directory | () |
locate-find-directory-other-window | () |
locate-get-dirname | () |
locate-get-file-positions | () |
locate-in-alternate-database | (SEARCH-STRING DATABASE) |
locate-insert-header | (SEARCH-STRING) |
locate-main-listing-line-p | () |
locate-mode | () |
locate-mouse-view-file | (EVENT) |
locate-tags | () |
locate-update | (IGNORE1 IGNORE2) |
locate-with-filter | (SEARCH-STRING FILTER &optional ARG) |