Skip to content

Commit

Permalink
Replace most uses of global ErrorBuffer struct with EditorState::err
Browse files Browse the repository at this point in the history
  • Loading branch information
craigbarnes committed Dec 30, 2024
1 parent ed60741 commit f1ff10b
Show file tree
Hide file tree
Showing 37 changed files with 402 additions and 421 deletions.
16 changes: 10 additions & 6 deletions src/change.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
#include "change.h"
#include "buffer.h"
#include "edit.h"
#include "editor.h"
#include "error.h"
#include "util/debug.h"
#include "util/xmalloc.h"
#include "window.h"

// NOLINTBEGIN(*-avoid-non-const-global-variables)
static ChangeMergeEnum change_merge;
Expand Down Expand Up @@ -232,7 +234,8 @@ bool undo(View *view)
count++;
}
if (count > 1) {
info_msg("Undid %lu changes", count);
ErrorBuffer *ebuf = view->window->editor->err;
info_msg(ebuf, "Undid %lu changes", count);
}
} else {
reverse_change(view, change);
Expand All @@ -244,12 +247,13 @@ bool undo(View *view)

bool redo(View *view, unsigned long change_id)
{
ErrorBuffer *ebuf = view->window->editor->err;
Change *change = view->buffer->cur_change;
view_reset_preferred_x(view);
if (!change->prev) {
// Don't complain if change_id is 0
if (change_id) {
error_msg("Nothing to redo");
error_msg(ebuf, "Nothing to redo");
}
return false;
}
Expand All @@ -261,14 +265,14 @@ bool redo(View *view, unsigned long change_id)
change_id = nr_prev - 1;
if (nr_prev > 1) {
unsigned long i = change_id + 1;
info_msg("Redoing newest (%lu) of %lu possible changes", i, nr_prev);
info_msg(ebuf, "Redoing newest (%lu) of %lu possible changes", i, nr_prev);
}
} else {
if (--change_id >= nr_prev) {
if (nr_prev == 1) {
return error_msg("There is only 1 possible change to redo");
return error_msg(ebuf, "There is only 1 possible change to redo");
}
return error_msg("There are only %lu possible changes to redo", nr_prev);
return error_msg(ebuf, "There are only %lu possible changes to redo", nr_prev);
}
}

Expand All @@ -284,7 +288,7 @@ bool redo(View *view, unsigned long change_id)
count++;
}
if (count > 1) {
info_msg("Redid %lu changes", count);
info_msg(ebuf, "Redid %lu changes", count);
}
} else {
reverse_change(view, change);
Expand Down
24 changes: 14 additions & 10 deletions src/command/args.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,33 +105,37 @@ ArgParseError do_parse_args(const Command *cmd, CommandArgs *a)
return 0;
}

static bool arg_parse_error_msg(const Command *cmd, const CommandArgs *a, ArgParseError err)
{
static bool arg_parse_error_msg (
struct EditorState *e,
const Command *cmd,
const CommandArgs *a,
ArgParseError err
) {
const char *name = cmd->name;
switch (err) {
case ARGERR_INVALID_OPTION:
return error_msg_for_cmd(name, "Invalid option -%c", a->flags[0]);
return error_msg_for_cmd(e, name, "Invalid option -%c", a->flags[0]);
case ARGERR_TOO_MANY_OPTIONS:
return error_msg_for_cmd(name, "Too many options given");
return error_msg_for_cmd(e, name, "Too many options given");
case ARGERR_OPTION_ARGUMENT_MISSING:
return error_msg_for_cmd(name, "Option -%c requires an argument", a->flags[0]);
return error_msg_for_cmd(e, name, "Option -%c requires an argument", a->flags[0]);
case ARGERR_OPTION_ARGUMENT_NOT_SEPARATE:
return error_msg_for_cmd (
name,
e, name,
"Option -%c must be given separately because it"
" requires an argument",
a->flags[0]
);
case ARGERR_TOO_FEW_ARGUMENTS:
return error_msg_for_cmd (
name,
e, name,
"Too few arguments (got: %zu, minimum: %u)",
a->nr_args,
(unsigned int)cmd->min_args
);
case ARGERR_TOO_MANY_ARGUMENTS:
return error_msg_for_cmd (
name,
e, name,
"Too many arguments (got: %zu, maximum: %u)",
a->nr_args,
(unsigned int)cmd->max_args
Expand All @@ -144,8 +148,8 @@ static bool arg_parse_error_msg(const Command *cmd, const CommandArgs *a, ArgPar
return false;
}

bool parse_args(const Command *cmd, CommandArgs *a)
bool parse_args(struct EditorState *e, const Command *cmd, CommandArgs *a)
{
ArgParseError err = do_parse_args(cmd, a);
return likely(err == ARGERR_NONE) || arg_parse_error_msg(cmd, a, err);
return likely(err == ARGERR_NONE) || arg_parse_error_msg(e, cmd, a, err);
}
4 changes: 3 additions & 1 deletion src/command/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ static inline unsigned int cmdargs_convert_flags (
return val;
}

bool parse_args(const Command *cmd, CommandArgs *a) NONNULL_ARGS WARN_UNUSED_RESULT;
struct EditorState;

bool parse_args(struct EditorState *e, const Command *cmd, CommandArgs *a) NONNULL_ARGS WARN_UNUSED_RESULT;
ArgParseError do_parse_args(const Command *cmd, CommandArgs *a) NONNULL_ARGS WARN_UNUSED_RESULT;

#endif
20 changes: 11 additions & 9 deletions src/command/run.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,25 @@ static bool run_command(CommandRunner *runner, char **av)
{
const CommandSet *cmds = runner->cmds;
const Command *cmd = cmds->lookup(av[0]);
struct EditorState *e = runner->e;

if (!cmd) {
const char *name = av[0];
if (!runner->lookup_alias) {
return error_msg("No such command: %s", name);
return error_msg_for_cmd(e, NULL, "No such command: %s", name);
}

const char *alias_value = runner->lookup_alias(runner->e, name);
const char *alias_value = runner->lookup_alias(e, name);
if (unlikely(!alias_value)) {
return error_msg("No such command or alias: %s", name);
return error_msg_for_cmd(e, NULL, "No such command or alias: %s", name);
}

PointerArray array = PTR_ARRAY_INIT;
CommandParseError err = parse_commands(runner, &array, alias_value);
if (unlikely(err != CMDERR_NONE)) {
const char *err_msg = command_parse_error_to_string(err);
ptr_array_free(&array);
return error_msg("Parsing alias %s: %s", name, err_msg);
return error_msg_for_cmd(e, NULL, "Parsing alias %s: %s", name, err_msg);
}

// Remove NULL
Expand All @@ -51,21 +53,21 @@ static bool run_command(CommandRunner *runner, char **av)
}

if (unlikely(current_config.file && !(cmd->cmdopts & CMDOPT_ALLOW_IN_RC))) {
return error_msg("Command %s not allowed in config file", cmd->name);
return error_msg_for_cmd(e, NULL, "Command %s not allowed in config file", cmd->name);
}

// Record command in macro buffer, if recording (this needs to be done
// before parse_args() mutates the array)
if (runner->allow_recording && runner->cmds->macro_record) {
runner->cmds->macro_record(runner->e, cmd, av + 1);
runner->cmds->macro_record(e, cmd, av + 1);
}

// By default change can't be merged with previous one.
// Any command can override this by calling begin_change() again.
begin_change(CHANGE_MERGE_NONE);

CommandArgs a = cmdargs_new(av + 1);
bool r = likely(parse_args(cmd, &a)) && command_func_call(runner->e, cmd, &a);
bool r = likely(parse_args(e, cmd, &a)) && command_func_call(e, cmd, &a);

end_change();
return r;
Expand All @@ -75,7 +77,7 @@ static bool run_command(CommandRunner *runner, char **av)
static bool run_commands(CommandRunner *runner, const PointerArray *array)
{
if (unlikely(runner->recursion_count > 16)) {
return error_msg("alias recursion limit reached");
return error_msg_for_cmd(runner->e, NULL, "alias recursion limit reached");
}

void **ptrs = array->ptrs;
Expand Down Expand Up @@ -119,7 +121,7 @@ bool handle_command(CommandRunner *runner, const char *cmd)
BUG_ON(runner->recursion_count != 0);
} else {
const char *str = command_parse_error_to_string(err);
error_msg("Command syntax error: %s", str);
error_msg_for_cmd(runner->e, NULL, "Command syntax error: %s", str);
r = false;
}
ptr_array_free(&array);
Expand Down
Loading

0 comments on commit f1ff10b

Please sign in to comment.