Hatena::ブログ(Diary)

わからん

2011.07.10

[] anything-project.el にファイルキャッシュを導入

anything-project.el にファイルキャッシュを導入しました。膨大なファイルからなる3つのプロジェクトに参加していて、毎日キャッシュ用のバッファを3回生成するのにストレスを感じたのがきっかけです。既存の anything-project() コマンドをのっとるので、いつものように、M-x anything-project か anything-project() に割り当てたキーバインドで呼び出します。C-u M-x anything-project とプレフィックスを付ければキャッシュを再生成します。プロジェクトのルートディレクトリに .ap-project-files というファイルを生成するので .gitignore などで管理しましょう。なお anything-project とは、プロジェクトのファイルを anything の UI で一覧表示し、絞り込み、バッファで開く emacs 拡張で、https://github.com/imakado/anything-project から取得できます。

(require 'anything-project)

(defvar ap:project-files ".ap-project-files")

(defadvice anything-project (around anything-project-by-file-cache activate)
  (interactive)
  (anything-project-init)
  (anything-other-buffer
   `(((name . "Project Files in File Cache")
      (candidates-file . ,(ap:get-project-root-path))
      (action . (("Find file" .
                  (lambda (c)
                    (find-file (ap:expand-file c))))
                 ("Find file other window" .
                  (lambda (c)
                    (find-file-other-window (ap:expand-file c))))
                 ("Find file other frame" .
                  (lambda (c)
                    (find-file-other-frame (ap:expand-file c))))
                 ("Open dired in file's directory" .
                  (lambda (c)
                    (ap:anything-c-open-dired (ap:expand-file c))))))))
   "*project files in file cache*"))

(defun ap:make-cache-file ()
  (interactive)
  (with-temp-buffer
    (insert (mapconcat 'identity (ap:get-project-files) "\n"))
    (write-file (ap:get-project-root-path))))

(defun ap:get-project-root-path ()
  (ap:aif (car (ap:get-root-directory))
      (concat it ap:project-files)
     nil))

(defun anything-project-init ()
  (ap:set-project-root-path)
  (when (or current-prefix-arg (not (ap:project-files-p)))
     (ap:make-cache-file)))

(defun ap:set-project-root-path ()
  (setq ap:root-directory (car (ap:get-root-directory))))

(defmacro ap:aif (test-form then-form &optional else-form)
  `(let ((it ,test-form))
     (if it ,then-form ,else-form)))

(defun ap:project-files-p ()
  (file-exists-p (ap:get-project-root-path)))

;; キーバインドの設定例
;; C-u M-i でキャッシュを作り直す
;; (global-set-key (kbd "M-i") 'anything-project)

はてなユーザーのみコメントできます。はてなへログインもしくは新規登録をおこなってください。

Google