LoginSignup
1
2

More than 5 years have passed since last update.

counsel-recentf の対象を Project 内ファイルに絞る

Posted at

現在の Project 内のファイルのみを対象に counsel-recentf を実行する package を作りました。

本体は GitHub/git-counsel-recentf に置いておきます。

これは何

counsel-recentf の対象を「現在の Project のファイルのみ」に絞る package です。

Before: 対象を絞り込まない

before.png

After: Project 内のファイルのみを対象にする

after.png

前提と課題と解決と

recentf は最近開いたファイルを一覧表示してくれる機能です。
そして counsel と組み合わせることで、インクリメンタルな絞り込みが可能になっています(See: counsel-recentf

しかし現在の Project 以外のファイルもすべて表示してしまい、絞り込み切れないこともあります。
なので Git 管理下の Project のファイルのみを対象にして、問題を解決します。

使い方

init.el に以下を記述します。

(add-to-list 'load-path "YOUR PATH")
(require 'git-counsel-recentf)

;; お好みでどうぞ
(global-set-key (kbd "YOUR KEY") 'git-counsel-recentf) 

Git 管理下の Project で M-x git-counsel-recentf を実行すれば OK です。

ちょっとした説明

(defun git-counsel-recentf ()
  "Find a file on filterd `recentf-list'."
  (interactive)
  ;; Git Project でないなら、発火させない
  (if (not (check-git-project)) 
      (message "Not a Git Repository")
    (let* ((original-recentf-list recentf-list)
       ;; root ディレクトリの path を取得
       (git-root-dir (git-root-dir-path))
       ;; recentf-list の要素で、root ディレクトリの path を含む要素のみに filter する
       (filtered-list (--filter (check-path-match git-root-dir it) original-recentf-list)))
      ;; ivy に結果を渡す(ここは counsel と同じ)
      (ivy-read "Git Recentf: " (mapcar #'substring-no-properties filtered-list)
        :action (lambda (f)
              (with-ivy-window
                (find-file f)))
        :caller 'counsel-recentf))))
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2