vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

commit 0a3b43d0588d6a1906567c5a53ebcf3610652651
parent 5f49465bd55ec86e85e8363fd14b6d5b891e2e1a
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Wed, 13 May 2020 08:56:22 +0200

text: introduce text_save_method, remove text_save_range

This utility function is analogous to text_load_method and allows the
caller to specify how the file should be saved. It is implemented as a
wrapper around the lower level text_save_{begin,write,commit} primitives.

The unused text_save_range function has been removed. If needed, use
the aforementioned lower level functionality.

Diffstat:
Mtext.c | 18+++++++++---------
Mtext.h | 21+++++++++++++--------
2 files changed, 22 insertions(+), 17 deletions(-)

diff --git a/text.c b/text.c @@ -1067,26 +1067,26 @@ void text_save_cancel(TextSave *ctx) { errno = saved_errno; } -bool text_save(Text *txt, const char *filename) { - Filerange r = (Filerange){ .start = 0, .end = text_size(txt) }; - return text_save_range(txt, &r, filename); -} - /* 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_range(Text *txt, Filerange *range, const char *filename) { +bool text_save(Text *txt, const char *filename) { + return text_save_method(txt, filename, TEXT_SAVE_AUTO); +} + +bool text_save_method(Text *txt, const char *filename, enum TextSaveMethod method) { if (!filename) { txt->saved_revision = txt->history; text_snapshot(txt); return true; } - TextSave *ctx = text_save_begin(txt, filename, TEXT_SAVE_AUTO); + TextSave *ctx = text_save_begin(txt, filename, method); if (!ctx) return false; - ssize_t written = text_write_range(txt, range, ctx->fd); - if (written == -1 || (size_t)written != text_range_size(range)) { + 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; } diff --git a/text.h b/text.h @@ -302,14 +302,6 @@ size_t text_mark_get(Text*, Mark); * @{ */ /** - * Save the whole text to the given file name. - */ -bool text_save(Text*, const char *filename); -/** - * Save a file range to the given file name. - */ -bool text_save_range(Text*, Filerange*, const char *filename); -/** * Method used to save the text. */ enum TextSaveMethod { @@ -346,6 +338,19 @@ enum TextSaveMethod { }; /** + * Save the whole text to the given file name. + */ +bool text_save(Text*, const char *filename); +/** + * Save the whole text to the given file name, using the specified method. + * + * @rst + * .. note:: Equivalent to ``text_save_method(filename, TEXT_SAVE_AUTO)``. + * @endrst + */ +bool text_save_method(Text*, const char *filename, enum TextSaveMethod); + +/** * Setup a sequence of write operations. * * The returned `TextSave` pointer can be used to write multiple, possibly