CE edit w command

This commit is contained in:
2026-03-09 01:59:55 +01:00
parent a352e9c501
commit 3148e0e367
3 changed files with 41 additions and 13 deletions

View File

@@ -5,9 +5,11 @@
#include "self.h" #include "self.h"
#include "walloc.h" #include "walloc.h"
#include <arena.h> #include <arena.h>
#include <filewriter.h>
#include <kb.h> #include <kb.h>
#include <liballoc.h> #include <liballoc.h>
#include <list.h> #include <list.h>
#include <path.h>
#include <printf.h> #include <printf.h>
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
@@ -59,6 +61,8 @@ struct editor {
size_t total_lines; size_t total_lines;
struct edit_select select; struct edit_select select;
int mode; int mode;
const char* volume;
const char* path;
}; };
static struct editor editor; static struct editor editor;
@@ -124,7 +128,10 @@ static size_t count_digits (size_t n) {
return count; return count;
} }
void edit_start (const char* file_path, const char* text, size_t text_len) { void edit_start (const char* volume, const char* file_path, const char* text, size_t text_len) {
editor.volume = volume;
editor.path = file_path;
mprintf (ANSIQ_SCR_SAVE); mprintf (ANSIQ_SCR_SAVE);
prepare_lines (text, text_len); prepare_lines (text, text_len);
@@ -211,15 +218,16 @@ void edit_start (const char* file_path, const char* text, size_t text_len) {
current_idx++; current_idx++;
} }
bbptr += snprintf (bbptr, (backbuffer_max - (bbptr - backbuffer)), bbptr +=
ANSIQ_DYN_CUR_SET ANSIQ_SCR_CLR_LINE STATUS_LINE_STYLE snprintf (bbptr, (backbuffer_max - (bbptr - backbuffer)),
"%*s" ANSIQ_DYN_CUR_SET ANSIQ_DYN_CUR_SET ANSIQ_SCR_CLR_LINE STATUS_LINE_STYLE
" Editing %s; line %zu col %zu; Mode: %s" ANSIQ_GR_RESET ANSIQ_DYN_CUR_SET "%*s" ANSIQ_DYN_CUR_SET
ANSIQ_CUR_VISIBLE, " Editing %s:%s; line %zu col %zu; Mode: %s" ANSIQ_GR_RESET ANSIQ_DYN_CUR_SET
(int)rows, 1, (int)cols, "", (int)rows, 1, file_path, editor.cursor.line + 1, ANSIQ_CUR_VISIBLE,
editor.cursor.col + 1, string_modes[editor.mode], (int)rows, 1, (int)cols, "", (int)rows, 1, volume, file_path,
(int)(editor.cursor.line - editor.row_offset) + 1, editor.cursor.line + 1, editor.cursor.col + 1, string_modes[editor.mode],
(int)(editor.cursor.col - editor.col_offset) + 1 + (int)gutter_width); (int)(editor.cursor.line - editor.row_offset) + 1,
(int)(editor.cursor.col - editor.col_offset) + 1 + (int)gutter_width);
mail_send (e_pgid, backbuffer, bbptr - backbuffer); mail_send (e_pgid, backbuffer, bbptr - backbuffer);
@@ -362,6 +370,26 @@ void edit_start (const char* file_path, const char* text, size_t text_len) {
edit_run = false; edit_run = false;
editor.mode = EDIT_MODE_NORMAL; editor.mode = EDIT_MODE_NORMAL;
break; break;
case 'w': {
struct filewriter fw;
if (!(filewriter_init (&fw, editor.volume, editor.path, FW_CREATE_FILE | FW_APPEND) <
0)) {
struct list_node_link *line_link, *tmp_line_link;
list_foreach (editor.lines, line_link, tmp_line_link) {
struct edit_line* line = list_entry (line_link, struct edit_line, lines_link);
filewriter_write (&fw, (uint8_t*)line->gb.buffer, line->gb.gap_start);
filewriter_write (&fw, (uint8_t*)line->gb.buffer + line->gb.gap_end,
line->gb.size - line->gb.gap_end);
if (line->lines_link.next != NULL)
filewriter_write (&fw, (uint8_t*)"\n", 1);
}
filewriter_fini (&fw);
}
editor.mode = EDIT_MODE_NORMAL;
} break;
default: default:
editor.mode = EDIT_MODE_NORMAL; editor.mode = EDIT_MODE_NORMAL;
break; break;

View File

@@ -3,6 +3,6 @@
#include <stddef.h> #include <stddef.h>
void edit_start (const char* path, const char* text, size_t text_len); void edit_start (const char* volume, const char* path, const char* text, size_t text_len);
#endif // _EDIT_H #endif // _EDIT_H

View File

@@ -265,9 +265,9 @@ static void edit (struct context* context, const char* path_string) {
return; return;
} }
edit_start (path_string, str, desc.size);
volume_close (); volume_close ();
edit_start (volume, path, str, desc.size);
} }
static void quit1 (struct context* context) { static void quit1 (struct context* context) {