Skip to content

Commit

Permalink
refactor: code cleanup and minor rewrites
Browse files Browse the repository at this point in the history
  • Loading branch information
abougouffa committed Nov 5, 2023
1 parent 9341311 commit 4b28273
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 80 deletions.
26 changes: 10 additions & 16 deletions core/me-core-ui.el
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,17 @@
(defun +theme--tweaks-h (&optional _)
"Use smaller font (75% of the default) for line numbers in graphic mode."
(when (display-graphic-p)
(set-face-attribute
'line-number nil
:background (face-attribute 'default :background)
:height (truncate (* 0.75 (face-attribute 'default :height)))
:weight 'semi-light)
(set-face-attribute
'line-number-current-line nil
:height (truncate (* 0.75 (face-attribute 'default :height)))
:weight 'bold))))
(set-face-attribute 'line-number nil
:background (face-attribute 'default :background)
:height (truncate (* 0.75 (face-attribute 'default :height)))
:weight 'semi-light)
(set-face-attribute 'line-number-current-line nil
:height (truncate (* 0.75 (face-attribute 'default :height)))
:weight 'bold))))

;; Save enabled theme
(add-hook
'enable-theme-functions
(defun +theme--save-enabled-theme-h (theme)
"Save the enabled theme to `minemacs-theme'.
Useful for keeping track of the enabled theme."
(setq minemacs-theme theme)))
;; Save the enabled theme to `minemacs-theme', useful for keeping track of the enabled theme.
(+setq-hook! 'enable-theme-functions
minemacs-theme (car args))

;; Disable previously enabled custom themes before enabling a new one.
(advice-add
Expand Down
51 changes: 28 additions & 23 deletions elisp/+minemacs.el
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ If NO-MESSAGE-LOG is non-nil, do not print any message to *Messages* buffer."
;;;###autoload
(defmacro +cmdfy! (&rest body)
"Convert BODY to an interactive command."
`(lambda ()
(interactive)
,@body))
`(lambda () (interactive) ,@body))

;;;###autoload
(defun +load-theme ()
Expand Down Expand Up @@ -108,22 +106,19 @@ If NO-MESSAGE-LOG is non-nil, do not print any message to *Messages* buffer."
"Evaluate BODY when Emacs becomes idle."
(declare (indent 0))
`(+eval-when-idle ,+eval-when-idle-delay
(lambda ()
,@body)))
(lambda () ,@body)))

;;;###autoload
(defmacro +eval-when-idle-for! (delay &rest body)
"Evaluate BODY after DELAY seconds from Emacs becoming idle."
(declare (indent 1))
`(+eval-when-idle ,delay
(lambda ()
,@body)))
(lambda () ,@body)))

;;;###autoload
(defmacro +deferred! (&rest body)
"Run BODY after Emacs gets loaded, a.k.a. after `minemacs-loaded'."
`(with-eval-after-load 'minemacs-loaded
,@body))
`(with-eval-after-load 'minemacs-loaded ,@body))

;;;###autoload
(defmacro +deferred-when! (condition &rest body)
Expand All @@ -141,8 +136,7 @@ If NO-MESSAGE-LOG is non-nil, do not print any message to *Messages* buffer."
(defmacro +lazy! (&rest body)
"Run BODY as a lazy block (see `minemacs-lazy')."
`(with-eval-after-load 'minemacs-lazy
(+eval-when-idle-for! +lazy-delay
,@body)))
(+eval-when-idle-for! +lazy-delay ,@body)))

;;;###autoload
(defmacro +lazy-when! (condition &rest body)
Expand All @@ -167,9 +161,8 @@ If NO-MESSAGE-LOG is non-nil, do not print any message to *Messages* buffer."
(cond
((memq feature '(:or :any))
(macroexp-progn
(cl-loop
for next in (cdr features)
collect `(with-eval-after-load ',(+unquote next) ,@body))))
(cl-loop for next in (cdr features)
collect `(with-eval-after-load ',(+unquote next) ,@body))))
((memq feature '(:and :all))
(dolist (next (reverse (cdr features)) (car body))
(setq body `((with-eval-after-load ',(+unquote next) ,@body)))))
Expand Down Expand Up @@ -197,8 +190,7 @@ DEPTH and LOCAL are passed as is to `add-hook'."
(declare (indent 1))
(let ((hook (+unquote hook))
(fn-name (intern (format "+hook-once--function-%d-h" (cl-incf +hook-once-num)))))
`(add-hook
',hook
`(add-hook ',hook
(defun ,fn-name (&rest _)
,(macroexp-progn body)
(remove-hook ',hook ',fn-name)))))
Expand All @@ -217,7 +209,8 @@ EXT-REGEXP. When it runs, this function provides a feature named
(fn-name (intern (format "+first-file-%s-h" (if filetype (format "-%s" filetype) ""))))
(hook-name (intern (format "minemacs-first%s-file-hook" (if filetype (format "-%s" filetype) ""))))
(feature-name (intern (format "minemacs-first%s-file" (if filetype (format "-%s" filetype) ""))))
(hook-docs (format "This hook will be run after opening the first %s file (files that matches \"%s\").\n\nExecuted before `after-find-file', it runs all hooks in `%s' and provide the `%s' feature."
(hook-docs (format "This hook will be run after opening the first %s file (files that matches \"%s\").
Executed before `after-find-file', it runs all hooks in `%s' and provide the `%s' feature."
filetype ext-regexp hook-name feature-name)))
`(progn
(+log! "Setting up hook `%s' -- function `%s' -- feature `%s'."
Expand Down Expand Up @@ -265,13 +258,12 @@ list is returned as-is."
vars))
(nreverse vars))
for hook in (+resolve-hook-forms hooks)
for mode = (string-remove-suffix "-hook" (symbol-name hook))
append
(cl-loop for (var . val) in vars
collect
(list var val hook
(intern (format "minemacs--setq-%s-for-%s-h"
var mode))))))
(intern (format "+setq--%s-in-%s-h"
var hook))))))

;; From Doom Emacs
;;;###autoload
Expand All @@ -289,6 +281,12 @@ This macro accepts, in order:
thereof, a list of `defun' or `cl-defun' forms, or arbitrary forms (will
implicitly be wrapped in a lambda).
If the hook function should receive an argument (like in
`enable-theme-functions'), the `args' variable can be expanded in the forms
(+add-hook! 'enable-theme-functions
(message \"Enabled theme: %s\" (car args)))
\(fn HOOKS [:append :local [:depth N]] FUNCTIONS-OR-FORMS...)"
(declare (indent (lambda (indent-point state)
(goto-char indent-point)
Expand Down Expand Up @@ -320,7 +318,7 @@ This macro accepts, in order:
((memq first '(defun cl-defun))
(push next defn-forms)
(list 'function (cadr next)))
((prog1 `(lambda (&rest _) ,@(cons next rest))
((prog1 `(lambda (&rest args) ,@(cons next rest))
(setq rest nil))))
func-forms)))
`(progn
Expand Down Expand Up @@ -349,11 +347,18 @@ If N and M = 1, there's no benefit to using this macro over `remove-hook'.
(defmacro +setq-hook! (hooks &rest var-vals)
"Sets buffer-local variables on HOOKS.
HOOKS can be expect receiving arguments (like in `enable-theme-functions'), the
`args' variable can be used inside VAR-VALS forms to get the arguments passed
the the function.
(+setq-hook! 'enable-theme-functions
current-theme (car args))
\(fn HOOKS &rest [SYM VAL]...)"
(declare (indent 1))
(macroexp-progn
(cl-loop for (var val hook fn) in (+setq-hook-fns hooks var-vals)
collect `(defun ,fn (&rest _)
collect `(defun ,fn (&rest args)
,(format "%s = %s" var (pp-to-string val))
(setq-local ,var ,val))
collect `(add-hook ',hook #',fn -90))))
Expand All @@ -363,7 +368,7 @@ If N and M = 1, there's no benefit to using this macro over `remove-hook'.
(defmacro +unsetq-hook! (hooks &rest vars)
"Unbind setq hooks on HOOKS for VARS.
\(fn HOOKS &rest [SYM VAL]...)"
\(fn HOOKS &rest VAR1 VAR2...)"
(declare (indent 1))
(macroexp-progn
(cl-loop for (_var _val hook fn)
Expand Down
56 changes: 17 additions & 39 deletions init.el
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@
'emacs-startup-hook
(defun +mineamcs--restore-file-name-handler-alist-h ()
(setq file-name-handler-alist
(delete-dups
(append file-name-handler-alist
(get 'file-name-handler-alist 'original-value)))))
(delete-dups (append file-name-handler-alist
(get 'file-name-handler-alist 'original-value)))))
100)

;; HACK: At this point, MinEmacs variables defined in `me-vars' should be
Expand Down Expand Up @@ -114,8 +113,7 @@
;; some new features when configuring them.
(when (< emacs-major-version 29)
(let ((backports-dir (concat minemacs-core-dir "backports/")))
(mapc (apply-partially #'+load backports-dir)
(directory-files backports-dir nil "\\.el$"))))
(mapc (apply-partially #'+load backports-dir) (directory-files backports-dir nil "\\.el$"))))

(setq
;; Enable debugging on error when Emacs is launched with the "--debug-init"
Expand Down Expand Up @@ -150,29 +148,23 @@
(defun minemacs-generate-loaddefs ()
"Generate MinEmacs' loaddefs file."
(interactive)
(when (file-exists-p minemacs-loaddefs-file)
(delete-file minemacs-loaddefs-file))

(loaddefs-generate
(list minemacs-core-dir minemacs-elisp-dir minemacs-extras-dir)
minemacs-loaddefs-file))
(when (file-exists-p minemacs-loaddefs-file) (delete-file minemacs-loaddefs-file))
(loaddefs-generate (list minemacs-core-dir minemacs-elisp-dir minemacs-extras-dir) minemacs-loaddefs-file))

;; Some of MinEmacs commands and libraries are defined to be auto-loaded. In
;; particular, these in the `minemacs-core-dir', `minemacs-elisp-dir', and
;; `minemacs-extras-dir' directories. The generated loaddefs file will be stored
;; in `minemacs-loaddefs-file'. We first regenerate the loaddefs file if it
;; doesn't exist.
(unless (file-exists-p minemacs-loaddefs-file)
(minemacs-generate-loaddefs))
(unless (file-exists-p minemacs-loaddefs-file) (minemacs-generate-loaddefs))

;; Then we load the loaddefs file
(+load minemacs-loaddefs-file)

;; Load user init tweaks from "$MINEMACSDIR/init-tweaks.el" when available
(unless (memq 'init-tweaks minemacs-ignore-user-config)
(let ((user-init-tweaks (concat minemacs-config-dir "init-tweaks.el")))
(when (file-exists-p user-init-tweaks)
(+load user-init-tweaks))))
(when (file-exists-p user-init-tweaks) (+load user-init-tweaks))))

;; HACK: Load the environment variables saved from shell using `+env-save' to
;; `+env-file'. `+env-save' saves all environment variables except these matched
Expand Down Expand Up @@ -212,17 +204,7 @@
;; Switch to the previous buffer
(switch-to-buffer buf)
;; And kill the Emacs' default scratch buffer
(when-let ((s (get-buffer "*scratch*"))) (kill-buffer s))))

;; In `me-defaults', the `initial-major-mode' is set to `fundamental-mode'
;; to enhance startup time. However, I like to use the scratch buffer to
;; evaluate Elisp code, so we switch to Elisp mode in the scratch buffer
;; when Emacs is idle for 10 seconds.
(+eval-when-idle-for! 10.0
(setq initial-major-mode 'lisp-interaction-mode)
(when-let ((scratch-buffer (get-buffer "*scratch*")))
(with-current-buffer scratch-buffer
(emacs-lisp-mode)))))
(when-let ((s (get-buffer "*scratch*"))) (kill-buffer s)))))

;; Require the virtual package to triggre loading packages depending on it
(require 'minemacs-loaded))
Expand All @@ -248,8 +230,7 @@
(unless (memq 'modules minemacs-ignore-user-config)
;; The modules.el file can override minemacs-modules and minemacs-core-modules
(let ((user-conf-modules (concat minemacs-config-dir "modules.el")))
(when (file-exists-p user-conf-modules)
(+load user-conf-modules)))))
(when (file-exists-p user-conf-modules) (+load user-conf-modules)))))

;; NOTE: Ensure the `me-gc' module is in the core modules list. This module
;; enables the `gcmh-mode' package (a.k.a. the Garbage Collector Magic Hack).
Expand All @@ -268,13 +249,12 @@
;; functionality.
(setq minemacs-core-modules
(delete-dups
(append
'(me-defaults)
(when (memq 'me-splash minemacs-core-modules) '(me-splash))
'(me-bootstrap)
(when (< emacs-major-version 29) '(me-compat))
'(me-builtin me-gc me-fonts)
minemacs-core-modules)))
(append '(me-defaults)
(when (memq 'me-splash minemacs-core-modules) '(me-splash))
'(me-bootstrap)
(when (< emacs-major-version 29) '(me-compat))
'(me-builtin me-gc me-fonts)
minemacs-core-modules)))

;; Load MinEmacs modules
(dolist (module-file (append
Expand All @@ -288,14 +268,12 @@
(setq custom-file (concat minemacs-config-dir "custom-vars.el"))

;; Load the custom variables file if it exists
(when (file-exists-p custom-file)
(+load custom-file))
(when (file-exists-p custom-file) (+load custom-file))

;; Load user configuration from "$MINEMACSDIR/config.el" when available
(unless (memq 'config minemacs-ignore-user-config)
(let ((user-config (concat minemacs-config-dir "config.el")))
(when (file-exists-p user-config)
(+load user-config))))
(when (file-exists-p user-config) (+load user-config))))

(+lazy!
(when (featurep 'native-compile)
Expand Down
5 changes: 3 additions & 2 deletions modules/me-ui.el
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@

(use-package me-writing-mode
:init
(+map! "tw" #'+writing-mode
"tW" #'+writing-global-mode))
(+map!
"tw" #'+writing-mode
"tW" #'+writing-global-mode))

(use-package page-break-lines
:straight t
Expand Down

0 comments on commit 4b28273

Please sign in to comment.