Fix compile-time void-function errors from modeline

This commit is contained in:
Henrik Lissner 2018-05-29 14:50:42 +02:00
parent 442aa91eea
commit 456dc62051
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 46 additions and 36 deletions

View file

@ -111,36 +111,40 @@ with `doom//reload-theme').")
;; Modeline library ;; Modeline library
;; ;;
(defvar doom--modeline-alist ()) (defvar doom--modeline-fn-alist ())
(defvar doom--modeline-var-alist ())
(defmacro def-modeline-segment! (name &rest body) (defmacro def-modeline-segment! (name &rest body)
"Defines a modeline segment and byte compiles it." "Defines a modeline segment and byte compiles it."
(declare (indent defun) (doc-string 2)) (declare (indent defun) (doc-string 2))
(let ((docstring (if (stringp (car body)) (pop body)))) (let ((sym (intern (format "doom-modeline-segment--%s" name)))
(if (and (symbolp (car body)) (docstring (if (stringp (car body))
(not (cdr body))) (pop body)
`(map-put doom--modeline-alist ',name ',(car body)) (format "%s modeline segment" name))))
(let ((sym (intern (format "doom-modeline-segment--%s" name)))) (cond ((and (symbolp (car body))
`(progn (not (cdr body)))
(defun ,sym () (map-put doom--modeline-var-alist name (car body))
,(or docstring (format "%s modeline segment" name)) `(map-put doom--modeline-var-alist ',name ',(car body)))
,@body) (t
(map-put doom--modeline-alist ',name ',sym) (map-put doom--modeline-fn-alist name sym)
,(unless (bound-and-true-p byte-compile-current-file) `(progn
`(let (byte-compile-warnings) (fset ',sym (lambda () ,docstring ,@body))
(byte-compile #',sym)))))))) (map-put doom--modeline-fn-alist ',name ',sym)
,(unless (bound-and-true-p byte-compile-current-file)
`(let (byte-compile-warnings)
(byte-compile #',sym))))))))
(defsubst doom--prepare-modeline-segments (segments) (defsubst doom--prepare-modeline-segments (segments)
(let (forms it) (let (forms it)
(dolist (seg segments) (dolist (seg segments)
(cond ((stringp seg) (cond ((stringp seg)
(push seg forms)) (push seg forms))
((setq it (cdr (assq seg doom--modeline-alist))) ((symbolp seg)
(push (cond ((boundp it) it) (cond ((setq it (cdr (assq seg doom--modeline-fn-alist)))
((fboundp it) (list it)) (push (list it) forms))
((error "%s is not a valid segment" seg))) ((setq it (cdr (assq seg doom--modeline-var-alist)))
forms)) (push it forms))
((fboundp seg) (push (list seg) forms)) ((error "%s is not a defined segment" seg))))
((error "%s is not a valid segment" seg)))) ((error "%s is not a valid segment" seg))))
(nreverse forms))) (nreverse forms)))
@ -158,17 +162,21 @@ Example:
(lhs-forms (doom--prepare-modeline-segments lhs)) (lhs-forms (doom--prepare-modeline-segments lhs))
(rhs-forms (doom--prepare-modeline-segments rhs))) (rhs-forms (doom--prepare-modeline-segments rhs)))
`(progn `(progn
(defun ,sym () (fset ',sym
,(concat "Modeline:\n" (format " %s\n %s" lhs rhs)) (lambda ()
(let ((lhs (list ,@lhs-forms)) ,(concat "Modeline:\n"
(rhs (list ,@rhs-forms))) (format " %s\n %s"
(let ((rhs-str (format-mode-line rhs))) (prin1-to-string lhs)
(list lhs (prin1-to-string rhs)))
(propertize (let ((lhs (list ,@lhs-forms))
" " 'display (rhs (list ,@rhs-forms)))
`((space :align-to (- (+ right right-fringe right-margin) (let ((rhs-str (format-mode-line rhs)))
,(+ 1 (string-width rhs-str)))))) (list lhs
rhs-str)))) (propertize
" " 'display
`((space :align-to (- (+ right right-fringe right-margin)
,(+ 1 (string-width rhs-str))))))
rhs-str)))))
,(unless (bound-and-true-p byte-compile-current-file) ,(unless (bound-and-true-p byte-compile-current-file)
`(let (byte-compile-warnings) `(let (byte-compile-warnings)
(byte-compile #',sym)))))) (byte-compile #',sym))))))

View file

@ -372,7 +372,7 @@ directory, the file name, and its state (modified, read-only or non-existent)."
;; vcs ;; vcs
;; ;;
(defvar +doom-modeline--vcs nil) (defvar-local +doom-modeline--vcs nil)
(defun +doom-modeline--update-vcs () (defun +doom-modeline--update-vcs ()
(setq +doom-modeline--vcs (setq +doom-modeline--vcs
(when (and vc-mode buffer-file-name) (when (and vc-mode buffer-file-name)
@ -410,8 +410,9 @@ directory, the file name, and its state (modified, read-only or non-existent)."
(add-hook 'after-save-hook #'+doom-modeline--update-vcs) (add-hook 'after-save-hook #'+doom-modeline--update-vcs)
(add-hook 'find-file-hook #'+doom-modeline--update-vcs t) (add-hook 'find-file-hook #'+doom-modeline--update-vcs t)
(def-modeline-var! vcs +doom-modeline--vcs (def-modeline-segment! vcs
"Displays the current branch, colored based on its state.") "Displays the current branch, colored based on its state."
+doom-modeline--vcs)
;; ;;
@ -453,9 +454,10 @@ directory, the file name, and its state (modified, read-only or non-existent)."
('errored (+doom-ml-icon "sim_card_alert" "Error" 'doom-modeline-urgent)) ('errored (+doom-ml-icon "sim_card_alert" "Error" 'doom-modeline-urgent))
('interrupted (+doom-ml-icon "pause" "Interrupted" 'font-lock-doc-face))))) ('interrupted (+doom-ml-icon "pause" "Interrupted" 'font-lock-doc-face)))))
(def-modeline-var! flycheck +doom-modeline--flycheck (def-modeline-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."
+doom-modeline--flycheck)
;; ;;