move del_bytes

This commit is contained in:
Daniel Hahler
2019-06-09 19:14:53 +02:00
parent 0b3ee2e8ac
commit e454dce5e4
2 changed files with 87 additions and 211 deletions

View File

@@ -1398,98 +1398,6 @@ void ins_char(int c)
ins_char_bytes(buf, n);
}
/// Delete "count" bytes under the cursor.
/// If "fixpos" is true, don't leave the cursor on the NUL after the line.
/// Caller must have prepared for undo.
///
/// @param count number of bytes to be deleted
/// @param fixpos_arg leave the cursor on the NUL after the line
/// @param use_delcombine 'delcombine' option applies
///
/// @return FAIL for failure, OK otherwise
int del_bytes(colnr_T count, bool fixpos_arg, bool use_delcombine)
{
linenr_T lnum = curwin->w_cursor.lnum;
colnr_T col = curwin->w_cursor.col;
bool fixpos = fixpos_arg;
char_u *oldp = ml_get(lnum);
colnr_T oldlen = (colnr_T)STRLEN(oldp);
// Can't do anything when the cursor is on the NUL after the line.
if (col >= oldlen) {
return FAIL;
}
// If "count" is zero there is nothing to do.
if (count == 0) {
return OK;
}
// If "count" is negative the caller must be doing something wrong.
if (count < 1) {
IEMSGN("E950: Invalid count for del_bytes(): %ld", count);
return FAIL;
}
/* If 'delcombine' is set and deleting (less than) one character, only
* delete the last combining character. */
if (p_deco && use_delcombine && enc_utf8
&& utfc_ptr2len(oldp + col) >= count) {
int cc[MAX_MCO];
int n;
(void)utfc_ptr2char(oldp + col, cc);
if (cc[0] != NUL) {
/* Find the last composing char, there can be several. */
n = col;
do {
col = n;
count = utf_ptr2len(oldp + n);
n += count;
} while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
fixpos = false;
}
}
// When count is too big, reduce it.
int movelen = oldlen - col - count + 1; // includes trailing NUL
if (movelen <= 1) {
/*
* If we just took off the last character of a non-blank line, and
* fixpos is TRUE, we don't want to end up positioned at the NUL,
* unless "restart_edit" is set or 'virtualedit' contains "onemore".
*/
if (col > 0 && fixpos && restart_edit == 0
&& (ve_flags & VE_ONEMORE) == 0
) {
--curwin->w_cursor.col;
curwin->w_cursor.coladd = 0;
curwin->w_cursor.col -= utf_head_off(oldp, oldp + curwin->w_cursor.col);
}
count = oldlen - col;
movelen = 1;
}
// If the old line has been allocated the deletion can be done in the
// existing line. Otherwise a new line has to be allocated.
bool was_alloced = ml_line_alloced(); // check if oldp was allocated
char_u *newp;
if (was_alloced) {
ml_add_deleted_len(curbuf->b_ml.ml_line_ptr, oldlen);
newp = oldp; // use same allocated memory
} else { // need to allocate a new line
newp = xmalloc((size_t)(oldlen + 1 - count));
memmove(newp, oldp, (size_t)col);
}
memmove(newp + col, oldp + col + count, (size_t)movelen);
if (!was_alloced) {
ml_replace(lnum, newp, false);
}
/* mark the buffer as changed and prepare for displaying */
changed_bytes(lnum, curwin->w_cursor.col);
return OK;
}
/*
* Delete from cursor to end of line.
* Caller must have prepared for undo.