平衡点


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-openido-completiong-readconsult-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 がディレクトリ名で埋まる事もなく 最後のファイルを選択できるようになった.


連絡先など
最近の日記
一覧
2006|03|04|05|06|07|08|09|10|11|12|
2007|01|02|03|04|05|06|07|08|09|10|11|12|
2008|01|02|03|04|05|06|07|08|09|10|11|12|
2009|01|02|03|04|05|06|07|08|09|10|11|12|
2010|01|02|03|04|05|06|07|08|09|10|11|12|
2011|01|02|03|04|05|06|07|08|09|10|11|12|
2012|02|03|04|08|09|10|11|12|
2013|01|02|03|04|05|06|08|09|10|11|12|
2014|01|02|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|09|10|
2016|02|03|
2017|01|02|03|05|06|07|09|11|12|
2018|03|06|07|10|11|12|
2019|01|02|03|04|05|07|10|12|
2020|01|02|03|04|05|08|09|10|11|12|
2021|01|02|03|05|06|07|08|09|11|12|
2022|01|02|03|04|05|06|08|10|11|12|
2023|02|03|04|06|08|09|11|12|
2024|01|02|03|
Back to Top ▲