Refactor ui/doom-modeline

This commit is contained in:
Henrik Lissner 2017-07-02 16:48:31 +02:00
parent a002f7025c
commit 06b5ee8fb8
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -3,12 +3,20 @@
(def-package! eldoc-eval (def-package! eldoc-eval
:demand t :demand t
:config :config
(defun +doom-modeline-eldoc (text)
(concat (when (display-graphic-p)
(+doom-modeline--make-xpm
(face-background 'doom-modeline-eldoc-bar nil t)
+doom-modeline-height
+doom-modeline-bar-width))
text))
;; Show eldoc in the mode-line with `eval-expression' ;; Show eldoc in the mode-line with `eval-expression'
(defun +doom-modeline--show-eldoc (input) (defun +doom-modeline--show-eldoc (input)
"Display string STR in the mode-line next to minibuffer." "Display string STR in the mode-line next to minibuffer."
(with-current-buffer (eldoc-current-buffer) (with-current-buffer (eldoc-current-buffer)
(let* ((str (and (stringp input) input)) (let* ((str (and (stringp input) input))
(mode-line-format (or (and str (or (doom-modeline 'eldoc) str)) (mode-line-format (or (and str (or (+doom-modeline-eldoc str) str))
mode-line-format)) mode-line-format))
mode-line-in-non-selected-windows) mode-line-in-non-selected-windows)
(force-mode-line-update) (force-mode-line-update)
@ -35,10 +43,9 @@
anzu--last-isearch-string anzu--overflow-p)) anzu--last-isearch-string anzu--overflow-p))
;; Ensure anzu state is cleared when searches & iedit are done ;; Ensure anzu state is cleared when searches & iedit are done
(add-hook! :append '(isearch-mode-end-hook +evil-esc-hook) (add-hook 'isearch-mode-end-hook #'anzu--reset-status t)
#'anzu--reset-status) (add-hook '+evil-esc-hook #'anzu--reset-status t)
(after! iedit (add-hook 'iedit-mode-end-hook #'anzu--reset-status))
(add-hook 'iedit-mode-end-hook #'anzu--reset-status)))
;; Keep `+doom-modeline-current-window' up-to-date ;; Keep `+doom-modeline-current-window' up-to-date
@ -303,16 +310,14 @@ directory, the file name, and its state (modified, read-only or non-existent)."
;; ;;
(def-modeline-segment! buffer-encoding (def-modeline-segment! buffer-encoding
"Displays the encoding and eol style of the buffer the same way Atom does." "Displays the encoding and eol style of the buffer the same way Atom does."
(concat (let ((eol-type (coding-system-eol-type buffer-file-coding-system))) (concat (pcase (coding-system-eol-type buffer-file-coding-system)
(cond ((eq eol-type 0) "LF ") (0 "LF ")
((eq eol-type 1) "CRLF ") (1 "CRLF ")
((eq eol-type 2) "CR "))) (2 "CR "))
(let* ((sys (coding-system-plist buffer-file-coding-system)) (let ((sys (coding-system-plist buffer-file-coding-system)))
(sys-name (plist-get sys :name)) (cond ((memq (plist-get sys :category) '(coding-category-undecided coding-category-utf-8))
(sys-cat (plist-get sys :category)))
(cond ((memq sys-cat '(coding-category-undecided coding-category-utf-8))
"UTF-8") "UTF-8")
(t (upcase (symbol-name sys-name))))) (t (upcase (symbol-name (plist-get sys :name))))))
" ")) " "))
;; ;;
@ -320,8 +325,10 @@ directory, the file name, and its state (modified, read-only or non-existent)."
"The major mode, including process, environment and text-scale info." "The major mode, including process, environment and text-scale info."
(propertize (propertize
(concat (format-mode-line mode-name) (concat (format-mode-line mode-name)
(if (stringp mode-line-process) mode-line-process) (when (stringp mode-line-process)
(if +doom-modeline-env-version (concat " " +doom-modeline-env-version)) mode-line-process)
(when +doom-modeline-env-version
(concat " " +doom-modeline-env-version))
(and (featurep 'face-remap) (and (featurep 'face-remap)
(/= text-scale-mode-amount 0) (/= text-scale-mode-amount 0)
(format " (%+d)" text-scale-mode-amount))) (format " (%+d)" text-scale-mode-amount)))
@ -331,9 +338,9 @@ directory, the file name, and its state (modified, read-only or non-existent)."
(def-modeline-segment! vcs (def-modeline-segment! vcs
"Displays the current branch, colored based on its state." "Displays the current branch, colored based on its state."
(when (and vc-mode buffer-file-name) (when (and vc-mode buffer-file-name)
(let ((backend (vc-backend buffer-file-name)) (let* ((backend (vc-backend buffer-file-name))
(state (vc-state buffer-file-name)) (state (vc-state buffer-file-name backend)))
(face 'mode-line-inactive) (let ((face 'mode-line-inactive)
(active (active)) (active (active))
(all-the-icons-scale-factor 1.0) (all-the-icons-scale-factor 1.0)
(all-the-icons-default-adjust -0.1)) (all-the-icons-default-adjust -0.1))
@ -364,18 +371,16 @@ directory, the file name, and its state (modified, read-only or non-existent)."
" " " "
(propertize (substring vc-mode (+ (if (eq backend 'Hg) 2 3) 2)) (propertize (substring vc-mode (+ (if (eq backend 'Hg) 2 3) 2))
'face (if active face)) 'face (if active face))
" ")))) " ")))))
;; ;;
(defun +doom-ml-icon (icon &optional text face voffset) (defun +doom-ml-icon (icon &optional text face voffset)
"Displays an octicon ICON with FACE, followed by TEXT. Uses "Displays an octicon ICON with FACE, followed by TEXT. Uses
`all-the-icons-octicon' to fetch the icon." `all-the-icons-octicon' to fetch the icon."
(concat (concat (if vc-mode " " " ")
(if vc-mode " " " ")
(when icon (when icon
(concat (concat
(all-the-icons-material icon :face face :height 1.1 :v-adjust (or voffset -0.2)) (all-the-icons-material icon :face face :height 1.1 :v-adjust (or voffset -0.2))
;; (all-the-icons-octicon icon :face face :height 1.0 :v-adjust 0)
(if text +doom-modeline-vspc))) (if text +doom-modeline-vspc)))
(when text (when text
(propertize text 'face face)) (propertize text 'face face))
@ -461,7 +466,7 @@ lines are selected, or the NxM dimensions of a block selection."
'face (if (active) 'doom-modeline-panel)))) 'face (if (active) 'doom-modeline-panel))))
(defsubst +doom-modeline--evil-substitute () (defsubst +doom-modeline--evil-substitute ()
"Show number of :s matches in real time." "Show number of matches for evil-ex substitutions and highlights in real time."
(when (and evil-mode (when (and evil-mode
(or (assq 'evil-ex-substitute evil-ex-active-highlights-alist) (or (assq 'evil-ex-substitute evil-ex-active-highlights-alist)
(assq 'evil-ex-global-match evil-ex-active-highlights-alist) (assq 'evil-ex-global-match evil-ex-active-highlights-alist)
@ -473,9 +478,12 @@ lines are selected, or the NxM dimensions of a block selection."
(pattern (car-safe (evil-delimited-arguments evil-ex-argument 2)))) (pattern (car-safe (evil-delimited-arguments evil-ex-argument 2))))
(if pattern (if pattern
(format " %s matches " (how-many pattern (car range) (cdr range))) (format " %s matches " (how-many pattern (car range) (cdr range)))
" ... ")) " - "))
'face (if (active) 'doom-modeline-panel)))) 'face (if (active) 'doom-modeline-panel))))
(defun doom-themes--overlay-sort (a b)
(< (overlay-start a) (overlay-start b)))
(defsubst +doom-modeline--iedit () (defsubst +doom-modeline--iedit ()
"Show the number of iedit regions matches + what match you're on." "Show the number of iedit regions matches + what match you're on."
(when (and iedit-mode iedit-occurrences-overlays) (when (and iedit-mode iedit-occurrences-overlays)
@ -488,9 +496,9 @@ lines are selected, or the NxM dimensions of a block selection."
(format " %s/%d " (format " %s/%d "
(if this-oc (if this-oc
(- length (- length
(length (cdr (length (memq this-oc (sort (append iedit-occurrences-overlays nil)
(memq this-oc (sort (append iedit-occurrences-overlays (list)) #'doom-themes--overlay-sort)))
(lambda (x y) (< (overlay-start x) (overlay-start y)))))))) -1)
"-") "-")
length)) length))
'face (if (active) 'doom-modeline-panel)))) 'face (if (active) 'doom-modeline-panel))))
@ -503,42 +511,27 @@ with `evil-ex-substitute', and/or 4. The number of active `iedit' regions."
(+doom-modeline--anzu) (+doom-modeline--anzu)
(+doom-modeline--evil-substitute) (+doom-modeline--evil-substitute)
(+doom-modeline--iedit)))) (+doom-modeline--iedit))))
(or (and (not (string= 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
(def-modeline-segment! media-info (def-modeline-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)
(let ((size (image-size (image-get-display-property) :pixels))) (cl-destructuring-bind (width . height)
(format " %dx%d " (car size) (cdr size)))))) (image-size (image-get-display-property) :pixels)
(format " %dx%d " width height)))))
;; ;; The bar regulates the height of the mode-line in GUI Emacs.
(def-modeline-segment! eldoc
"Display eldoc documentation in the mode-line while using the minibuffer (e.g.
`eval-expression')."
(bound-and-true-p str))
;; These bars regulate the height of the mode-line in GUI Emacs.
(def-modeline-segment! bar (def-modeline-segment! bar
(if (display-graphic-p) (when (display-graphic-p)
(+doom-modeline--make-xpm (+doom-modeline--make-xpm
(face-background (if (active) (face-background (if (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)))
""))
(def-modeline-segment! eldoc-bar
"A differently colored bar, to signify an eldoc display."
(if (display-graphic-p)
(+doom-modeline--make-xpm
(face-background 'doom-modeline-eldoc-bar nil t)
+doom-modeline-height
+doom-modeline-bar-width)
""))
;; ;;
@ -549,10 +542,6 @@ with `evil-ex-substitute', and/or 4. The number of active `iedit' regions."
(bar matches " " buffer-info " %l:%c %p " selection-info) (bar matches " " buffer-info " %l:%c %p " selection-info)
(buffer-encoding major-mode vcs flycheck)) (buffer-encoding major-mode vcs flycheck))
(def-modeline! eldoc
(eldoc-bar " " eldoc)
(media-info major-mode))
(def-modeline! minimal (def-modeline! minimal
(bar matches " " buffer-info) (bar matches " " buffer-info)
(media-info major-mode)) (media-info major-mode))