diff --git a/ce/edit.c b/ce/edit.c index d629ff9..c81c068 100644 --- a/ce/edit.c +++ b/ce/edit.c @@ -48,8 +48,9 @@ static const char* string_modes[] = { [EDIT_MODE_COMAMND] = "Command", }; -struct edit_select { - struct list_node_link* lines; +struct edit_command_buf { + char buffer[256]; + size_t len; }; struct editor { @@ -59,10 +60,10 @@ struct editor { size_t col_offset; size_t row_offset; size_t total_lines; - struct edit_select select; int mode; const char* volume; const char* path; + struct edit_command_buf cmdbuf; }; static struct editor editor; @@ -218,16 +219,17 @@ void edit_start (const char* volume, const char* file_path, const char* text, si 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:%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); + char* command_str = (editor.mode == EDIT_MODE_COMAMND) ? editor.cmdbuf.buffer : ""; + + bbptr += snprintf ( + bbptr, (backbuffer_max - (bbptr - backbuffer)), + ANSIQ_DYN_CUR_SET ANSIQ_SCR_CLR_LINE STATUS_LINE_STYLE + "%*s" ANSIQ_DYN_CUR_SET + " %s | %s:%s | %zu:%zu | Mode: %s" ANSIQ_GR_RESET ANSIQ_DYN_CUR_SET ANSIQ_CUR_VISIBLE, + (int)rows, 1, (int)cols, "", (int)rows, 1, command_str, 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); @@ -299,6 +301,32 @@ void edit_start (const char* volume, const char* file_path, const char* text, si editor.cursor.line++; editor.current_line = new_line; editor.total_lines++; + } else if (editor.mode == EDIT_MODE_COMAMND) { + if (strcmp (editor.cmdbuf.buffer, "q") == 0) { + edit_run = false; + editor.mode = EDIT_MODE_NORMAL; + } else if (strcmp (editor.cmdbuf.buffer, "w") == 0) { + 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; + } else { + editor.mode = EDIT_MODE_NORMAL; + } } } break; case KB_DELETE: @@ -358,41 +386,23 @@ void edit_start (const char* volume, const char* file_path, const char* text, si editor.cursor.col = gapbuffer_length (&editor.current_line->gb); break; case KB_ESCAPE: - editor.mode = EDIT_MODE_NORMAL; + if (editor.mode == EDIT_MODE_COMAMND) { + editor.mode = EDIT_MODE_NORMAL; + editor.cmdbuf.len = 0; + memset (editor.cmdbuf.buffer, 0, sizeof (editor.cmdbuf.buffer)); + } break; case KB_CTRL ('X'): editor.mode = EDIT_MODE_COMAMND; + editor.cmdbuf.len = 0; + memset (editor.cmdbuf.buffer, 0, sizeof (editor.cmdbuf.buffer)); + break; default: if (isprint (ch)) { if (editor.mode == EDIT_MODE_COMAMND) { - switch (ch) { - case 'q': - 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; + if (editor.cmdbuf.len < sizeof (editor.cmdbuf.buffer) - 1) { + editor.cmdbuf.buffer[editor.cmdbuf.len++] = ch; + editor.cmdbuf.buffer[editor.cmdbuf.len] = '\0'; } } else if (editor.mode == EDIT_MODE_NORMAL) { gapbuffer_move (&editor.current_line->gb, editor.cursor.col);