dev: merge branch 'master' of github.com:doomemacs

This commit is contained in:
Matt Nish-Lapidus 2024-09-12 11:08:58 -04:00
commit a2ae771393
36 changed files with 792 additions and 241 deletions

View file

@ -43,7 +43,7 @@ performance, it is best to run Doom out of ~/.config/emacs or ~/.emacs.d."
;; Evaluate piped-in text directly, if given.
(eval (read input) t)
(doom-run-repl context))
(let* ((tempdir (doom-path (temporary-file-directory) "doom.run"))
(let* ((tempdir (doom-path (temporary-file-directory) "doom.run"))
(tempemacsdir (doom-path tempdir ".emacs.d")))
(delete-directory tempdir t) ; start from scratch
(make-directory tempemacsdir t)
@ -51,18 +51,20 @@ performance, it is best to run Doom out of ~/.config/emacs or ~/.emacs.d."
;; configs, or binscripts, we symlink these to the sandbox.
;; REVIEW: Use `--init-directory' when we drop 29 support OR when Doom is
;; in bootloader mode.
(dolist (dir (list (or (getenv "XDG_DATA_HOME") "~/.local/share")
(or (getenv "XDG_BIN_HOME") "~/.local/bin")
(or (getenv "XDG_CONFIG_HOME") "~/.config")
(or (getenv "XDG_CACHE_HOME") "~/.cache")))
(let* ((xdg-dir (doom-path dir))
(target (doom-path tempdir (file-relative-name xdg-dir "~"))))
(when (file-directory-p xdg-dir)
(dolist (dir (list (cons "XDG_DATA_HOME" ".local/share")
(cons "XDG_STATE_HOME" ".local/state")
(cons "XDG_BIN_HOME" ".local/bin")
(cons "XDG_CONFIG_HOME" ".config")
(cons "XDG_CACHE_HOME" ".cache")))
(let* ((source (expand-file-name (or (getenv (car dir)) (expand-file-name (cdr dir) "~"))))
(target (expand-file-name (cdr dir) tempdir)))
(when (file-directory-p source)
(unless (file-symlink-p target)
(make-directory (file-name-directory target) t)
(make-symbolic-link xdg-dir target)))))
(make-symbolic-link source target)))))
(with-temp-file (doom-path tempemacsdir "early-init.el")
(prin1 `(progn
;; Restore sane values for these envvars
(setenv "HOME" ,(getenv "HOME"))
(setenv "EMACSDIR" ,doom-emacs-dir)
(setenv "DOOMDIR" ,doom-user-dir)

View file

@ -361,6 +361,71 @@ Or to create aliases for functions that behave differently:
(:baz hello :boop nil)
(:bar 42)))
#+end_src
* letf!
:PROPERTIES:
:added: 3.0.0-pre
:END:
#+begin_src emacs-lisp :eval yes
(letf! (defun greet (name)
(message "Hello %s" name))
(greet "Doom")) ; #=> Hello Doom
#+end_src
#+RESULTS:
: Hello Doom
Multiple definitions:
#+begin_src emacs-lisp :eval yes :results output
(letf! ((defun greet (name)
(princ (format "Hello %s" name))
(terpri))
(defun destroy (name)
(princ (format "Blood for the %s god!" name))
(terpri)))
(greet "Doom")
(destroy "Doom"))
#+end_src
#+RESULTS:
: Hello Doom
: Blood for the Doom god!
If defining an already-existing function, the old definition will be bound to a
variable by the same name:
#+begin_src emacs-lisp :eval yes :results output
(letf! (defun princ (str)
(funcall princ (format "[overwritten] %s" str)))
(princ "Doom Emacs"))
#+end_src
#+RESULTS:
: [overwritten] Doom Emacs
Defining temporary advice:
#+begin_src emacs-lisp :eval yes
(letf! ((defun advised-message (fn message &rest args)
(apply fn (concat "[advised (1)] " message) args))
(defadvice #'message :around #'advised-message)
(defadvice message (:around (fn message &rest args))
(apply fn (concat "[advised (2)] " message) args)))
(message "Hello world"))
#+end_src
#+RESULTS:
: [advised (1)] [advised (2)] Hello world
Calling "lexical" functions recursively:
#+begin_src emacs-lisp :eval yes
(letf! (defun* triangle (number)
(cond ((<= number 0) 0)
((= number 1) 1)
((> number 1)
(+ number (triangle (1- number))))))
(triangle 5))
#+end_src
#+RESULTS:
: 15
* load!
:PROPERTIES:

View file

@ -1877,7 +1877,6 @@ errors to `doom-cli-error-file')."
(error "Cannot nest `run!' calls"))
(doom-run-hooks 'doom-after-init-hook)
(doom-context-with 'cli
;; (doom-modules-initialize)
(let* ((args (flatten-list args))
(context (make-doom-cli-context :prefix prefix :whole args))
(doom-cli--context context)

View file

@ -7,6 +7,7 @@
(define-error 'doom-font-error "Could not find a font on your system" 'doom-error)
(define-error 'doom-nosync-error "Doom hasn't been initialized yet; did you remember to run 'doom sync' in the shell?" 'doom-error)
(define-error 'doom-core-error "Unexpected error in Doom's core" 'doom-error)
(define-error 'doom-context-error "Incorrect context error" 'doom-error)
(define-error 'doom-hook-error "Error in a Doom startup hook" 'doom-error)
(define-error 'doom-autoload-error "Error in Doom's autoloads file" 'doom-error)
(define-error 'doom-user-error "Error caused by user's config or system" 'doom-error)
@ -19,10 +20,27 @@
;;; Logging
(defvar doom-inhibit-log (not (or noninteractive init-file-debug))
"If non-nil, suppress `doom-log' output.")
"If non-nil, suppress `doom-log' output completely.")
(defun doom--log (text &rest args)
(let ((inhibit-message (not init-file-debug))
(defvar doom-log-level
(if init-file-debug
(if-let ((level (getenv-internal "DEBUG"))
(level (string-to-number level))
((not (zerop level))))
level
2)
0)
"How verbosely to log from `doom-log' calls.
0 -- No logging at all.
1 -- Only warnings.
2 -- Warnings and notices.
3 -- Debug info, warnings, and notices.")
(defun doom--log (level text &rest args)
(let ((inhibit-message (if noninteractive
(not init-file-debug)
(> level doom-log-level)))
(absolute? (string-prefix-p ":" text)))
(apply #'message
(propertize (concat "* %.06f:%s" (if (not absolute?) ":") text)
@ -38,14 +56,19 @@
":")
args)))
;; This is a macro instead of a function to prevent the potentially expensive
;; evaluation of its arguments when debug mode is off. Return non-nil.
(defmacro doom-log (message &rest args)
"Log a message in *Messages*.
Does not emit the message in the echo area. This is a macro instead of a
function to prevent the potentially expensive evaluation of its arguments when
debug mode is off. Return non-nil."
"Log a message to stderr or *Messages* (without displaying in the echo area)."
(declare (debug t))
`(unless doom-inhibit-log (doom--log ,message ,@args)))
(let ((level (if (integerp message)
(prog1 message
(setq message (pop args)))
2)))
`(when (and (not doom-inhibit-log)
(or (not noninteractive)
(<= ,level doom-log-level)))
(doom--log ,level ,message ,@args))))
;;
@ -290,14 +313,13 @@ TRIGGER-HOOK is a list of quoted hooks and/or sharp-quoted functions."
(defmacro file! ()
"Return the file of the file this macro was called."
(or
;; REVIEW: Use `macroexp-file-name' once 27 support is dropped.
(let ((file (car (last current-load-list))))
(if (stringp file) file))
(bound-and-true-p byte-compile-current-file)
load-file-name
buffer-file-name ; for `eval'
(error "file!: cannot deduce the current file path")))
(or (bound-and-true-p byte-compile-current-file)
load-file-name
(buffer-file-name (buffer-base-buffer)) ; for `eval'
;; REVIEW: Use `macroexp-file-name' once 27 support is dropped.
(let ((file (car (last current-load-list))))
(if (stringp file) file))
(error "file!: cannot deduce the current file path")))
(defmacro dir! ()
"Return the directory of the file in which this macro was called."
@ -311,24 +333,26 @@ TRIGGER-HOOK is a list of quoted hooks and/or sharp-quoted functions."
"Temporarily rebind function, macros, and advice in BODY.
Intended as syntax sugar for `cl-letf', `cl-labels', `cl-macrolet', and
temporary advice.
temporary advice (`define-advice').
BINDINGS is either:
A list of, or a single, `defun', `defun*', `defmacro', or `defadvice' forms.
A list of (PLACE VALUE) bindings as `cl-letf*' would accept.
A list of, or a single, `defun', `defun*', `defmacro', or `defadvice' forms.
TYPE is one of:
The def* forms accepted are:
`defun' (uses `cl-letf')
`defun*' (uses `cl-labels'; allows recursive references),
`defmacro' (uses `cl-macrolet')
`defadvice' (uses `defadvice!' before BODY, then `undefadvice!' after)
NAME, ARGLIST, and BODY are the same as `defun', `defun*', `defmacro', and
`defadvice!', respectively.
\(fn ((TYPE NAME ARGLIST &rest BODY) ...) BODY...)"
(defun NAME (ARGS...) &rest BODY)
Defines a temporary function with `cl-letf'
(defun* NAME (ARGS...) &rest BODY)
Defines a temporary function with `cl-labels' (allows recursive
definitions).
(defmacro NAME (ARGS...) &rest BODY)
Uses `cl-macrolet'.
(defadvice FUNCTION WHERE ADVICE)
Uses `advice-add' (then `advice-remove' afterwards).
(defadvice FUNCTION (HOW LAMBDA-LIST &optional NAME DEPTH) &rest BODY)
Defines temporary advice with `define-advice'."
(declare (indent defun))
(setq body (macroexp-progn body))
(when (memq (car bindings) '(defun defun* defmacro defadvice))
@ -339,16 +363,33 @@ NAME, ARGLIST, and BODY are the same as `defun', `defun*', `defmacro', and
(setq
body (pcase type
(`defmacro `(cl-macrolet ((,@rest)) ,body))
(`defadvice `(progn (defadvice! ,@rest)
(unwind-protect ,body (undefadvice! ,@rest))))
((or `defun `defun*)
(`defadvice
(if (keywordp (cadr rest))
(cl-destructuring-bind (target where fn) rest
`(when-let (fn ,fn)
(advice-add ,target ,where fn)
(unwind-protect ,body (advice-remove ,target fn))))
(let* ((fn (pop rest))
(argspec (pop rest)))
(when (< (length argspec) 3)
(setq argspec
(list (nth 0 argspec)
(nth 1 argspec)
(or (nth 2 argspec) (gensym (format "%s-a" (symbol-name fn)))))))
(let ((name (nth 2 argspec)))
`(progn
(define-advice ,fn ,argspec ,@rest)
(unwind-protect ,body
(advice-remove #',fn #',name)
,(if name `(fmakunbound ',name))))))))
(`defun
`(cl-letf ((,(car rest) (symbol-function #',(car rest))))
(ignore ,(car rest))
,(if (eq type 'defun*)
`(cl-labels ((,@rest)) ,body)
`(cl-letf (((symbol-function #',(car rest))
(lambda! ,(cadr rest) ,@(cddr rest))))
,body))))
(cl-letf (((symbol-function #',(car rest))
(lambda! ,(cadr rest) ,@(cddr rest))))
,body)))
(`defun*
`(cl-labels ((,@rest)) ,body))
(_
(when (eq (car-safe type) 'function)
(setq type (list 'symbol-function type)))

View file

@ -50,7 +50,7 @@ NOT IMPLEMENTED YET. This file contains a module's metadata: their version,
maintainers, checks, features, submodules, debug information, etc. And are used
to locate modules in the user's file tree.")
;; DEPRECATED: Module warnings will be rewritten in v3, and this variable will no longer be needed.
;; DEPRECATED: Remove in v3, as it will be handled in the CLI
(make-obsolete-variable 'doom-obsolete-modules nil "3.0.0")
(defconst doom-obsolete-modules
'((:feature (version-control (:emacs vc) (:ui vc-gutter))

View file

@ -143,11 +143,6 @@
(setq selection-coding-system 'utf-8))
;;; Support for Doom-specific file extensions
(add-to-list 'auto-mode-alist '("/\\.doom\\(?:project\\|module\\|profile\\)\\'" . lisp-data-mode))
(add-to-list 'auto-mode-alist '("/\\.doomrc\\'" . emacs-lisp-mode))
;;
;;; MODE-local-vars-hook
@ -339,9 +334,8 @@ If RETURN-P, return the message as a string instead of displaying it."
;; TODO: Catch errors
(load! (string-remove-suffix ".el" doom-module-init-file) doom-user-dir t)
;;; Load the rest of $DOOMDIR + modules if noninteractive
;; If the user is loading this file from a batch script, let's assume they want
;; to load their userland config as well.
;; to load their userland config immediately.
(when noninteractive
(doom-require 'doom-profiles)
(let ((init-file (doom-profile-init-file)))
@ -353,6 +347,7 @@ If RETURN-P, return the message as a string instead of displaying it."
(doom-load init-file 'noerror)
(doom-initialize-packages))))
;;; Entry point
;; HACK: This advice hijacks Emacs' initfile loader to accomplish the following:
;;

View file

@ -1,6 +1,6 @@
;;; doom-ui.el --- defaults for Doom's aesthetics -*- lexical-binding: t; -*-
;;; Commentary:
;;; Code;
;;; Code:
;;
;;; Variables
@ -590,10 +590,18 @@ windows, switch to `doom-fallback-buffer'. Otherwise, delegate to original
(put 'doom-theme 'previous-themes (or last-themes 'none))
;; DEPRECATED Hook into `enable-theme-functions' when we target 29
(doom-run-hooks 'doom-load-theme-hook)
(when-let* ((fg (face-foreground 'default nil t))
(bg (face-background 'default nil t)))
(setf (alist-get 'foreground-color default-frame-alist) fg
(alist-get 'background-color default-frame-alist) bg)))))))
;; Fix incorrect fg/bg in new frames created after the initial frame
;; (which are reroneously displayed as black).
(pcase-dolist (`(,param ,fn ,face)
'((foreground-color face-foreground default)
(background-color face-background default)
(cursor-color face-background cursor)
(border-color face-background border)
(mouse-color face-background mouse)))
(when-let* ((color (funcall fn face nil t))
((stringp color))
((not (string-prefix-p "unspecified-" color))))
(setf (alist-get param default-frame-alist) color))))))))
;;
@ -657,11 +665,11 @@ triggering hooks during startup."
(fset 'set-fontset-font #'ignore))
(after! whitespace
(defun doom-is-childframes-p ()
(defun doom--in-parent-frame-p ()
"`whitespace-mode' inundates child frames with whitespace markers, so
disable it to fix all that visual noise."
(null (frame-parameter nil 'parent-frame)))
(add-function :before-while whitespace-enable-predicate #'doom-is-childframes-p))
(add-function :before-while whitespace-enable-predicate #'doom--in-parent-frame-p))
(provide 'doom-ui)
;;; doom-ui.el ends here

View file

@ -54,7 +54,8 @@
;; - hook: `window-setup-hook'
;; - hook: `doom-init-ui-hook'
;; - hook: `doom-after-init-hook'
;; > After startup is complete:
;; > After startup is complete (if file(s) have been opened from the command
;; line, these will trigger much earlier):
;; - On first input: `doom-first-input-hook'
;; - On first switched-to buffer: `doom-first-buffer-hook'
;; - On first opened file: `doom-first-file-hook'
@ -374,13 +375,13 @@ users).")
(let ((old-value (default-toplevel-value 'file-name-handler-alist)))
(set-default-toplevel-value
'file-name-handler-alist
;; HACK: The libraries bundled with Emacs can either be compiled,
;; compressed, or neither. We use calc-loaddefs.el as a heuristic to
;; guess what state all these libraries are in. If they're compressed, we
;; need to leave the gzip file handler in `file-name-handler-alist' so
;; Emacs knows how to load them. If they're compiled or neither, we can
;; omit the gzip handler altogether (at least during startup) for a boost
;; in startup and package load time.
;; HACK: The elisp libraries bundled with Emacs are either compressed or
;; not, never both. So if calc-loaddefs.el.gz exists, calc-loaddefs.el
;; won't, and vice versa. This heuristic is used to guess the state of
;; all other built-in (or site); if they're compressed, we must leave the
;; gzip file handler in `file-name-handler-alist' so Emacs knows how to
;; load them. Otherwise, we can omit it (at least during startup) for a
;; boost in package load time.
(if (eval-when-compile
(locate-file-internal "calc-loaddefs.el" load-path))
nil
@ -469,28 +470,35 @@ users).")
(add-hook! 'doom-before-init-hook
(defun doom--reset-custom-dont-initialize-h ()
(setq custom-dont-initialize nil)))
(define-advice command-line-1 (:around (fn args-left) respect-defcustom-setters)
(let ((custom-dont-initialize nil))
(funcall fn args-left)))
;; PERF: The mode-line procs a couple dozen times during startup, before the
;; user even sees the first mode-line. This is normally fast, but we can't
;; predict what the user (or packages) will put into the mode-line. Also,
;; mode-line packages have a bad habit of throwing performance to the
;; wind, so best we just disable the mode-line until we can see one.
(put 'mode-line-format 'initial-value (default-toplevel-value 'mode-line-format))
(setq-default mode-line-format nil)
(dolist (buf (buffer-list))
(with-current-buffer buf (setq mode-line-format nil)))
;; PERF,UX: Premature redisplays/redraws can substantially affect startup
;; times and/or flash a white/unstyled Emacs frame during startup, so I
;; try real hard to suppress them until we're sure the session is ready.
(setq-default inhibit-redisplay t
inhibit-message t)
;; COMPAT: If the above vars aren't reset, Emacs could appear frozen or
;; garbled after startup (or in case of an startup error).
(defun doom--reset-inhibited-vars-h ()
(setq-default inhibit-redisplay nil
inhibit-message nil)
(remove-hook 'post-command-hook #'doom--reset-inhibited-vars-h))
(add-hook 'post-command-hook #'doom--reset-inhibited-vars-h -100)
;; These optimizations are brittle, difficult to debug, and obscure other
;; issues, so bow out when debug mode is on.
(unless init-file-debug
;; PERF: The mode-line procs a couple dozen times during startup, before
;; the user even sees the first mode-line. This is normally fast, but we
;; can't predict what the user (or packages) will put into the
;; mode-line. Also, mode-line packages have a bad habit of throwing
;; performance to the wind, so best we just disable the mode-line until
;; we can see one.
(put 'mode-line-format 'initial-value (default-toplevel-value 'mode-line-format))
(setq-default mode-line-format nil)
(dolist (buf (buffer-list))
(with-current-buffer buf (setq mode-line-format nil)))
;; PERF,UX: Premature redisplays/redraws can substantially affect startup
;; times and/or flash a white/unstyled Emacs frame during startup, so I
;; try real hard to suppress them until we're sure the session is ready.
(setq-default inhibit-redisplay t
inhibit-message t)
;; COMPAT: If the above vars aren't reset, Emacs could appear frozen or
;; garbled after startup (or in case of an startup error).
(defun doom--reset-inhibited-vars-h ()
(setq-default inhibit-redisplay nil
inhibit-message nil)
(remove-hook 'post-command-hook #'doom--reset-inhibited-vars-h))
(add-hook 'post-command-hook #'doom--reset-inhibited-vars-h -100))
;; PERF: Doom disables the UI elements by default, so that there's less for
;; the frame to initialize. However, `tool-bar-setup' is still called and
@ -515,10 +523,11 @@ users).")
(progn
(when (setq site-run-file (get 'site-run-file 'initial-value))
(let ((inhibit-startup-screen inhibit-startup-screen))
(letf! ((defun load-file (file) (load file nil 'nomessage))
(letf! ((defun load-file (file)
(load file nil (not init-file-debug)))
(defun load (file &optional noerror _nomessage &rest args)
(apply load file noerror t args)))
(load site-run-file t t))))
(apply load file noerror (not init-file-debug) args)))
(load site-run-file t))))
(apply fn args))
;; Now it's safe to be verbose.
(setq-default inhibit-message nil)
@ -762,7 +771,7 @@ appropriately against `noninteractive' or the `cli' context."
;;; Last minute initialization
(when (daemonp)
(message "Starting Doom Emacs in daemon mode!")
(message "Starting Doom Emacs in daemon mode...")
(unless doom-inhibit-log
(add-hook! 'doom-after-init-hook :depth 106
(unless doom-inhibit-log
@ -783,8 +792,8 @@ appropriately against `noninteractive' or the `cli' context."
(when (doom-context-push 'init)
;; HACK: Ensure OS checks are as fast as possible (given their ubiquity).
(setq features (cons :system (delq :system features)))
;; Remember these variables' initial values, so we can safely reset them at
;; a later time, or consult them without fear of contamination.
;; Remember these variables' initial values, so we can safely reset them
;; at a later time, or consult them without fear of contamination.
(dolist (var '(exec-path load-path process-environment))
(put var 'initial-value (default-toplevel-value var))))))