Optimize performance of updating buffer information (~2x faster).

This commit is contained in:
Vincent Zhang 2018-11-24 17:10:11 +08:00
parent 2b5a3b103e
commit d4973e2bc6

View file

@ -670,57 +670,40 @@ buffer where knowing the current project directory is important."
'face face)))) 'face face))))
;; ;;
(defun doom-modeline-fix-buffer-file-name () (defvar-local doom-modeline--buffer-file-icon nil)
"Fix buffer file name in mode-line. (defun doom-modeline-update-buffer-file-icon (&rest _)
Show buffer name if it doesn't equal the file name.
Format: \"buffer-file-name[buffer-name]\".
Except the same buffer names in different directories."
(when-let ((file-name (doom-modeline-buffer-file-name))
(buffer-name (buffer-name))
(buffer-file-name buffer-file-name))
(if (or (eq doom-modeline-buffer-file-name-style 'buffer-name)
(not (file-exists-p buffer-file-name))
(string-equal
(file-name-nondirectory buffer-file-name)
(replace-regexp-in-string "<.+>$" "" buffer-name)))
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." "Update file icon in mode-line."
(when doom-modeline-major-mode-icon (setq doom-modeline--buffer-file-icon
(setq doom-modeline--file-icon
(let ((icon (doom-modeline-icon-for-mode major-mode))) (let ((icon (doom-modeline-icon-for-mode major-mode)))
(unless (symbolp icon) (unless (symbolp icon)
(concat (concat
(propertize icon (propertize icon
'help-echo (format "Major-mode: `%s'" major-mode) 'help-echo (format "Major-mode: `%s'" major-mode)
'display '(raise 0) 'display '(raise 0)
'face (cond (buffer-read-only 'doom-modeline-warninng) 'face (cond (buffer-read-only 'doom-modeline-warning)
((buffer-modified-p) 'doom-modeline-buffer-modified) ((buffer-modified-p) 'doom-modeline-buffer-modified)
((and buffer-file-name ((and buffer-file-name
(not (file-exists-p buffer-file-name))) (not (file-exists-p buffer-file-name)))
'doom-modeline-urgent) 'doom-modeline-urgent)
((buffer-narrowed-p) 'doom-modeline-warning))) ((buffer-narrowed-p) 'doom-modeline-warning)))
" ")))))) " ")))))
(add-hook 'find-file-hook 'doom-modeline-update-file-icon) (add-hook 'find-file-hook 'doom-modeline-update-buffer-file-icon)
(add-hook 'after-save-hook 'doom-modeline-update-file-icon) (add-hook 'after-save-hook 'doom-modeline-update-buffer-file-icon)
(add-hook 'after-revert-hook 'doom-modeline-update-file-icon) (add-hook 'after-revert-hook 'doom-modeline-update-buffer-file-icon)
(add-hook 'after-change-major-mode-hook 'doom-modeline-update-file-icon) ;; (add-hook 'read-only-mode-hook 'doom-modeline-update-buffer-file-icon)
(add-hook 'after-change-major-mode-hook 'doom-modeline-update-buffer-file-icon)
(add-hook 'clone-indirect-buffer-hook 'doom-modeline-update-buffer-file-icon)
;; This hook will cause magit exception
;; (add-hook 'after-change-functions 'doom-modeline-update-buffer-file-icon)
(advice-add #'undo :after #'doom-modeline-update-buffer-file-icon)
(advice-add #'undo-tree-undo :after #'doom-modeline-update-buffer-file-icon)
;; (advice-add #'narrow-to-region :after #'doom-modeline-update-buffer-file-icon)
(add-variable-watcher (defvar-local doom-modeline--buffer-file-state-icon nil)
'doom-modeline-major-mode-icon (defun doom-modeline-update-buffer-file-state-icon (&rest _)
(lambda (_sym _val op _where) "Update the buffer or file state in mode-line."
(when (eq op 'set) (setq doom-modeline--buffer-file-state-icon
(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)."
(let ((active (doom-modeline--active))) (let ((active (doom-modeline--active)))
(concat
(cond (buffer-read-only (cond (buffer-read-only
(concat (doom-modeline-icon-octicon (concat (doom-modeline-icon-octicon
"lock" "lock"
@ -743,15 +726,62 @@ directory, the file name, and its state (modified, read-only or non-existent)."
((buffer-narrowed-p) ((buffer-narrowed-p)
(concat (doom-modeline-icon-octicon (concat (doom-modeline-icon-octicon
"fold" "fold"
:display '(raise 0)
:face (if active 'doom-modeline-warning) :face (if active 'doom-modeline-warning)
:v-adjus t -0.05) :v-adjus t -0.05)
" "))) " "))))))
(add-hook 'find-file-hook 'doom-modeline-update-buffer-file-state-icon)
(add-hook 'after-save-hook 'doom-modeline-update-buffer-file-state-icon)
(add-hook 'after-revert-hook 'doom-modeline-update-buffer-file-state-icon)
(add-hook 'read-only-mode-hook 'doom-modeline-update-buffer-file-state-icon)
(add-hook 'after-change-functions 'doom-modeline-update-buffer-file-state-icon)
(advice-add #'undo :after #'doom-modeline-update-buffer-file-state-icon)
(advice-add #'undo-tree-undo :after #'doom-modeline-update-buffer-file-state-icon)
(advice-add #'narrow-to-region :after #'doom-modeline-update-buffer-file-state-icon)
(or doom-modeline--file-icon (doom-modeline-update-file-icon)) (defun doom-modeline-fix-buffer-file-name ()
"Fix buffer file name in mode-line.
Show buffer name if it doesn't equal the file name.
Format: \"buffer-file-name[buffer-name]\".
Except the same buffer names in different directories."
(when-let ((file-name (doom-modeline-buffer-file-name))
(buffer-name (buffer-name))
(buffer-file-name buffer-file-name))
(if (or (eq doom-modeline-buffer-file-name-style 'buffer-name)
(not (file-exists-p buffer-file-name))
(string-equal
(file-name-nondirectory buffer-file-name)
(replace-regexp-in-string "<.+>$" "" buffer-name)))
file-name
(format "%s[%s]" file-name buffer-name))))
(defvar-local doom-modeline--buffer-file-name nil)
(defun doom-modeline-update-buffer-file-name (&rest _)
"Update buffer file name in mode-line."
(setq doom-modeline--buffer-file-name
(if buffer-file-name (if buffer-file-name
(doom-modeline-fix-buffer-file-name) (doom-modeline-fix-buffer-file-name)
(propertize "%b" 'face (if active 'doom-modeline-buffer-file)))))) (propertize "%b" 'face (if (doom-modeline--active) 'doom-modeline-buffer-file)))))
(add-hook 'find-file-hook 'doom-modeline-update-buffer-file-name)
(add-hook 'after-save-hook 'doom-modeline-update-buffer-file-name)
(add-hook 'after-revert-hook 'doom-modeline-update-buffer-file-name)
(add-hook 'after-change-functions 'doom-modeline-update-buffer-file-name)
(add-hook 'clone-indirect-buffer-hook 'doom-modeline-update-buffer-file-name)
(advice-add #'rename-buffer :after #'doom-modeline-update-buffer-file-name)
(advice-add #'set-visited-file-name :after #'doom-modeline-update-buffer-file-name)
(advice-add #'select-window :after #'doom-modeline-update-buffer-file-name)
(advice-add #'undo :after #'doom-modeline-update-buffer-file-name)
(advice-add #'undo-tree-undo :after #'doom-modeline-update-buffer-file-name)
(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)."
(concat
(or doom-modeline--buffer-file-state-icon (doom-modeline-update-buffer-file-state-icon))
(when (and doom-modeline-icon doom-modeline-major-mode-icon)
(or doom-modeline--buffer-file-icon (doom-modeline-update-buffer-file-icon)))
(or doom-modeline--buffer-file-name (doom-modeline-update-buffer-file-name))))
(doom-modeline-def-segment buffer-info-simple (doom-modeline-def-segment buffer-info-simple
"Display only the current buffer's name, but with fontification." "Display only the current buffer's name, but with fontification."