commit fd4a5fc473f4f603c7b5b1ab6933a4580fe4fb28
parent 66e64dd2d27d80fdf17a2d8cf3e0c0dcc3d68309
Author: Benno Schulenberg <bensberg@telfort.nl>
Date: Wed, 1 Apr 2020 19:56:39 +0200
files: write out a marked region without partitioning the buffer
Use the same mechanism as when copying a marked region: temporarily
clipping the buffer.
Diffstat:
M | src/files.c | | | 43 | +++++++++++++++++++++++++++---------------- |
1 file changed, 27 insertions(+), 16 deletions(-)
diff --git a/src/files.c b/src/files.c
@@ -2021,29 +2021,40 @@ bool write_file(const char *name, FILE *thefile, bool tmp,
bool write_marked_file(const char *name, FILE *stream, bool tmp,
kind_of_writing_type method)
{
- bool retval;
- bool added_magicline = FALSE;
- linestruct *top, *bot;
+ linestruct *birthline, *topline, *botline, *stopper, *afterline;
+ char *was_datastart, saved_byte;
size_t top_x, bot_x;
+ bool retval;
- /* Partition the buffer so that it contains only the marked text. */
- get_region(&top, &top_x, &bot, &bot_x);
- partition_buffer(top, top_x, bot, bot_x);
+ get_region(&topline, &top_x, &botline, &bot_x);
- /* If we are using a magic line, and the last line of the partition
- * isn't blank, then add a newline at the end of the buffer. */
- if (!ISSET(NO_NEWLINES) && openfile->filebot->data[0] != '\0') {
- new_magicline();
- added_magicline = TRUE;
- }
+ /* When needed, prepare a magic end line for the region. */
+ if (bot_x > 0 && !ISSET(NO_NEWLINES)) {
+ stopper = make_new_node(botline);
+ stopper->data = copy_of("");
+ } else
+ stopper = NULL;
+
+ /* Make the marked area look like a separate buffer. */
+ afterline = botline->next;
+ botline->next = stopper;
+ saved_byte = botline->data[bot_x];
+ botline->data[bot_x] = '\0';
+ was_datastart = topline->data;
+ topline->data += top_x;
+ birthline = openfile->filetop;
+ openfile->filetop = topline;
retval = write_file(name, stream, tmp, method, FALSE);
- if (added_magicline)
- remove_magicline();
+ /* Restore the proper state of the buffer. */
+ openfile->filetop = birthline;
+ topline->data = was_datastart;
+ botline->data[bot_x] = saved_byte;
+ botline->next = afterline;
- /* Unpartition the buffer so that it contains all the text again. */
- unpartition_buffer();
+ if (stopper)
+ delete_node(stopper);
return retval;
}