summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/dged/buffer.h6
-rw-r--r--src/dged/buffers.h12
-rw-r--r--src/dged/command.h10
-rw-r--r--src/dged/path.h22
-rw-r--r--src/dged/reactor.h3
-rw-r--r--src/dged/settings-parse.h2
-rw-r--r--src/dged/settings.h33
-rw-r--r--src/main/cmds.c49
8 files changed, 66 insertions, 71 deletions
diff --git a/src/dged/buffer.h b/src/dged/buffer.h
index 417ca03..dad6ef1 100644
--- a/src/dged/buffer.h
+++ b/src/dged/buffer.h
@@ -221,6 +221,6 @@ void buffer_to_file(struct buffer *buffer);
void buffer_write_to(struct buffer *buffer, const char *filename);
void buffer_reload(struct buffer *buffer);
-void buffer_update(struct buffer_view *view, uint32_t window_id, uint32_t width, uint32_t height,
- struct command_list *commands, uint64_t frame_time,
- uint32_t *relline, uint32_t *relcol);
+void buffer_update(struct buffer_view *view, uint32_t window_id, uint32_t width,
+ uint32_t height, struct command_list *commands,
+ uint64_t frame_time, uint32_t *relline, uint32_t *relcol);
diff --git a/src/dged/buffers.h b/src/dged/buffers.h
index cb37fcb..f4c0a37 100644
--- a/src/dged/buffers.h
+++ b/src/dged/buffers.h
@@ -20,11 +20,15 @@ void buffers_init(struct buffers *buffers, uint32_t initial_capacity);
struct buffer *buffers_add(struct buffers *buffers, struct buffer buffer);
struct buffer *buffers_find(struct buffers *buffers, const char *name);
-struct buffer *buffers_find_by_filename(struct buffers *buffers, const char *path);
+struct buffer *buffers_find_by_filename(struct buffers *buffers,
+ const char *path);
-uint32_t buffers_add_add_hook(struct buffers *buffers, buffers_hook_cb callback, void *userdata);
-uint32_t buffers_add_remove_hook(struct buffers *buffers, buffers_hook_cb callback, void *userdata);
+uint32_t buffers_add_add_hook(struct buffers *buffers, buffers_hook_cb callback,
+ void *userdata);
+uint32_t buffers_add_remove_hook(struct buffers *buffers,
+ buffers_hook_cb callback, void *userdata);
-void buffers_for_each(struct buffers *buffers, buffers_hook_cb callback, void *userdata);
+void buffers_for_each(struct buffers *buffers, buffers_hook_cb callback,
+ void *userdata);
void buffers_destroy(struct buffers *buffers);
diff --git a/src/dged/command.h b/src/dged/command.h
index bbc57f2..956c10a 100644
--- a/src/dged/command.h
+++ b/src/dged/command.h
@@ -77,6 +77,16 @@ struct command {
};
/**
+ * Convenience macro for creating a command from a function.
+ */
+#define COMMAND_FN(name_, command_name, function, userdata_) \
+ static struct command command_name##_command = { \
+ .fn = function, \
+ .name = #name_, \
+ .userdata = userdata_, \
+ };
+
+/**
* A command registry
*/
HASHMAP_ENTRY_TYPE(command_entry, struct command);
diff --git a/src/dged/path.h b/src/dged/path.h
index d74f723..6e11d6a 100644
--- a/src/dged/path.h
+++ b/src/dged/path.h
@@ -9,19 +9,19 @@ static char *expanduser(const char *path) {
if (tilde_pos != NULL) {
char *home = getenv("HOME");
if (home != NULL) {
- // allocate a new string based with the new len
- size_t home_len = strlen(home);
- size_t path_len = strlen(path);
- size_t total_len = path_len + home_len;
- res = malloc(total_len);
- size_t initial_len = tilde_pos - path;
- strncpy(res, path, initial_len);
+ // allocate a new string based with the new len
+ size_t home_len = strlen(home);
+ size_t path_len = strlen(path);
+ size_t total_len = path_len + home_len;
+ res = malloc(total_len);
+ size_t initial_len = tilde_pos - path;
+ strncpy(res, path, initial_len);
- strncpy(res + initial_len, home, home_len);
+ strncpy(res + initial_len, home, home_len);
- size_t rest_len = path_len - initial_len - 1;
- strncpy(res + initial_len + home_len, path + initial_len + 1, rest_len);
- res[total_len-1] = '\0';
+ size_t rest_len = path_len - initial_len - 1;
+ strncpy(res + initial_len + home_len, path + initial_len + 1, rest_len);
+ res[total_len - 1] = '\0';
}
}
diff --git a/src/dged/reactor.h b/src/dged/reactor.h
index 1fa014d..39a94a9 100644
--- a/src/dged/reactor.h
+++ b/src/dged/reactor.h
@@ -24,7 +24,8 @@ void reactor_update(struct reactor *reactor);
bool reactor_poll_event(struct reactor *reactor, uint32_t ev_id);
uint32_t reactor_register_interest(struct reactor *reactor, int fd,
enum interest interest);
-uint32_t reactor_watch_file(struct reactor *reactor, const char *path, uint32_t mask);
+uint32_t reactor_watch_file(struct reactor *reactor, const char *path,
+ uint32_t mask);
void reactor_unwatch_file(struct reactor *reactor, uint32_t id);
bool reactor_next_file_event(struct reactor *reactor, struct file_event *out);
void reactor_unregister_interest(struct reactor *reactor, uint32_t ev_id);
diff --git a/src/dged/settings-parse.h b/src/dged/settings-parse.h
index 63a0f1e..0a782eb 100644
--- a/src/dged/settings-parse.h
+++ b/src/dged/settings-parse.h
@@ -1,6 +1,6 @@
#include <stdbool.h>
-#include <stdint.h>
#include <stddef.h>
+#include <stdint.h>
#include "vec.h"
diff --git a/src/dged/settings.h b/src/dged/settings.h
index 80f6b0f..637844b 100644
--- a/src/dged/settings.h
+++ b/src/dged/settings.h
@@ -137,23 +137,24 @@ void setting_set_value(struct setting *setting, struct setting_value val);
void setting_to_string(struct setting *setting, char *buf, size_t n);
/**
- * Parse settings from a string in TOML format.
- *
- * @param toml Pointer to a NULL-terminated string containing TOML settings.
- * @param errmsgs Pointer to a string array where error messages will be placed. These
- * messages must be freed after use.
- * @returns 0 on success, n > 0 where n denotes the number of error messages in
- * @ref errmsgs
- */
+ * Parse settings from a string in TOML format.
+ *
+ * @param toml Pointer to a NULL-terminated string containing TOML settings.
+ * @param errmsgs Pointer to a string array where error messages will be placed.
+ * These messages must be freed after use.
+ * @returns 0 on success, n > 0 where n denotes the number of error messages in
+ * @ref errmsgs
+ */
int32_t settings_from_string(const char *toml, char **errmsgs[]);
/**
- * Parse settings from a file in TOML format.
- *
- * @param toml Pointer to a NULL-terminated string describing a path to a TOML file with settings.
- * @param errmsgs Pointer to a string array where error messages will be placed. These
- * messages must be freed after use.
- * @returns 0 on success, n > 0 where n denotes the number of error messages in
- * @ref errmsgs
- */
+ * Parse settings from a file in TOML format.
+ *
+ * @param toml Pointer to a NULL-terminated string describing a path to a TOML
+ * file with settings.
+ * @param errmsgs Pointer to a string array where error messages will be placed.
+ * These messages must be freed after use.
+ * @returns 0 on success, n > 0 where n denotes the number of error messages in
+ * @ref errmsgs
+ */
int32_t settings_from_file(const char *path, char **errmsgs[]);
diff --git a/src/main/cmds.c b/src/main/cmds.c
index c7dc894..82fddf0 100644
--- a/src/main/cmds.c
+++ b/src/main/cmds.c
@@ -115,12 +115,11 @@ int32_t do_switch_buffer(struct command_ctx ctx, int argc, const char *argv[]) {
}
}
-static struct command do_switch_buffer_cmd = {.fn = do_switch_buffer,
- .name = "do-switch-buffer"};
+COMMAND_FN("do-switch-buffer", do_switch_buffer, do_switch_buffer, NULL);
int32_t switch_buffer(struct command_ctx ctx, int argc, const char *argv[]) {
if (argc == 0) {
- ctx.self = &do_switch_buffer_cmd;
+ ctx.self = &do_switch_buffer_command;
if (window_has_prev_buffer(ctx.active_window)) {
return minibuffer_prompt(ctx, "buffer (default %s): ",
window_prev_buffer(ctx.active_window)->name);
@@ -129,8 +128,8 @@ int32_t switch_buffer(struct command_ctx ctx, int argc, const char *argv[]) {
}
}
- return execute_command(&do_switch_buffer_cmd, ctx.commands, ctx.active_window,
- ctx.buffers, argc, argv);
+ return execute_command(&do_switch_buffer_command, ctx.commands,
+ ctx.active_window, ctx.buffers, argc, argv);
}
static char *g_last_search = NULL;
@@ -227,17 +226,11 @@ int32_t search_interactive(struct command_ctx ctx, int argc,
static bool search_dir_backward = true;
static bool search_dir_forward = false;
-static struct command search_forward_command = {
- .fn = search_interactive,
- .name = "search-forward",
- .userdata = &search_dir_forward,
-};
-static struct command search_backward_command = {
- .fn = search_interactive,
- .name = "search-backward",
- .userdata = &search_dir_backward,
-};
+COMMAND_FN("search-forward", search_forward, search_interactive,
+ &search_dir_forward);
+COMMAND_FN("search-backward", search_backward, search_interactive,
+ &search_dir_backward);
int32_t find(struct command_ctx ctx, int argc, const char *argv[]) {
bool reverse = strcmp((char *)ctx.userdata, "backward") == 0;
@@ -258,7 +251,6 @@ int32_t find(struct command_ctx ctx, int argc, const char *argv[]) {
}
int32_t timers(struct command_ctx ctx, int argc, const char *argv[]) {
-
struct buffer *b = buffers_add(ctx.buffers, buffer_create("timers"));
buffer_set_readonly(b, true);
struct window *new_window_a, *new_window_b;
@@ -296,18 +288,12 @@ int32_t buflist_visit_cmd(struct command_ctx ctx, int argc,
if (end != NULL) {
uint32_t len = end - (char *)text.text;
char *bufname = (char *)malloc(len + 1);
- memcpy(bufname, text.text, len);
- bufname[len] = '\0';
+ strncpy(bufname, text.text, len);
struct buffer *target = buffers_find(ctx.buffers, bufname);
free(bufname);
if (target != NULL) {
- struct window *tgt_window = window_find_by_buffer(target);
- if (tgt_window != NULL) {
- windows_set_active(tgt_window);
- } else {
- window_set_buffer(w, target);
- }
+ window_set_buffer(w, target);
}
}
return 0;
@@ -334,18 +320,13 @@ int32_t buflist_refresh_cmd(struct command_ctx ctx, int argc,
}
int32_t buffer_list(struct command_ctx ctx, int argc, const char *argv[]) {
- struct buffer *b = buffers_find(ctx.buffers, "buffers");
+ struct buffer *b = buffers_find(ctx.buffers, "*buffers*");
if (b == NULL) {
- b = buffers_add(ctx.buffers, buffer_create("buffers"));
+ b = buffers_add(ctx.buffers, buffer_create("*buffers*"));
}
- struct window *w = window_find_by_buffer(b);
- if (w == NULL) {
- struct window *new_window_a;
- window_split(ctx.active_window, &new_window_a, &w);
-
- window_set_buffer(w, b);
- }
+ struct window *w = ctx.active_window;
+ window_set_buffer(ctx.active_window, b);
buflist_refresh(ctx.buffers, window_buffer_view(w));
@@ -377,7 +358,6 @@ int32_t buffer_list(struct command_ctx ctx, int argc, const char *argv[]) {
void register_global_commands(struct commands *commands,
void (*terminate_cb)()) {
-
struct command global_commands[] = {
{.name = "find-file", .fn = find_file},
{.name = "write-file", .fn = write_file},
@@ -460,7 +440,6 @@ static int32_t buffer_goto_line(struct command_ctx ctx, int argc,
}
void register_buffer_commands(struct commands *commands) {
-
static struct command buffer_commands[] = {
{.name = "kill-line", .fn = buffer_kill_line_cmd},
{.name = "delete-word", .fn = buffer_forward_delete_word_cmd},