2017-06-08 11:47:56 +02:00
|
|
|
;;; core-lib.el -*- lexical-binding: t; -*-
|
2017-01-16 23:15:48 -05:00
|
|
|
|
2021-10-08 23:31:03 +02:00
|
|
|
(require 'cl-lib)
|
|
|
|
|
|
|
|
|
2017-03-02 18:14:52 -05:00
|
|
|
;;
|
2019-03-08 18:06:35 -05:00
|
|
|
;;; Helpers
|
2017-03-02 18:14:52 -05:00
|
|
|
|
2017-09-27 01:21:48 +02:00
|
|
|
(defun doom--resolve-hook-forms (hooks)
|
2019-05-01 19:12:52 -04:00
|
|
|
"Converts a list of modes into a list of hook symbols.
|
|
|
|
|
2019-05-13 14:37:00 -04:00
|
|
|
If a mode is quoted, it is left as is. If the entire HOOKS list is quoted, the
|
|
|
|
list is returned as-is."
|
2018-06-24 19:54:50 +02:00
|
|
|
(declare (pure t) (side-effect-free t))
|
2019-05-13 14:37:00 -04:00
|
|
|
(let ((hook-list (doom-enlist (doom-unquote hooks))))
|
|
|
|
(if (eq (car-safe hooks) 'quote)
|
|
|
|
hook-list
|
|
|
|
(cl-loop for hook in hook-list
|
|
|
|
if (eq (car-safe hook) 'quote)
|
|
|
|
collect (cadr hook)
|
|
|
|
else collect (intern (format "%s-hook" (symbol-name hook)))))))
|
2017-03-02 18:14:52 -05:00
|
|
|
|
2019-07-21 14:35:45 +02:00
|
|
|
(defun doom--setq-hook-fns (hooks rest &optional singles)
|
|
|
|
(unless (or singles (= 0 (% (length rest) 2)))
|
|
|
|
(signal 'wrong-number-of-arguments (list #'evenp (length rest))))
|
|
|
|
(cl-loop with vars = (let ((args rest)
|
|
|
|
vars)
|
|
|
|
(while args
|
|
|
|
(push (if singles
|
|
|
|
(list (pop args))
|
|
|
|
(cons (pop args) (pop args)))
|
|
|
|
vars))
|
|
|
|
(nreverse vars))
|
|
|
|
for hook in (doom--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 "doom--setq-%s-for-%s-h"
|
|
|
|
var mode))))))
|
|
|
|
|
2018-05-24 18:38:50 +02:00
|
|
|
|
|
|
|
;;
|
2019-03-08 18:06:35 -05:00
|
|
|
;;; Public library
|
2018-05-24 18:38:50 +02:00
|
|
|
|
2022-06-18 15:07:43 +02:00
|
|
|
(define-obsolete-function-alias 'doom-enlist 'ensure-list "v3.0.0")
|
|
|
|
|
2017-06-12 02:48:26 +02:00
|
|
|
(defun doom-unquote (exp)
|
|
|
|
"Return EXP unquoted."
|
2018-06-24 19:54:50 +02:00
|
|
|
(declare (pure t) (side-effect-free t))
|
2017-06-12 02:48:26 +02:00
|
|
|
(while (memq (car-safe exp) '(quote function))
|
|
|
|
(setq exp (cadr exp)))
|
|
|
|
exp)
|
|
|
|
|
|
|
|
(defun doom-enlist (exp)
|
|
|
|
"Return EXP wrapped in a list, or as-is if already a list."
|
2018-06-24 19:54:50 +02:00
|
|
|
(declare (pure t) (side-effect-free t))
|
2021-10-20 18:19:31 +02:00
|
|
|
(if (proper-list-p exp) exp (list exp)))
|
2017-06-12 02:48:26 +02:00
|
|
|
|
2018-05-19 16:32:12 +02:00
|
|
|
(defun doom-keyword-intern (str)
|
2018-05-23 19:09:09 +02:00
|
|
|
"Converts STR (a string) into a keyword (`keywordp')."
|
2018-06-24 19:54:50 +02:00
|
|
|
(declare (pure t) (side-effect-free t))
|
|
|
|
(cl-check-type str string)
|
2018-05-19 16:32:12 +02:00
|
|
|
(intern (concat ":" str)))
|
|
|
|
|
|
|
|
(defun doom-keyword-name (keyword)
|
2018-05-23 19:09:09 +02:00
|
|
|
"Returns the string name of KEYWORD (`keywordp') minus the leading colon."
|
2018-06-24 19:54:50 +02:00
|
|
|
(declare (pure t) (side-effect-free t))
|
2019-07-24 15:26:43 +02:00
|
|
|
(cl-check-type keyword keyword)
|
2018-05-19 16:32:12 +02:00
|
|
|
(substring (symbol-name keyword) 1))
|
|
|
|
|
2019-07-21 19:13:21 +02:00
|
|
|
(defalias 'doom-partial #'apply-partially)
|
|
|
|
|
|
|
|
(defun doom-rpartial (fn &rest args)
|
2020-12-11 17:38:59 -05:00
|
|
|
"Return a partial application of FUN to right-hand ARGS.
|
2019-07-21 19:13:21 +02:00
|
|
|
|
|
|
|
ARGS is a list of the last N arguments to pass to FUN. The result is a new
|
|
|
|
function which does the same as FUN, except that the last N arguments are fixed
|
|
|
|
at the values with which this function was called."
|
2020-04-08 15:29:29 -04:00
|
|
|
(declare (side-effect-free t))
|
2019-07-21 19:13:21 +02:00
|
|
|
(lambda (&rest pre-args)
|
|
|
|
(apply fn (append pre-args args))))
|
|
|
|
|
2021-02-06 06:54:18 -05:00
|
|
|
(defun doom-lookup-key (keys &rest keymaps)
|
2021-01-03 22:40:06 -05:00
|
|
|
"Like `lookup-key', but search active keymaps if KEYMAP is omitted."
|
2021-02-06 06:54:18 -05:00
|
|
|
(if keymaps
|
|
|
|
(cl-some (doom-rpartial #'lookup-key keys) keymaps)
|
2021-01-03 22:40:06 -05:00
|
|
|
(cl-loop for keymap
|
2021-01-05 01:50:48 -05:00
|
|
|
in (append (cl-loop for alist in emulation-mode-map-alists
|
2021-01-18 17:43:57 -05:00
|
|
|
append (mapcar #'cdr
|
|
|
|
(if (symbolp alist)
|
|
|
|
(if (boundp alist) (symbol-value alist))
|
|
|
|
alist)))
|
2021-01-03 22:40:06 -05:00
|
|
|
(list (current-local-map))
|
2021-01-05 01:50:48 -05:00
|
|
|
(mapcar #'cdr minor-mode-overriding-map-alist)
|
2021-01-18 17:43:57 -05:00
|
|
|
(mapcar #'cdr minor-mode-map-alist)
|
2021-01-03 22:40:06 -05:00
|
|
|
(list (current-global-map)))
|
2021-01-05 01:50:48 -05:00
|
|
|
if (keymapp keymap)
|
2021-01-03 22:40:06 -05:00
|
|
|
if (lookup-key keymap keys)
|
|
|
|
return it)))
|
|
|
|
|
2021-10-08 23:31:03 +02:00
|
|
|
(defun doom-load-envvars-file (file &optional noerror)
|
|
|
|
"Read and set envvars from FILE.
|
|
|
|
If NOERROR is non-nil, don't throw an error if the file doesn't exist or is
|
|
|
|
unreadable. Returns the names of envvars that were changed."
|
|
|
|
(if (null (file-exists-p file))
|
|
|
|
(unless noerror
|
|
|
|
(signal 'file-error (list "No envvar file exists" file)))
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert-file-contents file)
|
|
|
|
(when-let (env (read (current-buffer)))
|
2021-11-24 21:16:21 +01:00
|
|
|
(let ((tz (getenv-internal "TZ")))
|
|
|
|
(setq-default
|
|
|
|
process-environment
|
|
|
|
(append env (default-value 'process-environment))
|
|
|
|
exec-path
|
|
|
|
(append (split-string (getenv "PATH") path-separator t)
|
|
|
|
(list exec-directory))
|
|
|
|
shell-file-name
|
|
|
|
(or (getenv "SHELL")
|
|
|
|
(default-value 'shell-file-name)))
|
|
|
|
(when-let (newtz (getenv-internal "TZ"))
|
|
|
|
(unless (equal tz newtz)
|
|
|
|
(set-time-zone-rule newtz))))
|
2021-10-08 23:31:03 +02:00
|
|
|
env))))
|
|
|
|
|
|
|
|
(defun doom-run-hook (hook)
|
|
|
|
"Run HOOK (a hook function) with better error handling.
|
|
|
|
Meant to be used with `run-hook-wrapped'."
|
|
|
|
(condition-case-unless-debug e
|
|
|
|
(funcall hook)
|
|
|
|
(error
|
|
|
|
(signal 'doom-hook-error (list hook e))))
|
|
|
|
;; return nil so `run-hook-wrapped' won't short circuit
|
|
|
|
nil)
|
|
|
|
|
|
|
|
(defun doom-run-hooks (&rest hooks)
|
|
|
|
"Run HOOKS (a list of hook variable symbols) with better error handling.
|
|
|
|
Is used as advice to replace `run-hooks'."
|
|
|
|
(dolist (hook hooks)
|
|
|
|
(condition-case-unless-debug e
|
|
|
|
(run-hook-wrapped hook #'doom-run-hook)
|
|
|
|
(doom-hook-error
|
|
|
|
(unless debug-on-error
|
|
|
|
(lwarn hook :error "Error running hook %S because: %s"
|
|
|
|
(if (symbolp (cadr e))
|
|
|
|
(symbol-name (cadr e))
|
|
|
|
(cadr e))
|
|
|
|
(caddr e)))
|
|
|
|
(signal 'doom-hook-error (cons hook (cdr e)))))))
|
|
|
|
|
|
|
|
(defun doom-run-hook-on (hook-var trigger-hooks)
|
|
|
|
"Configure HOOK-VAR to be invoked exactly once when any of the TRIGGER-HOOKS
|
|
|
|
are invoked *after* Emacs has initialized (to reduce false positives). Once
|
|
|
|
HOOK-VAR is triggered, it is reset to nil.
|
|
|
|
|
|
|
|
HOOK-VAR is a quoted hook.
|
|
|
|
TRIGGER-HOOK is a list of quoted hooks and/or sharp-quoted functions."
|
|
|
|
(dolist (hook trigger-hooks)
|
|
|
|
(let ((fn (intern (format "%s-init-on-%s-h" hook-var hook))))
|
|
|
|
(fset
|
|
|
|
fn (lambda (&rest _)
|
|
|
|
;; Only trigger this after Emacs has initialized.
|
|
|
|
(when (and after-init-time
|
|
|
|
(or (daemonp)
|
|
|
|
;; In some cases, hooks may be lexically unset to
|
|
|
|
;; inhibit them during expensive batch operations on
|
|
|
|
;; buffers (such as when processing buffers
|
|
|
|
;; internally). In these cases we should assume this
|
|
|
|
;; hook wasn't invoked interactively.
|
|
|
|
(and (boundp hook)
|
|
|
|
(symbol-value hook))))
|
|
|
|
(doom-run-hooks hook-var)
|
|
|
|
(set hook-var nil))))
|
|
|
|
(cond ((daemonp)
|
|
|
|
;; In a daemon session we don't need all these lazy loading
|
|
|
|
;; shenanigans. Just load everything immediately.
|
|
|
|
(add-hook 'after-init-hook fn 'append))
|
|
|
|
((eq hook 'find-file-hook)
|
|
|
|
;; Advise `after-find-file' instead of using `find-file-hook'
|
|
|
|
;; because the latter is triggered too late (after the file has
|
|
|
|
;; opened and modes are all set up).
|
|
|
|
(advice-add 'after-find-file :before fn '((depth . -101))))
|
|
|
|
((add-hook hook fn -101)))
|
|
|
|
fn)))
|
|
|
|
|
2018-09-07 19:38:16 -04:00
|
|
|
|
|
|
|
;;
|
2019-07-21 14:40:38 +02:00
|
|
|
;;; Sugars
|
2018-09-07 19:38:16 -04:00
|
|
|
|
2020-04-29 20:45:29 -04:00
|
|
|
(defun dir! ()
|
2022-01-09 17:16:57 +01:00
|
|
|
"Returns the directory of the emacs lisp file this function is called from."
|
2020-04-29 20:45:29 -04:00
|
|
|
(when-let (path (file!))
|
|
|
|
(directory-file-name (file-name-directory path))))
|
|
|
|
|
2019-07-21 14:44:04 +02:00
|
|
|
(defun file! ()
|
2022-01-09 17:16:57 +01:00
|
|
|
"Return the emacs lisp file this function is called from."
|
refactor!(cli): rewrite CLI framework libraries
BREAKING CHANGE: this changes Doom's CLI framework in subtle ways, which
is listed in greater detail below. If you've never extended Doom's CLI,
then this won't affect you, but otherwise it'd be recommended you read
on below.
This commit focuses on the CLI framework itself and backports some
foundational changes to its DSL and how it resolves command line
arguments to CLIs, validates input, displays documentation, and persists
state across sessions -- and more. This is done in preparation for the
final stretch towarding completing the CLI rewrite (see #4273).
This is also an effort to generalize Doom's CLI (both its framework and
bin/doom), to increase it versatility and make it a viable dev tool for
other Doom projects (on our Github org) and beyond.
However, there is a *lot* to cover so I'll try to be brief:
- Refactor: generalize Doom's CLI framework by moving all bin/doom
specific configuration/commands out of core-cli into bin/doom. This
makes it easier to use bin/doom as a project-agnostic development
tool (or for users to write their own).
- Refactor: change the namespace for CLI variables/functions from
doom-cli-X to doom-X.
- Fix: subcommands being mistaken as arguments. "doom make index" will
resolve to (defcli! (doom make index)) if it exists,
otherwise (defcli! (doom make)) with "index" as an argument. Before
this, it would resolve to the latter no matter what. &rest can
override this; with (defcli! (doom make) (&rest args)), (defcli! (doom
make index)) will never be invoked.
- Refactor!: redesign our output library (was core/autoload/output.el,
is now core/autoload/print.el), and how our CLI framework buffers and
logs output, and now merges logs across (exit! ...) restarts.
- Feat: add support for :before and :after pseudo commands. E.g.
(defcli! (:before doom help) () ...)
(defcli! (:after doom sync) () ...)
Caveat: unlike advice, only one of each can be defined per-command.
- Feat: option arguments now have rudimentary type validation (see
`doom-cli-option-arg-types`). E.g.
(defcli! (doom foo) ((foo ("--foo" num))) ...)
If NUM is not a numeric, it will throw a validation error.
Any type that isn't in `doom-cli-option-arg-types` will be treated as a
wildcard string type. `num` can also be replaced with a specification,
e.g. "HOST[:PORT]", and can be formatted by using symbol quotes:
"`HOST'[:`PORT']".
- Feat: it is no longer required that options *immediately* follow the command
that defines them (but it must be somewhere after it, not before). E.g.
With:
(defcli! (:before doom foo) ((foo ("--foo"))) ...)
(defcli! (doom foo baz) () ...)
Before:
FAIL: doom --foo foo baz
GOOD: doom foo --foo baz
FAIL: doom foo baz --foo
After:
FAIL: doom --foo foo baz
GOOD: doom foo --foo baz
GOOD: doom foo baz --foo
- Refactor: CLI session state is now kept in a doom-cli-context struct (which
can be bound to a CLI-local variable with &context in the arglist):
(defcli! (doom sync) (&context context)
(print! "Command: " (doom-cli-context-command context)))
These contexts are persisted across sessions (when restarted). This is
necessary to support seamless script restarting (i.e. execve
emulation) in post-3.0.
- Feat: Doom's CLI framework now understands "--". Everything after it will be
treated as regular arguments, instead of sub-commands or options.
- Refactor!: the semantics of &rest for CLIs has changed. It used to be "all
extra literal, non-option arguments". It now means *all* unprocessed
arguments, and its use will suppress "unrecognized option" errors, and
tells the framework not to process any further subcommands. Use &args
if you just want "all literal arguments following this command".
- Feat: add new auxiliary keywords for CLI arglists: &context, &multiple,
&flags, &args, &stdin, &whole, and &cli.
- &context SYM: binds the currently running context to SYM (a
`doom-cli-context` struct). Helpful for introspection or passing
along state when calling subcommands by hand (with `call!`).
- &stdin SYM: SYM will be bound to a string containing any input piped
into the running script, or nil if none. Use
`doom-cli-context-pipe-p` to detect whether the script has been
piped into or out of.
- &multiple OPTIONS...: allows all following OPTIONS to be repeated. E.g. "foo
-x a -x b -x c" will pass (list ("-x" . "a") ("-x" . "b") ("-x" .
"c")) as -x's value.
- &flags OPTIONS...: All options after "&flags" get an implicit --no-* switch
and cannot accept arguments. Will be set to :yes or :no depending on which flag is
provided, and nil if the flag isn't provided. Otherwise, a default
value can be specified in that options' arglist. E.g.
(defcli! (doom foo) (&flags (foo ("--foo" :no))) ...)
When called, this command sets FOO to :yes if --foo, :no if --no-foo, and
defaults to :no otherwise.
- &args SYM: this replaces what &rest used to be; it binds to SYM a
list of all unprocessed (non-option) arguments.
- &rest SYM: now binds SYM to a list of all unprocessed arguments, including
options. This also suppresses "unrecognized option" errors, but will render
any sub-commands inaccessible. E.g.
(defcli! (doom make) (&rest rest) ...)
;; These are now inaccessible!
(defcli! (doom make foo) (&rest rest) ...)
(defcli! (doom make bar) (&rest rest) ...)
- &cli SYM: binds SYM to the currently running `doom-cli` struct. Can also be
obtained via `(doom-cli-get (doom-cli-context-command context))`. Possibly
useful for introspection.
- feat: add defobsolete! macro for quickly defining obsolete commands.
- feat: add defalias! macro for quickly defining alias commands.
- feat: add defautoload! macro for defining an autoloaded command (won't
be loaded until it is called for).
- refactor!: rename defcligroup! to defgroup! for consistency.
- fix: CLIs will now recursively inherit plist properties from parent
defcli-group!'s (but will stack :prefix).
- refactor!: remove obsolete 'doom update':
- refactor!: further generalize 'doom ci'
- In an effort to generalize 'doom ci' (so other Doom--or
non-doom--projects can use it), all its subcommands have been
changed to operate on the current working directory's repo instead
of $EMACSDIR.
- Doom-specific CI configuration was moved to .github/ci.el.
- All 'doom ci' commands will now preload one of \$CURRENT_REPO_ROOT/ci.el or
\$DOOMDIR/ci.el before executing.
- refactor!: changed 'doom env'
- 'doom env {-c,--clear}' is now 'doom env {clear,c}'
- -r/--reject and -a/--allow may now be specified multiple times
- refactor!: rewrote CLI help framework and error handling to be more
sophisticated and detailed.
- feat: can now initiate $PAGER on output with (exit! :pager) (or use
:pager? to only invoke pager is output is longer than the terminal is
tall).
- refactor!: changed semantics+conventions for global bin/doom options
- Single-character global options are now uppercased, to distinguish them from
local options:
- -d (for debug mode) is now -D
- -y (to suppress prompts) is now -!
- -l (to load elisp) is now -L
- -h (short for --help) is now -?
- Replace --yes/-y switches with --force/-!
- -L/--load FILE: now silently ignores file errors.
- Add --strict-load FILE: does the same as -L/--load, but throws an error if
FILE does not exist/is unreadable.
- Add -E/--eval FORM: evaluates arbitrary lisp before commands are processed.
- -L/--load, --strict-load, and -E/--eval can now be used multiple times in
one command.
- Add --pager COMMAND to specify an explicit pager. Will also obey
$DOOMPAGER envvar. Does not obey $PAGER.
- Fix #3746: which was likely caused by the generated post-script overwriting
the old mid-execution. By salting the postscript filenames (with both an
overarching session ID and a step counter).
- Docs: document websites, environment variables, and exit codes in
'doom --help'
- Feat: add imenu support for def{cli,alias,obsolete}!
Ref: #4273
Fix: #3746
Fix: #3844
2022-06-18 19:16:06 +02:00
|
|
|
(cond (load-in-progress load-file-name)
|
|
|
|
((bound-and-true-p byte-compile-current-file))
|
2019-07-21 14:44:04 +02:00
|
|
|
((stringp (car-safe current-load-list))
|
|
|
|
(car current-load-list))
|
2019-07-27 17:00:12 +02:00
|
|
|
(buffer-file-name)
|
|
|
|
((error "Cannot get this file-path"))))
|
2019-07-21 14:44:04 +02:00
|
|
|
|
2020-04-29 20:45:29 -04:00
|
|
|
(defmacro letenv! (envvars &rest body)
|
|
|
|
"Lexically bind ENVVARS in BODY, like `let' but for `process-environment'."
|
|
|
|
(declare (indent 1))
|
|
|
|
`(let ((process-environment (copy-sequence process-environment)))
|
2022-03-19 21:12:41 +01:00
|
|
|
,@(cl-loop for (var val) in envvars
|
|
|
|
collect `(setenv ,var ,val))
|
2020-04-29 20:45:29 -04:00
|
|
|
,@body))
|
|
|
|
|
2020-04-29 21:08:17 -04:00
|
|
|
(defmacro letf! (bindings &rest body)
|
2021-10-01 19:07:37 +02:00
|
|
|
"Temporarily rebind function, macros, and advice in BODY.
|
2020-04-29 21:08:17 -04:00
|
|
|
|
2021-10-01 19:07:37 +02:00
|
|
|
Intended as syntax sugar for `cl-letf', `cl-labels', `cl-macrolet', and
|
|
|
|
temporary advice.
|
2020-04-29 21:08:17 -04:00
|
|
|
|
2021-10-01 19:07:37 +02:00
|
|
|
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.
|
|
|
|
|
|
|
|
TYPE is one of:
|
|
|
|
|
|
|
|
`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.
|
2020-04-29 21:08:17 -04:00
|
|
|
|
|
|
|
\(fn ((TYPE NAME ARGLIST &rest BODY) ...) BODY...)"
|
|
|
|
(declare (indent defun))
|
|
|
|
(setq body (macroexp-progn body))
|
2021-10-01 19:07:37 +02:00
|
|
|
(when (memq (car bindings) '(defun defun* defmacro defadvice))
|
2020-04-29 21:08:17 -04:00
|
|
|
(setq bindings (list bindings)))
|
2021-10-01 19:07:37 +02:00
|
|
|
(dolist (binding (reverse bindings) body)
|
2020-04-29 21:08:17 -04:00
|
|
|
(let ((type (car binding))
|
|
|
|
(rest (cdr binding)))
|
|
|
|
(setq
|
|
|
|
body (pcase type
|
2020-06-04 20:02:46 -04:00
|
|
|
(`defmacro `(cl-macrolet ((,@rest)) ,body))
|
2021-10-01 19:07:37 +02:00
|
|
|
(`defadvice `(progn (defadvice! ,@rest)
|
|
|
|
(unwind-protect ,body (undefadvice! ,@rest))))
|
|
|
|
((or `defun `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))
|
2022-06-21 20:56:02 +02:00
|
|
|
(lambda! ,(cadr rest) ,@(cddr rest))))
|
2021-10-01 19:07:37 +02:00
|
|
|
,body))))
|
2020-04-29 21:08:17 -04:00
|
|
|
(_
|
|
|
|
(when (eq (car-safe type) 'function)
|
2020-05-14 22:32:03 -04:00
|
|
|
(setq type (list 'symbol-function type)))
|
|
|
|
(list 'cl-letf (list (cons type rest)) body)))))))
|
2020-04-29 21:08:17 -04:00
|
|
|
|
2020-04-29 20:45:29 -04:00
|
|
|
(defmacro quiet! (&rest forms)
|
|
|
|
"Run FORMS without generating any output.
|
|
|
|
|
2020-05-14 22:37:39 -04:00
|
|
|
This silences calls to `message', `load', `write-region' and anything that
|
2022-06-21 14:40:15 +02:00
|
|
|
writes to `standard-output'. In interactive sessions this inhibits output to the
|
|
|
|
echo-area, but not to *Messages*."
|
2020-05-25 02:58:07 -04:00
|
|
|
`(if doom-debug-p
|
2020-05-14 22:36:43 -04:00
|
|
|
(progn ,@forms)
|
2020-05-25 02:58:07 -04:00
|
|
|
,(if doom-interactive-p
|
2020-05-14 22:36:43 -04:00
|
|
|
`(let ((inhibit-message t)
|
|
|
|
(save-silently t))
|
|
|
|
(prog1 ,@forms (message "")))
|
|
|
|
`(letf! ((standard-output (lambda (&rest _)))
|
|
|
|
(defun message (&rest _))
|
2020-05-14 22:37:39 -04:00
|
|
|
(defun load (file &optional noerror nomessage nosuffix must-suffix)
|
|
|
|
(funcall load file noerror t nosuffix must-suffix))
|
2020-05-14 22:36:43 -04:00
|
|
|
(defun write-region (start end filename &optional append visit lockname mustbenew)
|
|
|
|
(unless visit (setq visit 'no-message))
|
|
|
|
(funcall write-region start end filename append visit lockname mustbenew)))
|
|
|
|
,@forms))))
|
2020-04-29 20:45:29 -04:00
|
|
|
|
2020-08-26 21:40:53 -04:00
|
|
|
(defmacro eval-if! (cond then &rest body)
|
2020-04-29 21:09:10 -04:00
|
|
|
"Expands to THEN if COND is non-nil, to BODY otherwise.
|
2020-08-26 21:40:53 -04:00
|
|
|
COND is checked at compile/expansion time, allowing BODY to be omitted entirely
|
|
|
|
when the elisp is byte-compiled. Use this for forms that contain expensive
|
|
|
|
macros that could safely be removed at compile time."
|
2020-04-29 21:09:10 -04:00
|
|
|
(declare (indent 2))
|
|
|
|
(if (eval cond)
|
|
|
|
then
|
|
|
|
(macroexp-progn body)))
|
|
|
|
|
2020-08-26 21:40:53 -04:00
|
|
|
(defmacro eval-when! (cond &rest body)
|
2020-04-29 21:09:10 -04:00
|
|
|
"Expands to BODY if CONDITION is non-nil at compile/expansion time.
|
2020-08-26 21:40:53 -04:00
|
|
|
See `eval-if!' for details on this macro's purpose."
|
2020-04-29 21:09:10 -04:00
|
|
|
(declare (indent 1))
|
|
|
|
(when (eval cond)
|
|
|
|
(macroexp-progn body)))
|
|
|
|
|
2020-04-29 20:45:29 -04:00
|
|
|
|
2020-05-20 15:32:34 -04:00
|
|
|
;;; Closure factories
|
2022-06-21 20:56:02 +02:00
|
|
|
(defmacro lambda! (arglist &rest body)
|
2020-12-11 17:38:59 -05:00
|
|
|
"Returns (cl-function (lambda ARGLIST BODY...))
|
|
|
|
The closure is wrapped in `cl-function', meaning ARGLIST will accept anything
|
2021-09-16 02:42:32 +02:00
|
|
|
`cl-defun' will. Implicitly adds `&allow-other-keys' if `&key' is present in
|
|
|
|
ARGLIST."
|
2020-05-20 15:32:34 -04:00
|
|
|
(declare (indent defun) (doc-string 1) (pure t) (side-effect-free t))
|
2021-10-18 00:44:51 +02:00
|
|
|
`(cl-function
|
|
|
|
(lambda
|
|
|
|
,(letf! (defun* allow-other-keys (args)
|
|
|
|
(mapcar
|
|
|
|
(lambda (arg)
|
2021-10-19 22:29:08 +02:00
|
|
|
(cond ((nlistp (cdr-safe arg)) arg)
|
|
|
|
((listp arg) (allow-other-keys arg))
|
|
|
|
(arg)))
|
2021-10-18 00:44:51 +02:00
|
|
|
(if (and (memq '&key args)
|
|
|
|
(not (memq '&allow-other-keys args)))
|
|
|
|
(if (memq '&aux args)
|
|
|
|
(let (newargs arg)
|
|
|
|
(while args
|
|
|
|
(setq arg (pop args))
|
|
|
|
(when (eq arg '&aux)
|
|
|
|
(push '&allow-other-keys newargs))
|
|
|
|
(push arg newargs))
|
|
|
|
(nreverse newargs))
|
|
|
|
(append args (list '&allow-other-keys)))
|
|
|
|
args)))
|
|
|
|
(allow-other-keys arglist))
|
|
|
|
,@body)))
|
2020-05-20 15:32:34 -04:00
|
|
|
|
2022-06-21 23:19:09 +02:00
|
|
|
(put 'doom--fn-crawl 'lookup-table
|
|
|
|
'((_ . 0) (_ . 1) (%2 . 2) (%3 . 3) (%4 . 4)
|
|
|
|
(%5 . 5) (%6 . 6) (%7 . 7) (%8 . 8) (%9 . 9)))
|
2022-03-19 19:43:03 +01:00
|
|
|
(defun doom--fn-crawl (data args)
|
|
|
|
(cond ((symbolp data)
|
2022-06-21 23:19:09 +02:00
|
|
|
(when-let
|
|
|
|
(pos (cond ((eq data '%*) 0)
|
|
|
|
((memq data '(% %1)) 1)
|
|
|
|
((cdr (assq data (get 'doom--fn-crawl 'lookup-table))))))
|
2022-03-19 19:43:03 +01:00
|
|
|
(when (and (= pos 1)
|
|
|
|
(aref args 1)
|
|
|
|
(not (eq data (aref args 1))))
|
|
|
|
(error "%% and %%1 are mutually exclusive"))
|
|
|
|
(aset args pos data)))
|
|
|
|
((and (not (eq (car-safe data) '!))
|
|
|
|
(or (listp data)
|
|
|
|
(vectorp data)))
|
2022-06-21 23:19:09 +02:00
|
|
|
(let ((len (length data))
|
|
|
|
(i 0))
|
|
|
|
(while (< i len)
|
|
|
|
(doom--fn-crawl (elt data i) args)
|
|
|
|
(cl-incf i))))))
|
2022-03-19 19:43:03 +01:00
|
|
|
|
2022-06-21 20:56:02 +02:00
|
|
|
(defmacro fn! (&rest args)
|
2022-03-19 19:43:03 +01:00
|
|
|
"Return an lambda with implicit, positional arguments.
|
|
|
|
|
|
|
|
The function's arguments are determined recursively from ARGS. Each symbol from
|
|
|
|
`%1' through `%9' that appears in ARGS is treated as a positional argument.
|
|
|
|
Missing arguments are named `_%N', which keeps the byte-compiler quiet. `%' is
|
|
|
|
a shorthand for `%1'; only one of these can appear in ARGS. `%*' represents
|
|
|
|
extra `&rest' arguments.
|
|
|
|
|
|
|
|
Instead of:
|
|
|
|
|
|
|
|
(lambda (a _ c &rest d)
|
|
|
|
(if a c (cadr d)))
|
|
|
|
|
|
|
|
you can use this macro and write:
|
|
|
|
|
2022-06-21 20:56:02 +02:00
|
|
|
(fn! (if %1 %3 (cadr %*)))
|
2022-03-19 19:43:03 +01:00
|
|
|
|
|
|
|
which expands to:
|
|
|
|
|
|
|
|
(lambda (%1 _%2 %3 &rest %*)
|
|
|
|
(if %1 %3 (cadr %*)))
|
|
|
|
|
|
|
|
This macro was adapted from llama.el (see https://git.sr.ht/~tarsius/llama),
|
|
|
|
minus font-locking, the outer function call, and minor optimizations."
|
|
|
|
`(lambda ,(let ((argv (make-vector 10 nil)))
|
|
|
|
(doom--fn-crawl args argv)
|
2022-06-21 22:42:31 +02:00
|
|
|
`(,@(let ((i (1- (length argv)))
|
|
|
|
(n -1)
|
|
|
|
sym arglist)
|
|
|
|
(while (> i 0)
|
|
|
|
(setq sym (aref argv i))
|
2022-06-21 23:01:13 +02:00
|
|
|
(unless (and (= n -1) (null sym))
|
2022-06-21 22:42:31 +02:00
|
|
|
(cl-incf n)
|
|
|
|
(push (or sym (intern (format "_%%%d" (1+ n))))
|
|
|
|
arglist))
|
|
|
|
(cl-decf i))
|
|
|
|
arglist)
|
2022-03-19 19:43:03 +01:00
|
|
|
,@(and (aref argv 0) '(&rest %*))))
|
|
|
|
,@args))
|
|
|
|
|
2020-05-27 16:12:45 -04:00
|
|
|
(defmacro cmd! (&rest body)
|
2020-12-11 17:38:59 -05:00
|
|
|
"Returns (lambda () (interactive) ,@body)
|
2020-05-27 16:12:45 -04:00
|
|
|
A factory for quickly producing interaction commands, particularly for keybinds
|
|
|
|
or aliases."
|
|
|
|
(declare (doc-string 1) (pure t) (side-effect-free t))
|
|
|
|
`(lambda (&rest _) (interactive) ,@body))
|
|
|
|
|
2020-07-22 17:09:38 -04:00
|
|
|
(defmacro cmd!! (command &optional prefix-arg &rest args)
|
2020-12-11 17:38:59 -05:00
|
|
|
"Returns a closure that interactively calls COMMAND with ARGS and PREFIX-ARG.
|
|
|
|
Like `cmd!', but allows you to change `current-prefix-arg' or pass arguments to
|
|
|
|
COMMAND. This macro is meant to be used as a target for keybinds (e.g. with
|
|
|
|
`define-key' or `map!')."
|
2020-05-27 16:12:45 -04:00
|
|
|
(declare (doc-string 1) (pure t) (side-effect-free t))
|
2020-07-22 17:09:38 -04:00
|
|
|
`(lambda (arg &rest _) (interactive "P")
|
|
|
|
(let ((current-prefix-arg (or ,prefix-arg arg)))
|
2020-07-24 18:17:33 -04:00
|
|
|
(,(if args
|
|
|
|
'funcall-interactively
|
|
|
|
'call-interactively)
|
|
|
|
,command ,@args))))
|
2020-05-27 16:12:45 -04:00
|
|
|
|
|
|
|
(defmacro cmds! (&rest branches)
|
2020-12-11 17:38:59 -05:00
|
|
|
"Returns a dispatcher that runs the a command in BRANCHES.
|
|
|
|
Meant to be used as a target for keybinds (e.g. with `define-key' or `map!').
|
|
|
|
|
|
|
|
BRANCHES is a flat list of CONDITION COMMAND pairs. CONDITION is a lisp form
|
|
|
|
that is evaluated when (and each time) the dispatcher is invoked. If it returns
|
|
|
|
non-nil, COMMAND is invoked, otherwise it falls through to the next pair.
|
|
|
|
|
|
|
|
The last element of BRANCHES can be a COMMANd with no CONDITION. This acts as
|
|
|
|
the fallback if all other conditions fail.
|
|
|
|
|
|
|
|
Otherwise, Emacs will fall through the keybind and search the next keymap for a
|
|
|
|
keybind (as if this keybind never existed).
|
|
|
|
|
|
|
|
See `general-key-dispatch' for what other arguments it accepts in BRANCHES."
|
2020-05-27 16:12:45 -04:00
|
|
|
(declare (doc-string 1))
|
|
|
|
(let ((docstring (if (stringp (car branches)) (pop branches) ""))
|
|
|
|
fallback)
|
|
|
|
(when (cl-oddp (length branches))
|
|
|
|
(setq fallback (car (last branches))
|
|
|
|
branches (butlast branches)))
|
2021-02-11 15:51:43 -05:00
|
|
|
(let ((defs (cl-loop for (key value) on branches by 'cddr
|
|
|
|
unless (keywordp key)
|
|
|
|
collect (list key value))))
|
|
|
|
`'(menu-item
|
|
|
|
,(or docstring "") nil
|
|
|
|
:filter (lambda (&optional _)
|
|
|
|
(let (it)
|
|
|
|
(cond ,@(mapcar (lambda (pred-def)
|
|
|
|
`((setq it ,(car pred-def))
|
|
|
|
,(cadr pred-def)))
|
|
|
|
defs)
|
|
|
|
(t ,fallback))))))))
|
2020-05-27 16:12:45 -04:00
|
|
|
|
2022-03-19 21:12:41 +01:00
|
|
|
(defalias 'kbd! #'general-simulate-key)
|
2020-12-11 16:59:47 -05:00
|
|
|
|
2020-05-27 16:12:45 -04:00
|
|
|
;; For backwards compatibility
|
2022-03-19 21:12:41 +01:00
|
|
|
(defalias 'λ! #'cmd!)
|
|
|
|
(defalias 'λ!! #'cmd!!)
|
2020-05-27 16:12:45 -04:00
|
|
|
|
2020-05-20 15:32:34 -04:00
|
|
|
|
2020-04-29 20:45:29 -04:00
|
|
|
;;; Mutation
|
|
|
|
(defmacro appendq! (sym &rest lists)
|
|
|
|
"Append LISTS to SYM in place."
|
|
|
|
`(setq ,sym (append ,sym ,@lists)))
|
|
|
|
|
|
|
|
(defmacro setq! (&rest settings)
|
2022-03-19 21:16:10 +01:00
|
|
|
"A more sensible `setopt' for setting customizable variables.
|
2020-04-29 20:45:29 -04:00
|
|
|
|
2022-03-19 21:16:10 +01:00
|
|
|
This can be used as a drop-in replacement for `setq' and *should* be used
|
|
|
|
instead of `setopt'. Unlike `setq', this triggers custom setters on variables.
|
|
|
|
Unlike `setopt', this won't needlessly pull in dependencies."
|
2020-04-29 20:45:29 -04:00
|
|
|
(macroexp-progn
|
|
|
|
(cl-loop for (var val) on settings by 'cddr
|
2020-05-20 01:05:27 -04:00
|
|
|
collect `(funcall (or (get ',var 'custom-set) #'set)
|
|
|
|
',var ,val))))
|
2020-04-29 20:45:29 -04:00
|
|
|
|
|
|
|
(defmacro delq! (elt list &optional fetcher)
|
|
|
|
"`delq' ELT from LIST in-place.
|
|
|
|
|
|
|
|
If FETCHER is a function, ELT is used as the key in LIST (an alist)."
|
2022-03-19 21:12:41 +01:00
|
|
|
`(setq ,list (delq ,(if fetcher
|
|
|
|
`(funcall ,fetcher ,elt ,list)
|
|
|
|
elt)
|
|
|
|
,list)))
|
2020-04-29 20:45:29 -04:00
|
|
|
|
|
|
|
(defmacro pushnew! (place &rest values)
|
|
|
|
"Push VALUES sequentially into PLACE, if they aren't already present.
|
|
|
|
This is a variadic `cl-pushnew'."
|
|
|
|
(let ((var (make-symbol "result")))
|
|
|
|
`(dolist (,var (list ,@values) (with-no-warnings ,place))
|
|
|
|
(cl-pushnew ,var ,place :test #'equal))))
|
|
|
|
|
|
|
|
(defmacro prependq! (sym &rest lists)
|
|
|
|
"Prepend LISTS to SYM in place."
|
|
|
|
`(setq ,sym (append ,@lists ,sym)))
|
|
|
|
|
|
|
|
|
|
|
|
;;; Loading
|
|
|
|
(defmacro add-load-path! (&rest dirs)
|
|
|
|
"Add DIRS to `load-path', relative to the current file.
|
|
|
|
The current file is the file from which `add-to-load-path!' is used."
|
|
|
|
`(let ((default-directory ,(dir!))
|
|
|
|
file-name-handler-alist)
|
|
|
|
(dolist (dir (list ,@dirs))
|
2020-08-07 18:28:36 +02:00
|
|
|
(cl-pushnew (expand-file-name dir) load-path :test #'string=))))
|
2019-07-21 14:44:04 +02:00
|
|
|
|
2020-01-30 16:57:49 -05:00
|
|
|
(defmacro after! (package &rest body)
|
|
|
|
"Evaluate BODY after PACKAGE have loaded.
|
|
|
|
|
|
|
|
PACKAGE is a symbol or list of them. These are package names, not modes,
|
|
|
|
functions or variables. It can be:
|
|
|
|
|
|
|
|
- An unquoted package symbol (the name of a package)
|
|
|
|
(after! helm BODY...)
|
|
|
|
- An unquoted list of package symbols (i.e. BODY is evaluated once both magit
|
|
|
|
and git-gutter have loaded)
|
|
|
|
(after! (magit git-gutter) BODY...)
|
|
|
|
- An unquoted, nested list of compound package lists, using any combination of
|
|
|
|
:or/:any and :and/:all
|
|
|
|
(after! (:or package-a package-b ...) BODY...)
|
|
|
|
(after! (:and package-a package-b ...) BODY...)
|
|
|
|
(after! (:and package-a (:or package-b package-c) ...) BODY...)
|
|
|
|
Without :or/:any/:and/:all, :and/:all are implied.
|
|
|
|
|
|
|
|
This is a wrapper around `eval-after-load' that:
|
|
|
|
|
|
|
|
1. Suppresses warnings for disabled packages at compile-time
|
|
|
|
2. No-ops for package that are disabled by the user (via `package!')
|
|
|
|
3. Supports compound package statements (see below)
|
|
|
|
4. Prevents eager expansion pulling in autoloaded macros all at once"
|
|
|
|
(declare (indent defun) (debug t))
|
|
|
|
(if (symbolp package)
|
|
|
|
(unless (memq package (bound-and-true-p doom-disabled-packages))
|
|
|
|
(list (if (or (not (bound-and-true-p byte-compile-current-file))
|
|
|
|
(require package nil 'noerror))
|
|
|
|
#'progn
|
|
|
|
#'with-no-warnings)
|
2021-02-24 18:28:23 -05:00
|
|
|
;; We intentionally avoid `with-eval-after-load' to prevent eager
|
|
|
|
;; macro expansion from pulling (or failing to pull) in autoloaded
|
|
|
|
;; macros/packages.
|
|
|
|
`(eval-after-load ',package ',(macroexp-progn body))))
|
2020-01-30 16:57:49 -05:00
|
|
|
(let ((p (car package)))
|
2021-09-16 20:16:54 +02:00
|
|
|
(cond ((memq p '(:or :any))
|
2020-01-30 16:57:49 -05:00
|
|
|
(macroexp-progn
|
|
|
|
(cl-loop for next in (cdr package)
|
|
|
|
collect `(after! ,next ,@body))))
|
|
|
|
((memq p '(:and :all))
|
2021-09-16 20:16:54 +02:00
|
|
|
(dolist (next (reverse (cdr package)) (car body))
|
|
|
|
(setq body `((after! ,next ,@body)))))
|
|
|
|
(`(after! (:and ,@package) ,@body))))))
|
2020-01-30 16:57:49 -05:00
|
|
|
|
2020-04-29 20:45:29 -04:00
|
|
|
(defun doom--handle-load-error (e target path)
|
|
|
|
(let* ((source (file-name-sans-extension target))
|
|
|
|
(err (cond ((not (featurep 'core))
|
|
|
|
(cons 'error (file-name-directory path)))
|
|
|
|
((file-in-directory-p source doom-core-dir)
|
|
|
|
(cons 'doom-error doom-core-dir))
|
|
|
|
((file-in-directory-p source doom-private-dir)
|
|
|
|
(cons 'doom-private-error doom-private-dir))
|
refactor!(cli): rewrite CLI framework libraries
BREAKING CHANGE: this changes Doom's CLI framework in subtle ways, which
is listed in greater detail below. If you've never extended Doom's CLI,
then this won't affect you, but otherwise it'd be recommended you read
on below.
This commit focuses on the CLI framework itself and backports some
foundational changes to its DSL and how it resolves command line
arguments to CLIs, validates input, displays documentation, and persists
state across sessions -- and more. This is done in preparation for the
final stretch towarding completing the CLI rewrite (see #4273).
This is also an effort to generalize Doom's CLI (both its framework and
bin/doom), to increase it versatility and make it a viable dev tool for
other Doom projects (on our Github org) and beyond.
However, there is a *lot* to cover so I'll try to be brief:
- Refactor: generalize Doom's CLI framework by moving all bin/doom
specific configuration/commands out of core-cli into bin/doom. This
makes it easier to use bin/doom as a project-agnostic development
tool (or for users to write their own).
- Refactor: change the namespace for CLI variables/functions from
doom-cli-X to doom-X.
- Fix: subcommands being mistaken as arguments. "doom make index" will
resolve to (defcli! (doom make index)) if it exists,
otherwise (defcli! (doom make)) with "index" as an argument. Before
this, it would resolve to the latter no matter what. &rest can
override this; with (defcli! (doom make) (&rest args)), (defcli! (doom
make index)) will never be invoked.
- Refactor!: redesign our output library (was core/autoload/output.el,
is now core/autoload/print.el), and how our CLI framework buffers and
logs output, and now merges logs across (exit! ...) restarts.
- Feat: add support for :before and :after pseudo commands. E.g.
(defcli! (:before doom help) () ...)
(defcli! (:after doom sync) () ...)
Caveat: unlike advice, only one of each can be defined per-command.
- Feat: option arguments now have rudimentary type validation (see
`doom-cli-option-arg-types`). E.g.
(defcli! (doom foo) ((foo ("--foo" num))) ...)
If NUM is not a numeric, it will throw a validation error.
Any type that isn't in `doom-cli-option-arg-types` will be treated as a
wildcard string type. `num` can also be replaced with a specification,
e.g. "HOST[:PORT]", and can be formatted by using symbol quotes:
"`HOST'[:`PORT']".
- Feat: it is no longer required that options *immediately* follow the command
that defines them (but it must be somewhere after it, not before). E.g.
With:
(defcli! (:before doom foo) ((foo ("--foo"))) ...)
(defcli! (doom foo baz) () ...)
Before:
FAIL: doom --foo foo baz
GOOD: doom foo --foo baz
FAIL: doom foo baz --foo
After:
FAIL: doom --foo foo baz
GOOD: doom foo --foo baz
GOOD: doom foo baz --foo
- Refactor: CLI session state is now kept in a doom-cli-context struct (which
can be bound to a CLI-local variable with &context in the arglist):
(defcli! (doom sync) (&context context)
(print! "Command: " (doom-cli-context-command context)))
These contexts are persisted across sessions (when restarted). This is
necessary to support seamless script restarting (i.e. execve
emulation) in post-3.0.
- Feat: Doom's CLI framework now understands "--". Everything after it will be
treated as regular arguments, instead of sub-commands or options.
- Refactor!: the semantics of &rest for CLIs has changed. It used to be "all
extra literal, non-option arguments". It now means *all* unprocessed
arguments, and its use will suppress "unrecognized option" errors, and
tells the framework not to process any further subcommands. Use &args
if you just want "all literal arguments following this command".
- Feat: add new auxiliary keywords for CLI arglists: &context, &multiple,
&flags, &args, &stdin, &whole, and &cli.
- &context SYM: binds the currently running context to SYM (a
`doom-cli-context` struct). Helpful for introspection or passing
along state when calling subcommands by hand (with `call!`).
- &stdin SYM: SYM will be bound to a string containing any input piped
into the running script, or nil if none. Use
`doom-cli-context-pipe-p` to detect whether the script has been
piped into or out of.
- &multiple OPTIONS...: allows all following OPTIONS to be repeated. E.g. "foo
-x a -x b -x c" will pass (list ("-x" . "a") ("-x" . "b") ("-x" .
"c")) as -x's value.
- &flags OPTIONS...: All options after "&flags" get an implicit --no-* switch
and cannot accept arguments. Will be set to :yes or :no depending on which flag is
provided, and nil if the flag isn't provided. Otherwise, a default
value can be specified in that options' arglist. E.g.
(defcli! (doom foo) (&flags (foo ("--foo" :no))) ...)
When called, this command sets FOO to :yes if --foo, :no if --no-foo, and
defaults to :no otherwise.
- &args SYM: this replaces what &rest used to be; it binds to SYM a
list of all unprocessed (non-option) arguments.
- &rest SYM: now binds SYM to a list of all unprocessed arguments, including
options. This also suppresses "unrecognized option" errors, but will render
any sub-commands inaccessible. E.g.
(defcli! (doom make) (&rest rest) ...)
;; These are now inaccessible!
(defcli! (doom make foo) (&rest rest) ...)
(defcli! (doom make bar) (&rest rest) ...)
- &cli SYM: binds SYM to the currently running `doom-cli` struct. Can also be
obtained via `(doom-cli-get (doom-cli-context-command context))`. Possibly
useful for introspection.
- feat: add defobsolete! macro for quickly defining obsolete commands.
- feat: add defalias! macro for quickly defining alias commands.
- feat: add defautoload! macro for defining an autoloaded command (won't
be loaded until it is called for).
- refactor!: rename defcligroup! to defgroup! for consistency.
- fix: CLIs will now recursively inherit plist properties from parent
defcli-group!'s (but will stack :prefix).
- refactor!: remove obsolete 'doom update':
- refactor!: further generalize 'doom ci'
- In an effort to generalize 'doom ci' (so other Doom--or
non-doom--projects can use it), all its subcommands have been
changed to operate on the current working directory's repo instead
of $EMACSDIR.
- Doom-specific CI configuration was moved to .github/ci.el.
- All 'doom ci' commands will now preload one of \$CURRENT_REPO_ROOT/ci.el or
\$DOOMDIR/ci.el before executing.
- refactor!: changed 'doom env'
- 'doom env {-c,--clear}' is now 'doom env {clear,c}'
- -r/--reject and -a/--allow may now be specified multiple times
- refactor!: rewrote CLI help framework and error handling to be more
sophisticated and detailed.
- feat: can now initiate $PAGER on output with (exit! :pager) (or use
:pager? to only invoke pager is output is longer than the terminal is
tall).
- refactor!: changed semantics+conventions for global bin/doom options
- Single-character global options are now uppercased, to distinguish them from
local options:
- -d (for debug mode) is now -D
- -y (to suppress prompts) is now -!
- -l (to load elisp) is now -L
- -h (short for --help) is now -?
- Replace --yes/-y switches with --force/-!
- -L/--load FILE: now silently ignores file errors.
- Add --strict-load FILE: does the same as -L/--load, but throws an error if
FILE does not exist/is unreadable.
- Add -E/--eval FORM: evaluates arbitrary lisp before commands are processed.
- -L/--load, --strict-load, and -E/--eval can now be used multiple times in
one command.
- Add --pager COMMAND to specify an explicit pager. Will also obey
$DOOMPAGER envvar. Does not obey $PAGER.
- Fix #3746: which was likely caused by the generated post-script overwriting
the old mid-execution. By salting the postscript filenames (with both an
overarching session ID and a step counter).
- Docs: document websites, environment variables, and exit codes in
'doom --help'
- Feat: add imenu support for def{cli,alias,obsolete}!
Ref: #4273
Fix: #3746
Fix: #3844
2022-06-18 19:16:06 +02:00
|
|
|
((file-in-directory-p source (expand-file-name "cli" doom-core-dir))
|
|
|
|
(cons 'doom-cli-error (expand-file-name "cli" doom-core-dir)))
|
2020-04-29 20:45:29 -04:00
|
|
|
((cons 'doom-module-error doom-emacs-dir)))))
|
|
|
|
(signal (car err)
|
|
|
|
(list (file-relative-name
|
|
|
|
(concat source ".el")
|
|
|
|
(cdr err))
|
|
|
|
e))))
|
2020-01-24 16:38:44 -05:00
|
|
|
|
2020-04-29 20:45:29 -04:00
|
|
|
(defmacro load! (filename &optional path noerror)
|
|
|
|
"Load a file relative to the current executing file (`load-file-name').
|
2019-07-21 19:11:43 +02:00
|
|
|
|
2020-04-29 20:45:29 -04:00
|
|
|
FILENAME is either a file path string or a form that should evaluate to such a
|
|
|
|
string at run time. PATH is where to look for the file (a string representing a
|
|
|
|
directory path). If omitted, the lookup is relative to either `load-file-name',
|
|
|
|
`byte-compile-current-file' or `buffer-file-name' (checked in that order).
|
2019-07-21 14:39:16 +02:00
|
|
|
|
2020-04-29 20:45:29 -04:00
|
|
|
If NOERROR is non-nil, don't throw an error if the file doesn't exist."
|
|
|
|
(let* ((path (or path
|
|
|
|
(dir!)
|
|
|
|
(error "Could not detect path to look for '%s' in"
|
|
|
|
filename)))
|
|
|
|
(file (if path
|
refactor!(cli): rewrite CLI framework libraries
BREAKING CHANGE: this changes Doom's CLI framework in subtle ways, which
is listed in greater detail below. If you've never extended Doom's CLI,
then this won't affect you, but otherwise it'd be recommended you read
on below.
This commit focuses on the CLI framework itself and backports some
foundational changes to its DSL and how it resolves command line
arguments to CLIs, validates input, displays documentation, and persists
state across sessions -- and more. This is done in preparation for the
final stretch towarding completing the CLI rewrite (see #4273).
This is also an effort to generalize Doom's CLI (both its framework and
bin/doom), to increase it versatility and make it a viable dev tool for
other Doom projects (on our Github org) and beyond.
However, there is a *lot* to cover so I'll try to be brief:
- Refactor: generalize Doom's CLI framework by moving all bin/doom
specific configuration/commands out of core-cli into bin/doom. This
makes it easier to use bin/doom as a project-agnostic development
tool (or for users to write their own).
- Refactor: change the namespace for CLI variables/functions from
doom-cli-X to doom-X.
- Fix: subcommands being mistaken as arguments. "doom make index" will
resolve to (defcli! (doom make index)) if it exists,
otherwise (defcli! (doom make)) with "index" as an argument. Before
this, it would resolve to the latter no matter what. &rest can
override this; with (defcli! (doom make) (&rest args)), (defcli! (doom
make index)) will never be invoked.
- Refactor!: redesign our output library (was core/autoload/output.el,
is now core/autoload/print.el), and how our CLI framework buffers and
logs output, and now merges logs across (exit! ...) restarts.
- Feat: add support for :before and :after pseudo commands. E.g.
(defcli! (:before doom help) () ...)
(defcli! (:after doom sync) () ...)
Caveat: unlike advice, only one of each can be defined per-command.
- Feat: option arguments now have rudimentary type validation (see
`doom-cli-option-arg-types`). E.g.
(defcli! (doom foo) ((foo ("--foo" num))) ...)
If NUM is not a numeric, it will throw a validation error.
Any type that isn't in `doom-cli-option-arg-types` will be treated as a
wildcard string type. `num` can also be replaced with a specification,
e.g. "HOST[:PORT]", and can be formatted by using symbol quotes:
"`HOST'[:`PORT']".
- Feat: it is no longer required that options *immediately* follow the command
that defines them (but it must be somewhere after it, not before). E.g.
With:
(defcli! (:before doom foo) ((foo ("--foo"))) ...)
(defcli! (doom foo baz) () ...)
Before:
FAIL: doom --foo foo baz
GOOD: doom foo --foo baz
FAIL: doom foo baz --foo
After:
FAIL: doom --foo foo baz
GOOD: doom foo --foo baz
GOOD: doom foo baz --foo
- Refactor: CLI session state is now kept in a doom-cli-context struct (which
can be bound to a CLI-local variable with &context in the arglist):
(defcli! (doom sync) (&context context)
(print! "Command: " (doom-cli-context-command context)))
These contexts are persisted across sessions (when restarted). This is
necessary to support seamless script restarting (i.e. execve
emulation) in post-3.0.
- Feat: Doom's CLI framework now understands "--". Everything after it will be
treated as regular arguments, instead of sub-commands or options.
- Refactor!: the semantics of &rest for CLIs has changed. It used to be "all
extra literal, non-option arguments". It now means *all* unprocessed
arguments, and its use will suppress "unrecognized option" errors, and
tells the framework not to process any further subcommands. Use &args
if you just want "all literal arguments following this command".
- Feat: add new auxiliary keywords for CLI arglists: &context, &multiple,
&flags, &args, &stdin, &whole, and &cli.
- &context SYM: binds the currently running context to SYM (a
`doom-cli-context` struct). Helpful for introspection or passing
along state when calling subcommands by hand (with `call!`).
- &stdin SYM: SYM will be bound to a string containing any input piped
into the running script, or nil if none. Use
`doom-cli-context-pipe-p` to detect whether the script has been
piped into or out of.
- &multiple OPTIONS...: allows all following OPTIONS to be repeated. E.g. "foo
-x a -x b -x c" will pass (list ("-x" . "a") ("-x" . "b") ("-x" .
"c")) as -x's value.
- &flags OPTIONS...: All options after "&flags" get an implicit --no-* switch
and cannot accept arguments. Will be set to :yes or :no depending on which flag is
provided, and nil if the flag isn't provided. Otherwise, a default
value can be specified in that options' arglist. E.g.
(defcli! (doom foo) (&flags (foo ("--foo" :no))) ...)
When called, this command sets FOO to :yes if --foo, :no if --no-foo, and
defaults to :no otherwise.
- &args SYM: this replaces what &rest used to be; it binds to SYM a
list of all unprocessed (non-option) arguments.
- &rest SYM: now binds SYM to a list of all unprocessed arguments, including
options. This also suppresses "unrecognized option" errors, but will render
any sub-commands inaccessible. E.g.
(defcli! (doom make) (&rest rest) ...)
;; These are now inaccessible!
(defcli! (doom make foo) (&rest rest) ...)
(defcli! (doom make bar) (&rest rest) ...)
- &cli SYM: binds SYM to the currently running `doom-cli` struct. Can also be
obtained via `(doom-cli-get (doom-cli-context-command context))`. Possibly
useful for introspection.
- feat: add defobsolete! macro for quickly defining obsolete commands.
- feat: add defalias! macro for quickly defining alias commands.
- feat: add defautoload! macro for defining an autoloaded command (won't
be loaded until it is called for).
- refactor!: rename defcligroup! to defgroup! for consistency.
- fix: CLIs will now recursively inherit plist properties from parent
defcli-group!'s (but will stack :prefix).
- refactor!: remove obsolete 'doom update':
- refactor!: further generalize 'doom ci'
- In an effort to generalize 'doom ci' (so other Doom--or
non-doom--projects can use it), all its subcommands have been
changed to operate on the current working directory's repo instead
of $EMACSDIR.
- Doom-specific CI configuration was moved to .github/ci.el.
- All 'doom ci' commands will now preload one of \$CURRENT_REPO_ROOT/ci.el or
\$DOOMDIR/ci.el before executing.
- refactor!: changed 'doom env'
- 'doom env {-c,--clear}' is now 'doom env {clear,c}'
- -r/--reject and -a/--allow may now be specified multiple times
- refactor!: rewrote CLI help framework and error handling to be more
sophisticated and detailed.
- feat: can now initiate $PAGER on output with (exit! :pager) (or use
:pager? to only invoke pager is output is longer than the terminal is
tall).
- refactor!: changed semantics+conventions for global bin/doom options
- Single-character global options are now uppercased, to distinguish them from
local options:
- -d (for debug mode) is now -D
- -y (to suppress prompts) is now -!
- -l (to load elisp) is now -L
- -h (short for --help) is now -?
- Replace --yes/-y switches with --force/-!
- -L/--load FILE: now silently ignores file errors.
- Add --strict-load FILE: does the same as -L/--load, but throws an error if
FILE does not exist/is unreadable.
- Add -E/--eval FORM: evaluates arbitrary lisp before commands are processed.
- -L/--load, --strict-load, and -E/--eval can now be used multiple times in
one command.
- Add --pager COMMAND to specify an explicit pager. Will also obey
$DOOMPAGER envvar. Does not obey $PAGER.
- Fix #3746: which was likely caused by the generated post-script overwriting
the old mid-execution. By salting the postscript filenames (with both an
overarching session ID and a step counter).
- Docs: document websites, environment variables, and exit codes in
'doom --help'
- Feat: add imenu support for def{cli,alias,obsolete}!
Ref: #4273
Fix: #3746
Fix: #3844
2022-06-18 19:16:06 +02:00
|
|
|
`(expand-file-name ,filename ,path)
|
|
|
|
filename)))
|
2020-04-29 20:45:29 -04:00
|
|
|
`(condition-case-unless-debug e
|
|
|
|
(let (file-name-handler-alist)
|
|
|
|
(load ,file ,noerror 'nomessage))
|
|
|
|
(doom-error (signal (car e) (cdr e)))
|
|
|
|
(error (doom--handle-load-error e ,file ,path)))))
|
2019-07-21 14:39:16 +02:00
|
|
|
|
2020-04-29 20:45:29 -04:00
|
|
|
(defmacro defer-until! (condition &rest body)
|
|
|
|
"Run BODY when CONDITION is true (checks on `after-load-functions'). Meant to
|
|
|
|
serve as a predicated alternative to `after!'."
|
|
|
|
(declare (indent defun) (debug t))
|
|
|
|
`(if ,condition
|
|
|
|
(progn ,@body)
|
|
|
|
,(let ((fn (intern (format "doom--delay-form-%s-h" (sxhash (cons condition body))))))
|
|
|
|
`(progn
|
|
|
|
(fset ',fn (lambda (&rest args)
|
|
|
|
(when ,(or condition t)
|
|
|
|
(remove-hook 'after-load-functions #',fn)
|
|
|
|
(unintern ',fn nil)
|
|
|
|
(ignore args)
|
|
|
|
,@body)))
|
|
|
|
(put ',fn 'permanent-local-hook t)
|
|
|
|
(add-hook 'after-load-functions #',fn)))))
|
2019-08-10 11:07:25 -04:00
|
|
|
|
2020-06-05 01:41:49 -04:00
|
|
|
(defmacro defer-feature! (feature &rest fns)
|
2020-04-29 20:45:29 -04:00
|
|
|
"Pretend FEATURE hasn't been loaded yet, until FEATURE-hook or FN runs.
|
2019-04-08 23:01:30 -04:00
|
|
|
|
2020-04-29 20:45:29 -04:00
|
|
|
Some packages (like `elisp-mode' and `lisp-mode') are loaded immediately at
|
|
|
|
startup, which will prematurely trigger `after!' (and `with-eval-after-load')
|
|
|
|
blocks. To get around this we make Emacs believe FEATURE hasn't been loaded yet,
|
|
|
|
then wait until FEATURE-hook (or MODE-hook, if FN is provided) is triggered to
|
|
|
|
reverse this and trigger `after!' blocks at a more reasonable time."
|
2020-06-05 01:41:49 -04:00
|
|
|
(let ((advice-fn (intern (format "doom--defer-feature-%s-a" feature))))
|
2020-04-29 20:45:29 -04:00
|
|
|
`(progn
|
2020-06-05 01:41:49 -04:00
|
|
|
(delq! ',feature features)
|
|
|
|
(defadvice! ,advice-fn (&rest _)
|
|
|
|
:before ',fns
|
2020-04-29 20:45:29 -04:00
|
|
|
;; Some plugins (like yasnippet) will invoke a fn early to parse
|
|
|
|
;; code, which would prematurely trigger this. In those cases, well
|
|
|
|
;; behaved plugins will use `delay-mode-hooks', which we can check for:
|
2020-06-05 01:41:49 -04:00
|
|
|
(unless delay-mode-hooks
|
2020-04-29 20:45:29 -04:00
|
|
|
;; ...Otherwise, announce to the world this package has been loaded,
|
|
|
|
;; so `after!' handlers can react.
|
|
|
|
(provide ',feature)
|
2020-06-05 01:41:49 -04:00
|
|
|
(dolist (fn ',fns)
|
|
|
|
(advice-remove fn #',advice-fn)))))))
|
2019-12-08 16:11:29 -05:00
|
|
|
|
2019-10-23 04:24:06 -04:00
|
|
|
|
2020-03-03 18:58:45 -05:00
|
|
|
;;; Hooks
|
2018-07-09 15:33:31 +02:00
|
|
|
(defmacro add-transient-hook! (hook-or-function &rest forms)
|
|
|
|
"Attaches a self-removing function to HOOK-OR-FUNCTION.
|
2017-06-05 16:41:39 +02:00
|
|
|
|
2019-05-01 19:12:52 -04:00
|
|
|
FORMS are evaluated once, when that function/hook is first invoked, then never
|
2018-07-09 15:33:31 +02:00
|
|
|
again.
|
2017-06-05 16:41:39 +02:00
|
|
|
|
2018-07-09 15:33:31 +02:00
|
|
|
HOOK-OR-FUNCTION can be a quoted hook or a sharp-quoted function (which will be
|
|
|
|
advised)."
|
2017-03-02 18:22:37 -05:00
|
|
|
(declare (indent 1))
|
2018-05-15 13:40:18 +02:00
|
|
|
(let ((append (if (eq (car forms) :after) (pop forms)))
|
2020-03-03 18:58:45 -05:00
|
|
|
;; Avoid `make-symbol' and `gensym' here because an interned symbol is
|
|
|
|
;; easier to debug in backtraces (and is visible to `describe-function')
|
2021-10-09 19:55:47 +02:00
|
|
|
(fn (intern (format "doom--transient-%d-h"
|
|
|
|
(put 'add-transient-hook! 'counter
|
|
|
|
(1+ (or (get 'add-transient-hook! 'counter)
|
|
|
|
0)))))))
|
2019-05-04 18:55:24 -04:00
|
|
|
`(let ((sym ,hook-or-function))
|
2019-07-21 14:40:38 +02:00
|
|
|
(defun ,fn (&rest _)
|
2020-03-03 18:58:45 -05:00
|
|
|
,(format "Transient hook for %S" (doom-unquote hook-or-function))
|
2019-07-21 14:40:38 +02:00
|
|
|
,@forms
|
|
|
|
(let ((sym ,hook-or-function))
|
|
|
|
(cond ((functionp sym) (advice-remove sym #',fn))
|
|
|
|
((symbolp sym) (remove-hook sym #',fn))))
|
|
|
|
(unintern ',fn nil))
|
2019-05-04 18:55:24 -04:00
|
|
|
(cond ((functionp sym)
|
2018-07-09 15:33:31 +02:00
|
|
|
(advice-add ,hook-or-function ,(if append :after :before) #',fn))
|
2019-05-04 18:55:24 -04:00
|
|
|
((symbolp sym)
|
2018-05-24 22:35:45 +02:00
|
|
|
(put ',fn 'permanent-local-hook t)
|
2019-05-04 18:55:24 -04:00
|
|
|
(add-hook sym #',fn ,append))))))
|
2017-03-02 01:43:00 -05:00
|
|
|
|
2019-07-26 19:57:13 +02:00
|
|
|
(defmacro add-hook! (hooks &rest rest)
|
2019-05-01 19:12:52 -04:00
|
|
|
"A convenience macro for adding N functions to M hooks.
|
|
|
|
|
|
|
|
This macro accepts, in order:
|
2017-01-16 23:15:48 -05:00
|
|
|
|
2020-01-20 05:41:42 -05:00
|
|
|
1. The mode(s) or hook(s) to add to. This is either an unquoted mode, an
|
|
|
|
unquoted list of modes, a quoted hook variable or a quoted list of hook
|
|
|
|
variables.
|
2021-05-16 21:19:30 -04:00
|
|
|
2. Optional properties :local, :append, and/or :depth [N], which will make the
|
|
|
|
hook buffer-local or append to the list of hooks (respectively),
|
2021-10-11 14:43:14 +02:00
|
|
|
3. The function(s) to be added: this can be a quoted function, a quoted list
|
|
|
|
thereof, a list of `defun' or `cl-defun' forms, or arbitrary forms (will
|
|
|
|
implicitly be wrapped in a lambda).
|
2017-01-16 23:15:48 -05:00
|
|
|
|
2021-10-08 23:21:04 +02:00
|
|
|
\(fn HOOKS [:append :local [:depth N]] FUNCTIONS-OR-FORMS...)"
|
2019-07-26 19:57:13 +02:00
|
|
|
(declare (indent (lambda (indent-point state)
|
|
|
|
(goto-char indent-point)
|
|
|
|
(when (looking-at-p "\\s-*(")
|
|
|
|
(lisp-indent-defform state indent-point))))
|
|
|
|
(debug t))
|
|
|
|
(let* ((hook-forms (doom--resolve-hook-forms hooks))
|
|
|
|
(func-forms ())
|
|
|
|
(defn-forms ())
|
2021-10-08 23:21:04 +02:00
|
|
|
append-p local-p remove-p depth)
|
2019-07-26 19:57:13 +02:00
|
|
|
(while (keywordp (car rest))
|
|
|
|
(pcase (pop rest)
|
2017-02-28 15:29:23 -05:00
|
|
|
(:append (setq append-p t))
|
2021-05-09 13:06:24 -04:00
|
|
|
(:depth (setq depth (pop rest)))
|
2017-06-08 11:47:56 +02:00
|
|
|
(:local (setq local-p t))
|
2019-07-26 19:57:13 +02:00
|
|
|
(:remove (setq remove-p t))))
|
2021-10-08 23:21:04 +02:00
|
|
|
(while rest
|
|
|
|
(let* ((next (pop rest))
|
|
|
|
(first (car-safe next)))
|
2021-11-20 00:52:51 +01:00
|
|
|
(push (cond ((memq first '(function nil))
|
2021-10-08 23:21:04 +02:00
|
|
|
next)
|
2021-11-20 00:52:51 +01:00
|
|
|
((eq first 'quote)
|
|
|
|
(let ((quoted (cadr next)))
|
|
|
|
(if (atom quoted)
|
|
|
|
next
|
|
|
|
(when (cdr quoted)
|
|
|
|
(setq rest (cons (list first (cdr quoted)) rest)))
|
|
|
|
(list first (car quoted)))))
|
2021-10-08 23:21:04 +02:00
|
|
|
((memq first '(defun cl-defun))
|
|
|
|
(push next defn-forms)
|
|
|
|
(list 'function (cadr next)))
|
|
|
|
((prog1 `(lambda (&rest _) ,@(cons next rest))
|
|
|
|
(setq rest nil))))
|
|
|
|
func-forms)))
|
|
|
|
`(progn
|
|
|
|
,@defn-forms
|
|
|
|
(dolist (hook (nreverse ',hook-forms))
|
|
|
|
(dolist (func (list ,@func-forms))
|
|
|
|
,(if remove-p
|
|
|
|
`(remove-hook hook func ,local-p)
|
|
|
|
`(add-hook hook func ,(or depth append-p) ,local-p)))))))
|
2017-01-16 23:15:48 -05:00
|
|
|
|
2019-07-26 19:57:13 +02:00
|
|
|
(defmacro remove-hook! (hooks &rest rest)
|
2019-05-01 19:12:52 -04:00
|
|
|
"A convenience macro for removing N functions from M hooks.
|
|
|
|
|
|
|
|
Takes the same arguments as `add-hook!'.
|
|
|
|
|
|
|
|
If N and M = 1, there's no benefit to using this macro over `remove-hook'.
|
2019-03-10 08:15:46 -04:00
|
|
|
|
2019-09-16 12:07:32 -04:00
|
|
|
\(fn HOOKS [:append :local] FUNCTIONS)"
|
2018-05-07 22:35:14 +02:00
|
|
|
(declare (indent defun) (debug t))
|
2019-07-26 19:57:13 +02:00
|
|
|
`(add-hook! ,hooks :remove ,@rest))
|
2017-02-28 15:38:47 -05:00
|
|
|
|
2019-07-21 14:35:45 +02:00
|
|
|
(defmacro setq-hook! (hooks &rest var-vals)
|
2019-05-01 19:12:52 -04:00
|
|
|
"Sets buffer-local variables on HOOKS.
|
2018-05-07 22:35:14 +02:00
|
|
|
|
2019-07-21 14:35:45 +02:00
|
|
|
\(fn HOOKS &rest [SYM VAL]...)"
|
2018-05-07 22:35:14 +02:00
|
|
|
(declare (indent 1))
|
2019-07-21 14:35:45 +02:00
|
|
|
(macroexp-progn
|
|
|
|
(cl-loop for (var val hook fn) in (doom--setq-hook-fns hooks var-vals)
|
|
|
|
collect `(defun ,fn (&rest _)
|
|
|
|
,(format "%s = %s" var (pp-to-string val))
|
|
|
|
(setq-local ,var ,val))
|
|
|
|
collect `(remove-hook ',hook #',fn) ; ensure set order
|
2019-07-29 21:04:04 +02:00
|
|
|
collect `(add-hook ',hook #',fn))))
|
2019-07-21 14:35:45 +02:00
|
|
|
|
|
|
|
(defmacro unsetq-hook! (hooks &rest vars)
|
|
|
|
"Unbind setq hooks on HOOKS for VARS.
|
|
|
|
|
|
|
|
\(fn HOOKS &rest [SYM VAL]...)"
|
|
|
|
(declare (indent 1))
|
|
|
|
(macroexp-progn
|
2019-10-07 16:10:33 -04:00
|
|
|
(cl-loop for (_var _val hook fn)
|
|
|
|
in (doom--setq-hook-fns hooks vars 'singles)
|
2019-07-21 14:35:45 +02:00
|
|
|
collect `(remove-hook ',hook #',fn))))
|
2018-05-07 22:35:14 +02:00
|
|
|
|
2019-07-18 15:25:01 +02:00
|
|
|
|
2019-07-23 17:24:56 +02:00
|
|
|
;;; Definers
|
|
|
|
(defmacro defadvice! (symbol arglist &optional docstring &rest body)
|
2019-08-05 18:27:56 -05:00
|
|
|
"Define an advice called SYMBOL and add it to PLACES.
|
2019-07-23 17:24:56 +02:00
|
|
|
|
|
|
|
ARGLIST is as in `defun'. WHERE is a keyword as passed to `advice-add', and
|
|
|
|
PLACE is the function to which to add the advice, like in `advice-add'.
|
|
|
|
DOCSTRING and BODY are as in `defun'.
|
|
|
|
|
|
|
|
\(fn SYMBOL ARGLIST &optional DOCSTRING &rest [WHERE PLACES...] BODY\)"
|
|
|
|
(declare (doc-string 3) (indent defun))
|
|
|
|
(unless (stringp docstring)
|
|
|
|
(push docstring body)
|
|
|
|
(setq docstring nil))
|
|
|
|
(let (where-alist)
|
|
|
|
(while (keywordp (car body))
|
|
|
|
(push `(cons ,(pop body) (doom-enlist ,(pop body)))
|
|
|
|
where-alist))
|
|
|
|
`(progn
|
|
|
|
(defun ,symbol ,arglist ,docstring ,@body)
|
2019-12-19 14:49:17 -05:00
|
|
|
(dolist (targets (list ,@(nreverse where-alist)))
|
|
|
|
(dolist (target (cdr targets))
|
|
|
|
(advice-add target (car targets) #',symbol))))))
|
2019-07-23 17:24:56 +02:00
|
|
|
|
2020-01-01 14:31:49 -05:00
|
|
|
(defmacro undefadvice! (symbol _arglist &optional docstring &rest body)
|
|
|
|
"Undefine an advice called SYMBOL.
|
|
|
|
|
|
|
|
This has the same signature as `defadvice!' an exists as an easy undefiner when
|
|
|
|
testing advice (when combined with `rotate-text').
|
|
|
|
|
|
|
|
\(fn SYMBOL ARGLIST &optional DOCSTRING &rest [WHERE PLACES...] BODY\)"
|
|
|
|
(declare (doc-string 3) (indent defun))
|
|
|
|
(let (where-alist)
|
|
|
|
(unless (stringp docstring)
|
|
|
|
(push docstring body))
|
|
|
|
(while (keywordp (car body))
|
|
|
|
(push `(cons ,(pop body) (doom-enlist ,(pop body)))
|
|
|
|
where-alist))
|
|
|
|
`(dolist (targets (list ,@(nreverse where-alist)))
|
|
|
|
(dolist (target (cdr targets))
|
|
|
|
(advice-remove target #',symbol)))))
|
|
|
|
|
2020-05-24 16:45:55 -04:00
|
|
|
|
|
|
|
;;
|
|
|
|
;;; Backports
|
|
|
|
|
2022-06-21 21:28:07 +02:00
|
|
|
;; `format-spec' wasn't autoloaded until 28.1
|
|
|
|
(unless (fboundp 'format-spec)
|
|
|
|
(autoload #'format-spec "format-spec"))
|
2022-06-20 02:36:50 +02:00
|
|
|
|
2022-06-21 21:28:07 +02:00
|
|
|
;; Introduced in Emacs 28.1
|
|
|
|
(unless (fboundp 'ensure-list)
|
2022-06-18 15:07:43 +02:00
|
|
|
(defun ensure-list (object)
|
|
|
|
"Return OBJECT as a list.
|
|
|
|
If OBJECT is already a list, return OBJECT itself. If it's
|
|
|
|
not a list, return a one-element list containing OBJECT."
|
|
|
|
(declare (pure t) (side-effect-free t))
|
|
|
|
(if (listp object)
|
|
|
|
object
|
|
|
|
(list object))))
|
2021-01-03 17:40:33 -05:00
|
|
|
|
2017-01-16 23:15:48 -05:00
|
|
|
(provide 'core-lib)
|
|
|
|
;;; core-lib.el ends here
|