ui/modeline: refactor buffer-id segment

This commit is contained in:
Henrik Lissner 2020-08-26 21:42:39 -04:00
parent b3006ecabb
commit 2e2f72a2de
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -157,9 +157,8 @@ LHS and RHS will accept."
(push docstring plist) (push docstring plist)
(setq docstring nil)) (setq docstring nil))
`(progn `(progn
(defvar ,name nil ,docstring) (defconst ,name ,body ,docstring)
,@(if (plist-get plist :local) `((make-variable-buffer-local ',name))) ,@(if (plist-get plist :local) `((make-variable-buffer-local ',name)))
(setq-default ,name ,body)
(put ',name 'risky-local-variable t))) (put ',name 'risky-local-variable t)))
@ -355,7 +354,6 @@ Requires `anzu', also `evil-anzu' if using `evil-mode' for compatibility with
;;; `+modeline-buffer-identification' ;;; `+modeline-buffer-identification'
(defvar-local +modeline--buffer-id-cache nil) (defvar-local +modeline--buffer-id-cache nil)
(add-transient-hook! 'doom-first-buffer-hook
;; REVIEW Generating the buffer's file name can be relatively expensive. ;; REVIEW Generating the buffer's file name can be relatively expensive.
;; Compounded with how often the modeline updates this can add up, so ;; Compounded with how often the modeline updates this can add up, so
;; we cache it ahead of time. ;; we cache it ahead of time.
@ -371,24 +369,22 @@ Requires `anzu', also `evil-anzu' if using `evil-mode' for compatibility with
;; ...when the underlying file changes ;; ...when the underlying file changes
after-revert-hook) after-revert-hook)
(defun +modeline--generate-buffer-id-cache-h () (defun +modeline--generate-buffer-id-cache-h ()
(when after-init-time
(setq +modeline--buffer-id-cache (setq +modeline--buffer-id-cache
(let ((file-name (buffer-file-name (buffer-base-buffer)))) (let ((file-name (buffer-file-name (buffer-base-buffer))))
(unless (or (null default-directory) (unless (or (null default-directory)
(null file-name) (null file-name)
(file-remote-p file-name)) (file-remote-p file-name))
(when-let ((project-root (doom-project-root))) (when-let (project-root (doom-project-root))
(file-relative-name (file-relative-name (or buffer-file-truename (file-truename file-name))
(or buffer-file-truename (file-truename file-name))
(concat project-root ".."))))))))) (concat project-root "..")))))))))
(def-modeline-var! +modeline-buffer-identification ; slightly more informative buffer id (def-modeline-var! +modeline-buffer-identification ; slightly more informative buffer id
'((:eval '((:eval
(propertize (propertize
(or +modeline--buffer-id-cache "%b") (or +modeline--buffer-id-cache "%b")
'face (cond ((buffer-modified-p) 'face (cond ((buffer-modified-p) '(error bold mode-line-buffer-id))
'(error bold mode-line-buffer-id)) ((+modeline-active) 'mode-line-buffer-id))
((+modeline-active)
'mode-line-buffer-id))
'help-echo (or +modeline--buffer-id-cache (buffer-name)))) 'help-echo (or +modeline--buffer-id-cache (buffer-name))))
(buffer-read-only (:propertize " RO" face warning)))) (buffer-read-only (:propertize " RO" face warning))))