Skip to content

Commit

Permalink
Clean up reg_replace()
Browse files Browse the repository at this point in the history
This is more efficient and (I think) also clearer.
  • Loading branch information
craigbarnes committed Jan 12, 2025
1 parent 93d7ea6 commit b082b6b
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/replace.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,21 +172,24 @@ bool reg_replace(View *view, const char *pattern, const char *format, ReplaceFla
return false;
}

BlockIter bi = block_iter(view->buffer);
size_t nr_bytes;
bool swapped = false;
BlockIter bi;
bool swapped;
size_t nr_bytes = 0;
if (view->selection) {
SelectionInfo info = init_selection(view);
bi = info.si;
view->cursor = info.si;
view->sel_so = info.so;
view->sel_eo = info.eo;
swapped = info.swapped;
bi = view->cursor;
nr_bytes = info.eo - info.so;
swapped = info.swapped;
} else {
BlockIter eof = bi;
block_iter_eof(&eof);
nr_bytes = block_iter_get_offset(&eof);
const Block *blk;
block_for_each(blk, &view->buffer->blocks) {
nr_bytes += blk->size;
}
bi = block_iter(view->buffer); // BOF
swapped = false;
}

// Record multiple changes as one chain only when replacing all
Expand Down

0 comments on commit b082b6b

Please sign in to comment.