From 97d6b692224c2461a5faefe94a658ff3c5514a27 Mon Sep 17 00:00:00 2001 From: Vincent Zhang Date: Tue, 13 Nov 2018 23:00:39 +0800 Subject: [PATCH] Optimize the performance of getting the file names. --- doom-modeline.el | 59 ++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/doom-modeline.el b/doom-modeline.el index 08b6b2e..265eb06 100644 --- a/doom-modeline.el +++ b/doom-modeline.el @@ -494,32 +494,37 @@ active.") (if (eq idx len) "\"};" "\",\n"))))) 'xpm t :ascent 'center))))) -(defun doom-modeline-buffer-file-name () +(defvar-local doom-modeline-buffer-file-name nil) +(defun doom-modeline-update-buffer-file-name () "Propertized variable `buffer-file-name' based on `doom-modeline-buffer-file-name-style'." - (let ((buffer-file-name (or (buffer-file-name (buffer-base-buffer)) ""))) - (unless buffer-file-truename - (setq buffer-file-truename (file-truename buffer-file-name))) - (propertize - (pcase doom-modeline-buffer-file-name-style - (`truncate-upto-project - (doom-modeline--buffer-file-name buffer-file-name buffer-file-truename 'shrink)) - (`truncate-upto-root - (doom-modeline--buffer-file-name-truncate buffer-file-name buffer-file-truename)) - (`truncate-all - (doom-modeline--buffer-file-name-truncate buffer-file-name buffer-file-truename t)) - (`relative-to-project - (doom-modeline--buffer-file-name-relative buffer-file-name buffer-file-truename)) - (`relative-from-project - (doom-modeline--buffer-file-name-relative buffer-file-name buffer-file-truename 'include-project)) - (`file-name - (propertize (file-name-nondirectory buffer-file-name) - 'face - (let ((face (or (and (buffer-modified-p) - 'doom-modeline-buffer-modified) - (and (doom-modeline--active) - 'doom-modeline-buffer-file)))) - (when face `(:inherit ,face)))))) - 'help-echo buffer-file-truename))) + (setq doom-modeline-buffer-file-name + (let ((buffer-file-name (or (buffer-file-name (buffer-base-buffer)) ""))) + (unless buffer-file-truename + (setq buffer-file-truename (file-truename buffer-file-name))) + (propertize + (pcase doom-modeline-buffer-file-name-style + (`truncate-upto-project + (doom-modeline--buffer-file-name buffer-file-name buffer-file-truename 'shrink)) + (`truncate-upto-root + (doom-modeline--buffer-file-name-truncate buffer-file-name buffer-file-truename)) + (`truncate-all + (doom-modeline--buffer-file-name-truncate buffer-file-name buffer-file-truename t)) + (`relative-to-project + (doom-modeline--buffer-file-name-relative buffer-file-name buffer-file-truename)) + (`relative-from-project + (doom-modeline--buffer-file-name-relative buffer-file-name buffer-file-truename 'include-project)) + (`file-name + (propertize (file-name-nondirectory buffer-file-name) + 'face + (let ((face (or (and (buffer-modified-p) + 'doom-modeline-buffer-modified) + (and (doom-modeline--active) + 'doom-modeline-buffer-file)))) + (when face `(:inherit ,face)))))) + 'help-echo buffer-file-truename))) + (message "#####%s####" doom-modeline-buffer-file-name)) +(add-hook 'find-file-hook #'doom-modeline-update-buffer-file-name) +(add-hook 'after-save-hook #'doom-modeline-update-buffer-file-name) (defun doom-modeline--buffer-file-name-truncate (file-path true-file-path &optional truncate-tail) "Propertized variable `buffer-file-name' that truncates every dir along path. @@ -637,8 +642,8 @@ directory, the file name, and its state (modified, read-only or non-existent)." :face 'doom-modeline-warning :v-adjust -0.05) " "))) - (if buffer-file-name - (doom-modeline-buffer-file-name) + (if doom-modeline-buffer-file-name + doom-modeline-buffer-file-name "%b"))) (doom-modeline-def-segment buffer-info-simple