Add doom-modeline-add-variable-watcher.

This commit is contained in:
Vincent Zhang 2019-11-21 01:36:09 +08:00
parent 0dd6a32b7e
commit 7a44cbc1cc
2 changed files with 179 additions and 182 deletions

View file

@ -638,14 +638,14 @@ It requires `circe' or `erc' package."
(defun doom-modeline (key) (defun doom-modeline (key)
"Return a mode-line configuration associated with KEY (a symbol). "Return a mode-line configuration associated with KEY (a symbol).
Throws an error if it doesn't exist." Throws an error if it doesn't exist."
(let ((fn (intern-soft (format "doom-modeline-format--%s" key)))) (let ((fn (intern-soft (format "doom-modeline-format--%s" key))))
(when (functionp fn) (when (functionp fn)
`(:eval (,fn))))) `(:eval (,fn)))))
(defun doom-modeline-set-modeline (key &optional default) (defun doom-modeline-set-modeline (key &optional default)
"Set the modeline format. Does nothing if the modeline KEY doesn't exist. "Set the modeline format. Does nothing if the modeline KEY doesn't exist.
If DEFAULT is non-nil, set the default mode-line for all buffers." If DEFAULT is non-nil, set the default mode-line for all buffers."
(when-let ((modeline (doom-modeline key))) (when-let ((modeline (doom-modeline key)))
(setf (if default (setf (if default
(default-value 'mode-line-format) (default-value 'mode-line-format)
@ -765,11 +765,18 @@ It requires `circe' or `erc' package."
((floatp height) (* height (frame-char-height))) ((floatp height) (* height (frame-char-height)))
(t (frame-char-height))))))) (t (frame-char-height)))))))
(defun doom-modeline-add-variable-watcher (symbol watch-function)
"Cause WATCH-FUNCTION to be called when SYMBOL is set if possible.
See docs of `add-variable-watcher'."
(when (fboundp 'add-variable-watcher)
(add-variable-watcher symbol watch-function)))
(defun doom-modeline-icon (icon-set icon-name unicode text face &rest args) (defun doom-modeline-icon (icon-set icon-name unicode text face &rest args)
"Display icon of ICON-NAME with FACE and ARGS in mode-line. "Display icon of ICON-NAME with FACE and ARGS in mode-line.
ICON-SET includes `octicon', `faicon', `material', `alltheicons' and `fileicon'. ICON-SET includes `octicon', `faicon', `material', `alltheicons' and `fileicon'.
UNICODE is the unicode char fallback. TEXT is the ASCII char fallback." UNICODE is the unicode char fallback. TEXT is the ASCII char fallback."
(let ((face (or face 'mode-line))) (let ((face (or face 'mode-line)))
(or (when (and icon-name (not (string-empty-p icon-name))) (or (when (and icon-name (not (string-empty-p icon-name)))
(pcase icon-set (pcase icon-set
@ -793,7 +800,7 @@ It requires `circe' or `erc' package."
(defvar-local doom-modeline-project-root nil) (defvar-local doom-modeline-project-root nil)
(defun doom-modeline-project-root () (defun doom-modeline-project-root ()
"Get the path to the root of your project. "Get the path to the root of your project.
Return `default-directory' if no project was found." Return `default-directory' if no project was found."
(setq doom-modeline-project-root (setq doom-modeline-project-root
(or doom-modeline-project-root (or doom-modeline-project-root
(pcase doom-modeline-project-detection (pcase doom-modeline-project-detection

View file

@ -244,15 +244,14 @@ buffer where knowing the current project directory is important."
(add-hook 'after-change-major-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) (add-hook 'clone-indirect-buffer-hook #'doom-modeline-update-buffer-file-icon)
(when (>= emacs-major-version 26) (doom-modeline-add-variable-watcher
(add-variable-watcher 'doom-modeline-icon
'doom-modeline-icon (lambda (_sym val op _where)
(lambda (_sym val op _where) (when (eq op 'set)
(when (eq op 'set) (setq doom-modeline-icon val)
(setq doom-modeline-icon val) (dolist (buf (buffer-list))
(dolist (buf (buffer-list)) (with-current-buffer buf
(with-current-buffer buf (doom-modeline-update-buffer-file-icon))))))
(doom-modeline-update-buffer-file-icon)))))))
(defun doom-modeline-buffer-file-state-icon (icon unicode text face) (defun doom-modeline-buffer-file-state-icon (icon unicode text face)
"Displays an ICON of buffer state with FACE. "Displays an ICON of buffer state with FACE.
@ -310,21 +309,20 @@ Uses `all-the-icons-material' to fetch the icon."
(advice-add #'org-edit-src-save :after #'doom-modeline-update-buffer-file-state-icon) (advice-add #'org-edit-src-save :after #'doom-modeline-update-buffer-file-state-icon)
(advice-add #'symbol-overlay-rename :after #'doom-modeline-update-buffer-file-state-icon) (advice-add #'symbol-overlay-rename :after #'doom-modeline-update-buffer-file-state-icon)
(when (>= emacs-major-version 26) (doom-modeline-add-variable-watcher
(add-variable-watcher 'buffer-read-only
'buffer-read-only (lambda (_sym val _op _where)
(lambda (_sym val _op _where) (setq buffer-read-only val)
(setq buffer-read-only val) (doom-modeline-update-buffer-file-state-icon)))
(doom-modeline-update-buffer-file-state-icon)))
(add-variable-watcher (doom-modeline-add-variable-watcher
'doom-modeline-icon 'doom-modeline-icon
(lambda (_sym val op _where) (lambda (_sym val op _where)
(when (eq op 'set) (when (eq op 'set)
(setq doom-modeline-icon val) (setq doom-modeline-icon val)
(dolist (buf (buffer-list)) (dolist (buf (buffer-list))
(with-current-buffer buf (with-current-buffer buf
(doom-modeline-update-buffer-file-state-icon))))))) (doom-modeline-update-buffer-file-state-icon))))))
(defvar-local doom-modeline--buffer-file-name nil) (defvar-local doom-modeline--buffer-file-name nil)
(defun doom-modeline-update-buffer-file-name (&rest _) (defun doom-modeline-update-buffer-file-name (&rest _)
@ -367,16 +365,15 @@ mouse-1: Previous buffer\nmouse-3: Next buffer"
(doom-modeline-update-buffer-file-name)))) (doom-modeline-update-buffer-file-name))))
(add-hook 'focus-in-hook #'doom-modeline-update-buffer-file-name t))) (add-hook 'focus-in-hook #'doom-modeline-update-buffer-file-name t)))
(when (>= emacs-major-version 26) (doom-modeline-add-variable-watcher
(add-variable-watcher 'doom-modeline-buffer-file-name-style
'doom-modeline-buffer-file-name-style (lambda (_sym val op _where)
(lambda (_sym val op _where) (when (eq op 'set)
(when (eq op 'set) (setq doom-modeline-buffer-file-name-style val)
(setq doom-modeline-buffer-file-name-style val) (dolist (buf (buffer-list))
(dolist (buf (buffer-list)) (with-current-buffer buf
(with-current-buffer buf (when buffer-file-name
(when buffer-file-name (doom-modeline-update-buffer-file-name)))))))
(doom-modeline-update-buffer-file-name))))))))
;; Optimize: just update the face of the buffer name in `after-change-functions', since ;; Optimize: just update the face of the buffer name in `after-change-functions', since
;; `doom-modeline--buffer-file-name' may consume lots of CPU if it's called too frequently. ;; `doom-modeline--buffer-file-name' may consume lots of CPU if it's called too frequently.
@ -620,24 +617,23 @@ Uses `all-the-icons-octicon' to fetch the icon."
(add-hook 'after-save-hook #'doom-modeline-update-vcs-icon) (add-hook 'after-save-hook #'doom-modeline-update-vcs-icon)
(advice-add #'vc-refresh-state :after #'doom-modeline-update-vcs-icon) (advice-add #'vc-refresh-state :after #'doom-modeline-update-vcs-icon)
(when (>= emacs-major-version 26) (doom-modeline-add-variable-watcher
(add-variable-watcher 'doom-modeline-icon
'doom-modeline-icon (lambda (_sym val op _where)
(lambda (_sym val op _where) (when (eq op 'set)
(when (eq op 'set) (setq doom-modeline-icon val)
(setq doom-modeline-icon val) (dolist (buf (buffer-list))
(dolist (buf (buffer-list)) (with-current-buffer buf
(with-current-buffer buf (doom-modeline-update-vcs-icon))))))
(doom-modeline-update-vcs-icon))))))
(add-variable-watcher (doom-modeline-add-variable-watcher
'doom-modeline-unicode-fallback 'doom-modeline-unicode-fallback
(lambda (_sym val op _where) (lambda (_sym val op _where)
(when (eq op 'set) (when (eq op 'set)
(setq doom-modeline-unicode-fallback val) (setq doom-modeline-unicode-fallback val)
(dolist (buf (buffer-list)) (dolist (buf (buffer-list))
(with-current-buffer buf (with-current-buffer buf
(doom-modeline-update-vcs-icon))))))) (doom-modeline-update-vcs-icon))))))
(defvar-local doom-modeline--vcs-text nil) (defvar-local doom-modeline--vcs-text nil)
(defun doom-modeline-update-vcs-text (&rest _) (defun doom-modeline-update-vcs-text (&rest _)
@ -747,26 +743,25 @@ mouse-2: Show help for minor mode")
(add-hook 'flycheck-status-changed-functions #'doom-modeline-update-flycheck-icon) (add-hook 'flycheck-status-changed-functions #'doom-modeline-update-flycheck-icon)
(add-hook 'flycheck-mode-hook #'doom-modeline-update-flycheck-icon) (add-hook 'flycheck-mode-hook #'doom-modeline-update-flycheck-icon)
(when (>= emacs-major-version 26) (doom-modeline-add-variable-watcher
(add-variable-watcher 'doom-modeline-icon
'doom-modeline-icon (lambda (_sym val op _where)
(lambda (_sym val op _where) (when (eq op 'set)
(when (eq op 'set) (setq doom-modeline-icon val)
(setq doom-modeline-icon val) (dolist (buf (buffer-list))
(dolist (buf (buffer-list)) (with-current-buffer buf
(with-current-buffer buf (when (bound-and-true-p flycheck-mode)
(when (bound-and-true-p flycheck-mode) (flycheck-buffer)))))))
(flycheck-buffer)))))))
(add-variable-watcher (doom-modeline-add-variable-watcher
'doom-modeline-unicode-fallback 'doom-modeline-unicode-fallback
(lambda (_sym val op _where) (lambda (_sym val op _where)
(when (eq op 'set) (when (eq op 'set)
(setq doom-modeline-unicode-fallback val) (setq doom-modeline-unicode-fallback val)
(dolist (buf (buffer-list)) (dolist (buf (buffer-list))
(with-current-buffer buf (with-current-buffer buf
(when (bound-and-true-p flycheck-mode) (when (bound-and-true-p flycheck-mode)
(flycheck-buffer)))))))) (flycheck-buffer)))))))
(defvar-local doom-modeline--flycheck-text nil) (defvar-local doom-modeline--flycheck-text nil)
(defun doom-modeline-update-flycheck-text (&optional status) (defun doom-modeline-update-flycheck-text (&optional status)
@ -903,26 +898,25 @@ mouse-2: Show help for minor mode"
map)))))) map))))))
(advice-add #'flymake--handle-report :after #'doom-modeline-update-flymake-icon) (advice-add #'flymake--handle-report :after #'doom-modeline-update-flymake-icon)
(when (>= emacs-major-version 26) (doom-modeline-add-variable-watcher
(add-variable-watcher 'doom-modeline-icon
'doom-modeline-icon (lambda (_sym val op _where)
(lambda (_sym val op _where) (when (eq op 'set)
(when (eq op 'set) (setq doom-modeline-icon val)
(setq doom-modeline-icon val) (dolist (buf (buffer-list))
(dolist (buf (buffer-list)) (with-current-buffer buf
(with-current-buffer buf (when (bound-and-true-p flymake-mode)
(when (bound-and-true-p flymake-mode) (flymake-start)))))))
(flymake-start)))))))
(add-variable-watcher (doom-modeline-add-variable-watcher
'doom-modeline-unicode-fallback 'doom-modeline-unicode-fallback
(lambda (_sym val op _where) (lambda (_sym val op _where)
(when (eq op 'set) (when (eq op 'set)
(setq doom-modeline-unicode-fallback val) (setq doom-modeline-unicode-fallback val)
(dolist (buf (buffer-list)) (dolist (buf (buffer-list))
(with-current-buffer buf (with-current-buffer buf
(when (bound-and-true-p flymake-mode) (when (bound-and-true-p flymake-mode)
(flymake-start)))))))) (flymake-start)))))))
(defvar-local doom-modeline--flymake-text nil) (defvar-local doom-modeline--flymake-text nil)
(defun doom-modeline-update-flymake-text (&rest _) (defun doom-modeline-update-flymake-text (&rest _)
@ -1287,18 +1281,17 @@ of active `multiple-cursors'."
doom-modeline--bar-inactive doom-modeline--bar-inactive
(doom-modeline--make-xpm 'doom-modeline-bar-inactive width height)))) (doom-modeline--make-xpm 'doom-modeline-bar-inactive width height))))
(when (>= emacs-major-version 26) (doom-modeline-add-variable-watcher
(add-variable-watcher 'doom-modeline-height
'doom-modeline-height (lambda (_sym val op _where)
(lambda (_sym val op _where) (when (and (eq op 'set) (integerp val))
(when (and (eq op 'set) (integerp val)) (doom-modeline-refresh-bars doom-modeline-bar-width val))))
(doom-modeline-refresh-bars doom-modeline-bar-width val))))
(add-variable-watcher (doom-modeline-add-variable-watcher
'doom-modeline-bar-width 'doom-modeline-bar-width
(lambda (_sym val op _where) (lambda (_sym val op _where)
(when (and (eq op 'set) (integerp val)) (when (and (eq op 'set) (integerp val))
(doom-modeline-refresh-bars val doom-modeline-height))))) (doom-modeline-refresh-bars val doom-modeline-height))))
(add-hook 'after-setting-font-hook #'doom-modeline-refresh-bars) (add-hook 'after-setting-font-hook #'doom-modeline-refresh-bars)
(add-hook 'window-configuration-change-hook #'doom-modeline-refresh-bars) (add-hook 'window-configuration-change-hook #'doom-modeline-refresh-bars)
@ -1460,18 +1453,17 @@ See `column-number-indicator-zero-based'.")
"Specification of \"percentage offset\" of window through buffer. "Specification of \"percentage offset\" of window through buffer.
See `mode-line-percent-position'.") See `mode-line-percent-position'.")
(when (>= emacs-major-version 26) (doom-modeline-add-variable-watcher
(add-variable-watcher 'column-number-indicator-zero-based
'column-number-indicator-zero-based (lambda (_sym val op _where)
(lambda (_sym val op _where) (when (eq op 'set)
(when (eq op 'set) (setq doom-modeline-column-zero-based val))))
(setq doom-modeline-column-zero-based val))))
(add-variable-watcher (doom-modeline-add-variable-watcher
'mode-line-percent-position 'mode-line-percent-position
(lambda (_sym val op _where) (lambda (_sym val op _where)
(when (eq op 'set) (when (eq op 'set)
(setq doom-modeline-percent-position val))))) (setq doom-modeline-percent-position val))))
(doom-modeline-def-segment buffer-position (doom-modeline-def-segment buffer-position
"The buffer position information." "The buffer position information."
@ -1892,13 +1884,12 @@ Example:
doom-modeline-github-interval doom-modeline-github-interval
#'doom-modeline--github-fetch-notifications)))) #'doom-modeline--github-fetch-notifications))))
(when (>= emacs-major-version 26) (doom-modeline-add-variable-watcher
(add-variable-watcher 'doom-modeline-github
'doom-modeline-github (lambda (_sym val op _where)
(lambda (_sym val op _where) (when (eq op 'set)
(when (eq op 'set) (setq doom-modeline-github val)
(setq doom-modeline-github val) (doom-modeline-github-timer))))
(doom-modeline-github-timer)))))
(doom-modeline-github-timer) (doom-modeline-github-timer)
@ -2259,68 +2250,67 @@ mouse-3: Switch to next unread buffer")))
(defvar doom-modeline--battery-status nil) (defvar doom-modeline--battery-status nil)
(defun doom-modeline-update-battery-status () (defun doom-modeline-update-battery-status ()
"Update battery status." "Update battery status."
(setq doom-modeline--battery-status (setq doom-modeline--battery-status
(when (bound-and-true-p display-battery-mode) (when (bound-and-true-p display-battery-mode)
(let* ((data (and (bound-and-true-p battery-status-function) (let* ((data (and (bound-and-true-p battery-status-function)
(funcall battery-status-function))) (funcall battery-status-function)))
(charging? (string-equal "AC" (cdr (assoc ?L data)))) (charging? (string-equal "AC" (cdr (assoc ?L data))))
(percentage (car (read-from-string (or (cdr (assq ?p data)) "ERR")))) (percentage (car (read-from-string (or (cdr (assq ?p data)) "ERR"))))
(valid-percentage? (and (numberp percentage) (valid-percentage? (and (numberp percentage)
(>= percentage 0) (>= percentage 0)
(<= percentage battery-mode-line-limit))) (<= percentage battery-mode-line-limit)))
(face (if valid-percentage? (face (if valid-percentage?
(cond (charging? 'doom-modeline-battery-charging) (cond (charging? 'doom-modeline-battery-charging)
((< percentage battery-load-critical) 'doom-modeline-battery-critical) ((< percentage battery-load-critical) 'doom-modeline-battery-critical)
((< percentage 25) 'doom-modeline-battery-warning) ((< percentage 25) 'doom-modeline-battery-warning)
((< percentage 95) 'doom-modeline-battery-normal) ((< percentage 95) 'doom-modeline-battery-normal)
(t 'doom-modeline-battery-full)) (t 'doom-modeline-battery-full))
'doom-modeline-battery-error)) 'doom-modeline-battery-error))
(icon (if valid-percentage? (icon (if valid-percentage?
(cond (charging? (cond (charging?
(doom-modeline-icon 'alltheicon "battery-charging" "🔋" "+" (doom-modeline-icon 'alltheicon "battery-charging" "🔋" "+"
face :height 1.4 :v-adjust -0.1)) face :height 1.4 :v-adjust -0.1))
((> percentage 95) ((> percentage 95)
(doom-modeline-icon 'faicon "battery-full" "🔋" "-" (doom-modeline-icon 'faicon "battery-full" "🔋" "-"
face :v-adjust -0.0575)) face :v-adjust -0.0575))
((> percentage 70) ((> percentage 70)
(doom-modeline-icon 'faicon "battery-three-quarters" "🔋" "-" (doom-modeline-icon 'faicon "battery-three-quarters" "🔋" "-"
face :v-adjust -0.0575)) face :v-adjust -0.0575))
((> percentage 40) ((> percentage 40)
(doom-modeline-icon 'faicon "battery-half" "🔋" "-" (doom-modeline-icon 'faicon "battery-half" "🔋" "-"
face :v-adjust -0.0575)) face :v-adjust -0.0575))
((> percentage battery-load-critical) ((> percentage battery-load-critical)
(doom-modeline-icon 'faicon "battery-quarter" "🔋" "-" (doom-modeline-icon 'faicon "battery-quarter" "🔋" "-"
face :v-adjust -0.0575)) face :v-adjust -0.0575))
(t (doom-modeline-icon 'faicon "battery-empty" "🔋" "!" (t (doom-modeline-icon 'faicon "battery-empty" "🔋" "!"
face :v-adjust -0.0575))) face :v-adjust -0.0575)))
(doom-modeline-icon 'faicon "battery-empty" "" "N/A" (doom-modeline-icon 'faicon "battery-empty" "" "N/A"
face :v-adjust -0.0575))) face :v-adjust -0.0575)))
(text (if valid-percentage? (format "%d%%%%" percentage) "")) (text (if valid-percentage? (format "%d%%%%" percentage) ""))
(help-echo (if (and battery-echo-area-format data valid-percentage?) (help-echo (if (and battery-echo-area-format data valid-percentage?)
(battery-format battery-echo-area-format data) (battery-format battery-echo-area-format data)
"Battery status not available"))) "Battery status not available")))
(cons (propertize icon 'help-echo help-echo) (cons (propertize icon 'help-echo help-echo)
(propertize text 'face face 'help-echo help-echo)))))) (propertize text 'face face 'help-echo help-echo))))))
(when (>= emacs-major-version 26) (doom-modeline-add-variable-watcher
(add-variable-watcher 'doom-modeline-icon
'doom-modeline-icon (lambda (_sym val op _where)
(lambda (_sym val op _where) (when (eq op 'set)
(when (eq op 'set) (setq doom-modeline-icon val)
(setq doom-modeline-icon val) (dolist (buf (buffer-list))
(dolist (buf (buffer-list)) (with-current-buffer buf
(with-current-buffer buf (doom-modeline-update-battery-status))))))
(doom-modeline-update-battery-status))))))
(add-variable-watcher (doom-modeline-add-variable-watcher
'doom-modeline-unicode-fallback 'doom-modeline-unicode-fallback
(lambda (_sym val op _where) (lambda (_sym val op _where)
(when (eq op 'set) (when (eq op 'set)
(setq doom-modeline-unicode-fallback val) (setq doom-modeline-unicode-fallback val)
(dolist (buf (buffer-list)) (dolist (buf (buffer-list))
(with-current-buffer buf (with-current-buffer buf
(doom-modeline-update-battery-status))))))) (doom-modeline-update-battery-status))))))
(doom-modeline-def-segment battery (doom-modeline-def-segment battery
"Display battery status." "Display battery status."