From 5df02c199959b9da42baff0c876bf87125178ca0 Mon Sep 17 00:00:00 2001 From: Randy Palamar Date: Sun, 23 Nov 2025 20:05:59 -0700 Subject: text: remove a bunch of unused save functions These functions were only used for testing the text system. One of them was moved to text-test.c to continue to facilitate this. Otherwise these functions are just cluttering up the code and making it hard to modify. --- sam.c | 2 +- test/core/text-test.c | 24 +++++++++++++++++++----- text-io.c | 42 +----------------------------------------- text.h | 20 ++------------------ vis-cmds.c | 2 +- vis-lua.c | 2 +- vis-prompt.c | 2 +- 7 files changed, 26 insertions(+), 68 deletions(-) diff --git a/sam.c b/sam.c index 379b674..d32066d 100644 --- a/sam.c +++ b/sam.c @@ -1642,7 +1642,7 @@ static bool cmd_write(Vis *vis, Win *win, Command *cmd, const char *argv[], Sele } /* make sure the file is marked as saved i.e. not modified */ - text_save(text, NULL); + text_mark_current_revision(text); vis_event_emit(vis, VIS_EVENT_FILE_SAVE_POST, file, (char*)NULL); return true; } diff --git a/test/core/text-test.c b/test/core/text-test.c index efb81ef..f6004c1 100644 --- a/test/core/text-test.c +++ b/test/core/text-test.c @@ -1,8 +1,9 @@ -#include -#include -#include #include +#include +#include +#include #include +#include #include #include "tap.h" #include "text.h" @@ -88,6 +89,19 @@ static void iterator_find_prev(Text *txt, size_t start, char b, size_t match) { ok((found && it.pos == match) || (!found && it.pos == 0),"Iterator byte find prev (start: %zu, match: %zu)", start, match); } +static bool text_save_method(Text *txt, const char *filename, enum TextSaveMethod method) { + TextSave ctx = text_save_default(.txt = txt, .filename = filename, .method = method); + if (!text_save_begin(&ctx)) + return false; + Filerange range = (Filerange){ .start = 0, .end = text_size(txt) }; + ssize_t written = text_save_write_range(&ctx, &range); + if (written == -1 || (size_t)written != text_range_size(&range)) { + text_save_cancel(&ctx); + return false; + } + return text_save_commit(&ctx); +} + int main(int argc, char *argv[]) { Text *txt; @@ -117,7 +131,7 @@ int main(int argc, char *argv[]) { char buf[BUFSIZ] = "Hello World!\n"; txt = text_load(NULL); ok(txt && insert(txt, 0, buf) && compare(txt, buf), "Inserting into empty text"); - ok(txt && text_save(txt, filename), "Text save"); + ok(txt && text_save_method(txt, filename, TEXT_SAVE_AUTO), "Text save"); text_free(txt); for (size_t i = 0; i < LENGTH(load_method); i++) { @@ -163,7 +177,7 @@ int main(int argc, char *argv[]) { snprintf(buf, sizeof buf, "%s\n", names[i]); txt = text_load(NULL); ok(txt && insert(txt, 0, buf) && compare(txt, buf), "Preparing %s content", names[i]); - ok(txt && text_save(txt, linkname), "Text save %s", names[i]); + ok(txt && text_save_method(txt, linkname, TEXT_SAVE_AUTO), "Text save %s", names[i]); text_free(txt); txt = text_load(linkname); diff --git a/text-io.c b/text-io.c index 94654ae..bbd1f8d 100644 --- a/text-io.c +++ b/text-io.c @@ -166,10 +166,6 @@ Text *text_load(const char *filename) { return text_load_method(filename, TEXT_LOAD_AUTO); } -Text *text_loadat(int dirfd, const char *filename) { - return text_loadat_method(dirfd, filename, TEXT_LOAD_AUTO); -} - Text *text_load_method(const char *filename, enum TextLoadMethod method) { return text_loadat_method(AT_FDCWD, filename, method); } @@ -453,48 +449,12 @@ bool text_save_commit(TextSave *ctx) { return result; } -/* First try to save the file atomically using rename(2) if this does not - * work overwrite the file in place. However if something goes wrong during - * this overwrite the original file is permanently damaged. - */ -bool text_save(Text *txt, const char *filename) { - return text_saveat(txt, AT_FDCWD, filename); -} - -bool text_saveat(Text *txt, int dirfd, const char *filename) { - return text_saveat_method(txt, dirfd, filename, TEXT_SAVE_AUTO); -} - -bool text_save_method(Text *txt, const char *filename, enum TextSaveMethod method) { - return text_saveat_method(txt, AT_FDCWD, filename, method); -} - -bool text_saveat_method(Text *txt, int dirfd, const char *filename, enum TextSaveMethod method) { - if (!filename) { - text_saved(txt, NULL); - return true; - } - TextSave ctx = text_save_default(.txt = txt, .dirfd = dirfd, .filename = filename, .method = method); - if (!text_save_begin(&ctx)) - return false; - Filerange range = (Filerange){ .start = 0, .end = text_size(txt) }; - ssize_t written = text_save_write_range(&ctx, &range); - if (written == -1 || (size_t)written != text_range_size(&range)) { - text_save_cancel(&ctx); - return false; - } - return text_save_commit(&ctx); -} +void text_mark_current_revision(Text *txt) { text_saved(txt, 0); } ssize_t text_save_write_range(TextSave *ctx, const Filerange *range) { return text_write_range(ctx->txt, range, ctx->fd); } -ssize_t text_write(const Text *txt, int fd) { - Filerange r = (Filerange){ .start = 0, .end = text_size(txt) }; - return text_write_range(txt, &r, fd); -} - ssize_t text_write_range(const Text *txt, const Filerange *range, int fd) { size_t size = text_range_size(range), rem = size; for (Iterator it = text_iterator_get(txt, range->start); diff --git a/text.h b/text.h index 08b1643..33666ff 100644 --- a/text.h +++ b/text.h @@ -94,7 +94,6 @@ enum TextLoadMethod { * @endrst */ Text *text_load(const char *filename); -Text *text_loadat(int dirfd, const char *filename); /** * Create a text instance populated with the given file content. * @@ -362,19 +361,9 @@ typedef struct { #define text_save_default(...) (TextSave){.dirfd = AT_FDCWD, .fd = -1, __VA_ARGS__} /** - * Save the whole text to the given file name. - * - * @rst - * .. note:: Equivalent to ``text_save_method(filename, TEXT_SAVE_AUTO)``. - * @endrst - */ -bool text_save(Text*, const char *filename); -bool text_saveat(Text*, int dirfd, const char *filename); -/** - * Save the whole text to the given file name, using the specified method. + * Marks the current text revision as saved. */ -bool text_save_method(Text*, const char *filename, enum TextSaveMethod); -bool text_saveat_method(Text*, int dirfd, const char *filename, enum TextSaveMethod); +void text_mark_current_revision(Text*); /** * Setup a sequence of write operations. @@ -411,11 +400,6 @@ bool text_save_commit(TextSave*); * @endrst */ void text_save_cancel(TextSave*); -/** - * Write whole text content to file descriptor. - * @return The number of bytes written or ``-1`` in case of an error. - */ -ssize_t text_write(const Text*, int fd); /** * Write file range to file descriptor. * @return The number of bytes written or ``-1`` in case of an error. diff --git a/vis-cmds.c b/vis-cmds.c index 892f5a5..482508f 100644 --- a/vis-cmds.c +++ b/vis-cmds.c @@ -867,7 +867,7 @@ static bool cmd_help(Vis *vis, Win *win, Command *cmd, const char *argv[], Selec for (size_t i = 0; i < LENGTH(configs); i++) text_appendf(txt, " %-32s\t%s\n", configs[i].name, configs[i].enabled ? "yes" : "no"); - text_save(txt, NULL); + text_mark_current_revision(txt); view_cursors_to(vis->win->view.selection, 0); if (argv[1]) diff --git a/vis-lua.c b/vis-lua.c index f7e9001..695ebdb 100644 --- a/vis-lua.c +++ b/vis-lua.c @@ -2579,7 +2579,7 @@ static int file_newindex(lua_State *L) { text_insert(file->text, 0, " ", 1); text_delete(file->text, 0, 1); } else { - text_save(file->text, NULL); + text_mark_current_revision(file->text); } return 0; } diff --git a/vis-prompt.c b/vis-prompt.c index bf1aeb2..089a931 100644 --- a/vis-prompt.c +++ b/vis-prompt.c @@ -197,7 +197,7 @@ void vis_message_show(Vis *vis, const char *msg) { Text *txt = win->file->text; size_t pos = text_size(txt); text_appendf(txt, "%s\n", msg); - text_save(txt, NULL); + text_mark_current_revision(txt); view_cursors_to(win->view.selection, pos); vis_window_focus(win); } -- cgit v1.2.3