From ffe4bb81f76fca12944dc6012eb32a351835d59a Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Mon, 19 Jun 2023 22:25:26 +0100 Subject: [PATCH] Add the noether-from-modeline macro to easily port modeline format strings to noether --- noether-units.el | 9 +++++++-- noether.el | 39 +++++++++++++++++++++++++++++++++++++++ noether.example.el | 3 ++- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/noether-units.el b/noether-units.el index 8be5ff3..30b934f 100644 --- a/noether-units.el +++ b/noether-units.el @@ -69,8 +69,7 @@ (format "%s%s %s" (if (buffer-modified-p buf) "*" "-") (if (verify-visited-file-modtime buf) "-" "*") - (string-trim v))) - ) + (string-trim v)))) (defunit buffer-name-unit @@ -119,6 +118,12 @@ knowing." :var 'noether--time :fn #'noether--time-format) +;; ============================================================================ +;; Mode name +;; ============================================================================ +(noether-from-modeline mode-name-unit + "Returns the current mode name" + "" "%m" 16) (provide 'noether-units) diff --git a/noether.el b/noether.el index d96f3f4..4ad34f3 100644 --- a/noether.el +++ b/noether.el @@ -66,6 +66,44 @@ It will returen a pair in form of (body . props)." (make-string (noether--unit-get unit :len 0) ? ))) +(defmacro noether-from-modeline (name docs label format-str len) + "Define a new unit with the given NAME and doc-string DOCS from the modeline. +It will use the given LABEL and LEN to pass the to the `defuit' macro. + +The most important part of this macro is the FORMAT-STR parameter. It +should be a format string that is understandable by `format-modeline' +function." + (declare (doc-string 2) (indent defun)) + (let ((new-var-sym (gensym))) + `(progn + (defvar ,(intern (format "%s--internal-state-var" (symbol-name name)))) + + (defun ,(intern (format "%s--update-internal-state-var" (symbol-name name))) () + (setq ,(intern (format "%s--internal-state-var" (symbol-name name))) + (format-mode-line ,format-str))) + + + (defun ,(intern (format "%s--format-final-result" (symbol-name name))) (_ ,new-var-sym _ _) + "Format the buffer name V." + (string-trim ,new-var-sym)) + + + (defunit ,name + ,docs + :label ,label + :len ,len + :init (lambda () + (add-hook 'post-command-hook + #',(intern (format "%s--update-internal-state-var" (symbol-name name))))) + + :deinit (lambda () + (remove-hook 'post-command-hook + #',(intern (format "%s--update-internal-state-var" (symbol-name name))))) + + :var ',(intern (format "%s--internal-state-var" (symbol-name name))) + :fn #',(intern (format "%s--format-final-result" (symbol-name name))))))) + + (defmacro defview (name docs &rest body) "Create a new view with the given NAME with the given DOCS and BODY. BODY will be parsed in a way that any starting pair of keyword and value @@ -274,5 +312,6 @@ It reports them back in a status bar like frame." (mapc #'noether--teardown-views noether-views))) + (provide 'noether) ;;; noether.el ends here diff --git a/noether.example.el b/noether.example.el index b4dfedc..f342c28 100644 --- a/noether.example.el +++ b/noether.example.el @@ -45,7 +45,8 @@ (list (line-unit) (time-unit :label "Time:") - (buffer-name-unit))) + (buffer-name-unit) + (mode-name-unit))) (setq noether-views (list example-bar))