From 229366ffe54a0219bee24783bf380d56abc7ad04 Mon Sep 17 00:00:00 2001 From: Vincent Zhang Date: Sat, 24 Nov 2018 13:18:33 +0800 Subject: [PATCH] Optimize performance of displaying file icons. --- doom-modeline.el | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/doom-modeline.el b/doom-modeline.el index b58778d..de7a014 100644 --- a/doom-modeline.el +++ b/doom-modeline.el @@ -687,6 +687,35 @@ buffer where knowing the current project directory is important." file-name (format "%s[%s]" file-name buffer-name)))) +(defvar-local doom-modeline--file-icon nil) +(defun doom-modeline-update-file-icon () + "Update file icon in mode-line." + (when doom-modeline-major-mode-icon + (setq doom-modeline--file-icon + (let ((icon (doom-modeline-icon-for-mode major-mode))) + (unless (symbolp icon) + (concat + (propertize icon + 'help-echo (format "Major-mode: `%s'" major-mode) + 'display '(raise 0) + 'face (cond (buffer-read-only 'doom-modeline-warninng) + ((buffer-modified-p) 'doom-modeline-buffer-modified) + ((and buffer-file-name + (not (file-exists-p buffer-file-name))) + 'doom-modeline-urgent) + ((buffer-narrowed-p) 'doom-modeline-warning))) + " ")))))) +(add-hook 'find-file-hook 'doom-modeline-update-file-icon) +(add-hook 'after-save-hook 'doom-modeline-update-file-icon) +(add-hook 'after-revert-hook 'doom-modeline-update-file-icon) +(add-hook 'after-change-major-mode-hook 'doom-modeline-update-file-icon) + +(add-variable-watcher + 'doom-modeline-major-mode-icon + (lambda (_sym _val op _where) + (when (eq op 'set) + (doom-modeline-update-file-icon)))) + (doom-modeline-def-segment buffer-info "Combined information about the current buffer, including the current working directory, the file name, and its state (modified, read-only or non-existent)." @@ -718,20 +747,7 @@ directory, the file name, and its state (modified, read-only or non-existent)." :v-adjus t -0.05) " "))) - (when doom-modeline-major-mode-icon - (let ((icon (doom-modeline-icon-for-mode major-mode))) - (unless (symbolp icon) - (concat - (propertize icon - 'help-echo (format "Major-mode: `%s'" major-mode) - 'display '(raise 0) - 'face (cond (buffer-read-only 'doom-modeline-warninng) - ((buffer-modified-p) 'doom-modeline-buffer-modified) - ((and buffer-file-name - (not (file-exists-p buffer-file-name))) - 'doom-modeline-urgent) - ((buffer-narrowed-p) 'doom-modeline-warning))) - " ")))) + (or doom-modeline--file-icon (doom-modeline-update-file-icon)) (if buffer-file-name (doom-modeline-fix-buffer-file-name)