ホームディレクトリ内でupdatedb/locate

ホームディレクトリ内限定でupdatedb/locateしてみる。Emacs内でlocate経由でファイルを開くとき、圧倒的にホームディレクトリ内が多いと思う。その上、俺の場合ホームディレクトリ内のファイルはシステム全体の1/13に過ぎない。

$ locate -S
Database /var/cache/locate/locatedb is in the LOCATE02 format.
Locate database size: 8856843 bytes
Filenames: 837600 with a cumulative length of 49906902 bytes
	of which 4770 contain whitespace, 
	1 contain newline characters, 
	and 2416 contain characters with the high bit set.
Compression ratio 82.25%

$ locate -S -d /log/home.locatedb
Database /log/home.locatedb is in the LOCATE02 format.
Locate database size: 625986 bytes
Filenames: 61011 with a cumulative length of 3431545 bytes
	of which 191 contain whitespace, 
	1 contain newline characters, 
	and 314 contain characters with the high bit set.
Compression ratio 81.76%

そこまで差がある以上、ホームディレクトリ内限定でlocatedbを作るのは意味があることに思える。

locatedbに入れる必要のないディレクトリはRCS/, _darcs/, .svn/, coverage/*1, .rdoc/, archive/。updatedbは内部で実行するfindコマンドのオプションを受け付ける。俺はよく find dir | grep -v exclude で除外しているが、その手は使えない。findコマンドで除外するディレクトリを指定するオプションは -name exclude_dir -prune で、複数あるときはそれらを -o でつなぐ。
長くなるが、直書きすると追加が大変になるのでrubyのお世話になることに。

locate_prunes=`ruby -e 'puts ARGV.map{|x| " -name #{x} -prune "}.join("-o")' RCS _darcs .svn CVS coverage .rdoc archive`
updatedb --output=/log/home.locatedb --localpaths=$HOME --findoptions="$locate_prunes"

anythingとの連携

http://www.emacswiki.org/cgi-bin/wiki/Anything

anything.elを使うとlocateからもファイルを開くことができる。anything-config.elでlocateから開く設定 anything-source-locate が入っているが、それはシステム全体から探索するため、時間も負荷もかかる。そこでホームディレクトリ限定版を作成。

(defvar locate-home-database "/log/home.locatedb")
(defvar anything-source-locate-home
  '((name . "Locate")
    (candidates . (lambda ()
                    (start-process "locate-process" nil
                                   "locate" "-d" locate-home-database "-i" "-r" "--"
                                   anything-pattern)))
    (type . file)
    (requires-pattern . 3)
    (delayed))
  "Source for retrieving files in home directory matching the current input pattern
with locate.")

俺はこんな設定にしている。iswitchbでバッファにないファイルを開くとき、すぐさまC-x C-fできるが、anythingを使うと「あら、ないわ」と思ってる間にすでにlocateが見付けてくれるという寸法。anything便利便利!

  (setq anything-sources (list anything-source-buffers
                               anything-source-file-cache
                               anything-source-bookmarks
                               anything-source-file-name-history
                               anything-source-locate-home
                               anything-source-complex-command-history))
  (setq anything-type-actions (list anything-actions-buffer
                                    anything-actions-file
                                    anything-actions-sexp))

*1:rcovが生成したファイルが収められているディレクト