Add a macro to define statusbar units that takes care of timers

This commit is contained in:
Sameer Rahmani 2021-12-12 19:51:09 +00:00
parent 0064223dec
commit d5059ac258
10 changed files with 192 additions and 87 deletions

View File

@ -245,11 +245,11 @@
;; their flag is active otherwise they will be skipped
(let ((mline (or (plist-get fg42/editor-cube-params :modeline)
fg42/default-modeline)))
#'fg42/default-modeline)))
(fg42/statusbar-cube)
;; (fg42/modeline-cube)
;; (fg42/setup-modeline-format mline)
(fg42/statusbar-cube))
(fg42/setup-modeline-format mline)
)
(fg42/font-cube)
(fg42/pinentry-cube)

41
core/cubes/icons.el Normal file
View File

@ -0,0 +1,41 @@
;;; IconsCubes --- The modeline related cubes for FG42 -*- lexical-binding: t; -*-
;;
;; Copyright (c) 2010-2021 Sameer Rahmani & Contributors
;;
;; Author: Sameer Rahmani <lxsameer@gnu.org>
;; URL: https://gitlab.com/FG42/FG42
;; Version: 3.0.0
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;
;;; Commentary:
;;; Code:
(require 'fpkg)
(require 'fg42/cube)
;; TODO: Break this into two cubes
(defcube fg42/all-the-icons-cube
"Install the [[https://github.com/domtronn/all-the-icons.el][all-the-icons.el]]
package."
(:title "All the icons cube"
:flag all-the-icons
:flag-default t)
(fpkg/use all-the-icons
:if (display-graphic-p)))
(provide 'cubes/icons)
;;; icons.el ends here

View File

@ -23,6 +23,11 @@
;;; Code:
(require 'fpkg)
(require 'fg42/cube)
(require 'cubes/icons)
(autoload-cube 'fg42/statusbar-default
"modeline/statusbar-default.el"
"The default StatusBar modeline.")
;; TODO: Break this into two cubes
@ -34,8 +39,10 @@
:flag-default t)
(defun fg42/mini-modeline-setter (def)
(setq-default mini-modeline-l-format (plist-get def :long))
(setq-default mini-modeline-r-format (plist-get def :brief)))
(let ((def-list (func def)))
(setq-default mini-modeline-l-format (plist-get def-list :long))
(setq-default mini-modeline-r-format (plist-get def-list :brief))))
(fpkg/use smart-mode-line
:straight (smart-mode-line :source melpa)
@ -59,6 +66,7 @@
:config
(add-hook 'fg42-after-init-hook #'mini-modeline-mode)))
(defcube fg42/statusbar-cube
"Status bar replaces the default mode line aned merges with
the mini buffer to provide a minimal interface for displaying
@ -68,8 +76,15 @@ valuable information."
:group modeline
:flag-default t)
(fg42/all-the-icons-cube)
(defun fg42/statusbar-setter (def)
(setq-default fg42/statusbar-components (plist-get def :brief)))
(unless (null def)
(setq-default fg42/statusbar-components
(plist-get (funcall def) :brief))
(setq-default fg42/statusbar-components
(plist-get (funcall #'fg42/statusbar-default) :brief))))
(setq fg42/modeline-setter #'fg42/statusbar-setter)

View File

@ -1,56 +0,0 @@
;;; ModelineCubes --- The modeline related cubes for FG42 -*- lexical-binding: t; -*-
;;
;; Copyright (c) 2010-2021 Sameer Rahmani & Contributors
;;
;; Author: Sameer Rahmani <lxsameer@gnu.org>
;; URL: https://gitlab.com/FG42/FG42
;; Version: 3.0.0
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;
;;; Commentary:
;;; Code:
(require 'doom-modeline)
(require 'mini-modeline)
(defun fg42/setup-mini-modeline ()
"Setup the doom modeline to work with mini modeline."
;; (add-hook 'minibuffer-inactive-mode-hook
;; (lambda ()
;; (make-local-variable 'face-remapping-alist)
;; (add-to-list 'face-remapping-alist '(default (:background "#21252b")))))
(doom-modeline-def-modeline 'minibuffer-line
'(modals
workspace-name
window-number
matches
buffer-info
remote-host
buffer-position
word-count
selection-info)
'(misc-info major-mode process vcs lsp checker " \n"))
(setq mini-modeline-r-format '(:eval (doom-modeline-format--minibuffer-line)))
(setq mini-modeline-right-padding 1
mini-modeline-enhance-visual nil)
(set-face-attribute 'mode-line nil :box nil)
(set-face-attribute 'mini-modeline-mode-line nil :background (face-background 'modus-theme-intense-blue) :height 0.2)
(set-face-attribute 'mini-modeline-mode-line-inactive nil :background (face-background 'fringe) :height 0.2))
(provide 'cubes/modeline/setup)
;;; setup.el ends here

View File

@ -0,0 +1,53 @@
;;; FG42 --- The mighty editor for the emacsians -*- lexical-binding: t; -*-
;;
;; Copyright (c) 2010-2022 Sameer Rahmani <lxsameer@gnu.org>
;;
;; Author: Sameer Rahmani <lxsameer@gnu.org>
;; URL: https://gitlab.com/FG42/FG42
;; Version: 3.0.0
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;
;;; Commentary:
;;; Code:
(require 'fg42/modeline)
(require 'fg42/statusbar)
(defbar-unit battery 30 "B-"
(format "B%s" (with-temp-buffer
(insert-file-contents "/sys/class/power_supply/BAT0/capacity")
(buffer-string))))
;; The default modeline for FG42 that is compatible with
;; the FG42's status bar only
(defmode-line fg42/statusbar-default
(if (display-graphic-p)
(progn
;; We are in the graphical world
(require 'all-the-icons)
(list
:brief (list
'battery
"|" '(buffer-name) "|" '(format-mode-line "%l:%c") "|" '(format-time-string "%m-%d %H:%M"))
:long " "))
;; No GUI
(list
:brief (list)
:long "")))
(provide 'fg42/modeline/statusbar-default)
;;; statusbar-default.el ends here

View File

@ -28,6 +28,9 @@
(defvar fg42-after-init-hook nil
"The hook tha runs when FG42 finished running the user configuration")
(defvar fg42/debug-p nil
"The hook tha runs when FG42 finished running the user configuration")
(defvar fg42-home (getenv "FG42_HOME")
"The pass to fg42-home.")

View File

@ -128,13 +128,13 @@
;; Run the pre init hook
(run-hooks (quote ,pre-init-hook))
(message "Initializing '%s' cube." (quote ,cube-name))
(fg42/info "Initializing '%s' cube." (quote ,cube-name))
;; Run the body
(let ((result (progn ,@body)))
;; Run the post init hook
(run-hooks (quote ,post-init-hook))
result))
(message "[SKIP] The '%s' cube is already active." ',cube-name)))
(fg42/info "The '%s' cube is already active." ',cube-name)))
(defun ,cube-name (&rest params)
(interactive)
@ -144,7 +144,7 @@
;; Otherwise check for the flag to be active
(if-flag ,flag-var
(,cube-name-internal params)
(message "The flag for '%s' cube is disabled. Skiping." ,(symbol-name cube-name)))))
(fg42/info "The flag for '%s' cube is disabled. Skiping." ,(symbol-name cube-name)))))
;; Set the symbol-plist of the cube-name to its props
(setplist ',cube-name ',complete-props))))

View File

@ -23,16 +23,23 @@
;;; Code:
(defvar fg42/modeline-setter #'fg42/default-mode-line-setter)
(defvar fg42/default-modeline
(list
:brief '("hnt |" (buffer-name))
:long " "))
(defun fg42/default-mode-line-setter (_)
(defmacro defmode-line (name &rest body)
`(defun ,name () ,@body))
;; The default modeline has to be nil to let the
;; modeline setters know that they can use their
;; default modeline
(defmode-line fg42/default-modeline)
(defun fg42/default-mode-line-setter (def)
"Set the modeline definition in X to the `fg42-modeline'."
(setq mode-line-format (plist-get fg42/default-modeline :brief)))
(unless (null def)
(setq mode-line-format (plist-get def :brief))
(setq mode-line-format (plist-get (fg42/default-modeline) :brief))))
(defun fg42/setup-modeline-format (definition)
@ -40,8 +47,6 @@
(funcall fg42/modeline-setter definition))
(defmacro defmode-line (name body)
`(defvar ,name ,@body))
(provide 'fg42/modeline)

View File

@ -22,9 +22,12 @@
;;; Commentary:
;;; Code:
;; Some of the functions in this module are borrowed from `mini-modeline'
;; at https://github.com/kiennq/emacs-mini-modeline (GPL). I modified to
;; fit my needs.
;;; Require
(require 'minibuffer)
(require 'dash)
(require 'frame)
(require 'timer)
(require 'face-remap)
@ -34,7 +37,7 @@
;; ============================================================================
(defgroup fg42/statusbar nil
"Modular tray bar."
"Status bar group."
:group 'fg42/statusbar)
@ -57,7 +60,7 @@
:group 'fg42/statusbar)
(defcustom fg42/statusbar-refresh-idle-delay 0.1
"Update idle delay of awesome tray, in seconds."
"Update idle delay of bar in seconds."
:type 'double
:group 'fg42/statusbar)
@ -93,6 +96,7 @@ Nil means current selected frame."
"Modeline face for active window."
:group 'fg42/statusbar)
(defface fg42/statusbar-mode-line-inactive
'((((background light))
:background "#dddddd" :height 0.1 :box nil)
@ -101,6 +105,7 @@ Nil means current selected frame."
"Modeline face for inactive window."
:group 'fg42/statusbar)
;; ============================================================================
;; Vars
;; ============================================================================
@ -132,8 +137,19 @@ Nil means current selected frame."
(defvar fg42/statusbar-command-state 'begin
"The state of current executed command begin -> [exec exec-read] -> end.")
(defvar fg42/statusbar-enable-hook nil)
(defvar fg42/statusbar-disable-hook nil)
(defvar-local fg42/statusbar--orig-mode-line mode-line-format)
(defvar-local fg42/statusbar--face-cookie nil)
(defvar fg42/statusbar--orig-mode-line-remap
(or (alist-get 'mode-line face-remapping-alist) 'mode-line))
(defvar fg42/statusbar--orig-mode-line-inactive-remap
(or (alist-get 'mode-line-inactive face-remapping-alist) 'mode-line-inactive))
;; ============================================================================
;; Definitions
;; ============================================================================
@ -147,6 +163,26 @@ BODY will be supplied with orig-func and args."
'((name . ,name)))))
(defmacro defbar-unit (name interval default &rest body)
"Create a status bar unit with a dedicated timer.
It will create a unit with the given NAME and DEFAULT value and a timer
that runs the given BODY at the given INTERVAL."
(declare (indent defun))
(let ((timer-name (intern (format "$%s-timer" name))))
`(progn
(defvar ,name ,default)
(defvar ,timer-name)
(add-hook 'fg42/statusbar-enable-hook
(lambda ()
(setq ,timer-name
(run-with-timer 0 ,interval (lambda () ,@body)))))
(add-hook 'fg42/statusbar-disable-hook
(lambda ()
(when (timerp ,timer-name)
(cancel-timer ,timer-name)))))))
(defsubst fg42/statusbar-pre-cmd ()
"Pre command hook of fg42/statusbar."
(setq fg42/statusbar-command-state 'begin))
@ -226,8 +262,7 @@ BODY will be supplied with orig-func and args."
;; Add update timer.
(setq fg42/statusbar-timer
;; (run-with-timer 0 fg42/statusbar-refresh-idle-delay 'fg42/statusbar-show-info)
(run-with-timer 0 fg42/statusbar-refresh-idle-delay 'fg42/statusbar-display))
(run-with-timer 0 fg42/statusbar-refresh-idle-delay #'fg42/statusbar-display))
(add-hook 'minibuffer-setup-hook #'fg42/statusbar-enter-minibuffer)
(add-hook 'minibuffer-exit-hook #'fg42/statusbar-exit-minibuffer)
@ -250,13 +285,9 @@ BODY will be supplied with orig-func and args."
(setq fg42/statusbar-command-state 'exec-read)
(apply orig-func args)))
(run-hooks 'fg42/statusbar-enable-hook)
(setq fg42/statusbar-active-p t))
(defvar-local fg42/statusbar--face-cookie nil)
(defvar fg42/statusbar--orig-mode-line-remap
(or (alist-get 'mode-line face-remapping-alist) 'mode-line))
(defvar fg42/statusbar--orig-mode-line-inactive-remap
(or (alist-get 'mode-line-inactive face-remapping-alist) 'mode-line-inactive))
(defun fg42/statusbar-disable ()
"Disable the status bar."
@ -311,6 +342,7 @@ BODY will be supplied with orig-func and args."
(with-current-buffer " *Minibuf-0*"
(erase-buffer))
(run-hooks 'fg42/statusbar-disable-hook)
(setq fg42/statusbar-active-p nil))
@ -322,7 +354,6 @@ BODY will be supplied with orig-func and args."
(when (not (stringp result))
(error "The result of %s is not string" result))
(when (> (length result) 0)
(fg42/statusbar--log "fg42/statusbar: ---%s\n" result)
result)))
fg42/statusbar-components
" "))
@ -343,7 +374,7 @@ as `snails'."
(string-width (or message-string ""))
fg42/statusbar-info-padding-right)))
(cond
;; Fill message's end with whitespace to keep tray info at right of minibuffer.
;; Fill message's end with whitespace to keep the info at right of minibuffer.
((> blank-length 0)
(progn
(let* ((inhibit-message t))

View File

@ -178,5 +178,18 @@ is non-nil value it means that the function can be called interactively."
nil))
(defmacro fg42/log (tag args)
"If the debug is enabled log the give ARGS."
(if fg42/debug-p
`(message "[%s]: %s" ,tag (apply #'format ,args))
nil))
(defmacro fg42/info (&rest args)
"If the debug is enabled log the give ARGS."
(fg42/log "INFO" args))
(provide 'fg42/utils)
;;; utils.el ends here