Files
doomemacs/lisp/lib/config.el
Henrik Lissner 8cafbe4408 refactor!: restructure Doom core
BREAKING CHANGE: This restructures Doom's core in an effort to slim it
down and partially mirror architectural changes coming in v3. This is
part 2 of 3 commits (part 1 being 1590434), done to facilitate a change
in part 3 that will introduce a new `doom!` syntax for pulling
third-party module libraries from remote sources (similar to `package!`
statements). I am backporting this from V3 so I can move our modules out
into separate repos sooner than later, so development on modules can
continue separately without interfering with v3's roll out.

Though this is labeled a breaking change, it shouldn't affect most users
except those few tinkering directly with Doom's internals.

Ref: 15904349cf
2024-11-03 01:52:28 -05:00

171 lines
5.9 KiB
EmacsLisp

;;; lisp/lib/config.el -*- lexical-binding: t; -*-
;;;###autoload
(defvar doom-after-reload-hook nil
"A list of hooks to run after `doom/reload' has reloaded Doom.")
;;;###autoload
(defvar doom-before-reload-hook nil
"A list of hooks to run before `doom/reload' has reloaded Doom.")
;;;###autoload
(defun doom/open-private-config ()
"Browse your `doom-user-dir'."
(interactive)
(unless (file-directory-p doom-user-dir)
(make-directory doom-user-dir t))
(doom-project-browse doom-user-dir))
;;;###autoload
(defun doom/find-file-in-private-config ()
"Search for a file in `doom-user-dir'."
(interactive)
(doom-project-find-file doom-user-dir))
;;;###autoload
(defun doom/goto-private-init-file ()
"Open your private init.el file.
And jumps to your `doom!' block."
(interactive)
(find-file (expand-file-name doom-module-init-file doom-user-dir))
(goto-char
(or (save-excursion
(goto-char (point-min))
(search-forward "(doom!" nil t))
(point))))
;;;###autoload
(defun doom/goto-private-config-file ()
"Open your private config.el file."
(interactive)
(find-file (expand-file-name doom-module-config-file doom-user-dir)))
;;;###autoload
(defun doom/goto-private-packages-file ()
"Open your private packages.el file."
(interactive)
(find-file (expand-file-name doom-module-packages-file doom-user-dir)))
;;
;;; Managements
(defmacro doom--if-compile (command on-success &optional on-failure)
(declare (indent 2))
`(let ((doom-bin "doom")
(default-directory doom-emacs-dir)
(exec-path (cons doom-bin-dir exec-path)))
(when (and (featurep :system 'windows)
(string-match-p "cmdproxy.exe$" shell-file-name))
(unless (executable-find "pwsh")
(user-error "Powershell 3.0+ is required, but pwsh.exe was not found in your $PATH"))
(setq doom-bin "doom.ps1"))
;; Ensure the bin/doom operates with the same environment as this
;; running session.
(letenv! (("PATH" (string-join exec-path path-separator))
("EMACS" (doom-path invocation-directory invocation-name))
("EMACSDIR" doom-emacs-dir)
("DOOMDIR" doom-user-dir)
("DOOMLOCALDIR" doom-local-dir)
("DEBUG" (if doom-debug-mode (number-to-string doom-log-level) "")))
(with-current-buffer
(compile (format ,command (expand-file-name doom-bin doom-bin-dir)) t)
(let ((w (get-buffer-window (current-buffer))))
(select-window w)
(add-hook
'compilation-finish-functions
(lambda (_buf status)
(if (equal status "finished\n")
(progn
(delete-window w)
,on-success)
,on-failure))
nil 'local))))))
(defvar doom-reload-command
(format "%s sync -B -e"
;; /usr/bin/env doesn't exist on Android
(if (featurep :system 'android)
"sh %s"
"%s"))
"Command that `doom/reload' runs.")
;;;###autoload
(defun doom/reload ()
"Reloads your private config.
WARNING: This command is experimental! If you haven't configured your config to
be idempotent, then this could cause compounding slowness or errors.
This is experimental! It will try to do as `bin/doom sync' does, but from within
this Emacs session. i.e. it reload autoloads files (if necessary), reloads your
package list, and lastly, reloads your private config.el.
Runs `doom-after-reload-hook' afterwards."
(interactive)
(mapc #'require (cdr doom-incremental-packages))
(doom--if-compile doom-reload-command
(with-doom-context '(reload module)
(doom-run-hooks 'doom-before-reload-hook)
(doom-load (file-name-concat doom-user-dir doom-module-init-file) t)
(with-demoted-errors "PRIVATE CONFIG ERROR: %s"
(general-auto-unbind-keys)
(unwind-protect
(startup--load-user-init-file nil)
(general-auto-unbind-keys t)))
(doom-run-hooks 'doom-after-reload-hook)
(message "Config successfully reloaded!"))
(user-error "Failed to reload your config")))
;;;###autoload
(defun doom/reload-autoloads ()
"Reload only the autoloads of the current profile.
This is much faster and safer than `doom/reload', but not as comprehensive. This
reloads your package and module visibility, but does not install new packages or
remove orphaned ones. It also doesn't reload your private config.
It is useful to only pull in changes performed by 'doom sync' on the command
line."
(interactive)
(doom-require 'doom-lib 'profiles)
;; TODO: Make this more robust
(with-doom-context 'reload
(dolist (file (mapcar #'car doom-profile-generators))
(when (string-match-p "/[0-9]+-loaddefs[.-]" file)
(load (doom-path doom-profile-dir doom-profile-init-dir-name file)
'noerror)))))
;;;###autoload
(defun doom/reload-env ()
"Reloads your envvar file.
DOES NOT REGENERATE IT. You must run 'doom env' in your shell OUTSIDE of Emacs.
Doing so from within Emacs will taint your shell environment.
An envvar file contains a snapshot of your shell environment, which can be
imported into Emacs."
(interactive)
(with-doom-context 'reload
(let ((default-directory doom-emacs-dir))
(with-temp-buffer
(doom-load-envvars-file doom-env-file)
(message "Reloaded %S" (abbreviate-file-name doom-env-file))))))
(defvar doom-upgrade-command
(format "%s upgrade -B --force"
;; /usr/bin/env doesn't exist on Android
(if (featurep :system 'android)
"sh %s"
"%s"))
"Command that `doom/upgrade' runs.")
;;;###autoload
(defun doom/upgrade ()
"Run 'doom upgrade' then prompt to restart Emacs."
(interactive)
(doom--if-compile doom-upgrade-command
(when (y-or-n-p "You must restart Emacs for the upgrade to take effect.\n\nRestart Emacs?")
(doom/restart-and-restore))))
(provide 'doom-lib '(config))
;;; config.el ends here