Add truncate-with-project style and set it to default.

This commit is contained in:
Vincent Zhang 2018-11-18 03:26:00 +08:00
parent 7f28abb514
commit 87ee2e7122

View file

@ -74,11 +74,12 @@
(defvar doom-modeline-bar-width 3 (defvar doom-modeline-bar-width 3
"How wide the mode-line bar should be (only respected in GUI Emacs).") "How wide the mode-line bar should be (only respected in GUI Emacs).")
(defvar doom-modeline-buffer-file-name-style 'truncate-upto-project (defvar doom-modeline-buffer-file-name-style 'truncate-with-project
"Determines the style used by `doom-modeline-buffer-file-name'. "Determines the style used by `doom-modeline-buffer-file-name'.
Given ~/Projects/FOSS/emacs/lisp/comint.el Given ~/Projects/FOSS/emacs/lisp/comint.el
truncate-upto-project => ~/P/F/emacs/lisp/comint.el truncate-upto-project => ~/P/F/emacs/lisp/comint.el
truncate-with-project => emacs/l/comint.el
truncate-upto-root => ~/P/F/e/lisp/comint.el truncate-upto-root => ~/P/F/e/lisp/comint.el
truncate-all => ~/P/F/e/l/comint.el truncate-all => ~/P/F/e/l/comint.el
relative-from-project => emacs/lisp/comint.el relative-from-project => emacs/lisp/comint.el
@ -503,6 +504,8 @@ active.")
(pcase doom-modeline-buffer-file-name-style (pcase doom-modeline-buffer-file-name-style
(`truncate-upto-project (`truncate-upto-project
(doom-modeline--buffer-file-name buffer-file-name buffer-file-truename 'shrink)) (doom-modeline--buffer-file-name buffer-file-name buffer-file-truename 'shrink))
(`truncate-with-project
( doom-modeline--buffer-file-name-truncate-with-project buffer-file-name buffer-file-truename))
(`truncate-upto-root (`truncate-upto-root
(doom-modeline--buffer-file-name-truncate buffer-file-name buffer-file-truename)) (doom-modeline--buffer-file-name-truncate buffer-file-name buffer-file-truename))
(`truncate-all (`truncate-all
@ -540,6 +543,29 @@ If TRUNCATE-TAIL is t also truncate the parent directory of the file."
(propertize (file-name-nondirectory file-path) (propertize (file-name-nondirectory file-path)
'face (if file-faces `(:inherit ,file-faces))))))))) 'face (if file-faces `(:inherit ,file-faces)))))))))
(defun doom-modeline--buffer-file-name-truncate-with-project (file-path _true-file-path)
"Propertized variable `buffer-file-name' based on FILE-PATH.
Show relative path to the project and truncate it.
Example:
~/Projects/FOSS/emacs/lisp/comint.el => emacs/l/comint.el"
(let* ((project-root (doom-modeline-project-root))
(relative-path (file-relative-name (file-name-directory file-path)
(file-truename project-root))))
(let ((active (doom-modeline--active))
(modified-faces (if (buffer-modified-p) 'doom-modeline-buffer-modified)))
(let ((project-faces (or modified-faces (if active 'font-lock-string-face)))
(relative-faces (or modified-faces (if active 'doom-modeline-buffer-path)))
(file-faces (or modified-faces (if active 'doom-modeline-buffer-file))))
(let ((project-props `(,@(if project-faces `(:inherit ,project-faces)) ,@(if active '(:weight bold))))
(relative-props `(,@(if relative-faces `(:inherit ,relative-faces))))
(file-props `(,@(if file-faces `(:inherit ,file-faces)))))
(concat (propertize
(concat (file-name-nondirectory (directory-file-name (doom-modeline-project-root))) "/")
'face project-props)
(unless (string-equal relative-path "./")
(propertize (shrink-path--dirs-internal relative-path t) 'face relative-props))
(propertize (file-name-nondirectory file-path) 'face file-props)))))))
(defun doom-modeline--buffer-file-name-relative (_file-path true-file-path &optional include-project) (defun doom-modeline--buffer-file-name-relative (_file-path true-file-path &optional include-project)
"Propertized variable `buffer-file-name' showing directories relative to project's root only." "Propertized variable `buffer-file-name' showing directories relative to project's root only."
@ -630,36 +656,35 @@ buffer where knowing the current project directory is important."
directory, the file name, and its state (modified, read-only or non-existent)." directory, the file name, and its state (modified, read-only or non-existent)."
(let ((active (doom-modeline--active))) (let ((active (doom-modeline--active)))
(concat (concat
(when active (if active
(cond (buffer-read-only (cond (buffer-read-only
(concat (doom-modeline-icon-octicon (concat (doom-modeline-icon-octicon
"lock" "lock"
:face 'doom-modeline-warning :face 'doom-modeline-warning
:v-adjust -0.05) :v-adjust -0.05)
" ")) " "))
((buffer-modified-p) ((buffer-modified-p)
(concat (doom-modeline-icon-faicon (concat (doom-modeline-icon-faicon
"floppy-o" "floppy-o"
:face 'doom-modeline-buffer-modified :face 'doom-modeline-buffer-modified
:v-adjust -0.0575) :v-adjust -0.0575)
" ")) " "))
((and buffer-file-name ((and buffer-file-name
(not (file-exists-p buffer-file-name))) (not (file-exists-p buffer-file-name)))
(concat (doom-modeline-icon-octicon (concat (doom-modeline-icon-octicon
"circle-slash" "circle-slash"
:face 'doom-modeline-urgent :face 'doom-modeline-urgent
:v-adjust -0.05) :v-adjust -0.05)
" ")) " "))
((buffer-narrowed-p) ((buffer-narrowed-p)
(concat (doom-modeline-icon-octicon (concat (doom-modeline-icon-octicon
"fold" "fold"
:face 'doom-modeline-warning :face 'doom-modeline-warning
:v-adjust -0.05) :v-adjust -0.05)
" ")))) " "))))
(if buffer-file-name (if buffer-file-name
(doom-modeline-update-buffer-file-name) (doom-modeline-update-buffer-file-name)
(propertize "%b" 'face (when (doom-modeline--active) (propertize "%b" 'face (if active 'doom-modeline-buffer-file))))))
'doom-modeline-buffer-file))))))
(doom-modeline-def-segment buffer-info-simple (doom-modeline-def-segment buffer-info-simple
"Display only the current buffer's name, but with fontification." "Display only the current buffer's name, but with fontification."