From 08e52fb49d30498b869dbad1e6b5cb92018b9cd0 Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Sun, 11 Jun 2023 12:47:00 +0100 Subject: [PATCH] Fix the issue with running a cube after initialization --- core/cubes/modeline.el | 19 ++++++++++-------- core/fg42.el | 6 +++++- core/fg42/cube.el | 45 +++++++++++++++++++++++++----------------- docs/videos.org | 8 ++++++-- 4 files changed, 49 insertions(+), 29 deletions(-) diff --git a/core/cubes/modeline.el b/core/cubes/modeline.el index 7e5ca7f..607d6e5 100644 --- a/core/cubes/modeline.el +++ b/core/cubes/modeline.el @@ -30,6 +30,17 @@ "The default StatusBar modeline.") +(->cube smart-mode-line + "Smart mode line is a pretty simple yet fantastic alternative +to Emacs modeline." + :straight (smart-mode-line :source melpa) + :defer nil + :init + (progn + (setq sml/theme 'respectful) + (setq sml/no-confirm-load-theme t) + (sml/setup))) + ;; TODO: Break this into two cubes (defcube fg42/modeline-cube "Modeline cube" @@ -59,14 +70,6 @@ (:eval (string-trim (format-mode-line mode-line-modes))) mode-line-misc-info )))) - (fpkg/use smart-mode-line - :straight (smart-mode-line :source melpa) - :defer nil - :init - (progn - (setq sml/theme 'respectful) - (setq sml/no-confirm-load-theme t) - (sml/setup))) (fpkg/use mini-modeline :straight (mini-modeline :repo "kiennq/emacs-mini-modeline" diff --git a/core/fg42.el b/core/fg42.el index 395abf5..0642277 100644 --- a/core/fg42.el +++ b/core/fg42.el @@ -28,6 +28,9 @@ (defvar fg42/-gc-cons-threshold 16777216 "Value of GC threshold of FG42.") +(defvar fg42/initialized nil + "A variable that indicates whether FG42 is passed initialization.") + (defun defer-garbage-collection () "Disable garbage collection." (setq gc-cons-threshold fg42/-gc-cons-threshold)) @@ -53,7 +56,8 @@ (run-hooks 'fg42/after-cubes-setup-hook) (run-hooks 'fg42/after-init-hook) (run-hooks 'fg42/after-initializing-theme-hook) - (run-hooks 'fg42/ui-hook)))) + (run-hooks 'fg42/ui-hook) + (setq fg42/initialized t)))) (defun fg42/-startup-optimization () diff --git a/core/fg42/cube.el b/core/fg42/cube.el index b18866c..33d8b47 100644 --- a/core/fg42/cube.el +++ b/core/fg42/cube.el @@ -60,6 +60,14 @@ It will returen a pair in form of (body . props)." (cons body-list (cdr acc))))) +(defun fg42/run-cube-after-initialization (f) + "Run the given Cube body function F after FG42's initialization. +If FG42 is already initialized, just run F." + (if (null fg42/initialized) + (add-hook 'fg42/-cubes-body-hook f) + (funcall f))) + + (defmacro defcube (cube-name docs &rest props-n-body) "Define a cube with the given CUBE-NAME, DOCS and a PROPS-N-BODY. @@ -174,26 +182,26 @@ TODO: Docs" (defun ,cube-name (&rest params) (interactive) - (let ((ui-hook ,ui-hook) - (init-hook ,init-hook)) + (let ((fg42/---f-sym + (lambda () + (when (not (null ,ui-hook)) + (add-hook 'fg42/ui-hook #',ui-hook)) + + (when (not (null ,init-hook)) + (funcall #',init-hook params)) + + ;; Run the cube internal after initialization or + ;; if Emacs is already up, just run it. + (fg42/run-cube-after-initialization + (lambda () + (,cube-name-internal params)))))) + (if ,no-flag? - (progn - (when (not (null ui-hook)) - (add-hook 'fg42/ui-hook ui-hook)) - ;; If no flag is need to control this cube - (when (not (null init-hook)) - (funcall init-hook params)) - (add-hook 'fg42/-cubes-body-hook (lambda () - (,cube-name-internal params)))) + ;; If no flag is need to control this cube + (funcall fg42/---f-sym) ;; Otherwise check for the flag to be active (if-flag ,flag-var - (progn - (when (not (null ui-hook)) - (add-hook 'fg42/ui-hook ,ui-hook)) - (when (not (null init-hook)) - (funcall init-hook params)) - (add-hook 'fg42/-cubes-body-hook (lambda () - (,cube-name-internal params)))) + (funcall fg42/---f-sym) (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 @@ -210,7 +218,8 @@ TODO: Docs" (defmacro ->cube (pkg docs &rest body) "A wrapper to create a cube that use only a PKG. -It passes the BODY to `fpkg/use'." +It passes the BODY to `fpkg/use'. +And passes DOCS to `defcube' as the cube documentation." (declare (indent defun) (doc-string 2)) `(defcube ,(intern (format "fg42/%s-cube" pkg)) ,docs diff --git a/docs/videos.org b/docs/videos.org index a48a821..1ab2045 100644 --- a/docs/videos.org +++ b/docs/videos.org @@ -884,8 +884,12 @@ and to enable it we can pass a positive integer. - ~minor-mode-list~: The value of this variable is a list of all minor mode commands. * Episode 14 - Editing Modes, Part 2 ** Quick overview: +*** A simple minor mode +*** Interactive functions *** Hooks *** Keymaps -*** Interactive functions -** A quick and useless minor mode +** Let's do it ** Resources: +- https://www.gnu.org/software/emacs/manual/html_node/elisp/Library-Headers +- https://www.gnu.org/software/emacs/manual/html_node/elisp/Hooks.html +- https://www.gnu.org/software/emacs/manual/html_node/elisp/Keymaps.html