2009年6月の記事の追補情報
「locateとanythingを組み合わせる」のために必要なもの
まんま前回の記事を写すのはまだまずそうなので、簡単に。
- find-utils の locate(mlocate ではダメ。updatedb でfindオプションが指定できない)
- $HOME/locate.db に locate db を作るためのスクリプト
- home以下、プロジェクト以下の locate db を扱うための elisp
$HOME/locate.db に locate db を作るためのスクリプト
こんな感じで
#!/bin/sh
PRUNES="
-name RCS
-o -name CVS
-o -name .svn
-o -name .hg
-o -name .git
-o -name classes
-o -path $HOME/tmp
"
updatedb.findutils \
--findoptions="( -type d ( $PRUNES ) -prune ) -o -type f" \
--localpaths=$HOME --output=$HOME/locate.db
home以下、プロジェクト以下の locate db を扱うための elisp
以下が必要
;; locate db のファイル名
(defvar locate-db-name "locate.db")
;; locate コマンド
(setq locate-command "/usr/bin/locate.findutils")
;; locate コマンドラインを作る為の共通関数
(defun locate-make-command-line (search-string dbpath &rest opts)
(append
;; 大文字小文字を虫するように設定
(list locate-command "-i")
(when (and dbpath (file-exists-p dbpath))
(list "-d" dbpath))
opts
(list search-string)))
;; home 以下のファイルを探す為の locate コマンドライン
(defun home-locate-make-command-line (search-string &optional &rest opts)
(apply 'locate-make-command-line
search-string
;; home 直下の locate.db を使うように指定
(expand-file-name locate-db-name "~")
opts))
;; 標準の locate で home 以下を探すように設定する。
(setq locate-make-command-line 'home-locate-make-command-line)
;; 指定したファイルを dir を起点に探す為の関数
(defun find-file-upward (name &optional dir)
(setq dir (file-name-as-directory (or dir default-directory)))
(cond
((string= dir (directory-file-name dir))
nil)
((file-exists-p name)
(expand-file-name name dir))
(t
(find-file-upward name (expand-file-name ".." dir)))))
;; プロジェクト毎の locate db を使った locate コマンドライン
(defun plocate-make-command-line (search-string &optional &rest opts)
(apply 'locate-make-command-line
search-string
(find-file-upward locate-db-name)
opts))
;; プロジェクト毎の locate db を使った locate コマンド
(defun plocate (search-string &optional arg)
(interactive
(list
(locate-prompt-for-search-string)
current-prefix-arg))
(let ((locate-make-command-line 'plocate-make-command-line))
(locate search-string nil arg)))