Refactor.

This commit is contained in:
Vincent Zhang 2018-06-19 19:06:49 +08:00
parent 63c5cde9a3
commit d3a2928efa

View file

@ -90,16 +90,16 @@
else else
collect (list (intern (format "doom-modeline-segment--%s" (symbol-name seg)))))) collect (list (intern (format "doom-modeline-segment--%s" (symbol-name seg))))))
(defmacro doom-modeline-def (name lhs &optional rhs) (defmacro doom-modeline-def-modeline (name lhs &optional rhs)
"Define a modeline format and byte-compiles it. "Define a modeline format and byte-compiles it.
NAME is a symbol to identify it (used by `doom-modeline' for retrieval). NAME is a symbol to identify it (used by `doom-modeline' for retrieval).
LHS and RHS are lists of symbols of modeline segments defined LHS and RHS are lists of symbols of modeline segments defined
with `doom-modeline-def-segment'. with `doom-modeline-def-segment'.
Example: Example:
(doom-modeline-def minimal (doom-modeline-def-modeline minimal
(bar matches \" \" buffer-info) (bar matches \" \" buffer-info)
(media-info major-mode)) (media-info major-mode))
(doom-modeline-set 'minimal t)" (doom-modeline-set-modeline 'minimal t)"
(let ((sym (intern (format "doom-modeline-format--%s" name))) (let ((sym (intern (format "doom-modeline-format--%s" name)))
(lhs-forms (doom-modeline--prepare-segments lhs)) (lhs-forms (doom-modeline--prepare-segments lhs))
(rhs-forms (doom-modeline--prepare-segments rhs))) (rhs-forms (doom-modeline--prepare-segments rhs)))
@ -125,7 +125,7 @@ Throws an error if it doesn't exist."
(when (functionp fn) (when (functionp fn)
`(:eval (,fn))))) `(:eval (,fn)))))
(defun doom-modeline-set (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)))
@ -313,24 +313,6 @@ active."
(advice-add #'select-window :after #'doom-modeline-set-selected-window) (advice-add #'select-window :after #'doom-modeline-set-selected-window)
;;
;; Bootstrap
;;
;; Show version string for multi-version managers like rvm, rbenv, pyenv, etc.
(defvar-local doom-modeline-env-version nil)
(defvar-local doom-modeline-env-command nil)
(add-hook 'focus-in-hook #'doom-modeline-update-env)
(add-hook 'find-file-hook #'doom-modeline-update-env)
(defun doom-modeline-update-env ()
"Update environment for mode-line."
(when doom-modeline-env-command
(let* ((default-directory (doom-modeline-project-root))
(s (shell-command-to-string doom-modeline-env-command)))
(setq doom-modeline-env-version (if (string-match "[ \t\n\r]+\\'" s)
(replace-match "" t t s)
s)))))
;; Only support python and ruby for now ;; Only support python and ruby for now
;; TODO torgeir ;; TODO torgeir
@ -516,12 +498,12 @@ directory, the file name, and its state (modified, read-only or non-existent)."
;; ;;
(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."
(propertize (propertize
"%b" "%b"
'face (cond ((and buffer-file-name (buffer-modified-p)) 'face (cond ((and buffer-file-name (buffer-modified-p))
'doom-modeline-buffer-modified) 'doom-modeline-buffer-modified)
((doom-modeline--active) 'doom-modeline-buffer-file)))) ((doom-modeline--active) 'doom-modeline-buffer-file))))
;; ;;
(doom-modeline-def-segment buffer-encoding (doom-modeline-def-segment buffer-encoding
@ -616,22 +598,22 @@ directory, the file name, and its state (modified, read-only or non-existent)."
(if vc-mode " " " "))) (if vc-mode " " " ")))
(doom-modeline-def-segment flycheck (doom-modeline-def-segment flycheck
"Displays color-coded flycheck error status in the current buffer with pretty "Displays color-coded flycheck error status in the current buffer with pretty
icons." icons."
(when (boundp 'flycheck-last-status-change) (when (boundp 'flycheck-last-status-change)
(pcase flycheck-last-status-change (pcase flycheck-last-status-change
(`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)
(let ((sum (+ (or .error 0) (or .warning 0)))) (let ((sum (+ (or .error 0) (or .warning 0))))
(doom-modeline-icon "do_not_disturb_alt" (doom-modeline-icon "do_not_disturb_alt"
(number-to-string sum) (number-to-string sum)
(if .error 'doom-modeline-urgent 'doom-modeline-warning) (if .error 'doom-modeline-urgent 'doom-modeline-warning)
-0.25))) -0.25)))
(doom-modeline-icon "check" nil 'doom-modeline-info))) (doom-modeline-icon "check" nil 'doom-modeline-info)))
(`running (doom-modeline-icon "access_time" nil 'font-lock-doc-face -0.25)) (`running (doom-modeline-icon "access_time" nil 'font-lock-doc-face -0.25))
(`no-checker (doom-modeline-icon "sim_card_alert" "-" 'font-lock-doc-face)) (`no-checker (doom-modeline-icon "sim_card_alert" "-" 'font-lock-doc-face))
(`errored (doom-modeline-icon "sim_card_alert" "Error" 'doom-modeline-urgent)) (`errored (doom-modeline-icon "sim_card_alert" "Error" 'doom-modeline-urgent))
(`interrupted (doom-modeline-icon "pause" "Interrupted" 'font-lock-doc-face))))) (`interrupted (doom-modeline-icon "pause" "Interrupted" 'font-lock-doc-face)))))
;; ('interrupted (doom-modeline-icon "x" "Interrupted" 'font-lock-doc-face))))) ;; ('interrupted (doom-modeline-icon "x" "Interrupted" 'font-lock-doc-face)))))
@ -737,36 +719,36 @@ Require `anzu', also `evil-anzu' if using `evil-mode'
'face (if (doom-modeline--active) 'doom-modeline-panel)))) 'face (if (doom-modeline--active) 'doom-modeline-panel))))
(doom-modeline-def-segment matches (doom-modeline-def-segment matches
"Displays: 1. the currently recording macro, 2. A current/total for the "Displays: 1. the currently recording macro, 2. A current/total for the
current search term (with anzu), 3. The number of substitutions being conducted current search term (with anzu), 3. The number of substitutions being conducted
with `evil-ex-substitute', and/or 4. The number of active `iedit' regions." with `evil-ex-substitute', and/or 4. The number of active `iedit' regions."
(let ((meta (concat (doom-modeline--macro-recording) (let ((meta (concat (doom-modeline--macro-recording)
(doom-modeline--anzu) (doom-modeline--anzu)
(doom-modeline--evil-substitute) (doom-modeline--evil-substitute)
(doom-modeline--iedit)))) (doom-modeline--iedit))))
(or (and (not (equal meta "")) meta) (or (and (not (equal meta "")) meta)
(if buffer-file-name " %I ")))) (if buffer-file-name " %I "))))
;; TODO Include other information ;; TODO Include other information
(doom-modeline-def-segment media-info (doom-modeline-def-segment media-info
"Metadata regarding the current file, such as dimensions for images." "Metadata regarding the current file, such as dimensions for images."
(cond ((eq major-mode 'image-mode) (cond ((eq major-mode 'image-mode)
(cl-destructuring-bind (width . height) (cl-destructuring-bind (width . height)
(image-size (image-get-display-property) :pixels) (image-size (image-get-display-property) :pixels)
(format " %dx%d " width height))))) (format " %dx%d " width height)))))
(doom-modeline-def-segment bar (doom-modeline-def-segment bar
"The bar regulates the height of the mode-line in GUI Emacs. "The bar regulates the height of the mode-line in GUI Emacs.
Returns \"\" to not break --no-window-system." Returns \"\" to not break --no-window-system."
(if (display-graphic-p) (if (display-graphic-p)
(doom-modeline--make-xpm (doom-modeline--make-xpm
(face-background (if (doom-modeline--active) (face-background (if (doom-modeline--active)
'doom-modeline-bar 'doom-modeline-bar
'doom-modeline-inactive-bar) 'doom-modeline-inactive-bar)
nil t) nil t)
doom-modeline-height doom-modeline-height
doom-modeline-bar-width) doom-modeline-bar-width)
"")) ""))
(defun doom-modeline-eyebrowse-number () (defun doom-modeline-eyebrowse-number ()
"The eyebrowse number." "The eyebrowse number."
@ -818,41 +800,41 @@ Returns \"\" to not break --no-window-system."
(declare-function eyebrowse--get 'eyebrowse) (declare-function eyebrowse--get 'eyebrowse)
(doom-modeline-def-segment workspace-number (doom-modeline-def-segment workspace-number
"The current workspace name or number. Requires `eyebrowse-mode' to be "The current workspace name or number. Requires `eyebrowse-mode' to be
enabled." enabled."
(if (and (bound-and-true-p eyebrowse-mode) (if (and (bound-and-true-p eyebrowse-mode)
(< 1 (length (eyebrowse--get 'window-configs)))) (< 1 (length (eyebrowse--get 'window-configs))))
(let* ((num (eyebrowse--get 'current-slot)) (let* ((num (eyebrowse--get 'current-slot))
(tag (when num (nth 2 (assoc num (eyebrowse--get 'window-configs))))) (tag (when num (nth 2 (assoc num (eyebrowse--get 'window-configs)))))
(str (if (and tag (< 0 (length tag))) (str (if (and tag (< 0 (length tag)))
tag tag
(when num (int-to-string num))))) (when num (int-to-string num)))))
(propertize (format "%s " str) 'face 'doom-modeline-eyebrowse)) (propertize (format "%s " str) 'face 'doom-modeline-eyebrowse))
"")) ""))
;; ;;
;; Mode lines ;; Mode lines
;; ;;
(doom-modeline-def main (doom-modeline-def-modeline main
(workspace-number bar matches " " buffer-info " %l:%c %p " selection-info) (workspace-number bar matches " " buffer-info " %l:%c %p " selection-info)
(buffer-encoding major-mode vcs flycheck)) (buffer-encoding major-mode vcs flycheck))
(doom-modeline-def minimal (doom-modeline-def-modeline minimal
(bar matches " " buffer-info) (bar matches " " buffer-info)
(media-info major-mode)) (media-info major-mode))
(doom-modeline-def special (doom-modeline-def-modeline special
(bar matches " " buffer-info-simple " %l:%c %p " selection-info) (bar matches " " buffer-info-simple " %l:%c %p " selection-info)
(buffer-encoding major-mode flycheck)) (buffer-encoding major-mode flycheck))
(doom-modeline-def project (doom-modeline-def-modeline project
(bar buffer-default-directory) (bar buffer-default-directory)
(major-mode)) (major-mode))
(doom-modeline-def media (doom-modeline-def-modeline media
(bar " %b ") (bar " %b ")
(media-info major-mode)) (media-info major-mode))
;; ;;
;; Bootstrap ;; Bootstrap
@ -861,27 +843,41 @@ enabled."
;;;###autoload ;;;###autoload
(defun doom-modeline-init () (defun doom-modeline-init ()
"Set the default modeline." "Set the default modeline."
(doom-modeline-set 'main t) (doom-modeline-set-modeline 'main t)
;; This scratch and messages buffer is already created and doesn't get a ;; This scratch and messages buffer is already created and doesn't get a
;; modeline. ;; modeline.
(with-current-buffer "*Messages*" (with-current-buffer "*Messages*"
(doom-modeline-set 'main)) (doom-modeline-set-modeline 'main))
(with-current-buffer "*scratch*" (with-current-buffer "*scratch*"
(doom-modeline-set 'main))) (doom-modeline-set-modeline 'main)))
(defun doom-modeline-set-special-modeline () (defun doom-modeline-set-special-modeline ()
"Set the special modeline." "Set the special modeline."
(doom-modeline-set 'special)) (doom-modeline-set-modeline 'special))
(defun doom-modeline-set-media-modeline () (defun doom-modeline-set-media-modeline ()
"Set the media modeline." "Set the media modeline."
(doom-modeline-set 'media)) (doom-modeline-set-modeline 'media))
(add-hook 'image-mode-hook #'doom-modeline-set-media-modeline) (add-hook 'image-mode-hook #'doom-modeline-set-media-modeline)
(add-hook 'org-src-mode-hook #'doom-modeline-set-special-modeline) (add-hook 'org-src-mode-hook #'doom-modeline-set-special-modeline)
(add-hook 'circe-mode-hook #'doom-modeline-set-special-modeline) (add-hook 'circe-mode-hook #'doom-modeline-set-special-modeline)
;; Show version string for multi-version managers like rvm, rbenv, pyenv, etc.
(defvar-local doom-modeline-env-version nil)
(defvar-local doom-modeline-env-command nil)
(add-hook 'focus-in-hook #'doom-modeline-update-env)
(add-hook 'find-file-hook #'doom-modeline-update-env)
(defun doom-modeline-update-env ()
"Update environment for mode-line."
(when doom-modeline-env-command
(let* ((default-directory (doom-modeline-project-root))
(s (shell-command-to-string doom-modeline-env-command)))
(setq doom-modeline-env-version (if (string-match "[ \t\n\r]+\\'" s)
(replace-match "" t t s)
s)))))
(provide 'doom-modeline) (provide 'doom-modeline)
;;; doom-modeline.el ends here ;;; doom-modeline.el ends here