[Enhancement] Display alternative texts while icons aren't applicable.

This commit is contained in:
Vincent Zhang 2019-01-17 00:10:02 +08:00
parent 4cc816c593
commit df8c25fcd9

View file

@ -803,6 +803,7 @@ buffer where knowing the current project directory is important."
(defun doom-modeline-update-buffer-file-icon (&rest _) (defun doom-modeline-update-buffer-file-icon (&rest _)
"Update file icon in mode-line." "Update file icon in mode-line."
(setq doom-modeline--buffer-file-icon (setq doom-modeline--buffer-file-icon
(when (and doom-modeline-icon doom-modeline-major-mode-icon)
(let* ((height (/ all-the-icons-scale-factor 1.3)) (let* ((height (/ all-the-icons-scale-factor 1.3))
(icon (doom-modeline-icon-for-mode major-mode :height height))) (icon (doom-modeline-icon-for-mode major-mode :height height)))
(if (symbolp icon) (if (symbolp icon)
@ -810,20 +811,32 @@ buffer where knowing the current project directory is important."
(unless (symbolp icon) (unless (symbolp icon)
(propertize icon (propertize icon
'help-echo (format "Major-mode: %s" mode-name) 'help-echo (format "Major-mode: %s" mode-name)
'display '(raise -0.125)))))) 'display '(raise -0.125)))))))
(add-hook 'find-file-hook #'doom-modeline-update-buffer-file-icon) (add-hook 'find-file-hook #'doom-modeline-update-buffer-file-icon)
(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)
(defun doom-modeline-buffer-file-state-icon (icon &optional face height voffset) (when (>= emacs-major-version 26)
(add-variable-watcher
'all-the-icons-scale-factor
(lambda (_sym val op _where)
(when (eq op 'set)
(setq all-the-icons-scale-factor val)
(doom-modeline-update-buffer-file-icon)))))
(defun doom-modeline-buffer-file-state-icon (icon &optional text face height voffset)
"Displays an ICON with FACE, HEIGHT and VOFFSET. "Displays an ICON with FACE, HEIGHT and VOFFSET.
TEXT is the alternative if it is not applicable.
Uses `all-the-icons-material' to fetch the icon." Uses `all-the-icons-material' to fetch the icon."
(if doom-modeline-icon
(when icon (when icon
(doom-modeline-icon-material (doom-modeline-icon-material
icon icon
:face (if (doom-modeline--active) face) :face (if (doom-modeline--active) face)
:height (or height 1.1) :height (or height 1.1)
:v-adjust (or voffset -0.225)))) :v-adjust (or voffset -0.225)))
(when text
(propertize text 'face face))))
(defvar-local doom-modeline--buffer-file-state-icon nil) (defvar-local doom-modeline--buffer-file-state-icon nil)
(defun doom-modeline-update-buffer-file-state-icon (&rest _) (defun doom-modeline-update-buffer-file-state-icon (&rest _)
@ -832,19 +845,23 @@ Uses `all-the-icons-material' to fetch the icon."
(cond (buffer-read-only (cond (buffer-read-only
(doom-modeline-buffer-file-state-icon (doom-modeline-buffer-file-state-icon
"lock" "lock"
"%1+"
'doom-modeline-warning)) 'doom-modeline-warning))
((buffer-modified-p) ((buffer-modified-p)
(doom-modeline-buffer-file-state-icon (doom-modeline-buffer-file-state-icon
"save" "save"
"%1*"
'doom-modeline-buffer-modified)) '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-buffer-file-state-icon (doom-modeline-buffer-file-state-icon
"do_not_disturb_alt" "do_not_disturb_alt"
"!"
'doom-modeline-urgent)) 'doom-modeline-urgent))
((buffer-narrowed-p) ((buffer-narrowed-p)
(doom-modeline-buffer-file-state-icon (doom-modeline-buffer-file-state-icon
"vertical_align_center" "vertical_align_center"
"><"
'doom-modeline-warning))))) 'doom-modeline-warning)))))
(add-hook 'find-file-hook #'doom-modeline-update-buffer-file-state-icon) (add-hook 'find-file-hook #'doom-modeline-update-buffer-file-state-icon)
(add-hook 'after-revert-hook #'doom-modeline-update-buffer-file-state-icon) (add-hook 'after-revert-hook #'doom-modeline-update-buffer-file-state-icon)
@ -867,12 +884,18 @@ Uses `all-the-icons-material' to fetch the icon."
(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-icon
(lambda (_sym val op _where)
(when (eq op 'set)
(setq doom-modeline-icon val)
(doom-modeline-update-buffer-file-state-icon))))
(add-variable-watcher (add-variable-watcher
'all-the-icons-scale-factor 'all-the-icons-scale-factor
(lambda (_sym val op _where) (lambda (_sym val op _where)
(when (eq op 'set) (when (eq op 'set)
(setq all-the-icons-scale-factor val) (setq all-the-icons-scale-factor val)
(doom-modeline-update-buffer-file-icon)
(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)
@ -922,22 +945,24 @@ directory, the file name, and its state (modified, read-only or non-existent)."
doom-modeline-vspc))) doom-modeline-vspc)))
;; state icon ;; state icon
(when doom-modeline-icon
(when-let ((icon (or doom-modeline--buffer-file-state-icon (when-let ((icon (or doom-modeline--buffer-file-state-icon
(doom-modeline-update-buffer-file-state-icon)))) (doom-modeline-update-buffer-file-state-icon))))
(concat (concat
(if active (if active
icon icon
(propertize icon (propertize icon
'face `(:height 'face
(if doom-modeline-icon
`(:height
,(doom-modeline-icon-height 1.3) ,(doom-modeline-icon-height 1.3)
:family :family
,(all-the-icons-icon-family icon) ,(all-the-icons-icon-family icon)
:inherit))) :inherit)
doom-modeline-vspc))) 'mode-line-inactive)))
doom-modeline-vspc))
;; buffer file name ;; buffer file name
(let ((name (or doom-modeline--buffer-file-name (when-let ((name (or doom-modeline--buffer-file-name
(doom-modeline-update-buffer-file-name)))) (doom-modeline-update-buffer-file-name))))
(if active (if active
name name
@ -1048,12 +1073,10 @@ mouse-1: Display minor modes menu"
TEXT is the alternative if it is not applicable. TEXT is the alternative if it is not applicable.
Uses `all-the-icons-octicon' to fetch the icon." Uses `all-the-icons-octicon' to fetch the icon."
(if doom-modeline-icon (if doom-modeline-icon
(if icon (when icon
(doom-modeline-icon-octicon icon :face face :v-adjust (or voffset -0.1)) (doom-modeline-icon-octicon icon :face face :v-adjust (or voffset -0.1)))
"") (when text
(if text (propertize text 'face face))))
(propertize text 'face face)
"")))
(defvar-local doom-modeline--vcs-icon nil) (defvar-local doom-modeline--vcs-icon nil)
(defun doom-modeline--update-vcs-icon (&rest _) (defun doom-modeline--update-vcs-icon (&rest _)
@ -1076,8 +1099,16 @@ 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)
(add-variable-watcher
'doom-modeline-icon
(lambda (_sym val op _where)
(when (eq op 'set)
(setq doom-modeline-icon val)
(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 _)
"Update text of vsc state in mode-line." "Update text of vsc state in mode-line."
(setq doom-modeline--vcs-text (setq doom-modeline--vcs-text
(when (and vc-mode buffer-file-name) (when (and vc-mode buffer-file-name)
@ -1089,15 +1120,15 @@ Uses `all-the-icons-octicon' to fetch the icon."
((memq state '(removed conflict unregistered)) ((memq state '(removed conflict unregistered))
'doom-modeline-urgent) 'doom-modeline-urgent)
(t 'doom-modeline-info))))))) (t 'doom-modeline-info)))))))
(add-hook 'find-file-hook #'doom-modeline--update-vcs-text t) (add-hook 'find-file-hook #'doom-modeline-update-vcs-text t)
(add-hook 'after-save-hook #'doom-modeline--update-vcs-text) (add-hook 'after-save-hook #'doom-modeline-update-vcs-text)
(advice-add #'vc-refresh-state :after #'doom-modeline--update-vcs-text) (advice-add #'vc-refresh-state :after #'doom-modeline-update-vcs-text)
(doom-modeline-def-segment vcs (doom-modeline-def-segment vcs
"Displays the current branch, colored based on its state." "Displays the current branch, colored based on its state."
(let ((active (doom-modeline--active))) (let ((active (doom-modeline--active)))
(when-let ((icon (or doom-modeline--vcs-icon (doom-modeline--update-vcs-icon))) (when-let ((icon (or doom-modeline--vcs-icon (doom-modeline--update-vcs-icon)))
(text (or doom-modeline--vcs-text (doom-modeline--update-vcs-text)))) (text (or doom-modeline--vcs-text (doom-modeline-update-vcs-text))))
(concat (concat
" " " "
(if active (if active
@ -1128,24 +1159,22 @@ Uses `all-the-icons-octicon' to fetch the icon."
TEXT is the alternative if it is not applicable. TEXT is the alternative if it is not applicable.
Uses `all-the-icons-material' to fetch the icon." Uses `all-the-icons-material' to fetch the icon."
(if doom-modeline-icon (if doom-modeline-icon
(if icon (when icon
(doom-modeline-icon-material icon :face face :height 1.1 :v-adjust (or voffset -0.225)) (doom-modeline-icon-material icon :face face :height 1.1 :v-adjust (or voffset -0.225)))
"") (when text
(if text (propertize text 'face face))))
(propertize text 'face face)
"")))
(defun doom-modeline-checker-text (text &optional face) (defun doom-modeline-checker-text (text &optional face)
"Displays TEXT with FACE." "Displays TEXT with FACE."
(if text (when text
(propertize text 'face face) (propertize text 'face face)))
""))
(defvar-local doom-modeline--flycheck-icon nil) (defvar-local doom-modeline--flycheck-icon nil)
(defun doom-modeline-update-flycheck-icon (&optional status) (defun doom-modeline-update-flycheck-icon (&optional status)
"Update flycheck icon via STATUS." "Update flycheck icon via STATUS."
(setq doom-modeline--flycheck-icon (setq doom-modeline--flycheck-icon
(propertize (when-let
((icon
(pcase status (pcase status
(`finished (if flycheck-current-errors (`finished (if flycheck-current-errors
(let-alist (flycheck-count-errors flycheck-current-errors) (let-alist (flycheck-count-errors flycheck-current-errors)
@ -1153,17 +1182,18 @@ Uses `all-the-icons-material' to fetch the icon."
(cond (.error 'doom-modeline-urgent) (cond (.error 'doom-modeline-urgent)
(.warning 'doom-modeline-warning) (.warning 'doom-modeline-warning)
(t 'doom-modeline-info)))) (t 'doom-modeline-info))))
(doom-modeline-checker-icon "check" "*" 'doom-modeline-info))) (doom-modeline-checker-icon "check" "-" 'doom-modeline-info)))
(`running (doom-modeline-checker-icon "access_time" "*" 'font-lock-doc-face)) (`running (doom-modeline-checker-icon "access_time" "*" 'font-lock-doc-face))
(`no-checker (doom-modeline-checker-icon "sim_card_alert" "?" 'font-lock-doc-face)) (`no-checker (doom-modeline-checker-icon "sim_card_alert" "?" 'font-lock-doc-face))
(`errored (doom-modeline-checker-icon "sim_card_alert" "!" 'doom-modeline-urgent)) (`errored (doom-modeline-checker-icon "sim_card_alert" "!" 'doom-modeline-urgent))
(`interrupted (doom-modeline-checker-icon "pause" "!" 'font-lock-doc-face)) (`interrupted (doom-modeline-checker-icon "pause" "!" 'font-lock-doc-face))
(`suspicious (doom-modeline-checker-icon "priority_high" "!" 'doom-modeline-urgent)) (`suspicious (doom-modeline-checker-icon "priority_high" "!" 'doom-modeline-urgent))
(_ "")) (_ nil))))
(propertize
icon
'help-echo (concat "Flycheck\n" 'help-echo (concat "Flycheck\n"
(pcase status (pcase status
('finished ('finished "mouse-1: Display minor mode menu
"mouse-1: Display minor mode menu
mouse-2: Show help for minor mode") mouse-2: Show help for minor mode")
('running "Running...") ('running "Running...")
('no-checker "No Checker") ('no-checker "No Checker")
@ -1178,17 +1208,27 @@ mouse-2: Show help for minor mode")
(lambda () (lambda ()
(interactive) (interactive)
(describe-function 'flycheck-mode))) (describe-function 'flycheck-mode)))
map)))) map)))))
(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)
(add-variable-watcher
'doom-modeline-icon
(lambda (_sym val op _where)
(when (eq op 'set)
(setq doom-modeline-icon val)
(when (bound-and-true-p flycheck-mode)
(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)
"Update flycheck text via STATUS." "Update flycheck text via STATUS."
(setq doom-modeline--flycheck-text (setq doom-modeline--flycheck-text
(propertize (when-let
((text
(pcase status (pcase status
(`finished (if flycheck-current-errors (`finished (when flycheck-current-errors
(let-alist (flycheck-count-errors flycheck-current-errors) (let-alist (flycheck-count-errors flycheck-current-errors)
(let ((error (or .error 0)) (let ((error (or .error 0))
(warning (or .warning 0)) (warning (or .warning 0))
@ -1199,14 +1239,15 @@ mouse-2: Show help for minor mode")
(doom-modeline-checker-text (number-to-string warning) (doom-modeline-checker-text (number-to-string warning)
'doom-modeline-warning) 'doom-modeline-warning)
(doom-modeline-checker-text (number-to-string info) (doom-modeline-checker-text (number-to-string info)
'doom-modeline-info)))) 'doom-modeline-info))))))
"")) (`running nil)
(`running "")
(`no-checker (doom-modeline-checker-text "-" 'font-lock-doc-face)) (`no-checker (doom-modeline-checker-text "-" 'font-lock-doc-face))
(`errored (doom-modeline-checker-text "Error" 'doom-modeline-urgent)) (`errored (doom-modeline-checker-text "Error" 'doom-modeline-urgent))
(`interrupted (doom-modeline-checker-text "Interrupted" 'font-lock-doc-face)) (`interrupted (doom-modeline-checker-text "Interrupted" 'font-lock-doc-face))
(`suspicious (doom-modeline-checker-text "Suspicious" 'doom-modeline-urgent)) (`suspicious (doom-modeline-checker-text "Suspicious" 'doom-modeline-urgent))
(_ "")) (_ nil))))
(propertize
text
'help-echo (pcase status 'help-echo (pcase status
('finished ('finished
(concat (concat
@ -1239,7 +1280,7 @@ wheel-up/wheel-down: Previous/next error"))
(interactive "e") (interactive "e")
(with-selected-window (posn-window (event-start event)) (with-selected-window (posn-window (event-start event))
(flycheck-next-error 1)))) (flycheck-next-error 1))))
map)))) map)))))
(add-hook 'flycheck-status-changed-functions #'doom-modeline-update-flycheck-text) (add-hook 'flycheck-status-changed-functions #'doom-modeline-update-flycheck-text)
(add-hook 'flycheck-mode-hook #'doom-modeline-update-flycheck-text) (add-hook 'flycheck-mode-hook #'doom-modeline-update-flycheck-text)
@ -1264,7 +1305,8 @@ wheel-up/wheel-down: Previous/next error"))
diags-by-type))) diags-by-type)))
(flymake--backend-state-diags state))) (flymake--backend-state-diags state)))
flymake--backend-state) flymake--backend-state)
(propertize (when-let
((icon
(cond (cond
(some-waiting (doom-modeline-checker-icon "access_time" "*" 'font-lock-doc-face)) (some-waiting (doom-modeline-checker-icon "access_time" "*" 'font-lock-doc-face))
((null known) (doom-modeline-checker-icon "sim_card_alert" "?" 'font-lock-doc-face)) ((null known) (doom-modeline-checker-icon "sim_card_alert" "?" 'font-lock-doc-face))
@ -1277,7 +1319,9 @@ wheel-up/wheel-down: Previous/next error"))
(cond ((> .error 0) 'doom-modeline-urgent) (cond ((> .error 0) 'doom-modeline-urgent)
((> .warning 0) 'doom-modeline-warning) ((> .warning 0) 'doom-modeline-warning)
(t 'doom-modeline-info))) (t 'doom-modeline-info)))
(doom-modeline-checker-icon "check" "*" 'doom-modeline-info))))) (doom-modeline-checker-icon "check" "-" 'doom-modeline-info)))))))
(propertize
icon
'help-echo (concat "Flymake\n" 'help-echo (concat "Flymake\n"
(cond (cond
(some-waiting "Running...") (some-waiting "Running...")
@ -1295,9 +1339,18 @@ mouse-2: Show help for minor mode"
(lambda () (lambda ()
(interactive) (interactive)
(describe-function 'flymake-mode))) (describe-function 'flymake-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)
(add-variable-watcher
'doom-modeline-icon
(lambda (_sym val op _where)
(when (eq op 'set)
(setq doom-modeline-icon val)
(when (bound-and-true-p flymake-mode)
(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 _)
"Update flymake text." "Update flymake text."
@ -1320,20 +1373,22 @@ mouse-2: Show help for minor mode"
(let ((.error (length (gethash :error diags-by-type))) (let ((.error (length (gethash :error diags-by-type)))
(.warning (length (gethash :warning diags-by-type))) (.warning (length (gethash :warning diags-by-type)))
(.note (length (gethash :note diags-by-type)))) (.note (length (gethash :note diags-by-type))))
(propertize (when-let
((text
(cond (cond
(some-waiting "Running..." "") (some-waiting "Running..." "")
((null known) (doom-modeline-checker-text "-" 'font-lock-doc-face)) ((null known) (doom-modeline-checker-text "-" 'font-lock-doc-face))
(all-disabled (doom-modeline-checker-text "-" 'doom-modeline-urgent)) (all-disabled (doom-modeline-checker-text "-" 'doom-modeline-urgent))
(t (if (> (+ .error .warning .note) 0) (t (when (> (+ .error .warning .note) 0)
(format "%s/%s/%s" (format "%s/%s/%s"
(doom-modeline-checker-text (number-to-string .error) (doom-modeline-checker-text (number-to-string .error)
'doom-modeline-urgent) 'doom-modeline-urgent)
(doom-modeline-checker-text (number-to-string .warning) (doom-modeline-checker-text (number-to-string .warning)
'doom-modeline-warning) 'doom-modeline-warning)
(doom-modeline-checker-text (number-to-string .note) (doom-modeline-checker-text (number-to-string .note)
'doom-modeline-info)) 'doom-modeline-info)))))))
""))) (propertize
text
'help-echo (cond 'help-echo (cond
(some-waiting "Running...") (some-waiting "Running...")
((null known) "No Checker") ((null known) "No Checker")
@ -1358,7 +1413,7 @@ wheel-up/wheel-down: Previous/next problem"
(interactive "e") (interactive "e")
(with-selected-window (posn-window (event-start event)) (with-selected-window (posn-window (event-start event))
(flymake-goto-next-error 1 nil t)))) (flymake-goto-next-error 1 nil t))))
map)))))) map)))))))
(advice-add #'flymake--handle-report :after #'doom-modeline-update-flymake-text) (advice-add #'flymake--handle-report :after #'doom-modeline-update-flymake-text)
(doom-modeline-def-segment checker (doom-modeline-def-segment checker
@ -1370,13 +1425,16 @@ icons."
`(,doom-modeline--flymake-icon . ,doom-modeline--flymake-text)) `(,doom-modeline--flymake-icon . ,doom-modeline--flymake-text))
((bound-and-true-p flycheck-mode) ((bound-and-true-p flycheck-mode)
`(,doom-modeline--flycheck-icon . ,doom-modeline--flycheck-text))))) `(,doom-modeline--flycheck-icon . ,doom-modeline--flycheck-text)))))
(when-let ((icon (car seg)) (let ((icon (car seg))
(text (cdr seg))) (text (cdr seg)))
(concat (concat
(if vc-mode " " " ") (if vc-mode " " " ")
(if active (if active
(concat icon doom-modeline-vspc text) (concat icon
(when (and icon text) doom-modeline-vspc)
text)
(concat (concat
(when icon
(propertize icon (propertize icon
'face 'face
(if doom-modeline-icon (if doom-modeline-icon
@ -1385,9 +1443,10 @@ icons."
:family :family
,(all-the-icons-icon-family icon) ,(all-the-icons-icon-family icon)
:inherit) :inherit)
'mode-line-inactive)) 'mode-line-inactive)))
doom-modeline-vspc (when (and icon text) doom-modeline-vspc)
(propertize text 'face 'mode-line-inactive))) (when text
(propertize text 'face 'mode-line-inactive))))
" ")))) " "))))