diff --git a/ce/edit.c b/ce/edit.c index 0d540d4..d629ff9 100644 --- a/ce/edit.c +++ b/ce/edit.c @@ -5,9 +5,11 @@ #include "self.h" #include "walloc.h" #include +#include #include #include #include +#include #include #include #include @@ -59,6 +61,8 @@ struct editor { size_t total_lines; struct edit_select select; int mode; + const char* volume; + const char* path; }; static struct editor editor; @@ -124,7 +128,10 @@ static size_t count_digits (size_t n) { 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); 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++; } - bbptr += snprintf (bbptr, (backbuffer_max - (bbptr - backbuffer)), - ANSIQ_DYN_CUR_SET ANSIQ_SCR_CLR_LINE STATUS_LINE_STYLE - "%*s" ANSIQ_DYN_CUR_SET - " Editing %s; line %zu col %zu; Mode: %s" ANSIQ_GR_RESET ANSIQ_DYN_CUR_SET - ANSIQ_CUR_VISIBLE, - (int)rows, 1, (int)cols, "", (int)rows, 1, file_path, editor.cursor.line + 1, - editor.cursor.col + 1, string_modes[editor.mode], - (int)(editor.cursor.line - editor.row_offset) + 1, - (int)(editor.cursor.col - editor.col_offset) + 1 + (int)gutter_width); + bbptr += + snprintf (bbptr, (backbuffer_max - (bbptr - backbuffer)), + ANSIQ_DYN_CUR_SET ANSIQ_SCR_CLR_LINE STATUS_LINE_STYLE + "%*s" ANSIQ_DYN_CUR_SET + " Editing %s:%s; line %zu col %zu; Mode: %s" ANSIQ_GR_RESET ANSIQ_DYN_CUR_SET + ANSIQ_CUR_VISIBLE, + (int)rows, 1, (int)cols, "", (int)rows, 1, volume, file_path, + editor.cursor.line + 1, editor.cursor.col + 1, string_modes[editor.mode], + (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); @@ -362,6 +370,26 @@ void edit_start (const char* file_path, const char* text, size_t text_len) { edit_run = false; editor.mode = EDIT_MODE_NORMAL; 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: editor.mode = EDIT_MODE_NORMAL; break; diff --git a/ce/edit.h b/ce/edit.h index ff6ade8..fde0d37 100644 --- a/ce/edit.h +++ b/ce/edit.h @@ -3,6 +3,6 @@ #include -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 diff --git a/ce/interp.c b/ce/interp.c index 5834bbb..d9bbd18 100644 --- a/ce/interp.c +++ b/ce/interp.c @@ -265,9 +265,9 @@ static void edit (struct context* context, const char* path_string) { return; } - edit_start (path_string, str, desc.size); - volume_close (); + + edit_start (volume, path, str, desc.size); } static void quit1 (struct context* context) {