summaryrefslogtreecommitdiffstats
path: root/src/memline.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/memline.c')
-rw-r--r--src/memline.c78
1 files changed, 65 insertions, 13 deletions
diff --git a/src/memline.c b/src/memline.c
index cf2dc8c..5ca50fc 100644
--- a/src/memline.c
+++ b/src/memline.c
@@ -1754,7 +1754,7 @@ ml_recover(int checkext)
for (idx = 1; idx <= lnum; ++idx)
{
// Need to copy one line, fetching the other one may flush it.
- p = vim_strsave(ml_get(idx));
+ p = vim_strnsave(ml_get(idx), ml_get_len(idx));
i = STRCMP(p, ml_get(idx + lnum));
vim_free(p);
if (i != 0)
@@ -2675,6 +2675,48 @@ ml_get_cursor(void)
curwin->w_cursor.col);
}
+// return length (excluding the NUL) of the given line
+ colnr_T
+ml_get_len(linenr_T lnum)
+{
+ return ml_get_buf_len(curbuf, lnum);
+}
+
+// return length (excluding the NUL) of the text after position "pos"
+ colnr_T
+ml_get_pos_len(pos_T *pos)
+{
+ return ml_get_buf_len(curbuf, pos->lnum) - pos->col;
+}
+
+// return length (excluding the NUL) of the cursor line
+ colnr_T
+ml_get_curline_len(void)
+{
+ return ml_get_buf_len(curbuf, curwin->w_cursor.lnum);
+}
+
+// return length (excluding the NUL) of the cursor position
+ colnr_T
+ml_get_cursor_len(void)
+{
+ return ml_get_buf_len(curbuf, curwin->w_cursor.lnum) - curwin->w_cursor.col;
+}
+
+// return length (excluding the NUL) of the given line in the given buffer
+ colnr_T
+ml_get_buf_len(buf_T *buf, linenr_T lnum)
+{
+ char_u *line;
+
+ if (*(line = ml_get_buf(buf, lnum, FALSE)) == NUL)
+ return 0;
+
+ if (buf->b_ml.ml_line_textlen <= 0)
+ buf->b_ml.ml_line_textlen = (int)STRLEN(line) + 1;
+ return buf->b_ml.ml_line_textlen - 1;
+}
+
/*
* Return a pointer to a line in a specific buffer
*
@@ -2706,6 +2748,7 @@ ml_get_buf(
errorret:
STRCPY(questions, "???");
buf->b_ml.ml_line_len = 4;
+ buf->b_ml.ml_line_textlen = buf->b_ml.ml_line_len;
buf->b_ml.ml_line_lnum = lnum;
return questions;
}
@@ -2715,6 +2758,7 @@ errorret:
if (buf->b_ml.ml_mfp == NULL) // there are no lines
{
buf->b_ml.ml_line_len = 1;
+ buf->b_ml.ml_line_textlen = buf->b_ml.ml_line_len;
return (char_u *)"";
}
@@ -2727,7 +2771,6 @@ errorret:
if (buf->b_ml.ml_line_lnum != lnum || mf_dont_release)
{
unsigned start, end;
- colnr_T len;
int idx;
ml_flush_line(buf);
@@ -2763,10 +2806,18 @@ errorret:
end = dp->db_txt_end;
else
end = ((dp->db_index[idx - 1]) & DB_INDEX_MASK);
- len = end - start;
buf->b_ml.ml_line_ptr = (char_u *)dp + start;
- buf->b_ml.ml_line_len = len;
+ buf->b_ml.ml_line_len = end - start;
+#if defined(FEAT_BYTEOFF) && defined(FEAT_PROP_POPUP)
+ // Text properties come after a NUL byte, so ml_line_len should be
+ // larger than the size of textprop_T if there is any.
+ if (buf->b_has_textprop
+ && (size_t)buf->b_ml.ml_line_len > sizeof(textprop_T))
+ buf->b_ml.ml_line_textlen = 0; // call STRLEN() later when needed
+ else
+#endif
+ buf->b_ml.ml_line_textlen = buf->b_ml.ml_line_len;
buf->b_ml.ml_line_lnum = lnum;
buf->b_ml.ml_flags &= ~(ML_LINE_DIRTY | ML_ALLOCATED);
}
@@ -3400,10 +3451,10 @@ ml_append_int(
#ifdef FEAT_NETBEANS_INTG
if (netbeans_active())
{
- if (STRLEN(line) > 0)
- netbeans_inserted(buf, lnum+1, (colnr_T)0, line, (int)STRLEN(line));
- netbeans_inserted(buf, lnum+1, (colnr_T)STRLEN(line),
- (char_u *)"\n", 1);
+ int line_len = (int)STRLEN(line);
+ if (line_len > 0)
+ netbeans_inserted(buf, lnum+1, (colnr_T)0, line, line_len);
+ netbeans_inserted(buf, lnum+1, (colnr_T)line_len, (char_u *)"\n", 1);
}
#endif
#ifdef FEAT_JOB_CHANNEL
@@ -3531,7 +3582,7 @@ ml_replace(linenr_T lnum, char_u *line, int copy)
* Replace a line for the current buffer. Like ml_replace() with:
* "len_arg" is the length of the text, excluding NUL.
* If "has_props" is TRUE then "line_arg" includes the text properties and
- * "len_arg" includes the NUL of the text.
+ * "len_arg" includes the NUL of the text and text properties.
* When "copy" is TRUE copy the text into allocated memory, otherwise
* "line_arg" must be allocated and will be consumed here.
*/
@@ -3571,7 +3622,7 @@ ml_replace_len(
#ifdef FEAT_NETBEANS_INTG
if (netbeans_active())
{
- netbeans_removed(curbuf, lnum, 0, (long)STRLEN(ml_get(lnum)));
+ netbeans_removed(curbuf, lnum, 0, (long)ml_get_len(lnum));
netbeans_inserted(curbuf, lnum, 0, line, (int)STRLEN(line));
}
#endif
@@ -3617,6 +3668,7 @@ ml_replace_len(
curbuf->b_ml.ml_line_ptr = line;
curbuf->b_ml.ml_line_len = len;
+ curbuf->b_ml.ml_line_textlen = !has_props ? len_arg + 1 : 0;
curbuf->b_ml.ml_line_lnum = lnum;
curbuf->b_ml.ml_flags = (curbuf->b_ml.ml_flags | ML_LINE_DIRTY) & ~ML_EMPTY;
@@ -5192,15 +5244,15 @@ findswapname(
{
char_u *name;
int dialog_result;
+ size_t len = STRLEN(_("Swap file \""));
name = alloc(STRLEN(fname)
- + STRLEN(_("Swap file \""))
+ + len
+ STRLEN(_("\" already exists!")) + 5);
if (name != NULL)
{
STRCPY(name, _("Swap file \""));
- home_replace(NULL, fname, name + STRLEN(name),
- 1000, TRUE);
+ home_replace(NULL, fname, name + len, 1000, TRUE);
STRCAT(name, _("\" already exists!"));
}
dialog_result = do_dialog(VIM_WARNING,