ui/doom-modeline: fontify path separately from filename

This commit is contained in:
Henrik Lissner 2017-05-14 00:23:18 +02:00
parent e256d0017a
commit b86634e79f

View file

@ -121,8 +121,8 @@
"Face used for the dirname part of the buffer path." "Face used for the dirname part of the buffer path."
:group '+doom-modeline) :group '+doom-modeline)
(defface doom-modeline-buffer-project (defface doom-modeline-buffer-file
'((t (:inherit doom-modeline-buffer-path :bold nil))) '((t (:inherit doom-modeline-buffer-path)))
"Face used for the filename part of the mode-line buffer path." "Face used for the filename part of the mode-line buffer path."
:group '+doom-modeline) :group '+doom-modeline)
@ -238,29 +238,33 @@ active."
data)))) data))))
'xpm t :ascent 'center))))) 'xpm t :ascent 'center)))))
(defun +doom-modeline--buffer-file ()
"TODO"
"%b")
(defun +doom-modeline--buffer-path () (defun +doom-modeline--buffer-path ()
"Displays the buffer's full path relative to the project root (includes the "Displays the buffer's full path relative to the project root (includes the
project root). Excludes the file basename. See `doom-buffer-name' for that." project root). Excludes the file basename. See `doom-buffer-name' for that."
(if buffer-file-name (when buffer-file-name
(let* ((default-directory (file-name-directory buffer-file-name)) (let ((buffer-path
(buffer-path (file-relative-name buffer-file-name (doom-project-root)))) (file-relative-name (file-name-directory buffer-file-name)
(when (and buffer-path (not (equal buffer-path "."))) (doom-project-root))))
(let ((max-length (truncate (* (window-body-width) 0.4)))) (unless (equal buffer-path "./")
(if (> (length buffer-path) max-length) (let ((max-length (truncate (* (window-body-width) 0.4))))
(let ((path (nreverse (split-string buffer-path "/" t))) (if (> (length buffer-path) max-length)
(output "")) (let ((path (nreverse (split-string buffer-path "/" t)))
(when (and path (equal "" (car path))) (output ""))
(setq path (cdr path))) (when (and path (equal "" (car path)))
(while (and path (<= (length output) (- max-length 4))) (setq path (cdr path)))
(setq output (concat (car path) "/" output)) (while (and path (<= (length output) (- max-length 4)))
(setq path (cdr path))) (setq output (concat (car path) "/" output))
(when path (setq path (cdr path)))
(setq output (concat "../" output))) (when path
(when (string-suffix-p "/" output) (setq output (concat "../" output)))
(setq output (substring output 0 -1))) (unless (string-suffix-p "/" output)
output) (setq output (concat output "/")))
buffer-path)))) output)
"%b")) buffer-path))))))
;; ;;
@ -270,7 +274,7 @@ project root). Excludes the file basename. See `doom-buffer-name' for that."
(def-modeline-segment! buffer-project (def-modeline-segment! buffer-project
"Displays `doom-project-root'. This is for special buffers like the scratch "Displays `doom-project-root'. This is for special buffers like the scratch
buffer where knowing the current project directory is important." buffer where knowing the current project directory is important."
(let ((face (if (active) 'doom-modeline-buffer-project))) (let ((face (if (active) 'doom-modeline-buffer-path)))
(concat (all-the-icons-octicon (concat (all-the-icons-octicon
"file-directory" "file-directory"
:face face :face face
@ -285,8 +289,8 @@ 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 ((all-the-icons-scale-factor 1.2) (let ((all-the-icons-scale-factor 1.2)
(modified-p (buffer-modified-p)) (modified-p (buffer-modified-p))
(active (active))
faces) faces)
(if (active) (push 'doom-modeline-buffer-path faces))
(if modified-p (push 'doom-modeline-buffer-modified faces)) (if modified-p (push 'doom-modeline-buffer-modified faces))
(concat (if buffer-read-only (concat (if buffer-read-only
(concat (all-the-icons-octicon (concat (all-the-icons-octicon
@ -306,8 +310,17 @@ directory, the file name, and its state (modified, read-only or non-existent)."
:face 'doom-modeline-urgent :face 'doom-modeline-urgent
:v-adjust -0.05) :v-adjust -0.05)
" ")) " "))
(propertize (+doom-modeline--buffer-path) (let ((dir-path (+doom-modeline--buffer-path))
'face (if faces `(:inherit ,faces)))))) (faces (or faces (if active 'doom-modeline-buffer-path))))
(when dir-path
(if faces
(propertize dir-path 'face `(:inherit ,faces))
dir-path)))
(let ((file-path (+doom-modeline--buffer-file))
(faces (or faces (if active 'doom-modeline-buffer-file))))
(if faces
(propertize file-path 'face `(:inherit ,faces))
file-path)))))
;; ;;
(def-modeline-segment! buffer-encoding (def-modeline-segment! buffer-encoding