Commit graph

26 commits

Author SHA1 Message Date
Henrik Lissner
14b2395424
refactor: remove unused core variables
doom-debug-p and doom-interactive-p have always been intentionally
redundant, because changing the variables they replaced had other
side-effects, which made writing tests for them difficult. Since our
new (yet unpublished) tests lean heavily toward integration testing more
than unit testing, this becomes an implementation detail.

And doom-init-p's only use was refactor out at some point in the past,
so it's no longer used.

Also done to reduce Doom's footprint, in general.
2022-06-29 18:14:20 +02:00
Henrik Lissner
a65e97bf2c
fix(cli): return *all* aliases from doom-cli-aliases
Now it walks the command tree breadth-first to collect *all* aliases
that lead to the given command.
2022-06-22 23:04:59 +02:00
Henrik Lissner
001065ba42
refactor(cli): minor refactors & allow flychecker in CLIs
- Add comments to doom-cli-context-parse.
- Simplify/inline code here and there.
- Remove sacrifice to appease byte-compiler.

Thanks to a346928 there's no need to appease the byte-compiler with this
eval-when-compile hackery.

Ref: a34692826f18
2022-06-22 23:04:54 +02:00
Henrik Lissner
e651547abb
fix(cli): &rest not consuming rest of arguments
An edge case that occurs when no options are supplied to make arguments
non-null.
2022-06-22 23:04:14 +02:00
Henrik Lissner
2aca862187
fix(cli): prevent oversized error data spam
A backtrace with a sufficiently large object (especially with circular
references) could unhelpfully flood the screen.
2022-06-22 23:04:14 +02:00
Henrik Lissner
2660fcce25
fix(cli): not printing straight log after it errors
This regression was introduced in 6c0b7e1, preventing the debugger from
printing straight.el's process log when a straight.el error occurs.
Instead, it would print a less-than-helpful backtrace.

Amend: 6c0b7e1530
2022-06-22 23:04:14 +02:00
Henrik Lissner
666cc1fda7
refactor(cli): remove :stub, :obsolete, :deprecated, :since
I've removed these CLI properties because they were either
unused (:deprecated and :since) or poorly implemented (:stub and
:obsolete). And I'd rather have fewer magical properties, and instead
delegate these roles to the defobsolete! and (new) defstub!  macros.
Also, in the future, the help API will ascertain :since dynamically, so
it won't be very useful.

In summary:
- Use defstub! instead of :stub
- Use defobsolete! instead of :obsolete or :deprecated
- This removes the doom-cli-deprecated-error type (it's not really an
  error to begin with).
- Removes :stub, :obsolete, :deprecated, and :since
2022-06-22 23:04:14 +02:00
Henrik Lissner
a4aab45656
fix(emacs-lisp): flycheck false positives in Doom configs 2022-06-22 23:04:00 +02:00
Henrik Lissner
e4aa07f811
feat(cli): record aliases in doom-cli-context-path
doom-cli-context-path stores the path taken to get to the current
command. For example, given:

  (defcli! (doom boop) () ...)
  (defcli! (doom baz) () (call! '(boop)))
  (defalias! (doom bar) (doom baz))
  (defalias! (doom foo) (doom bar))
  (run! "doom" "foo")

Then the doom-cli-context-path by 'doom boop' will be:

Before this commit: ((doom baz))
After this commit:  ((doom baz) (doom bar) (doom foo))
2022-06-22 20:35:52 +02:00
Henrik Lissner
23b289b9a5
feat(cli): fall back to :docs for docstring
Also allows for a dynamically generated docstring. Careful though, it's
evaluated when `defcli!` is evaluated, not when the CLI is executed.
2022-06-22 20:35:17 +02:00
Henrik Lissner
834ff3ea85
fix(cli): autoloaded clis aliased to wrong commands
I use a shortcut like this to autoload multiple (different) CLIs living
in the same file:

  (defautoload! ((foo bar baz)) "file")

However, this creates premature aliases between autoloaded CLIs. When
'baz' is invoked, instead of loading "file", it resolves to 'foo'
first *then* loads it, causing 'foo' to be executed instead of 'baz'.
This commit fixes that.

Also, minor refactor: I removed the plist argument from a
doom-cli-command-normalize call because it wasn't needed or useful to
the consumer of its return value.

Amend: d226946f59
2022-06-22 02:27:34 +02:00
Henrik Lissner
5519c030ff
fix(cli): -!/--force being ignored
Forgot to adapt the old code to use doom-cli-context struct!

Fix: #6485
2022-06-21 23:00:00 +02:00
Henrik Lissner
23feb482e9
refactor!(lib): rename fn!->lambda! & fn!!->fn!
BREAKING CHANGE: This renames the fn! macro to lambda! and fn!! to fn!.
I hadn't put much thought into their names when they were added, but now
that they're seeing more use, I've reconsidered.

The reasoning is (and I'll refer to them by their new names):

- If you're using fn!, you care more about the syntax's brevity, than if
  you were using lambda!, so I wanted fn! to have the (even if slightly)
  shorter name.
- lambda! decorates native lambda (with cl-function). Its old name
  did not suggest that connection like other !-macros in Doom's library
  do.
- Their old names implied the two macros were somehow related or that
  one decorated the other. They aren't and don't.
2022-06-21 21:29:08 +02:00
Henrik Lissner
b63b209dd3
fix(cli): force less to process ansi codes
I'm not certain how portable -r is for less. Fingers crossed!

Fix: #6488
2022-06-21 16:32:19 +02:00
Henrik Lissner
9f2d6262e5
refactor(cli): return unresolved CLIs from doom-cli-find
'doom help' is aliased to the pseudo command ':help'. (doom-cli-find
'("doom" "help")) would return a list of all CLIs that are pertinent to
'doom help's execution, but it resolves aliases, making it unclear to
recipients of this list what the initial command was.

This requires callers resolve the CLIs themselves, but at least now the
user has the choice.
2022-06-21 14:44:26 +02:00
Henrik Lissner
b0b727824a
feat(cli): allow docs to use %c/%p format specs
And replace magic section SYNOPSIS with EXAMPLES.
2022-06-21 02:08:19 +02:00
Henrik Lissner
0511445339
feat(cli): add :dump pseudo command
And fix a void-variable doom-cli--dump error accidentally introduced in
d231755.

Ref: d231755bdf
2022-06-21 00:32:15 +02:00
Henrik Lissner
d231755bdf
refactor(cli): rename struct constructors/copiers
To maintain our namespaces.
2022-06-20 23:43:23 +02:00
Henrik Lissner
ab7f7058f3
docs(cli): show correct command in error message
Before:
  $ doom ci hook commit-msg test
  Error: "doom ci" expected 0 arguments, but got 1

  Usage: doom ci COMMAND [ARGS...]

  See 'doom h[elp] ci' or 'doom ci {-?,--help}' for documentation.

After:
  $ doom ci hook commit-msg test
  Error: ":before doom ci" expected 0 arguments, but got 1

  Usage: doom ci hook commit-msg FILE

  See 'doom h[elp] ci hook commit-msg' or 'doom ci hook commit-msg {-?,--help}' for documentation.
2022-06-19 22:51:46 +02:00
Henrik Lissner
7862a9e15d
docs(cli): add more exit! usecases 2022-06-19 22:01:28 +02:00
Henrik Lissner
da74525cbc
tweak(cli): process arguments before executing CLIs 2022-06-19 21:39:17 +02:00
Henrik Lissner
d226946f59
fix(cli): aliases to pseudo commands
To understand what's going on here, understand this: this is a regular
CLI command:

  (defcli! (doom sync) () ...)

And this is a pseudo command:

  (defcli! (:before doom sync) () ...)

If a pseudo command is aliased to another pseudo command:

  (defcli! (:before doom (foo bar baz)) ...)

In which case, ':before doom bar' and ':before doom baz' are aliases for
':before doom foo', there was a bug that cut out the keyword, so in
actuality, ':before doom {bar,baz}' were aliased to 'doom bar'.

This fixes that, and the peculiar issue of 'doom purge' executing 'doom
build' due to this, living in core/cli/packages.el:

  (defcli! (:before (build b purge p)) (&context context)
    (require 'comp nil t)
    (doom-initialize-core-packages))

Ref: https://github.com/doomemacs/doomemacs/issues/4273#issuecomment-1159610824
2022-06-19 21:26:53 +02:00
Henrik Lissner
074f63ff31
fix(cli): ignore invalid __DOOMGEOM value 2022-06-19 02:44:00 +02:00
Henrik Lissner
beef0aef02
fix(cli): split tput call into two separate calls
This fixes an issue where, on some systems, `tput cols lines` does not
produce "N\nM" (where N = number of columns in the terminal and M =
number of lines), and instead produces "N\n", causing parsing errors.
2022-06-19 02:39:22 +02:00
Henrik Lissner
f4e7e8be56
fix(cli): type error when __DOOMGEOM is empty 2022-06-19 02:20:34 +02:00
Henrik Lissner
6c0b7e1530
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 23:53:12 +02:00