平衡点


2018/06/27

_ recentf-open を ido で + 短縮表示

recenf-list を ido-compreting-read を渡せば良きに図らってくれる訳だが, ファイルパスが長すぎて minibuffer におさまらない事が多くなって来た. ghquwabami/ido-ghq にべったり依存する様になったので, まあそんなモンか, と.

zsh 的な省略をしたかったので, slack の emacs-jp で「誰か氏〜」とお伺いをたてた所 tadasan, syohex, takaxp が反応して下さった.

一番参考になったのは以下

上記はheader-lineに表示させたかった様だったので, 自分好みになるようにじたばたと修正.

結果は以下の通り

(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 (string-join (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 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 77) f))
                       recentf-list)))
    (let ((selected (ido-completing-read "Files: " (mapcar #'car files))))
      (find-file (assoc-default selected files)))))
;;
(bind-key "C-x C-r" 'ido-recentf-open)

これで

$HOME/hoge/hero/fuga/foo/bar/baz/baz2/baz3/foo.c"
→ "~/hoge/hero/.../baz3/foo.c"

みたいになって表示されるようになった.


連絡先など
最近の日記
一覧
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 ▲