[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 _)
"Update file icon in mode-line."
(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))
(icon (doom-modeline-icon-for-mode major-mode :height height)))
(if (symbolp icon)
@ -810,20 +811,32 @@ buffer where knowing the current project directory is important."
(unless (symbolp icon)
(propertize icon
'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 'after-change-major-mode-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.
TEXT is the alternative if it is not applicable.
Uses `all-the-icons-material' to fetch the icon."
(if doom-modeline-icon
(when icon
(doom-modeline-icon-material
icon
:face (if (doom-modeline--active) face)
: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)
(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
(doom-modeline-buffer-file-state-icon
"lock"
"%1+"
'doom-modeline-warning))
((buffer-modified-p)
(doom-modeline-buffer-file-state-icon
"save"
"%1*"
'doom-modeline-buffer-modified))
((and buffer-file-name
(not (file-exists-p buffer-file-name)))
(doom-modeline-buffer-file-state-icon
"do_not_disturb_alt"
"!"
'doom-modeline-urgent))
((buffer-narrowed-p)
(doom-modeline-buffer-file-state-icon
"vertical_align_center"
"><"
'doom-modeline-warning)))))
(add-hook 'find-file-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)
(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
'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)
(doom-modeline-update-buffer-file-state-icon)))))
(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)))
;; state icon
(when doom-modeline-icon
(when-let ((icon (or doom-modeline--buffer-file-state-icon
(doom-modeline-update-buffer-file-state-icon))))
(concat
(if active
icon
(propertize icon
'face `(:height
'face
(if doom-modeline-icon
`(:height
,(doom-modeline-icon-height 1.3)
:family
,(all-the-icons-icon-family icon)
:inherit)))
doom-modeline-vspc)))
:inherit)
'mode-line-inactive)))
doom-modeline-vspc))
;; 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))))
(if active
name
@ -1048,12 +1073,10 @@ mouse-1: Display minor modes menu"
TEXT is the alternative if it is not applicable.
Uses `all-the-icons-octicon' to fetch the icon."
(if doom-modeline-icon
(if icon
(doom-modeline-icon-octicon icon :face face :v-adjust (or voffset -0.1))
"")
(if text
(propertize text 'face face)
"")))
(when icon
(doom-modeline-icon-octicon icon :face face :v-adjust (or voffset -0.1)))
(when text
(propertize text 'face face))))
(defvar-local doom-modeline--vcs-icon nil)
(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)
(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)
(defun doom-modeline--update-vcs-text (&rest _)
(defun doom-modeline-update-vcs-text (&rest _)
"Update text of vsc state in mode-line."
(setq doom-modeline--vcs-text
(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))
'doom-modeline-urgent)
(t 'doom-modeline-info)))))))
(add-hook 'find-file-hook #'doom-modeline--update-vcs-text t)
(add-hook 'after-save-hook #'doom-modeline--update-vcs-text)
(advice-add #'vc-refresh-state :after #'doom-modeline--update-vcs-text)
(add-hook 'find-file-hook #'doom-modeline-update-vcs-text t)
(add-hook 'after-save-hook #'doom-modeline-update-vcs-text)
(advice-add #'vc-refresh-state :after #'doom-modeline-update-vcs-text)
(doom-modeline-def-segment vcs
"Displays the current branch, colored based on its state."
(let ((active (doom-modeline--active)))
(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
" "
(if active
@ -1128,24 +1159,22 @@ Uses `all-the-icons-octicon' to fetch the icon."
TEXT is the alternative if it is not applicable.
Uses `all-the-icons-material' to fetch the icon."
(if doom-modeline-icon
(if icon
(doom-modeline-icon-material icon :face face :height 1.1 :v-adjust (or voffset -0.225))
"")
(if text
(propertize text 'face face)
"")))
(when icon
(doom-modeline-icon-material icon :face face :height 1.1 :v-adjust (or voffset -0.225)))
(when text
(propertize text 'face face))))
(defun doom-modeline-checker-text (text &optional face)
"Displays TEXT with FACE."
(if text
(propertize text 'face face)
""))
(when text
(propertize text 'face face)))
(defvar-local doom-modeline--flycheck-icon nil)
(defun doom-modeline-update-flycheck-icon (&optional status)
"Update flycheck icon via STATUS."
(setq doom-modeline--flycheck-icon
(propertize
(when-let
((icon
(pcase status
(`finished (if 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)
(.warning 'doom-modeline-warning)
(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))
(`no-checker (doom-modeline-checker-icon "sim_card_alert" "?" 'font-lock-doc-face))
(`errored (doom-modeline-checker-icon "sim_card_alert" "!" 'doom-modeline-urgent))
(`interrupted (doom-modeline-checker-icon "pause" "!" 'font-lock-doc-face))
(`suspicious (doom-modeline-checker-icon "priority_high" "!" 'doom-modeline-urgent))
(_ ""))
(_ nil))))
(propertize
icon
'help-echo (concat "Flycheck\n"
(pcase status
('finished
"mouse-1: Display minor mode menu
('finished "mouse-1: Display minor mode menu
mouse-2: Show help for minor mode")
('running "Running...")
('no-checker "No Checker")
@ -1178,17 +1208,27 @@ mouse-2: Show help for minor mode")
(lambda ()
(interactive)
(describe-function 'flycheck-mode)))
map))))
map)))))
(add-hook 'flycheck-status-changed-functions #'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)
(defun doom-modeline-update-flycheck-text (&optional status)
"Update flycheck text via STATUS."
(setq doom-modeline--flycheck-text
(propertize
(when-let
((text
(pcase status
(`finished (if flycheck-current-errors
(`finished (when flycheck-current-errors
(let-alist (flycheck-count-errors flycheck-current-errors)
(let ((error (or .error 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-warning)
(doom-modeline-checker-text (number-to-string info)
'doom-modeline-info))))
""))
(`running "")
'doom-modeline-info))))))
(`running nil)
(`no-checker (doom-modeline-checker-text "-" 'font-lock-doc-face))
(`errored (doom-modeline-checker-text "Error" 'doom-modeline-urgent))
(`interrupted (doom-modeline-checker-text "Interrupted" 'font-lock-doc-face))
(`suspicious (doom-modeline-checker-text "Suspicious" 'doom-modeline-urgent))
(_ ""))
(_ nil))))
(propertize
text
'help-echo (pcase status
('finished
(concat
@ -1239,7 +1280,7 @@ wheel-up/wheel-down: Previous/next error"))
(interactive "e")
(with-selected-window (posn-window (event-start event))
(flycheck-next-error 1))))
map))))
map)))))
(add-hook 'flycheck-status-changed-functions #'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)))
(flymake--backend-state-diags state)))
flymake--backend-state)
(propertize
(when-let
((icon
(cond
(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))
@ -1277,7 +1319,9 @@ wheel-up/wheel-down: Previous/next error"))
(cond ((> .error 0) 'doom-modeline-urgent)
((> .warning 0) 'doom-modeline-warning)
(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"
(cond
(some-waiting "Running...")
@ -1295,9 +1339,18 @@ mouse-2: Show help for minor mode"
(lambda ()
(interactive)
(describe-function 'flymake-mode)))
map)))))
map))))))
(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)
(defun doom-modeline-update-flymake-text (&rest _)
"Update flymake text."
@ -1320,20 +1373,22 @@ mouse-2: Show help for minor mode"
(let ((.error (length (gethash :error diags-by-type)))
(.warning (length (gethash :warning diags-by-type)))
(.note (length (gethash :note diags-by-type))))
(propertize
(when-let
((text
(cond
(some-waiting "Running..." "")
((null known) (doom-modeline-checker-text "-" 'font-lock-doc-face))
(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"
(doom-modeline-checker-text (number-to-string .error)
'doom-modeline-urgent)
(doom-modeline-checker-text (number-to-string .warning)
'doom-modeline-warning)
(doom-modeline-checker-text (number-to-string .note)
'doom-modeline-info))
"")))
'doom-modeline-info)))))))
(propertize
text
'help-echo (cond
(some-waiting "Running...")
((null known) "No Checker")
@ -1358,7 +1413,7 @@ wheel-up/wheel-down: Previous/next problem"
(interactive "e")
(with-selected-window (posn-window (event-start event))
(flymake-goto-next-error 1 nil t))))
map))))))
map)))))))
(advice-add #'flymake--handle-report :after #'doom-modeline-update-flymake-text)
(doom-modeline-def-segment checker
@ -1370,13 +1425,16 @@ icons."
`(,doom-modeline--flymake-icon . ,doom-modeline--flymake-text))
((bound-and-true-p flycheck-mode)
`(,doom-modeline--flycheck-icon . ,doom-modeline--flycheck-text)))))
(when-let ((icon (car seg))
(let ((icon (car seg))
(text (cdr seg)))
(concat
(if vc-mode " " " ")
(if active
(concat icon doom-modeline-vspc text)
(concat icon
(when (and icon text) doom-modeline-vspc)
text)
(concat
(when icon
(propertize icon
'face
(if doom-modeline-icon
@ -1385,9 +1443,10 @@ icons."
:family
,(all-the-icons-icon-family icon)
:inherit)
'mode-line-inactive))
doom-modeline-vspc
(propertize text 'face 'mode-line-inactive)))
'mode-line-inactive)))
(when (and icon text) doom-modeline-vspc)
(when text
(propertize text 'face 'mode-line-inactive))))
" "))))