aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/misc1.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/misc1.c')
-rw-r--r--src/nvim/misc1.c509
1 files changed, 296 insertions, 213 deletions
diff --git a/src/nvim/misc1.c b/src/nvim/misc1.c
index d72d8e8513..a66ded13f1 100644
--- a/src/nvim/misc1.c
+++ b/src/nvim/misc1.c
@@ -1,3 +1,6 @@
+// This is an open source non-commercial project. Dear PVS-Studio, please check
+// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
+
/*
* misc1.c: functions that didn't seem to fit elsewhere
*/
@@ -25,13 +28,13 @@
#include "nvim/getchar.h"
#include "nvim/indent.h"
#include "nvim/indent_c.h"
+#include "nvim/buffer_updates.h"
#include "nvim/main.h"
#include "nvim/mark.h"
#include "nvim/mbyte.h"
#include "nvim/memline.h"
#include "nvim/memory.h"
#include "nvim/message.h"
-#include "nvim/misc2.h"
#include "nvim/garray.h"
#include "nvim/move.h"
#include "nvim/mouse.h"
@@ -41,6 +44,7 @@
#include "nvim/regexp.h"
#include "nvim/screen.h"
#include "nvim/search.h"
+#include "nvim/state.h"
#include "nvim/strings.h"
#include "nvim/tag.h"
#include "nvim/ui.h"
@@ -48,9 +52,11 @@
#include "nvim/window.h"
#include "nvim/os/os.h"
#include "nvim/os/shell.h"
+#include "nvim/os/signal.h"
#include "nvim/os/input.h"
#include "nvim/os/time.h"
#include "nvim/event/stream.h"
+#include "nvim/buffer.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "misc1.c.generated.h"
@@ -93,7 +99,7 @@ open_line (
colnr_T newcol = 0; // new cursor column
int newindent = 0; // auto-indent of the new line
bool trunc_line = false; // truncate current line afterwards
- bool retval = false; // return value, default is false
+ bool retval = false; // return value
int extra_len = 0; // length of p_extra string
int lead_len; // length of comment leader
char_u *lead_flags; // position in 'comments' for comment leader
@@ -102,7 +108,8 @@ open_line (
char_u *p;
char_u saved_char = NUL; // init for GCC
pos_T *pos;
- bool do_si = (!p_paste && curbuf->b_p_si && !curbuf->b_p_cin);
+ bool do_si = (!p_paste && curbuf->b_p_si && !curbuf->b_p_cin
+ && *curbuf->b_p_inde == NUL);
bool no_si = false; // reset did_si afterwards
int first_char = NUL; // init for GCC
int vreplace_mode;
@@ -159,8 +166,8 @@ open_line (
*p_extra = NUL;
}
- u_clearline(); /* cannot do "U" command when adding lines */
- did_si = FALSE;
+ u_clearline(); // cannot do "U" command when adding lines
+ did_si = false;
ai_col = 0;
/*
@@ -196,7 +203,7 @@ open_line (
char_u *ptr;
char_u last_char;
- pos_T old_cursor = curwin->w_cursor;
+ old_cursor = curwin->w_cursor;
ptr = saved_line;
if (flags & OPENLINE_DO_COM)
lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
@@ -280,8 +287,8 @@ open_line (
* checking for "if" and the like.
*/
if (last_char == '{') {
- did_si = TRUE; /* do indent */
- no_si = TRUE; /* don't delete it when '{' typed */
+ did_si = true; // do indent
+ no_si = true; // don't delete it when '{' typed
}
/*
* Look for "if" and the like, use 'cinwords'.
@@ -290,7 +297,7 @@ open_line (
*/
else if (last_char != ';' && last_char != '}'
&& cin_is_cinword(ptr))
- did_si = TRUE;
+ did_si = true;
}
} else { // dir == BACKWARD
// Skip preprocessor directives, unless they are
@@ -314,17 +321,19 @@ open_line (
}
}
p = skipwhite(ptr);
- if (*p == '}') /* if line starts with '}': do indent */
- did_si = TRUE;
- else /* can delete indent when '{' typed */
- can_si_back = TRUE;
+ if (*p == '}') { // if line starts with '}': do indent
+ did_si = true;
+ } else { // can delete indent when '{' typed
+ can_si_back = true;
+ }
}
curwin->w_cursor = old_cursor;
}
- if (do_si)
- can_si = TRUE;
+ if (do_si) {
+ can_si = true;
+ }
- did_ai = TRUE;
+ did_ai = true;
}
/*
@@ -525,7 +534,7 @@ open_line (
int l;
while (old_size < repl_size && p > leader) {
- mb_ptr_back(leader, p);
+ MB_PTR_BACK(leader, p);
old_size += ptr2cells(p);
}
l = lead_repl_len - (int)(endp - p);
@@ -540,7 +549,7 @@ open_line (
/* blank-out any other chars from the old leader. */
while (--p >= leader) {
- int l = mb_head_off(leader, p);
+ int l = utf_head_off(leader, p);
if (l > 1) {
p -= l;
@@ -566,7 +575,7 @@ open_line (
int i;
int l;
- for (i = 0; p[i] != NUL && i < lead_len; i += l) {
+ for (i = 0; i < lead_len && p[i] != NUL; i += l) {
l = (*mb_ptr2len)(p + i);
if (vim_strnsize(p, i + l) > repl_size)
break;
@@ -659,7 +668,7 @@ open_line (
}
}
- did_si = can_si = FALSE;
+ did_si = can_si = false;
} else if (comment_end != NULL) {
// We have finished a comment, so we don't use the leader.
// If this was a C-comment and 'ai' or 'si' is set do a normal
@@ -693,17 +702,17 @@ open_line (
replace_push(NUL); /* end of extra blanks */
if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES)) {
while ((*p_extra == ' ' || *p_extra == '\t')
- && (!enc_utf8
- || !utf_iscomposing(utf_ptr2char(p_extra + 1)))
- ) {
- if (REPLACE_NORMAL(State))
+ && !utf_iscomposing(utf_ptr2char(p_extra + 1))) {
+ if (REPLACE_NORMAL(State)) {
replace_push(*p_extra);
- ++p_extra;
- ++less_cols_off;
+ }
+ p_extra++;
+ less_cols_off++;
}
}
- if (*p_extra != NUL)
- did_ai = FALSE; /* append some text, don't truncate now */
+ if (*p_extra != NUL) {
+ did_ai = false; // append some text, don't truncate now
+ }
/* columns for marks adjusted for removed columns */
less_cols = (int)(p_extra - saved_line);
@@ -730,7 +739,7 @@ open_line (
}
STRCAT(leader, p_extra);
p_extra = leader;
- did_ai = TRUE; /* So truncating blanks works with comments */
+ did_ai = true; // So truncating blanks works with comments
less_cols -= lead_len;
} else
end_comment_pending = NUL; /* turns out there was no leader */
@@ -742,10 +751,15 @@ open_line (
if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
== FAIL)
goto theend;
- /* Postpone calling changed_lines(), because it would mess up folding
- * with markers. */
- mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
- did_append = TRUE;
+ // Postpone calling changed_lines(), because it would mess up folding
+ // with markers.
+ // Skip mark_adjust when adding a line after the last one, there can't
+ // be marks there. But still needed in diff mode.
+ if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count
+ || curwin->w_p_diff) {
+ mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L, false);
+ }
+ did_append = true;
} else {
/*
* In VREPLACE mode we are starting to replace the next line.
@@ -758,7 +772,7 @@ open_line (
(void)u_save_cursor(); /* errors are ignored! */
vr_lines_changed++;
}
- ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
+ ml_replace(curwin->w_cursor.lnum, p_extra, true);
changed_bytes(curwin->w_cursor.lnum, 0);
curwin->w_cursor.lnum--;
did_append = FALSE;
@@ -801,8 +815,9 @@ open_line (
}
}
newcol += curwin->w_cursor.col;
- if (no_si)
- did_si = FALSE;
+ if (no_si) {
+ did_si = false;
+ }
}
/*
@@ -817,17 +832,18 @@ open_line (
if (dir == FORWARD) {
if (trunc_line || (State & INSERT)) {
- /* truncate current line at cursor */
+ // truncate current line at cursor
saved_line[curwin->w_cursor.col] = NUL;
- /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
- if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
+ // Remove trailing white space, unless OPENLINE_KEEPTRAIL used.
+ if (trunc_line && !(flags & OPENLINE_KEEPTRAIL)) {
truncate_spaces(saved_line);
- ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
+ }
+ ml_replace(curwin->w_cursor.lnum, saved_line, false);
saved_line = NULL;
if (did_append) {
changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
- curwin->w_cursor.lnum + 1, 1L);
- did_append = FALSE;
+ curwin->w_cursor.lnum + 1, 1L, true);
+ did_append = false;
/* Move marks after the line break to the new line. */
if (flags & OPENLINE_MARKFIX)
@@ -844,8 +860,9 @@ open_line (
*/
curwin->w_cursor.lnum = old_cursor.lnum + 1;
}
- if (did_append)
- changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
+ if (did_append) {
+ changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L, true);
+ }
curwin->w_cursor.col = newcol;
curwin->w_cursor.coladd = 0;
@@ -868,8 +885,7 @@ open_line (
&& curbuf->b_p_lisp
&& curbuf->b_p_ai) {
fixthisline(get_lisp_indent);
- p = get_cursor_line_ptr();
- ai_col = (colnr_T)(skipwhite(p) - p);
+ ai_col = (colnr_T)getwhitecols_curline();
}
/*
* May do indenting after opening a new line.
@@ -882,8 +898,7 @@ open_line (
? KEY_OPEN_FORW
: KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum))) {
do_c_expr_indent();
- p = get_cursor_line_ptr();
- ai_col = (colnr_T)(skipwhite(p) - p);
+ ai_col = (colnr_T)getwhitecols_curline();
}
if (vreplace_mode != 0)
State = vreplace_mode;
@@ -897,8 +912,8 @@ open_line (
/* Put new line in p_extra */
p_extra = vim_strsave(get_cursor_line_ptr());
- /* Put back original line */
- ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
+ // Put back original line
+ ml_replace(curwin->w_cursor.lnum, next_line, false);
/* Insert new stuff into line again */
curwin->w_cursor.col = 0;
@@ -908,7 +923,7 @@ open_line (
next_line = NULL;
}
- retval = TRUE; /* success! */
+ retval = true; // success!
theend:
curbuf->b_p_pi = saved_pi;
xfree(saved_line);
@@ -1107,8 +1122,9 @@ int get_last_leader_offset(char_u *line, char_u **flags)
if (ascii_iswhite(string[0])) {
if (i == 0 || !ascii_iswhite(line[i - 1]))
continue;
- while (ascii_iswhite(string[0]))
- ++string;
+ while (ascii_iswhite(*string)) {
+ string++;
+ }
}
for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
/* do nothing */;
@@ -1124,6 +1140,19 @@ int get_last_leader_offset(char_u *line, char_u **flags)
continue;
}
+ if (vim_strchr(part_buf, COM_MIDDLE) != NULL) {
+ // For a middlepart comment, only consider it to match if
+ // everything before the current position in the line is
+ // whitespace. Otherwise we would think we are inside a
+ // comment if the middle part appears somewhere in the middle
+ // of the line. E.g. for C the "*" appears often.
+ for (j = 0; ascii_iswhite(line[j]) && j <= i; j++) {
+ }
+ if (j < i) {
+ continue;
+ }
+ }
+
/*
* We have found a match, stop searching.
*/
@@ -1191,16 +1220,15 @@ int get_last_leader_offset(char_u *line, char_u **flags)
/*
* Return the number of window lines occupied by buffer line "lnum".
*/
-int plines(linenr_T lnum)
+int plines(const linenr_T lnum)
{
- return plines_win(curwin, lnum, TRUE);
+ return plines_win(curwin, lnum, true);
}
-int
-plines_win (
- win_T *wp,
- linenr_T lnum,
- int winheight /* when TRUE limit to window height */
+int plines_win(
+ win_T *const wp,
+ const linenr_T lnum,
+ const bool winheight // when true limit to window height
)
{
/* Check for filler lines above this buffer line. When folded the result
@@ -1208,34 +1236,34 @@ plines_win (
return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
}
-int plines_nofill(linenr_T lnum)
+int plines_nofill(const linenr_T lnum)
{
- return plines_win_nofill(curwin, lnum, TRUE);
+ return plines_win_nofill(curwin, lnum, true);
}
-int
-plines_win_nofill (
- win_T *wp,
- linenr_T lnum,
- int winheight /* when TRUE limit to window height */
+int plines_win_nofill(
+ win_T *const wp,
+ const linenr_T lnum,
+ const bool winheight // when true limit to window height
)
{
- int lines;
-
- if (!wp->w_p_wrap)
+ if (!wp->w_p_wrap) {
return 1;
+ }
- if (wp->w_width == 0)
+ if (wp->w_grid.Columns == 0) {
return 1;
+ }
- /* A folded lines is handled just like an empty line. */
- /* NOTE: Caller must handle lines that are MAYBE folded. */
- if (lineFolded(wp, lnum) == TRUE)
+ // A folded lines is handled just like an empty line.
+ if (lineFolded(wp, lnum)) {
return 1;
+ }
- lines = plines_win_nofold(wp, lnum);
- if (winheight > 0 && lines > wp->w_height)
- return wp->w_height;
+ const int lines = plines_win_nofold(wp, lnum);
+ if (winheight && lines > wp->w_grid.Rows) {
+ return wp->w_grid.Rows;
+ }
return lines;
}
@@ -1264,9 +1292,9 @@ int plines_win_nofold(win_T *wp, linenr_T lnum)
/*
* Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
*/
- width = wp->w_width - win_col_off(wp);
- if (width <= 0) {
- return 32000; // bigger than the number of lines of the screen
+ width = wp->w_grid.Columns - win_col_off(wp);
+ if (width <= 0 || col > 32000) {
+ return 32000; // bigger than the number of screen columns
}
if (col <= (unsigned int)width) {
return 1;
@@ -1290,8 +1318,9 @@ int plines_win_col(win_T *wp, linenr_T lnum, long column)
if (!wp->w_p_wrap)
return lines + 1;
- if (wp->w_width == 0)
+ if (wp->w_grid.Columns == 0) {
return lines + 1;
+ }
char_u *line = ml_get_buf(wp->w_buffer, lnum, false);
char_u *s = line;
@@ -1299,7 +1328,7 @@ int plines_win_col(win_T *wp, linenr_T lnum, long column)
colnr_T col = 0;
while (*s != NUL && --column >= 0) {
col += win_lbr_chartabsize(wp, line, s, col, NULL);
- mb_ptr_adv(s);
+ MB_PTR_ADV(s);
}
// If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
@@ -1312,7 +1341,7 @@ int plines_win_col(win_T *wp, linenr_T lnum, long column)
}
// Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
- int width = wp->w_width - win_col_off(wp);
+ int width = wp->w_grid.Columns - win_col_off(wp);
if (width <= 0) {
return 9999;
}
@@ -1335,11 +1364,12 @@ int plines_m_win(win_T *wp, linenr_T first, linenr_T last)
++count; /* count 1 for "+-- folded" line */
first += x;
} else {
- if (first == wp->w_topline)
- count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
- else
- count += plines_win(wp, first, TRUE);
- ++first;
+ if (first == wp->w_topline) {
+ count += plines_win_nofill(wp, first, true) + wp->w_topfill;
+ } else {
+ count += plines_win(wp, first, true);
+ }
+ first++;
}
}
return count;
@@ -1384,7 +1414,7 @@ void ins_bytes_len(char_u *p, size_t len)
void ins_char(int c)
{
char_u buf[MB_MAXBYTES + 1];
- size_t n = (size_t)(*mb_char2bytes)(c, buf);
+ size_t n = (size_t)utf_char2bytes(c, buf);
// When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
// Happens for CTRL-Vu9900.
@@ -1401,7 +1431,6 @@ void ins_char_bytes(char_u *buf, size_t charlen)
coladvance_force(getviscol());
}
- int c = buf[0];
size_t col = (size_t)curwin->w_cursor.col;
linenr_T lnum = curwin->w_cursor.lnum;
char_u *oldp = ml_get(lnum);
@@ -1461,7 +1490,7 @@ void ins_char_bytes(char_u *buf, size_t charlen)
}
}
- char_u *newp = (char_u *) xmalloc((size_t)(linelen + newlen - oldlen));
+ char_u *newp = xmalloc((size_t)(linelen + newlen - oldlen));
// Copy bytes before the cursor.
if (col > 0) {
@@ -1470,7 +1499,10 @@ void ins_char_bytes(char_u *buf, size_t charlen)
// Copy bytes after the changed character(s).
char_u *p = newp + col;
- memmove(p + newlen, oldp + col + oldlen, (size_t)(linelen - col - oldlen));
+ if (linelen > col + oldlen) {
+ memmove(p + newlen, oldp + col + oldlen,
+ (size_t)(linelen - col - oldlen));
+ }
// Insert or overwrite the new character.
memmove(p, buf, charlen);
@@ -1480,8 +1512,8 @@ void ins_char_bytes(char_u *buf, size_t charlen)
p[i] = ' ';
}
- /* Replace the line in the buffer. */
- ml_replace(lnum, newp, FALSE);
+ // Replace the line in the buffer.
+ ml_replace(lnum, newp, false);
// mark the buffer as changed and prepare for displaying
changed_bytes(lnum, (colnr_T)col);
@@ -1494,10 +1526,7 @@ void ins_char_bytes(char_u *buf, size_t charlen)
&& msg_silent == 0
&& !ins_compl_active()
) {
- if (has_mbyte)
- showmatch(mb_ptr2char(buf));
- else
- showmatch(c);
+ showmatch(utf_ptr2char(buf));
}
if (!p_ri || (State & REPLACE_FLAG)) {
@@ -1534,19 +1563,17 @@ void ins_str(char_u *s)
memmove(newp, oldp, (size_t)col);
memmove(newp + col, s, (size_t)newlen);
memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
- ml_replace(lnum, newp, FALSE);
+ ml_replace(lnum, newp, false);
changed_bytes(lnum, col);
curwin->w_cursor.col += newlen;
}
-/*
- * Delete one character under the cursor.
- * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
- * Caller must have prepared for undo.
- *
- * return FAIL for failure, OK otherwise
- */
-int del_char(int fixpos)
+// Delete one character under the cursor.
+// If "fixpos" is true, don't leave the cursor on the NUL after the line.
+// Caller must have prepared for undo.
+//
+// return FAIL for failure, OK otherwise
+int del_char(bool fixpos)
{
if (has_mbyte) {
/* Make sure the cursor is at the start of a character. */
@@ -1594,11 +1621,19 @@ int del_bytes(colnr_T count, bool fixpos_arg, bool use_delcombine)
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)
+ // 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. */
@@ -1633,9 +1668,7 @@ int del_bytes(colnr_T count, bool fixpos_arg, bool use_delcombine)
) {
--curwin->w_cursor.col;
curwin->w_cursor.coladd = 0;
- if (has_mbyte)
- curwin->w_cursor.col -=
- (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
+ curwin->w_cursor.col -= utf_head_off(oldp, oldp + curwin->w_cursor.col);
}
count = oldlen - col;
movelen = 1;
@@ -1652,8 +1685,9 @@ int del_bytes(colnr_T count, bool fixpos_arg, bool use_delcombine)
memmove(newp, oldp, (size_t)col);
}
memmove(newp + col, oldp + col + count, (size_t)movelen);
- if (!was_alloced)
- ml_replace(lnum, newp, FALSE);
+ if (!was_alloced) {
+ ml_replace(lnum, newp, false);
+ }
/* mark the buffer as changed and prepare for displaying */
changed_bytes(lnum, curwin->w_cursor.col);
@@ -1734,23 +1768,11 @@ del_lines (
int gchar_pos(pos_T *pos)
{
- char_u *ptr = ml_get_pos(pos);
-
- if (has_mbyte)
- return (*mb_ptr2char)(ptr);
- return (int)*ptr;
-}
-
-/*
- * Skip to next part of an option argument: Skip space and comma.
- */
-char_u *skip_to_option_part(char_u *p)
-{
- if (*p == ',')
- ++p;
- while (*p == ' ')
- ++p;
- return p;
+ // When searching columns is sometimes put at the end of a line.
+ if (pos->col == MAXCOL) {
+ return NUL;
+ }
+ return utf_ptr2char(ml_get_pos(pos));
}
/*
@@ -1796,7 +1818,7 @@ void changed(void)
}
changed_int();
}
- ++curbuf->b_changedtick;
+ buf_inc_changedtick(curbuf);
}
/*
@@ -1823,6 +1845,10 @@ void changed_bytes(linenr_T lnum, colnr_T col)
{
changedOneline(curbuf, lnum);
changed_common(lnum, col, lnum + 1, 0L);
+ // notify any channels that are watching
+ if (kv_size(curbuf->update_channels)) {
+ buf_updates_send_changes(curbuf, lnum, 1, 1, true);
+ }
/* Diff highlighting in other diff windows may need to be updated too. */
if (curwin->w_p_diff) {
@@ -1863,7 +1889,7 @@ static void changedOneline(buf_T *buf, linenr_T lnum)
*/
void appended_lines(linenr_T lnum, long count)
{
- changed_lines(lnum + 1, 0, lnum + 1, count);
+ changed_lines(lnum + 1, 0, lnum + 1, count, true);
}
/*
@@ -1871,8 +1897,12 @@ void appended_lines(linenr_T lnum, long count)
*/
void appended_lines_mark(linenr_T lnum, long count)
{
- mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
- changed_lines(lnum + 1, 0, lnum + 1, count);
+ // Skip mark_adjust when adding a line after the last one, there can't
+ // be marks there. But it's still needed in diff mode.
+ if (lnum + count < curbuf->b_ml.ml_line_count || curwin->w_p_diff) {
+ mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L, false);
+ }
+ changed_lines(lnum + 1, 0, lnum + 1, count, true);
}
/*
@@ -1882,7 +1912,7 @@ void appended_lines_mark(linenr_T lnum, long count)
*/
void deleted_lines(linenr_T lnum, long count)
{
- changed_lines(lnum, 0, lnum + count, -count);
+ changed_lines(lnum, 0, lnum + count, -count, true);
}
/*
@@ -1892,8 +1922,8 @@ void deleted_lines(linenr_T lnum, long count)
*/
void deleted_lines_mark(linenr_T lnum, long count)
{
- mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
- changed_lines(lnum, 0, lnum + count, -count);
+ mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count, false);
+ changed_lines(lnum, 0, lnum + count, -count, true);
}
/*
@@ -1908,20 +1938,24 @@ void deleted_lines_mark(linenr_T lnum, long count)
* Takes care of calling changed() and updating b_mod_*.
* Careful: may trigger autocommands that reload the buffer.
*/
-void
-changed_lines (
- linenr_T lnum, /* first line with change */
- colnr_T col, /* column in first line with change */
- linenr_T lnume, /* line below last changed line */
- long xtra /* number of extra lines (negative when deleting) */
+void
+changed_lines(
+ linenr_T lnum, // first line with change
+ colnr_T col, // column in first line with change
+ linenr_T lnume, // line below last changed line
+ long xtra, // number of extra lines (negative when deleting)
+ bool do_buf_event // some callers like undo/redo call changed_lines()
+ // and then increment changedtick *again*. This flag
+ // allows these callers to send the nvim_buf_lines_event
+ // events after they're done modifying changedtick.
)
{
changed_lines_buf(curbuf, lnum, lnume, xtra);
- if (xtra == 0 && curwin->w_p_diff) {
- /* When the number of lines doesn't change then mark_adjust() isn't
- * called and other diff buffers still need to be marked for
- * displaying. */
+ if (xtra == 0 && curwin->w_p_diff && !diff_internal()) {
+ // When the number of lines doesn't change then mark_adjust() isn't
+ // called and other diff buffers still need to be marked for
+ // displaying.
linenr_T wlnum;
FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
@@ -1937,6 +1971,12 @@ changed_lines (
}
changed_common(lnum, col, lnume, xtra);
+
+ if (do_buf_event && kv_size(curbuf->update_channels)) {
+ int64_t num_added = (int64_t)(lnume + xtra - lnum);
+ int64_t num_removed = lnume - lnum;
+ buf_updates_send_changes(curbuf, lnum, num_added, num_removed, true);
+ }
}
/// Mark line range in buffer as changed.
@@ -1984,6 +2024,10 @@ static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra
/* mark the buffer as modified */
changed();
+ if (curwin->w_p_diff && diff_internal()) {
+ curtab->tp_diff_update = true;
+ }
+
/* set the '. mark */
if (!cmdmod.keepjumps) {
RESET_FMARK(&curbuf->b_last_change, ((pos_T) {lnum, col, 0}), 0);
@@ -2151,7 +2195,7 @@ unchanged (
redraw_tabline = TRUE;
need_maketitle = TRUE; /* set window title later */
}
- ++buf->b_changedtick;
+ buf_inc_changedtick(buf);
}
/*
@@ -2202,12 +2246,12 @@ change_warning (
msg_start();
if (msg_row == Rows - 1)
msg_col = col;
- msg_source(hl_attr(HLF_W));
- MSG_PUTS_ATTR(_(w_readonly), hl_attr(HLF_W) | MSG_HIST);
+ msg_source(HL_ATTR(HLF_W));
+ MSG_PUTS_ATTR(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
set_vim_var_string(VV_WARNINGMSG, _(w_readonly), -1);
msg_clr_eos();
(void)msg_end();
- if (msg_silent == 0 && !silent_mode) {
+ if (msg_silent == 0 && !silent_mode && ui_active()) {
ui_flush();
os_delay(1000L, true); /* give the user time to think about it */
}
@@ -2218,44 +2262,47 @@ change_warning (
}
}
-/*
- * Ask for a reply from the user, a 'y' or a 'n'.
- * No other characters are accepted, the message is repeated until a valid
- * reply is entered or CTRL-C is hit.
- * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
- * from any buffers but directly from the user.
- *
- * return the 'y' or 'n'
- */
-int ask_yesno(char_u *str, int direct)
+/// Ask for a reply from the user, 'y' or 'n'
+///
+/// No other characters are accepted, the message is repeated until a valid
+/// reply is entered or <C-c> is hit.
+///
+/// @param[in] str Prompt: question to ask user. Is always followed by
+/// " (y/n)?".
+/// @param[in] direct Determines what function to use to get user input. If
+/// true then ui_inchar() will be used, otherwise vgetc().
+/// I.e. when direct is true then characters are obtained
+/// directly from the user without buffers involved.
+///
+/// @return 'y' or 'n'. Last is also what will be returned in case of interrupt.
+int ask_yesno(const char *const str, const bool direct)
{
- int r = ' ';
- int save_State = State;
+ const int save_State = State;
- ++no_wait_return;
- State = CONFIRM; /* mouse behaves like with :confirm */
- setmouse(); /* disables mouse for xterm */
- ++no_mapping;
- ++allow_keys; /* no mapping here, but recognize keys */
+ no_wait_return++;
+ State = CONFIRM; // Mouse behaves like with :confirm.
+ setmouse(); // Disable mouse in xterm.
+ no_mapping++;
+ int r = ' ';
while (r != 'y' && r != 'n') {
- /* same highlighting as for wait_return */
- smsg_attr(hl_attr(HLF_R),
- "%s (y/n)?", str);
- if (direct)
+ // Same highlighting as for wait_return.
+ smsg_attr(HL_ATTR(HLF_R), "%s (y/n)?", str);
+ if (direct) {
r = get_keystroke();
- else
+ } else {
r = plain_vgetc();
- if (r == Ctrl_C || r == ESC)
+ }
+ if (r == Ctrl_C || r == ESC) {
r = 'n';
- msg_putchar(r); /* show what you typed */
+ }
+ msg_putchar(r); // Show what you typed.
ui_flush();
}
- --no_wait_return;
+ no_wait_return--;
State = save_State;
setmouse();
- --no_mapping;
- --allow_keys;
+ no_mapping--;
return r;
}
@@ -2328,8 +2375,8 @@ int get_keystroke(void)
* terminal code to complete. */
n = os_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
if (n > 0) {
- /* Replace zero and CSI by a special key code. */
- n = fix_input_buffer(buf + len, n, FALSE);
+ // Replace zero and CSI by a special key code.
+ n = fix_input_buffer(buf + len, n);
len += n;
waited = 0;
} else if (len > 0)
@@ -2365,16 +2412,12 @@ int get_keystroke(void)
}
break;
}
- if (has_mbyte) {
- if (MB_BYTE2LEN(n) > len)
- continue; /* more bytes to get */
- buf[len >= buflen ? buflen - 1 : len] = NUL;
- n = (*mb_ptr2char)(buf);
+ if (MB_BYTE2LEN(n) > len) {
+ // more bytes to get.
+ continue;
}
-#ifdef UNIX
- if (n == intr_char)
- n = ESC;
-#endif
+ buf[len >= buflen ? buflen - 1 : len] = NUL;
+ n = utf_ptr2char(buf);
break;
}
xfree(buf);
@@ -2405,8 +2448,7 @@ get_number (
if (msg_silent != 0)
return 0;
- ++no_mapping;
- ++allow_keys; /* no mapping here, but recognize keys */
+ no_mapping++;
for (;; ) {
ui_cursor_goto(msg_row, msg_col);
c = safe_vgetc();
@@ -2434,8 +2476,7 @@ get_number (
} else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
break;
}
- --no_mapping;
- --allow_keys;
+ no_mapping--;
return n;
}
@@ -2461,7 +2502,7 @@ int prompt_for_number(int *mouse_used)
save_cmdline_row = cmdline_row;
cmdline_row = 0;
save_State = State;
- State = CMDLINE;
+ State = ASKMORE; // prevents a screen update when using a timer
i = get_number(TRUE, mouse_used);
if (KeyTyped) {
@@ -2511,8 +2552,9 @@ void msgmore(long n)
vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
_("%" PRId64 " fewer lines"), (int64_t)pn);
}
- if (got_int)
- vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
+ if (got_int) {
+ xstrlcat((char *)msg_buf, _(" (Interrupted)"), MSG_BUF_LEN);
+ }
if (msg(msg_buf)) {
set_keep_msg(msg_buf, 0);
keep_msg_more = TRUE;
@@ -2526,7 +2568,7 @@ void msgmore(long n)
void beep_flush(void)
{
if (emsg_silent == 0) {
- flush_buffers(false);
+ flush_buffers(FLUSH_MINIMAL);
vim_beep(BO_ERROR);
}
}
@@ -2538,17 +2580,17 @@ void vim_beep(unsigned val)
if (emsg_silent == 0) {
if (!((bo_flags & val) || (bo_flags & BO_ALL))) {
if (p_vb) {
- ui_visual_bell();
+ ui_call_visual_bell();
} else {
- ui_putc(BELL);
+ ui_call_bell();
}
}
/* When 'verbose' is set and we are sourcing a script or executing a
* function give the user a hint where the beep comes from. */
if (vim_strchr(p_debug, 'e') != NULL) {
- msg_source(hl_attr(HLF_W));
- msg_attr((char_u *)_("Beep!"), hl_attr(HLF_W));
+ msg_source(HL_ATTR(HLF_W));
+ msg_attr(_("Beep!"), HL_ATTR(HLF_W));
}
}
}
@@ -2611,24 +2653,28 @@ int match_user(char_u *name)
return result;
}
-/*
- * Preserve files and exit.
- * When called IObuff must contain a message.
- * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
- * functions, such as allocating memory.
- */
+/// Preserve files and exit.
+/// @note IObuff must contain a message.
+/// @note This may be called from deadly_signal() in a signal handler, avoid
+/// unsafe functions, such as allocating memory.
void preserve_exit(void)
+ FUNC_ATTR_NORETURN
{
// 'true' when we are sure to exit, e.g., after a deadly signal
static bool really_exiting = false;
// Prevent repeated calls into this method.
if (really_exiting) {
- stream_set_blocking(input_global_fd(), true); //normalize stream (#2598)
+ if (input_global_fd() >= 0) {
+ // normalize stream (#2598)
+ stream_set_blocking(input_global_fd(), true);
+ }
exit(2);
}
really_exiting = true;
+ // Ignore SIGHUP while we are already exiting. #9274
+ signal_reject_deadly();
mch_errmsg(IObuff);
mch_errmsg("\n");
ui_flush();
@@ -2639,7 +2685,7 @@ void preserve_exit(void)
if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL) {
mch_errmsg((uint8_t *)"Vim: preserving files...\n");
ui_flush();
- ml_sync_all(false, false); // preserve all swap files
+ ml_sync_all(false, false, true); // preserve all swap files
break;
}
}
@@ -2683,6 +2729,44 @@ void fast_breakcheck(void)
}
}
+/// os_call_shell() wrapper. Handles 'verbose', :profile, and v:shell_error.
+/// Invalidates cached tags.
+///
+/// @return shell command exit code
+int call_shell(char_u *cmd, ShellOpts opts, char_u *extra_shell_arg)
+{
+ int retval;
+ proftime_T wait_time;
+
+ if (p_verbose > 3) {
+ verbose_enter();
+ smsg(_("Executing command: \"%s\""), cmd == NULL ? p_sh : cmd);
+ msg_putchar('\n');
+ verbose_leave();
+ }
+
+ if (do_profiling == PROF_YES) {
+ prof_child_enter(&wait_time);
+ }
+
+ if (*p_sh == NUL) {
+ EMSG(_(e_shellempty));
+ retval = -1;
+ } else {
+ // The external command may update a tags file, clear cached tags.
+ tag_freematch();
+
+ retval = os_call_shell(cmd, opts, extra_shell_arg);
+ }
+
+ set_vim_var_nr(VV_SHELL_ERROR, (varnumber_T)retval);
+ if (do_profiling == PROF_YES) {
+ prof_child_exit(&wait_time);
+ }
+
+ return retval;
+}
+
/// Get the stdout of an external command.
/// If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
/// NULL store the length there.
@@ -2779,4 +2863,3 @@ int goto_im(void)
{
return p_im && stuff_empty() && typebuf_typed();
}
-