Change the cube loading interface to postpone the operations to after emacs startup

This commit is contained in:
Sameer Rahmani 2023-06-10 20:47:55 +01:00
parent 76602edf1b
commit d65037f57f
Signed by: lxsameer
GPG Key ID: B0A4AF28AB9FD90B
9 changed files with 169 additions and 82 deletions

View File

@ -237,8 +237,8 @@ data. The typical example of this would be Lisp or Scheme source code."
(defcube fg42/dracula-theme-cube (defcube fg42/dracula-theme-cube
"Replace this with a theme cube" "Replace this with a theme cube"
(:title "Dracula theme" (:title "Dracula theme"
:no-flag t) :no-flag t
:ui-hook (lambda ()
(fpkg/use dracula-theme (fpkg/use dracula-theme
:init :init
(fg42/setup-theme (fg42/setup-theme
@ -251,7 +251,7 @@ data. The typical example of this would be Lisp or Scheme source code."
'(font-lock-comment-face ((t (:foreground "#8B9298")))) '(font-lock-comment-face ((t (:foreground "#8B9298"))))
'(font-lock-comment-delimiter-face ((t (:foreground "#5B6268"))))) '(font-lock-comment-delimiter-face ((t (:foreground "#5B6268")))))
(enable-theme 'dracula) (enable-theme 'dracula)
(set-face-attribute 'region nil :background "#888")))) (set-face-attribute 'region nil :background "#888"))))))
(defcube fg42/badwolf-theme-cube (defcube fg42/badwolf-theme-cube
"Badwolf theme cube. An Emacs port of Bad Wolf theme for Vim. "Badwolf theme cube. An Emacs port of Bad Wolf theme for Vim.

View File

@ -61,14 +61,17 @@ This cube is enough to load everything and control what cube to execute
via flags." via flags."
(:title "Editor cube" (:title "Editor cube"
:flag-default t :flag-default t
:flag fg42-editor-cube) :flag fg42-editor-cube
:init-hook (lambda (params)
(mapc (mapc
(lambda (cube) (lambda (cube)
(let ((params (plist-get fg42/editor-params (when (not (string= (symbol-name cube)
"fg42/editor"))
(let ((cube-params (plist-get
params
(intern (concat ":" (symbol-name cube)))))) (intern (concat ":" (symbol-name cube))))))
(eval `(funcall #',cube ,@params)))) (eval `(funcall #',cube ,@cube-params)))))
fg42/available-cubes)) fg42/available-cubes))))

View File

@ -79,7 +79,7 @@
(setq fg42/modeline-setter #'fg42/mini-modeline-setter)) (setq fg42/modeline-setter #'fg42/mini-modeline-setter))
:defer nil :defer nil
:config :config
(add-hook 'fg42-after-init-hook #'mini-modeline-mode) (add-hook 'fg42/after-init-hook #'mini-modeline-mode)
(add-hook 'fg42/after-initializing-theme-hook (add-hook 'fg42/after-initializing-theme-hook
(lambda () (lambda ()
(custom-set-faces (custom-set-faces
@ -111,7 +111,7 @@ valuable information."
(setq fg42/modeline-setter #'fg42/statusbar-setter) (setq fg42/modeline-setter #'fg42/statusbar-setter)
(add-hook 'fg42-after-init-hook (add-hook 'fg42/after-init-hook
(lambda () (lambda ()
(require 'fg42/statusbar) (require 'fg42/statusbar)
(fg42/statusbar-mode t)))) (fg42/statusbar-mode t))))

View File

@ -25,16 +25,70 @@
(require 'fg42/utils) (require 'fg42/utils)
(defun fg42/before-initialize () (defvar fg42/-gc-cons-threshold 16777216
"Initialize the package manager before rendering the window instance." "Value of GC threshold of FG42.")
(require 'fpkg/core)
(fpkg/initialize)) (defun defer-garbage-collection ()
"Disable garbage collection."
(setq gc-cons-threshold fg42/-gc-cons-threshold))
(defun restore-garbage-collection ()
"Restore garbage collection to it's default value."
(run-at-time
1 nil (lambda () (setq gc-cons-threshold most-positive-fixnum))))
(defun fg42/initialize () (defun fg42/initialize ()
"Initialize FG42 after the Emacs window is rendered by loading the user init file." "Initialize FG42 after the Emacs window is rendered by loading the user init file."
(fg42/-startup-optimization)
(require 'fpkg/core)
(fpkg/initialize)
(when (file-exists-p user-init-file) (when (file-exists-p user-init-file)
(load user-init-file))) (require 'fg42/cube)
(load user-init-file))
(add-hook 'emacs-startup-hook
(lambda ()
(run-hooks 'fg42/-cubes-body-hook)
(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))))
(defun fg42/-startup-optimization ()
"Optimized FG42 startup."
;; by setting gc threshold to the largest number
;; Emacs can understand we are basically disabling it :).
(setq gc-cons-threshold most-positive-fixnum
gc-cons-percentage 0.6)
;; after initilization phase restore cons threshold to normal
(add-hook 'emacs-startup-hook
(lambda ()
(setq gc-cons-threshold fg42/-gc-cons-threshold ; 16mb
gc-cons-percentage 0.1)))
;; disable auto initilization of package.el
(setq package-enable-at-startup nil)
;; disable gc when we are in minibuffer using the same method we
;; use for initilization time
(add-hook 'minibuffer-setup-hook #'defer-garbage-collection)
;; just enable gc when exiting minibuffer
(add-hook 'minibuffer-exit-hook #'restore-garbage-collection)
;; we dont need Emacs to check every file type and look for file
;; handlers when we are initializing so we backup the original
;; value and set it to nil
(setq --file-name-handler-alist file-name-handler-alist)
(setq file-name-handler-alist nil)
;; after initialization we can restore that file-name-handler-alist
;; to original value.
(add-hook 'emacs-startup-hook
(lambda ()
(setq file-name-handler-alist
--file-name-handler-alist)))
;; initial mode for emacs can be fundamental mode we have nothing to lose
(setq initial-major-mode 'fundamental-mode))
(provide 'fg42) (provide 'fg42)

View File

@ -25,11 +25,11 @@
(defgroup fg42 nil (defgroup fg42 nil
"Customize your FG42 instance via this group of configurations.") "Customize your FG42 instance via this group of configurations.")
(defvar fg42-after-init-hook nil (defvar fg42/after-init-hook nil
"The hook tha runs when FG42 finished running the user configuration") "The hook tha runs when FG42 finished running the user configuration.")
(defvar fg42/debug-p nil (defvar fg42/debug-p nil
"The hook tha runs when FG42 finished running the user configuration") "The hook tha runs when FG42 finished running the user configuration.")
(defvar fg42-home (getenv "FG42_HOME") (defvar fg42-home (getenv "FG42_HOME")
"The pass to fg42-home.") "The pass to fg42-home.")

View File

@ -27,6 +27,7 @@
;;; Code: ;;; Code:
(require 'seq) (require 'seq)
(require 'fg42/utils) (require 'fg42/utils)
(require 'fg42/themes)
(defvar fg42/after-cubes-setup-hook nil (defvar fg42/after-cubes-setup-hook nil
"A hook that will be run after all the active cubes got setup. "A hook that will be run after all the active cubes got setup.
@ -40,6 +41,10 @@ it is to use `fg42/after-cubes' macro.")
(defvar fg42/available-cubes '() (defvar fg42/available-cubes '()
"A list of all the registered cubes.") "A list of all the registered cubes.")
(defvar fg42/-cubes-body-hook nil
"A Hook that is internal to FG42 and will be used to run the cub bodies.
The execution happens after Emacs is initialized.")
(defmacro defcube (cube-name docs props &rest body) (defmacro defcube (cube-name docs props &rest body)
"Define a cube with the given CUBE-NAME, a list of PROPS, DOCS and a BODY." "Define a cube with the given CUBE-NAME, a list of PROPS, DOCS and a BODY."
@ -55,6 +60,11 @@ it is to use `fg42/after-cubes' macro.")
(let ((complete-props (plist-put props :docs docs)) (let ((complete-props (plist-put props :docs docs))
(cube-name-internal (intern (format "%s-internal" cube-name))) (cube-name-internal (intern (format "%s-internal" cube-name)))
;; prop hooks
(init-hook (plist-get props :init-hook))
(ui-hook (plist-get props :ui-hook))
(params-var (intern (format "%s-params" cube-name))) (params-var (intern (format "%s-params" cube-name)))
(active-var (intern (format "%s-active-p" cube-name))) (active-var (intern (format "%s-active-p" cube-name)))
(pre-lang-server-up-hook (intern (format "%s-pre-lang-server-up-hook" cube-name))) (pre-lang-server-up-hook (intern (format "%s-pre-lang-server-up-hook" cube-name)))
@ -145,13 +155,27 @@ it is to use `fg42/after-cubes' macro.")
(defun ,cube-name (&rest params) (defun ,cube-name (&rest params)
(interactive) (interactive)
(let ((ui-hook ,ui-hook)
(init-hook ,init-hook))
(if ,no-flag? (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 ;; If no flag is need to control this cube
(,cube-name-internal params) (when (not (null init-hook))
(funcall init-hook params))
(add-hook 'fg42/-cubes-body-hook (lambda ()
(,cube-name-internal params))))
;; Otherwise check for the flag to be active ;; Otherwise check for the flag to be active
(if-flag ,flag-var (if-flag ,flag-var
(,cube-name-internal params) (progn
(fg42/info "The flag for '%s' cube is disabled. Skiping." ,(symbol-name cube-name))))) (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))))
(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 ;; Set the symbol-plist of the cube-name to its props
(setplist ',cube-name ',complete-props)))) (setplist ',cube-name ',complete-props))))

View File

@ -27,6 +27,10 @@
;;; Code: ;;; Code:
(require 'fg42/utils) (require 'fg42/utils)
(defvar fg42/ui-hook ()
"A hook that cubes can use via :ui-hook property.
It executes way before the rest of the cubes.")
(defvar fg42/before-initializing-theme-hook () (defvar fg42/before-initializing-theme-hook ()
"The hook to plug any configuration to before initialize event of themes.") "The hook to plug any configuration to before initialize event of themes.")

View File

@ -778,7 +778,8 @@ We can clone a library somewhere on the disk and add the path to it to the =load
and load the library files. and load the library files.
But that would be tedious to do so for all the libraries. That's why we use a package manager But that would be tedious to do so for all the libraries. That's why we use a package manager
* Episode 13 - Editing Modes, Part 1 * DONE Episode 13 - Editing Modes, Part 1
CLOSED: [2023-06-10 Sat 15:44]
Emacs provides a concept called ~editing mode~ that allows Emacs provides a concept called ~editing mode~ that allows
us to control different aspect of the editor. us to control different aspect of the editor.
@ -881,3 +882,10 @@ and to enable it we can pass a positive integer.
- ~global-minor-modes~: This variable lists the currently enabled global minor modes, - ~global-minor-modes~: This variable lists the currently enabled global minor modes,
and is a list of symbols. and is a list of symbols.
- ~minor-mode-list~: The value of this variable is a list of all minor mode commands. - ~minor-mode-list~: The value of this variable is a list of all minor mode commands.
* Episode 14 - Editing Modes, Part 2
** Quick overview:
*** Hooks
*** Keymaps
*** Interactive functions
** A quick and useless minor mode
** Resources:

View File

@ -44,13 +44,7 @@
(load custom-file)) (load custom-file))
(require 'fg42) (require 'fg42)
(fg42/before-initialize)
(fg42/initialize) (fg42/initialize)
(run-hooks 'fg42/after-cubes-setup-hook)
(run-hooks 'fg42-after-init-hook)
(run-hooks 'fg42/after-initializing-theme-hook)
(provide 'fg42-config) (provide 'fg42-config)
;;; fg42-config.el ends here ;;; fg42-config.el ends here