平衡点
2021/07/14
_ consult-recent-file
の候補が長すぎるのをなんとかする
history の深い階層にあるファイルを開きたい時に ディレクトリで隠れてしまうのをなんとかする, というか.
(defun my:shorten-file-path (fpath max-length)
"Show up to `max-length' characters of a directory name `fpath' like zsh"
(let* ((path (reverse (split-string (abbreviate-file-name fpath) "/")))
(output "")
(top (mapconcat 'identity (reverse (last path 3)) "/"))
(vmax (- max-length 4 (length top)))
(path (butlast path 3))
)
(while (and path
(and (< (length output) vmax)
(< (length (concat "/" (car path) output)) vmax)))
(setq output (concat "/" (car path) output))
(setq path (cdr path)))
;; 省略
(when path
(setq output (concat "/..." output)))
(format "%s%s" top output)))
(defun my:consult-recent-file ()
"Find recent using `completing-read' with shorten filename"
(interactive)
(let ((files (mapcar (lambda (f)
(cons (my:shorten-file-path f (- (window-width) 2)) f))
recentf-list)))
(let ((selected
(consult--read (mapcar #'car files)
:prompt "Find recent file: "
:sort nil
:require-match t
:category 'file
:state (consult--file-preview)
:history 'file-name-history)))
(find-file (assoc-default selected files)))))
…まあ, ほぼ昔書いた ido-recentf-open
の
ido-completiong-read
を consult-read
に置き換えただけなんだけど.
(defun ido-recentf-open ()
"Use `ido-completing-read' to \\[find-file] a recent file"
(interactive)
(let ((files (mapcar (lambda (f)
(cons (my:shorten-file-path f 70) f))
recentf-list)))
(let ((selected (ido-completing-read "Files: " (mapcar #'car files))))
(find-file (assoc-default selected files)))))
とりあえず, minibuffer がディレクトリ名で埋まる事もなく 最後のファイルを選択できるようになった.