From 848cc117c4555c424fcd7f16d363b5da8118f36d Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Wed, 26 Aug 2020 17:23:31 -0400 Subject: [PATCH] ui/modeline: cache buffer id for +light modeline --- modules/ui/modeline/+light.el | 45 +++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/modules/ui/modeline/+light.el b/modules/ui/modeline/+light.el index 6d1eab924..0eab73319 100644 --- a/modules/ui/modeline/+light.el +++ b/modules/ui/modeline/+light.el @@ -353,20 +353,41 @@ Requires `anzu', also `evil-anzu' if using `evil-mode' for compatibility with ;;; `+modeline-buffer-identification' +(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. + ;; Compounded with how often the modeline updates this can add up, so + ;; we cache it ahead of time. + (add-hook! '(change-major-mode-after-body-hook + ;; In case the user saves the file to a new location + after-save-hook + ;; ...or makes external changes then returns to Emacs + focus-in-hook + ;; ...or when we change the current project! + projectile-after-switch-project-hook + ;; ...when the underlying file changes + after-revert-hook) + (defun +modeline--generate-buffer-id-cache-h () + (setq +modeline--buffer-id-cache + (let ((file-name (buffer-file-name (buffer-base-buffer)))) + (unless (or (null default-directory) + (null file-name) + (file-remote-p file-name)) + (when-let ((project-root (doom-project-root))) + (file-relative-name + (or buffer-file-truename (file-truename file-name)) + (concat project-root ".."))))))))) + (def-modeline-var! +modeline-buffer-identification ; slightly more informative buffer id '((:eval - (let ((file-name (buffer-file-name (buffer-base-buffer)))) - (propertize - (or (when (and file-name (not (file-remote-p file-name))) - (when-let (project (doom-project-root file-name)) - (file-relative-name (or buffer-file-truename (file-truename file-name)) - (concat project "..")))) - "%b") - 'face (cond ((buffer-modified-p) - '(error bold mode-line-buffer-id)) - ((+modeline-active) - 'mode-line-buffer-id)) - 'help-echo file-name))) + (propertize + (or +modeline--buffer-id-cache "%b") + 'face (cond ((buffer-modified-p) + '(error bold mode-line-buffer-id)) + ((+modeline-active) + 'mode-line-buffer-id)) + 'help-echo (or +modeline--buffer-id-cache (buffer-name)))) (buffer-read-only (:propertize " RO" face warning))))