Add the projectile project unit with a universal buffer change detection mecha

This commit is contained in:
Sameer Rahmani 2023-06-22 00:37:38 +01:00
parent e31be3310f
commit 5a744e0402
Signed by: lxsameer
GPG Key ID: B0A4AF28AB9FD90B
4 changed files with 105 additions and 45 deletions

5
Eldev
View File

@ -1,6 +1,3 @@
; -*- mode: emacs-lisp; lexical-binding: t -*-
;; Uncomment some calls below as needed for your project.
;(eldev-use-package-archive 'gnu-elpa)
;(eldev-use-package-archive 'nongnu-elpa)
(eldev-use-package-archive 'melpa)
(eldev-add-extra-dependencies 'emacs 'projectile)

View File

@ -126,5 +126,36 @@ knowing."
"" "%m" 16)
;; ============================================================================
;; Projectile, Project Name
;; ============================================================================
(defvar noether--project "")
(defun noether--set-project ()
"Set the current time to the internal var which is being watched."
(message ">> %s" (projectile-project-name))
(setq noether--project (projectile-project-name)))
(defun noether--format-project (_ v _ _)
"Just return the current time V."
v)
(defunit projectile-project-unit
"just the time for your bar."
:label "P:"
:len 30
:init (lambda ()
(if (and (featurep 'projectile) projectile-mode)
(add-hook 'noether-on-buffer-change-hook #'noether--set-project)
(warn "Can't find feature `projectile'")))
:deinit (lambda ()
(remove-hook 'noether-on-buffer-change-hook #'noether--set-project))
:var 'noether--project
:fn #'noether--format-project)
(provide 'noether-units)
;;; noether-units.el ends here

View File

@ -28,13 +28,33 @@
(require 'posframe)
;; ============================================================================
;; Vars
;; ============================================================================
(defvar noether-views ()
"A list of views that noether should manage.
You should adding your views to this var, so noether can activate them
on demand.")
(defvar noether-on-buffer-change-hook ()
"A hook that runs whenever noether detects focus change on buffers.")
(defvar noether--frame-defaults
(list
:min-height 1
:min-width 10
:position '(0 . 0)
:border-width 0
:accewpt-focus nil
:timeout 10
:refresh 1))
;; ============================================================================
;; Macros
;; ============================================================================
(defmacro noether--unit-get (unit key &optional default)
"Return the value of the KEY in UNIT or the DEFAULT value if it doesn't exist."
`(or (plist-get ,unit ,key) ,default))
@ -45,27 +65,6 @@ on demand.")
`(or (plist-get ,view ,key) ,default))
(defun noether--extract-props (body-list &optional acc)
"Extract the props pairs from BODY-LIST with an optional accumulator ACC.
It will returen a pair in form of (body . props)."
(let ((k (car body-list))
(rest (cdr body-list)))
(if (and k (keywordp k))
(noether--extract-props
(cdr rest)
(cons (cdr rest) (plist-put (cdr acc) k (car rest))))
(cons body-list (cdr acc)))))
(defun noether--create-placeholder (unit)
"Create a placeholder for UNIT based on its :label and :len."
(concat
(noether--unit-get unit :label "")
(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.
@ -130,8 +129,6 @@ the show function."
(put ',name :initial-content ,initial-content)
t)))
(defmacro defunit (name docs &rest props)
"Define a unit with the given NAME, DOCS and a set of PROPS.
It will define a function with the given NAME that accepts any
@ -150,20 +147,36 @@ key/value from the original definition."
f-props (list ,@orig-props)))))
(defvar noether--frame-defaults
(list
:min-height 1 ;; (noether--view-get view :height 1)
:min-width 10 ;; (noether--view-get view :width 10)
:position '(0 . 0) ;;(cons (- (frame-outer-width) 10) (- (frame-outer-height) 10))
;;:position (noether--view-get view :position '(0 . 0))
;;:poshandler (noether--view-get view :poshandler)
:border-width 0 ;; (noether--view-get view :border 0)
;;:border-color (noether--view-get view :border-color "#eeeefe")
:accewpt-focus nil ;;(noether--view-get view :accept-focus)
:timeout 10 ;;(noether--view-get view :timeout 5)
:refresh 1 ;;(noether--view-get view :refresh 1)))
)
)
;; ============================================================================
;; Helper functions
;; ============================================================================
(defun noether--buffer-focus-change-runner (_)
"Run a certain hook whenever it detects that focus has change to another buffer.
Users can add a function to the `noether-on-buffer-change-hook' hook to run
some arbitary buffer related code when a focus change event happens."
(run-hooks 'noether-on-buffer-change-hook))
(defun noether--extract-props (body-list &optional acc)
"Extract the props pairs from BODY-LIST with an optional accumulator ACC.
It will returen a pair in form of (body . props)."
(let ((k (car body-list))
(rest (cdr body-list)))
(if (and k (keywordp k))
(noether--extract-props
(cdr rest)
(cons (cdr rest) (plist-put (cdr acc) k (car rest))))
(cons body-list (cdr acc)))))
(defun noether--create-placeholder (unit)
"Create a placeholder for UNIT based on its :label and :len."
(concat
(noether--unit-get unit :label "")
(make-string (noether--unit-get unit :len 0) ? )))
(defun noether-show (view)
"Draw the given VIEW on the screen."
@ -285,6 +298,7 @@ E.g. the updaters list."
(noether--view-get view :units)
0))
(defun noether--teardown-unit (unit)
"Tear down the given UNIT by calling the `:deinit' function and removing possible watches."
(let ((deinit (noether--unit-get unit :deinit (lambda ()))))
@ -301,6 +315,21 @@ E.g. the updaters list."
(funcall (noether--view-get view :deinit (lambda ())))))
(defun noether--enable ()
"Enable noether by setting up each view and necessary hooks."
(add-to-list 'window-buffer-change-functions #'noether--buffer-focus-change-runner)
(add-to-list 'window-selection-change-functions #'noether--buffer-focus-change-runner)
(mapc #'noether--setup-views noether-views))
(defun noether--disable ()
"Disable noether and clean up after it."
(delete 'window-buffer-change-functions #'noether--buffer-focus-change-runner)
(delete 'window-selection-change-functions #'noether--buffer-focus-change-runner)
(mapc #'noether--teardown-views noether-views))
(define-minor-mode noether-global-mode
"A minor mode that keep tracks of different status blocks.
It reports them back in a status bar like frame."
@ -308,8 +337,8 @@ It reports them back in a status bar like frame."
:lighter " ST42"
:keymap (make-sparse-keymap)
(if noether-global-mode
(mapc #'noether--setup-views noether-views)
(mapc #'noether--teardown-views noether-views)))
(noether--enable)
(noether--disable)))
(provide 'noether)

View File

@ -24,7 +24,7 @@
;;; Change Log:
;;; Code:
(setq debug-on-error t)
(require 'projectile)
(require 'noether-units)
;;(debug-on-entry 'noether--update-buffer-name)
@ -47,7 +47,8 @@
(line-unit)
(time-unit :label "Time:")
(buffer-name-unit)
(mode-name-unit)))
(mode-name-unit)
(projectile-project-unit)))
(defface noether-active-modeline
'((((background light))
@ -78,6 +79,8 @@
'noether-inactive-modeline
(default-value 'face-remapping-alist) face-remaps))))
(projectile-global-mode t)
(noether-global-mode t)