diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/command.c | 10 | ||||
| -rw-r--r-- | src/command.h | 97 | ||||
| -rw-r--r-- | src/minibuffer.c | 1 |
3 files changed, 100 insertions, 8 deletions
diff --git a/src/command.c b/src/command.c index 93ede01..f8add1b 100644 --- a/src/command.c +++ b/src/command.c @@ -28,14 +28,14 @@ uint32_t hash_command_name(const char *name) { return hash; } -uint32_t register_command(struct commands *commands, struct command *command) { +uint32_t register_command(struct commands *commands, struct command command) { if (commands->ncommands == commands->capacity) { commands->capacity *= 2; commands->commands = realloc( commands->commands, sizeof(struct hashed_command) * commands->capacity); } - uint32_t hash = hash_command_name(command->name); + uint32_t hash = hash_command_name(command.name); commands->commands[commands->ncommands] = (struct hashed_command){.command = command, .hash = hash}; @@ -46,7 +46,7 @@ uint32_t register_command(struct commands *commands, struct command *command) { void register_commands(struct commands *command_list, struct command *commands, uint32_t ncommands) { for (uint32_t ci = 0; ci < ncommands; ++ci) { - register_command(command_list, &commands[ci]); + register_command(command_list, commands[ci]); } } @@ -60,7 +60,7 @@ struct command *lookup_command_by_hash(struct commands *commands, uint32_t hash) { for (uint32_t ci = 0; ci < commands->ncommands; ++ci) { if (commands->commands[ci].hash == hash) { - return commands->commands[ci].command; + return &commands->commands[ci].command; } } @@ -88,7 +88,7 @@ int32_t find_file(struct command_ctx ctx, int argc, const char *argv[]) { pth = argv[0]; ctx.active_window->buffer = buffers_add(ctx.buffers, buffer_from_file((char *)pth)); - minibuffer_echo_timeout(4, "buffer %s loaded", + minibuffer_echo_timeout(4, "buffer \"%s\" loaded", ctx.active_window->buffer->name); } else { minibuffer_prompt(ctx, "find file: "); diff --git a/src/command.h b/src/command.h index 719422e..278a894 100644 --- a/src/command.h +++ b/src/command.h @@ -1,3 +1,6 @@ +/** + * Commands and command registries + */ #include <stdint.h> struct buffer; @@ -23,7 +26,7 @@ struct command { struct hashed_command { uint32_t hash; - struct command *command; + struct command command; }; struct commands { @@ -32,24 +35,112 @@ struct commands { uint32_t capacity; }; +/** + * Create a new command registry. + * + * @param[in] capacity The initial capacity for the registry + */ struct commands command_registry_create(uint32_t capacity); + +/** + * Destroy a command registry. + * + * This will free all memory associated with stored commands. + * @param[in] commands A pointer to a commands structure created by @ref + * command_registry_create(uint32_t) + */ void command_registry_destroy(struct commands *commands); -uint32_t register_command(struct commands *commands, struct command *command); +/** + * Register a new command in the registry @ref commands. + * + * @param[in] commands The registry to insert into + * @param[in] command The command to insert + */ +uint32_t register_command(struct commands *commands, struct command command); + +/** + * Register multiple commands in the registry @ref commands "command_list". + * + * @param[in] command_list The registry to insert into + * @param[in] commands The commands to insert + * @param[in] ncommands Number of commands contained in @ref commands + */ void register_commands(struct commands *command_list, struct command *commands, uint32_t ncommands); +/** + * Execute a command and return the result. + * + * @param[in] command The @ref command to execute + * @param[in] commands A @ref command "command registry" to use for context in + * the executed command. Can for example be used to implement commands that + * execute arbitrary other commands. + * @param[in] active_window A @ref window representing the currently active + * window in the editor. This provides a way to access the current buffer as + * well. + * @param[in] buffers The current list of buffers for context. Can be used for + * example to create a buffer list. + * @param[in] argc Number of arguments to the command. + * @param[in] argv The arguments to the command. + * + * @returns Integer representing the exit status where 0 means success and + * anything else means there was an error. + */ int32_t execute_command(struct command *command, struct commands *commands, struct window *active_window, struct buffers *buffers, int argc, const char *argv[]); +/** + * Hash the name of a command. + * + * @param[in] name The command name + * @returns An integer representing the hash of the name + */ uint32_t hash_command_name(const char *name); +/** + * Lookup a command by name. + * + * @param[in] commands The @ref commands "command registry" to look for the @ref + * command in. + * @param[in] name The name of the command to look for + * @returns A pointer to the command if found, NULL otherwise. + */ struct command *lookup_command(struct commands *commands, const char *name); + +/** + * Lookup a command by hash. + * + * The hash value is expected to have been computed with @ref + * hash_command_name(const char* name). + * + * @param[in] commands The @ref commands "command registry" to look for the @ref + * command in. + * @param[in] hash The hash value for the name of the command to look for + * @returns A pointer to the command if found, NULL otherwise. + */ struct command *lookup_command_by_hash(struct commands *commands, uint32_t hash); -// Common commands +/** + * @defgroup common-commands Implementation of common commands + * @{ + */ + +/** + * Find and visit a file in the current window. + */ int32_t find_file(struct command_ctx ctx, int argc, const char *argv[]); + +/** + * Run a command interactively from the minibuffer. + */ int32_t run_interactive(struct command_ctx ctx, int argc, const char *argv[]); + +/** + * Switch to another buffer in the currently active window + */ int32_t switch_buffer(struct command_ctx ctx, int argc, const char *argv[]); + +/**@}*/ diff --git a/src/minibuffer.c b/src/minibuffer.c index a424af8..cf09e73 100644 --- a/src/minibuffer.c +++ b/src/minibuffer.c @@ -31,6 +31,7 @@ int32_t execute(struct command_ctx ctx, int argc, const char *argv[]) { struct text_chunk line = buffer_get_line(g_minibuffer.buffer, 0); char *l = (char *)malloc(line.nbytes + 1); memcpy(l, line.text, line.nbytes); + l[line.nbytes] = '\0'; // split on ' ' const char *argv[128] = {l}; |
