缩短路径
(defun roife/shorten-path (path &optional max-len)
"Shorten PATH to MAX-LEN."
(unless max-len (setq max-len 0))
(if (and path (not (eq path "")))
(let* ((components (split-string (abbreviate-file-name path) "/"))
(len (+ (1- (length components))
(reduce '+ components :key 'length)))
(str ""))
(while (and (> len max-len)
(cdr components))
(setq str (concat str (if (= 0 (length (car components)))
"/"
(string (elt (car components) 0) ?/)))
len (- len (1- (length (car components))))
components (cdr components)))
(concat str (reduce (lambda (a b) (concat a "/" b)) components)))
""
)
)
样式
(defun roife/modeline-module-file ()
(let* ((prj-p (when (and (bound-and-true-p projectile-mode)
(projectile-project-p))
(abbreviate-file-name (projectile-project-p))))
(prj-dir (file-name-directory ;; get parent
(directory-file-name
(if prj-p prj-p
(abbreviate-file-name default-directory)))))
(prj-name (if prj-p
(file-name-nondirectory
(directory-file-name prj-p))
(file-name-nondirectory
(directory-file-name default-directory))))
(file-name (file-relative-name
buffer-file-name
(if prj-p prj-p
(abbreviate-file-name default-directory))))
)
(concat (propertize (roife/shorten-path prj-dir)
'face `(:foreground ,(face-foreground 'mode-line-inactive)
:weight bold))
(propertize (if (eq prj-dir nil)
"~"
prj-name)
'face 'roife/modeline-projectile-face)
(propertize "/" 'face '(:weight bold))
(propertize file-name 'face '(:weight bold)))
)
)