From f059c3394059254b35f9a9b1db3dd6a9ab97f3b4 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sun, 4 Jan 2026 00:33:02 +0100 Subject: [PATCH] BasicSaveBuffer and SaveAll improvements --- src/backup/todo.txt | 8 ++++++++ src/text_editor/buffer.cpp | 26 +++++++++++++++----------- src/text_editor/commands.cpp | 16 +++++++++++----- src/text_editor/process.cpp | 12 +++++++++--- src/text_editor/window_command.cpp | 2 +- 5 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/backup/todo.txt b/src/backup/todo.txt index 4ef25d6..3fb84a3 100644 --- a/src/backup/todo.txt +++ b/src/backup/todo.txt @@ -1,7 +1,15 @@ ! What precise workflow do I need for me to be viable to use this? ! From a user (novice) point of view, how does it look like? +Other: +- Try out syncthing and setup the synchronized notes, maybe other things +- Write some gmail rules to forward emails from there to my proton account + - Maybe I should try out an email aggregator thing like Mozzila thunderbolt, then I will have all the data and stuff locally +- Backup all the messages on phone using a tool +- Move music and other things from phone to proton and categorize + Use session 4 +- Option for inserting tab instead of space - Add <> <> template strings to Open (Then remove SEtWorkdirhere) - :Set Filename to name current buffer ??? :O and others like that!! - :Close Fuzzy search exact match doesn't match with Close diff --git a/src/text_editor/buffer.cpp b/src/text_editor/buffer.cpp index f75f581..3c41ce5 100644 --- a/src/text_editor/buffer.cpp +++ b/src/text_editor/buffer.cpp @@ -1517,6 +1517,20 @@ void ReopenBuffer(Buffer *buffer) { buffer->dirty = false; } +void BasicSaveBuffer(Buffer *buffer) { + Scratch scratch; + String string = AllocCharString(scratch, buffer); + bool success = WriteFile(buffer->name, string); + + if (success) { + buffer->file_mod_time = GetFileModTime(buffer->name); + buffer->dirty = false; + buffer->temp = false; + } else { + ReportWarningf("Failed to save file with name: %S", buffer->name); + } +} + void SaveBuffer(Buffer *buffer) { bool formatted = false; if (FormatCode) { @@ -1537,15 +1551,5 @@ void SaveBuffer(Buffer *buffer) { TrimWhitespace(buffer); } - Scratch scratch; - String string = AllocCharString(scratch, buffer); - bool success = WriteFile(buffer->name, string); - - if (success) { - buffer->file_mod_time = GetFileModTime(buffer->name); - buffer->dirty = false; - buffer->temp = false; - } else { - ReportWarningf("Failed to save file with name: %S", buffer->name); - } + BasicSaveBuffer(buffer); } diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index e87775f..a2e9ffb 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -318,9 +318,15 @@ void ConvertLineEndingsToLF(Buffer *buffer, bool trim_lines_with_caret = false) void ApplyFormattingTool(Buffer *buffer, String tool) { Scratch scratch; - String string = AllocCharString(scratch, buffer); - Buffer *temp_buffer = ExecAndWait(scratch, tool, GetDir(buffer), string); - ReplaceWithoutMovingCarets(buffer, GetRange(buffer), {temp_buffer->str, temp_buffer->len}); + String string = AllocCharString(scratch, buffer); + ExecResult exec_result = ExecAndWait(scratch, tool, GetDir(buffer), string); + String16 string16 = {exec_result.buffer->str, exec_result.buffer->len}; + if (exec_result.exit_code == 0) { + ReplaceWithoutMovingCarets(buffer, GetRange(buffer), string16); + } else { + View *view = GetView(NullViewID); + Append(view, string16, true); + } } void GotoNextInList(Window *window, Int line_offset = 1) { @@ -448,8 +454,8 @@ BSet ExecBuild(String cmd) { void CMD_SaveAll() { For(Buffers) { // NOTE: file_mod_time is only set when buffer got read or written to disk already so should be saved - if (it->file_mod_time) { - SaveBuffer(it); + if (it->file_mod_time && it->dirty) { + BasicSaveBuffer(it); } } } RegisterCommand(CMD_SaveAll, "ctrl-shift-s"); diff --git a/src/text_editor/process.cpp b/src/text_editor/process.cpp index 5b60e7b..7cbbb4d 100644 --- a/src/text_editor/process.cpp +++ b/src/text_editor/process.cpp @@ -36,17 +36,23 @@ void Exec(ViewID view, bool scroll_to_end, String16 cmd16, String working_dir) { Exec(view, scroll_to_end, cmd, working_dir); } -Buffer *ExecAndWait(Allocator allocator, String cmd, String working_dir, String stdin_string = {}) { +struct ExecResult { + Buffer *buffer; + int exit_code; +}; + +ExecResult ExecAndWait(Allocator allocator, String cmd, String working_dir, String stdin_string = {}) { ReportDebugf("ExecAndWait cmd = %S working_dir = %S stdin_string = %S", cmd, working_dir, stdin_string); Buffer *scratch_buff = CreateScratchBuffer(allocator, 4096 * 4); - for (Process process = SpawnProcess(cmd, working_dir, stdin_string, ProcessEnviroment); IsValid(&process);) { + Process process = SpawnProcess(cmd, working_dir, stdin_string, ProcessEnviroment); + for (;IsValid(&process);) { Scratch scratch(allocator); String poll = PollStdout(scratch, &process, true); if (poll.len) RawAppend(scratch_buff, poll); } - return scratch_buff; + return {scratch_buff, process.exit_code}; } void KillProcess(View *view) { diff --git a/src/text_editor/window_command.cpp b/src/text_editor/window_command.cpp index cbd947a..f8fb863 100644 --- a/src/text_editor/window_command.cpp +++ b/src/text_editor/window_command.cpp @@ -53,7 +53,7 @@ void CMD_ShowBufferList() { ResetBuffer(command_bar.buffer); For (Buffers) { if (it->special || it->temp || it->is_dir) { - if (it->id != NullBufferID && it->id != BuildBufferID) continue; + if (it->id != NullBufferID) continue; } RawAppendf(command_bar.buffer, "\n%S", it->name); }