CE edit w command
This commit is contained in:
38
ce/edit.c
38
ce/edit.c
@@ -5,9 +5,11 @@
|
||||
#include "self.h"
|
||||
#include "walloc.h"
|
||||
#include <arena.h>
|
||||
#include <filewriter.h>
|
||||
#include <kb.h>
|
||||
#include <liballoc.h>
|
||||
#include <list.h>
|
||||
#include <path.h>
|
||||
#include <printf.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
@@ -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,13 +218,14 @@ void edit_start (const char* file_path, const char* text, size_t text_len) {
|
||||
current_idx++;
|
||||
}
|
||||
|
||||
bbptr += snprintf (bbptr, (backbuffer_max - (bbptr - backbuffer)),
|
||||
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
|
||||
" 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, file_path, editor.cursor.line + 1,
|
||||
editor.cursor.col + 1, string_modes[editor.mode],
|
||||
(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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -3,6 +3,6 @@
|
||||
|
||||
#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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user