Add the noether-from-modeline macro to easily port modeline format strings to noether

This commit is contained in:
Sameer Rahmani 2023-06-19 22:25:26 +01:00
parent 21fcafd386
commit ffe4bb81f7
Signed by: lxsameer
GPG Key ID: B0A4AF28AB9FD90B
3 changed files with 48 additions and 3 deletions

View File

@ -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)

View File

@ -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

View File

@ -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))