Skip to content

Commit

Permalink
Merge pull request #976 from haolian9/inspect_foreign_cmd
Browse files Browse the repository at this point in the history
Fix `:command {cmd}` not recognizing FOREIGN_CMDs.
  • Loading branch information
xaizek committed Dec 30, 2023
2 parents a196fbb + 93fe3be commit 392c4c0
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 7 deletions.
3 changes: 2 additions & 1 deletion AUTHORS
Expand Up @@ -159,7 +159,8 @@ Completion for fish shell by Hoang Nguyen (a.k.a. FollieHiyuki).

Alexandr Keyp (a.k.a. IAmKapuze) contributed improvements for :compare.

高浩亮 (a.k.a. haolian9) enabled mouse handling.
高浩亮 (a.k.a. haolian9) enabled mouse handling and fixed displaying of
foreign commands in `:commands {prefix}`.

Zhipeng Xue fixed a couple of potential NULL dereferences.

Expand Down
3 changes: 3 additions & 0 deletions ChangeLog
Expand Up @@ -167,6 +167,9 @@
Fixed running tests with locales that use comma for decimal point. Thanks
to PRESFIL.

Fixed foreign commands missing from `:commands {prefix}` output. Patch by
高浩亮 (a.k.a. haolian9).

0.13-beta to 0.13 (2023-04-04)

Made "withicase" and "withrcase" affect how files are sorted before
Expand Down
45 changes: 39 additions & 6 deletions src/engine/cmds.c
Expand Up @@ -84,6 +84,8 @@ static int delcommand_cmd(const cmd_info_t *cmd_info);
TSTATIC char ** dispatch_line(const char args[], int *count, char sep,
int regexp, int quotes, int noescaping, int comments, int *last_arg,
int (**positions)[2]);
static int is_builtin_like_cmd(const cmd_t *cmd);
static int is_custom_cmd(const cmd_t *cmd);
static int is_separator(char c, char sep);

void
Expand Down Expand Up @@ -1122,8 +1124,7 @@ vle_cmds_add_user(const char name[], const char body[], const char descr[],
cmd_t *cur = &inner->head;
while(cur->next != NULL && (cmp = strcmp(cur->next->name, name)) < 0)
{
int builtin_like = (cur->next->type == BUILTIN_CMD)
|| (cur->next->type == FOREIGN_CMD);
const int builtin_like = is_builtin_like_cmd(cur->next);
if(has_emark && builtin_like && cur->next->emark &&
strncmp(name, cur->next->name, len - 1) == 0)
{
Expand All @@ -1143,7 +1144,7 @@ vle_cmds_add_user(const char name[], const char body[], const char descr[],
if(cmp == 0)
{
cur = cur->next;
if(cur->type == BUILTIN_CMD || cur->type == FOREIGN_CMD)
if(is_builtin_like_cmd(cur))
return CMDS_ERR_NO_BUILTIN_REDEFINE;
if(!overwrite)
return CMDS_ERR_NEED_BANG;
Expand Down Expand Up @@ -1621,7 +1622,7 @@ vle_cmds_print_udcs(const char beginning[])
void *ptr;
size_t new_size;

if(strncmp(cur->name, beginning, len) != 0 || cur->type != USER_CMD)
if(strncmp(cur->name, beginning, len) != 0 || !is_custom_cmd(cur))
{
cur = cur->next;
continue;
Expand All @@ -1637,20 +1638,52 @@ vle_cmds_print_udcs(const char beginning[])

content_len = strlen(content);
}
new_size = content_len + 1 + strlen(cur->name) + 10 + strlen(cur->cmd) + 1;

const char *detail = "";
if(cur->type == USER_CMD)
{
detail = cur->cmd;
}
else if(cur->type == FOREIGN_CMD)
{
detail = cur->descr;
}
else
{
assert(0 && "Expected user or foreign command type.");
}

new_size = content_len + 1 + strlen(cur->name) + 10 + strlen(detail) + 1;
ptr = realloc(content, new_size);
if(ptr != NULL)
{
content = ptr;
content_len += sprintf(content + content_len, "\n%-*s %s", 10, cur->name,
cur->cmd);
detail);
}

cur = cur->next;
}

return content;
}

/* Checks that a command has all the features of a builtin one. Returns
* non-zero if so. */
static int
is_builtin_like_cmd(const cmd_t *cmd)
{
return (cmd->type == BUILTIN_CMD || cmd->type == FOREIGN_CMD);
}

/* Checks that a command's definition is not part of the code base. Returns
* non-zero if so. */
static int
is_custom_cmd(const cmd_t *cmd)
{
return (cmd->type == USER_CMD || cmd->type == FOREIGN_CMD);
}

char *
vle_cmds_past_arg(const char args[])
{
Expand Down
22 changes: 22 additions & 0 deletions tests/cmds/foreign.c
@@ -1,6 +1,7 @@
#include <stic.h>

#include <stddef.h> /* NULL */
#include <stdlib.h> /* free() */

#include "../../src/engine/cmds.h"
#include "../../src/engine/completion.h"
Expand Down Expand Up @@ -102,6 +103,27 @@ TEST(foreign_command_is_listed_with_udcs)
free_string_array(list, len);
}

TEST(foreign_command_is_printed_with_udcs)
{
cmd_add_t command = {
.name = "foreign",
.abbr = NULL,
.id = -1,
.descr = "descr",
.flags = HAS_RANGE,
.handler = &foreign_cmd,
.min_args = 0,
.max_args = NOT_DEF,
};
assert_success(vle_cmds_add_foreign(&command));

char *desc = vle_cmds_print_udcs("fore");

assert_string_equal("Command -- Action\nforeign descr", desc);

free(desc);
}

static int
foreign_cmd(const cmd_info_t *cmd_info)
{
Expand Down

0 comments on commit 392c4c0

Please sign in to comment.