summaryrefslogtreecommitdiff
path: root/editor.c
diff options
context:
space:
mode:
authorcflip <cflip@cflip.net>2023-01-31 13:31:21 -0700
committercflip <cflip@cflip.net>2023-01-31 14:00:17 -0700
commit1b8261071ef3ad3b8d1c2db6e712b0e590c563b3 (patch)
treeca7e8528b37ccd1d7748b5c70582239b506f8b6a /editor.c
parent264c4186ed8e3d2e3275a6514c8714903036cb26 (diff)
Reimplement editor_prompt and 'Save as' using the new prompt modeHEADmaster
Now there there is a new way to prompt for text, we can reimplement the long abandoned editor_prompt function and use it to allow saving to new files. The new editor_prompt function takes a callback that will be called after the user presses enter on the prompt.
Diffstat (limited to 'editor.c')
-rw-r--r--editor.c67
1 files changed, 45 insertions, 22 deletions
diff --git a/editor.c b/editor.c
index 8e0f6a6..16f8152 100644
--- a/editor.c
+++ b/editor.c
@@ -7,6 +7,7 @@
#include <string.h>
#include <unistd.h>
+#include "file.h"
#include "input.h"
#include "syntax.h"
#include "window.h"
@@ -49,22 +50,56 @@ void editor_set_status_message(struct editor_state* editor, const char* format,
editor->status_message_time = time(NULL);
}
-char* editor_prompt(struct editor_state* editor, char* prompt, void (*callback)(struct editor_state*, char*, int))
+static prompt_callback_t saved_prompt_callback;
+
+void editor_prompt(struct editor_state* editor, char* prompt, prompt_callback_t callback)
{
- /* TODO: The previous implementation of this function relied on reading
- * input from the terminal, but now that we get input through window events
- * it's no longer possible to sit in an infinite loop waiting for keys here.
- */
- printf("TODO: editor_prompt unimplemented\n");
- return NULL;
+ saved_prompt_callback = callback;
+ editor_set_mode(editor, EDITOR_MODE_PROMPT);
}
void editor_run_command(struct editor_state *editor)
{
- /* TODO: Do something here */
+ textbuf_append(&editor->cmdline, "\0", 1);
+
+ if (saved_prompt_callback) {
+ saved_prompt_callback(editor, editor->cmdline.buffer, editor->cmdline.length);
+ }
+
+ /* TODO: Parse and run a command by its name */
editor_set_mode(editor, EDITOR_MODE_NORMAL);
}
+static void save_callback(struct editor_state *editor, char *filename, size_t namelen)
+{
+ if (filename == NULL)
+ return;
+
+ editor->filename = malloc(namelen);
+ memcpy(editor->filename, filename, namelen);
+
+ editor_select_syntax_highlight(editor);
+ window_set_filename(editor->filename);
+
+ editor_try_save(editor);
+}
+
+void editor_try_save(struct editor_state *editor)
+{
+ /*
+ * If there is no filename, set a callback to change the filename and
+ * come back here later once it is set.
+ */
+ if (editor->filename == NULL) {
+ editor_prompt(editor, "Save as: ", save_callback);
+ return;
+ }
+
+ int saverr = file_save_current_file(editor);
+ if (saverr != 0)
+ editor_set_status_message(editor, "Failed to save file: %s", strerror(saverr));
+}
+
void editor_try_quit(struct editor_state *editor)
{
if (editor->dirty && quit_message_time == 0) {
@@ -191,6 +226,7 @@ void editor_set_mode(struct editor_state *editor, enum editor_mode mode)
/* Clear the command line if we are leaving prompt mode. */
if (last_mode == EDITOR_MODE_PROMPT) {
textbuf_clear(&editor->cmdline);
+ saved_prompt_callback = NULL;
}
/* Ignore the extra first letter if we are entering a typing mode. */
@@ -263,20 +299,7 @@ static void editor_find_callback(struct editor_state* editor, char* query, int k
void editor_find(struct editor_state* editor)
{
- int saved_cursor_x = editor->cursor_x;
- int saved_cursor_y = editor->cursor_y;
- int saved_col_offset = editor->col_offset;
- int saved_line_offset = editor->line_offset;
-
- char* query = editor_prompt(editor, "Search: %s (Use Esc/Arrows/Enter)", editor_find_callback);
- if (query) {
- free(query);
- } else {
- editor->cursor_x = saved_cursor_x;
- editor->cursor_y = saved_cursor_y;
- editor->col_offset = saved_col_offset;
- editor->line_offset = saved_line_offset;
- }
+ /* TODO: Unimplemented */
}
void editor_scroll(struct editor_state* editor)