Skip to content

Commit

Permalink
vis: do not lazy allocate :-commands
Browse files Browse the repository at this point in the history
The built in commands should always be available.
  • Loading branch information
martanne committed Apr 21, 2016
1 parent 793f1e2 commit 59b8834
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
20 changes: 11 additions & 9 deletions sam.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,17 @@ static const CommandDef cmds[] = {
static const CommandDef cmddef_select =
{ { "s" }, 0, NULL, cmd_select };

bool sam_init(Vis *vis) {
if (!(vis->cmds = map_new()))
return false;
bool ret = true;
for (const CommandDef *cmd = cmds; cmd && cmd->name[0]; cmd++) {
for (const char *const *name = cmd->name; *name; name++)
ret &= map_put(vis->cmds, *name, cmd);
}
return ret;
}

const char *sam_error(enum SamError err) {
static const char *error_msg[] = {
[SAM_ERR_OK] = "Success",
Expand Down Expand Up @@ -474,15 +485,6 @@ static void command_free(Command *cmd) {
}

static const CommandDef *command_lookup(Vis *vis, const char *name) {
if (!vis->cmds) {
if (!(vis->cmds = map_new()))
return NULL;

for (const CommandDef *cmd = cmds; cmd && cmd->name[0]; cmd++) {
for (const char *const *name = cmd->name; *name; name++)
map_put(vis->cmds, *name, cmd);
}
}
return map_closest(vis->cmds, name);
}

Expand Down
3 changes: 2 additions & 1 deletion sam.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ enum SamError {
SAM_ERR_EXECUTE,
};

enum SamError sam_cmd(Vis *vis, const char *cmd);
bool sam_init(Vis*);
enum SamError sam_cmd(Vis*, const char *cmd);
const char *sam_error(enum SamError);

#endif
2 changes: 2 additions & 0 deletions vis.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ Vis *vis_new(Ui *ui, VisEvent *event) {
goto err;
if (!(vis->keymap = map_new()))
goto err;
if (!sam_init(vis))
goto err;
vis->mode_prev = vis->mode = &vis_modes[VIS_MODE_NORMAL];
vis->event = event;
if (event && event->vis_init)
Expand Down

0 comments on commit 59b8834

Please sign in to comment.