refactor: (if|when)-let -> (if|when)-let*

With the former macros' future in the air (and likely to be targeted in
future, potentially breaking changes), I'll deal with this now than have
it bite me later.

Ref: https://lists.gnu.org/archive/html/emacs-devel/2024-10/msg00637.html
This commit is contained in:
Henrik Lissner
2024-12-07 14:35:54 -05:00
parent e3bc367ba2
commit 0a715cc3f2
43 changed files with 207 additions and 205 deletions

View File

@ -69,10 +69,10 @@
(delq nil (mapcar #'doom-gc--repo repos))))))
(defun doom-gc--eln ()
(if-let (dirs
(cl-delete (expand-file-name comp-native-version-dir doom-packages--eln-output-path)
(directory-files doom-packages--eln-output-path t "^[^.]" t)
:test #'file-equal-p))
(if-let* ((dirs
(cl-delete (expand-file-name comp-native-version-dir doom-packages--eln-output-path)
(directory-files doom-packages--eln-output-path t "^[^.]" t)
:test #'file-equal-p)))
(progn
(print! (start "Purging old native bytecode..."))
(print-group!

View File

@ -272,7 +272,7 @@ execution. Can be generated from a `doom-cli-context' with
(defun doom-cli-key (cli)
"Return CLI's (type . command), used as a table key or unique identifier."
(let ((command (doom-cli-command cli)))
(if-let (type (doom-cli-type cli))
(if-let* ((type (doom-cli-type cli)))
(cons type command)
command)))
@ -648,11 +648,11 @@ Throws `doom-cli-invalid-option-error' for illegal values."
"A CLI context, containing all state pertinent to the current session."
(init-time before-init-time) ; When this context was created
;; A session-specific ID of the current context (defaults to number
(pid (if-let (pid (getenv "__DOOMPID"))
(pid (if-let* ((pid (getenv "__DOOMPID")))
(string-to-number pid)
(emacs-pid)))
;; Number of Emacs processes this context has been processed through
(step (if-let (step (getenv "__DOOMSTEP"))
(step (if-let* ((step (getenv "__DOOMSTEP")))
(string-to-number step)
-1))
;; The geometry of the terminal window.
@ -877,10 +877,10 @@ state tied to switches (\"--foo\" or \"-x\") or arbitrary symbols (state).
If KEY is a string, fetch KEY from context's OPTIONS (by switch).
If KEY is a symbol, fetch KEY from context's STATE.
Return NULL-VALUE if KEY does not exist."
(if-let (value
(if (stringp key)
(assoc key (doom-cli-context-options context))
(assq key (doom-cli-context-state context))))
(if-let* ((value
(if (stringp key)
(assoc key (doom-cli-context-options context))
(assq key (doom-cli-context-state context)))))
(cdr value)
null-value))
@ -1254,7 +1254,7 @@ Emacs' batch library lacks an implementation of the exec system call."
;; Run a custom action, defined in `doom-cli-exit-commands'.
((pred (keywordp))
(if-let (fn (alist-get command doom-cli-exit-commands))
(if-let* ((fn (alist-get command doom-cli-exit-commands)))
(funcall fn args context)
(error "Invalid exit command: %s" command)))
@ -1290,7 +1290,7 @@ Arguments don't have to be switches either."
(when omit
(while argv
(let ((arg (pop argv)))
(if-let (n (cdr (assoc arg omit)))
(if-let* ((n (cdr (assoc arg omit))))
(if (= n -1)
(setq argv nil)
(dotimes (i n) (pop argv)))
@ -1905,7 +1905,7 @@ errors to `doom-cli-error-file')."
(setq doom-print-backend nil))
(when (doom-cli-context-pipe-p context :in)
(with-current-buffer (doom-cli-context-stdin context)
(while (if-let (in (ignore-errors (read-from-minibuffer "")))
(while (if-let* ((in (ignore-errors (read-from-minibuffer ""))))
(insert in "\n")
(ignore-errors (delete-char -1))))))
(doom-cli--exit
@ -1960,9 +1960,9 @@ errors to `doom-cli-error-file')."
(doom-cli-invalid-prefix-error
(let ((prefix (cadr e)))
(print! (red "Error: `run!' called with invalid prefix %S") prefix)
(if-let (suggested (cl-loop for cli being the hash-value of doom-cli--table
unless (doom-cli-type cli)
return (car (doom-cli-command cli))))
(if-let* ((suggested (cl-loop for cli being the hash-value of doom-cli--table
unless (doom-cli-type cli)
return (car (doom-cli-command cli)))))
(print! "Did you mean %S?" suggested)
(print! "There are no commands defined under %S." prefix)))
4)

View File

@ -43,9 +43,9 @@ TYPE should be a keyword of any of the known doom-*-error errors (e.g. :font,
(defvar doom-log-level
(if init-file-debug
(if-let ((level (getenv-internal "DEBUG"))
(level (string-to-number level))
((not (zerop level))))
(if-let* ((level (getenv-internal "DEBUG"))
(level (string-to-number level))
((not (zerop level))))
level
2)
0)
@ -1346,7 +1346,7 @@ NAME is a symbol (e.g. \\='python). FILE is a string that will be appended to
the resulting path. If said path doesn't exist, this returns nil, otherwise an
absolute path."
(let (file-name-handler-alist)
(if-let ((path (doom-module-expand-path key file)))
(if-let* ((path (doom-module-expand-path key file)))
(if (or (null file)
(file-exists-p path))
path)
@ -1463,7 +1463,7 @@ For more about modules and flags, see `doom!'."
',(doom-module-context-flags doom-module-context)
(backquote ,flags))
`(let ((file (file!)))
(if-let ((module (doom-module-from-path file)))
(if-let* ((module (doom-module-from-path file)))
(doom-module--has-flag-p
(doom-module (car module) (cdr module) :flags)
(backquote ,flags))
@ -1670,9 +1670,9 @@ If DEFAULT? is non-nil, an unspecified CAR/CDR will fall bakc to (_default .
"Return PROFILE-NAME's PROFILE, otherwise its PROPERTY, otherwise NULL-VALUE."
(when (stringp profile-name)
(setq profile-name (intern profile-name)))
(if-let (profile (assq profile-name (doom-profiles)))
(if-let* ((profile (assq profile-name (doom-profiles))))
(if property
(if-let (propval (assq property (cdr profile)))
(if-let* ((propval (assq property (cdr profile))))
(cdr propval)
null-value)
profile)

View File

@ -191,9 +191,9 @@
;;; Fix $HOME on Windows
;; $HOME isn't normally defined on Windows, but many unix tools expect it.
(when doom--system-windows-p
(when-let (realhome
(and (null (getenv-internal "HOME"))
(getenv "USERPROFILE")))
(when-let* ((realhome
(and (null (getenv-internal "HOME"))
(getenv "USERPROFILE"))))
(setenv "HOME" realhome)
(setq abbreviated-home-dir nil)))
@ -221,7 +221,7 @@
"The time it took, in seconds (as a float), for Doom Emacs to start up.")
(defconst doom-profile
(if-let (profile (getenv-internal "DOOMPROFILE"))
(if-let* ((profile (getenv-internal "DOOMPROFILE")))
(save-match-data
(if (string-match "^\\([^@]+\\)@\\(.+\\)$" profile)
(cons (match-string 1 profile)
@ -244,7 +244,7 @@
(defvar doom-user-dir
(expand-file-name
(if-let (doomdir (getenv-internal "DOOMDIR"))
(if-let* ((doomdir (getenv-internal "DOOMDIR")))
(file-name-as-directory doomdir)
(or (let ((xdgdir
(file-name-concat
@ -280,7 +280,7 @@ slash.")
;; DEPRECATED: .local will be removed entirely in 3.0
(defvar doom-local-dir
(if-let (localdir (getenv-internal "DOOMLOCALDIR"))
(if-let* ((localdir (getenv-internal "DOOMLOCALDIR")))
(expand-file-name (file-name-as-directory localdir))
(expand-file-name ".local/" doom-emacs-dir))
"Root directory for local storage.

View File

@ -380,7 +380,7 @@ current project."
(defun doom/kill-project-buffers (project &optional interactive)
"Kill buffers for the specified PROJECT."
(interactive
(list (if-let (open-projects (doom-open-projects))
(list (if-let* ((open-projects (doom-open-projects)))
(completing-read
"Kill buffers for project: " open-projects
nil t nil nil

View File

@ -491,7 +491,7 @@ If FORCE-P, overwrite the destination file if it exists, without confirmation."
(let ((host (or (file-remote-p file 'host) "localhost")))
(concat "/" (when (file-remote-p file)
(concat (file-remote-p file 'method) ":"
(if-let (user (file-remote-p file 'user))
(if-let* ((user (file-remote-p file 'user)))
(concat user "@" host)
host)
"|"))
@ -561,7 +561,7 @@ which case it will save it without prompting."
"Save this file as root."
(interactive)
(let ((file (doom--sudo-file-path (buffer-file-name (buffer-base-buffer)))))
(if-let (buffer (find-file-noselect file))
(if-let* ((buffer (find-file-noselect file)))
(let ((origin (current-buffer)))
(copy-to-buffer buffer (point-min) (point-max))
(unwind-protect

View File

@ -157,7 +157,7 @@ selection of all minor-modes, active or not."
(append (apply #'doom--org-headings files plist)
extra-candidates))
ivy-sort-functions-alist)
(if-let (result (completing-read prompt alist nil nil initial-input))
(if-let* ((result (completing-read prompt alist nil nil initial-input)))
(cl-destructuring-bind (file &optional location)
(cdr (assoc result alist))
(if action
@ -557,7 +557,7 @@ If prefix arg is present, refresh the cache."
(`straight
(insert "Straight\n")
(package--print-help-section "Pinned")
(insert (if-let (pin (plist-get (cdr (assq package doom-packages)) :pin))
(insert (if-let* ((pin (plist-get (cdr (assq package doom-packages)) :pin)))
pin
"unpinned")
"\n")
@ -642,7 +642,7 @@ If prefix arg is present, refresh the cache."
(insert ")\n"))))
(package--print-help-section "Configs")
(if-let ((configs (doom--help-package-configs package)))
(if-let* ((configs (doom--help-package-configs package)))
(progn
(insert "This package is configured in the following locations:")
(dolist (location configs)

View File

@ -246,7 +246,7 @@ processed."
(memq (plist-get recipe :host) '(github gitlab bitbucket)))
(plist-put recipe :type 'git)
recipe))
(repo (if-let (local-repo (plist-get recipe :local-repo))
(repo (if-let* ((local-repo (plist-get recipe :local-repo)))
(directory-file-name local-repo)
(ignore-errors (straight-vc-local-repo-name recipe)))))
repo
@ -452,7 +452,7 @@ also be a list of module keys."
("gnu"
(format "https://elpa.gnu.org/packages/%s.html" package))
(archive
(if-let (src (cdr (assoc package package-archives)))
(if-let* ((src (cdr (assoc package package-archives))))
(format "%s" src)
(user-error "%s isn't installed through any known source (%s)"
package archive)))))
@ -475,7 +475,7 @@ also be a list of module keys."
(require 'straight)
(doom-plist-merge
(plist-get plist :recipe)
(if-let (recipe (straight-recipes-retrieve package))
(if-let* ((recipe (straight-recipes-retrieve package)))
(cdr (if (memq (car recipe) '(quote \`))
(eval recipe t)
recipe))
@ -623,7 +623,7 @@ each package."
(ignore-errors (intern (cadr module)))
current-prefix-arg)))
(mapc (lambda! (key)
(if-let (packages-file (doom-module-locate-path key doom-module-packages-file))
(if-let* ((packages-file (doom-module-locate-path key doom-module-packages-file)))
(with-current-buffer
(or (get-file-buffer packages-file)
(find-file-noselect packages-file))
@ -959,75 +959,77 @@ Must be run from a magit diff buffer."
(pinned (doom-package-pinned-alist)))
(add-hook 'native-comp-async-cu-done-functions #'doom-packages--native-compile-done-h)
(straight--make-build-cache-available)
(if-let (built
(doom-packages--with-recipes recipes (package local-repo recipe)
(let ((repo-dir (straight--repos-dir (or local-repo package)))
(build-dir (straight--build-dir package)))
(unless force-p
;; Ensure packages with outdated files/bytecode are rebuilt
(let* ((build (if (plist-member recipe :build)
(plist-get recipe :build)
t))
(want-byte-compile
(or (eq build t)
(memq 'compile build)))
(want-native-compile
(or (eq build t)
(memq 'native-compile build))))
(and (eq (car-safe build) :not)
(setq want-byte-compile (not want-byte-compile)
want-native-compile (not want-native-compile)))
(when (or (not (featurep 'native-compile))
(not straight--native-comp-available))
(setq want-native-compile nil))
(and (or want-byte-compile want-native-compile)
(or (file-newer-than-file-p repo-dir build-dir)
(file-exists-p (straight--modified-dir package))
(cl-loop with outdated = nil
for file in (doom-files-in build-dir :match "\\.el$" :full t)
if (or (if want-byte-compile (doom-packages--elc-file-outdated-p file))
(if want-native-compile (doom-packages--eln-file-outdated-p file)))
do (setq outdated t)
(when want-native-compile
(push file doom-packages--eln-output-expected))
finally return outdated))
(puthash package t straight--packages-to-rebuild))))
(unless (file-directory-p repo-dir)
(doom-packages--cli-recipes-update))
(condition-case-unless-debug e
(let ((straight-vc-git-post-clone-hook
(cons (lambda! (&key commit)
(print-group!
(if-let (pin (cdr (assoc package pinned)))
(print! (item "Pinned to %s") pin)
(when commit
(print! (item "Checked out %s") commit)))))
straight-vc-git-post-clone-hook)))
(straight-use-package (intern package))
(when (file-in-directory-p repo-dir straight-base-dir)
;; HACK: Straight can sometimes fail to clone a repo,
;; leaving behind an empty directory which, in
;; future invocations, it will assume indicates a
;; successful clone (causing load errors later).
(let ((try 0))
(while (not (file-directory-p (doom-path repo-dir ".git")))
(when (= try 3)
(error "Failed to clone package"))
(print! (warn "Failed to clone %S, trying again (attempt #%d)...") package (1+ try))
(delete-directory repo-dir t)
(delete-directory build-dir t)
(straight-use-package (intern package))
(cl-incf try)))
;; HACK: Line encoding issues can plague repos with
;; dirty worktree prompts when updating packages or
;; "Local variables entry is missing the suffix"
;; errors when installing them (see #2637), so have
;; git handle conversion by force.
(when doom--system-windows-p
(let ((default-directory repo-dir))
(straight--process-run "git" "config" "core.autocrlf" "true")))))
(error
(signal 'doom-package-error (list package e)))))))
(if-let* ((built
(doom-packages--with-recipes recipes (package local-repo recipe)
(let ((repo-dir (straight--repos-dir (or local-repo package)))
(build-dir (straight--build-dir package)))
(unless force-p
;; Ensure packages w/ outdated files/bytecode are rebuilt
(let* ((build (if (plist-member recipe :build)
(plist-get recipe :build)
t))
(want-byte-compile
(or (eq build t)
(memq 'compile build)))
(want-native-compile
(or (eq build t)
(memq 'native-compile build))))
(and (eq (car-safe build) :not)
(setq want-byte-compile (not want-byte-compile)
want-native-compile (not want-native-compile)))
(when (or (not (featurep 'native-compile))
(not straight--native-comp-available))
(setq want-native-compile nil))
(and (or want-byte-compile want-native-compile)
(or (file-newer-than-file-p repo-dir build-dir)
(file-exists-p (straight--modified-dir package))
(cl-loop with outdated = nil
for file in (doom-files-in build-dir :match "\\.el$" :full t)
if (or (if want-byte-compile (doom-packages--elc-file-outdated-p file))
(if want-native-compile (doom-packages--eln-file-outdated-p file)))
do (setq outdated t)
(when want-native-compile
(push file doom-packages--eln-output-expected))
finally return outdated))
(puthash package t straight--packages-to-rebuild))))
(unless (file-directory-p repo-dir)
(doom-packages--cli-recipes-update))
(condition-case-unless-debug e
(let ((straight-vc-git-post-clone-hook
(cons (lambda! (&key commit)
(print-group!
(if-let* ((pin (cdr (assoc package pinned))))
(print! (item "Pinned to %s") pin)
(when commit
(print! (item "Checked out %s") commit)))))
straight-vc-git-post-clone-hook)))
(straight-use-package (intern package))
(when (file-in-directory-p repo-dir straight-base-dir)
;; HACK: Straight can sometimes fail to clone a
;; repo, leaving behind an empty directory which,
;; in future invocations, it will assume
;; indicates a successful clone (causing load
;; errors later).
(let ((try 0))
(while (not (file-directory-p (doom-path repo-dir ".git")))
(when (= try 3)
(error "Failed to clone package"))
(print! (warn "Failed to clone %S, trying again (attempt #%d)...") package (1+ try))
(delete-directory repo-dir t)
(delete-directory build-dir t)
(straight-use-package (intern package))
(cl-incf try)))
;; HACK: Line encoding issues can plague repos with
;; dirty worktree prompts when updating packages
;; or "Local variables entry is missing the
;; suffix" errors when installing them (see
;; #2637), so have git handle conversion by
;; force.
(when doom--system-windows-p
(let ((default-directory repo-dir))
(straight--process-run "git" "config" "core.autocrlf" "true")))))
(error
(signal 'doom-package-error (list package e))))))))
(progn
(when (and (featurep 'native-compile)
straight--native-comp-available)
@ -1279,10 +1281,10 @@ Must be run from a magit diff buffer."
e)))))))
(defun doom-packages--purge-eln ()
(if-let (dirs
(cl-delete (expand-file-name comp-native-version-dir doom-packages--eln-output-path)
(directory-files doom-packages--eln-output-path t "^[^.]" t)
:test #'file-equal-p))
(if-let* ((dirs
(cl-delete (expand-file-name comp-native-version-dir doom-packages--eln-output-path)
(directory-files doom-packages--eln-output-path t "^[^.]" t)
:test #'file-equal-p)))
(progn
(print! (start "Purging old native bytecode..."))
(print-group!

View File

@ -10,7 +10,7 @@ Profile directories are in the format {data-profiles-dir}/$NAME/@/$VERSION, for
example: '~/.local/share/doom/_/@/0/'")
(defvar doom-profile-load-path
(if-let (path (getenv-internal "DOOMPROFILELOADPATH"))
(if-let* ((path (getenv-internal "DOOMPROFILELOADPATH")))
(mapcar #'expand-file-name (split-string-and-unquote path path-separator))
(list (file-name-concat doom-user-dir "profiles.el")
(file-name-concat doom-emacs-dir "profiles.el")
@ -114,7 +114,7 @@ run.")
`(,val)
`(,(abbreviate-file-name path) ,val))))
(cons `(user-emacs-directory :path ,@val)
(if-let (profile-file (file-exists-p! doom-profile-rcfile path))
(if-let* ((profile-file (file-exists-p! doom-profile-rcfile path)))
(car (doom-file-read profile-file :by 'read*))
(when (file-exists-p (doom-path path subdir "lisp/doom.el"))
'((doom-user-dir :path ,@val)))))))

View File

@ -119,8 +119,8 @@ Returns nil if not in a project."
"Return the name of the current project.
Returns '-' if not in a valid project."
(if-let (project-root (or (doom-project-root dir)
(if dir (expand-file-name dir))))
(if-let* ((project-root (or (doom-project-root dir)
(if dir (expand-file-name dir)))))
(funcall projectile-project-name-function project-root)
"-"))