# HG changeset patch # User Bram Moolenaar # Date 1659622508 -7200 # Node ID f2d7f20d83c3939699545fa9fad85bfb65465662 # Parent aea55855290421be9aee60fc10200e307377f641 patch 9.0.0139: truncating virtual text after a line not implemented Commit: https://github.com/vim/vim/commit/398649ee44edeb309c77361de697320378104b70 Author: Bram Moolenaar Date: Thu Aug 4 15:03:48 2022 +0100 patch 9.0.0139: truncating virtual text after a line not implemented Problem: Truncating virtual text after a line not implemented. Cursor positioning wrong with Newline in the text. Solution: Implement truncating. Disallow control characters in the text. (closes #10842) diff --git a/src/charset.c b/src/charset.c --- a/src/charset.c +++ b/src/charset.c @@ -655,6 +655,9 @@ char2cells(int c) int ptr2cells(char_u *p) { + if (!has_mbyte) + return byte2cells(*p); + // For UTF-8 we need to look at more bytes if the first byte is >= 0x80. if (enc_utf8 && *p >= 0x80) return utf_ptr2cells(p); @@ -682,16 +685,13 @@ vim_strnsize(char_u *s, int len) int size = 0; while (*s != NUL && --len >= 0) - if (has_mbyte) - { - int l = (*mb_ptr2len)(s); + { + int l = (*mb_ptr2len)(s); - size += ptr2cells(s); - s += l; - len -= l - 1; - } - else - size += byte2cells(*s++); + size += ptr2cells(s); + s += l; + len -= l - 1; + } return size; } @@ -1026,6 +1026,40 @@ lbr_chartabsize_adv(chartabsize_T *cts) return retval; } +#if defined(FEAT_PROP_POPUP) || defined(PROTO) +/* + * Return the cell size of virtual text after truncation. + */ + int +textprop_size_after_trunc( + win_T *wp, + int below, + int added, + char_u *text, + int *n_used_ptr) +{ + int space = below ? wp->w_width : added; + int len = (int)STRLEN(text); + int strsize = 0; + int n_used; + + // if the remaining size is to small wrap + // anyway and use the next line + if (space < PROP_TEXT_MIN_CELLS) + space += wp->w_width; + for (n_used = 0; n_used < len; n_used += (*mb_ptr2len)(text + n_used)) + { + int clen = ptr2cells(text + n_used); + + if (strsize + clen > space) + break; + strsize += clen; + } + *n_used_ptr = n_used; + return strsize; +} +#endif + /* * Return the screen size of the character indicated by "cts". * "cts->cts_cur_text_width" is set to the extra size for a text property that @@ -1110,16 +1144,28 @@ win_lbr_chartabsize( { char_u *p = ((char_u **)wp->w_buffer->b_textprop_text.ga_data)[ -tp->tp_id - 1]; - int len = vim_strsize(p); + int cells = vim_strsize(p); + added = wp->w_width - (vcol + size) % wp->w_width; if (tp->tp_col == MAXCOL) { - // TODO: truncating - if (tp->tp_flags & TP_FLAG_ALIGN_BELOW) - len += wp->w_width - (vcol + size) % wp->w_width; + int below = (tp->tp_flags & TP_FLAG_ALIGN_BELOW); + int wrap = (tp->tp_flags & TP_FLAG_WRAP); + int len = (int)STRLEN(p); + int n_used = len; + + // Keep in sync with where textprop_size_after_trunc() is + // called in win_line(). + if (!wrap) + cells = textprop_size_after_trunc(wp, + below, added, p, &n_used); + // right-aligned does not really matter here, same as + // "after" + if (below) + cells += wp->w_width - (vcol + size) % wp->w_width; } - cts->cts_cur_text_width += len; - size += len; + cts->cts_cur_text_width += cells; + size += cells; } if (tp->tp_col - 1 > col) break; diff --git a/src/drawline.c b/src/drawline.c --- a/src/drawline.c +++ b/src/drawline.c @@ -1554,6 +1554,8 @@ win_line( & TP_FLAG_ALIGN_RIGHT); int below = (text_props[used_tpi].tp_flags & TP_FLAG_ALIGN_BELOW); + int wrap = (text_props[used_tpi].tp_flags + & TP_FLAG_WRAP); p_extra = p; c_extra = NUL; @@ -1566,26 +1568,53 @@ win_line( // don't combine char attr after EOL text_prop_combine = FALSE; - // TODO: truncation if it doesn't fit - if (right || below) + // Keep in sync with where + // textprop_size_after_trunc() is called in + // win_lbr_chartabsize(). + if ((right || below || !wrap) && wp->w_width > 2) { int added = wp->w_width - col; + int n_used = n_extra; char_u *l; + int strsize = wrap + ? vim_strsize(p_extra) + : textprop_size_after_trunc(wp, + below, added, p_extra, &n_used); - // Right-align: fill with spaces - if (right) - added -= vim_strsize(p_extra); - if (added < 0 || (below && col == 0)) - added = 0; - l = alloc(n_extra + added + 1); - if (l != NULL) + if (wrap || right || below || n_used < n_extra) { - vim_memset(l, ' ', added); - STRCPY(l + added, p); - vim_free(p_extra_free); - p_extra = p_extra_free = l; - n_extra += added; - n_attr_skip = added; + // Right-align: fill with spaces + if (right) + added -= strsize; + if (added < 0 || (below && col == 0) + || (!below && n_used < n_extra)) + added = 0; + // add 1 for NUL, 2 for when '…' is used + l = alloc(n_used + added + 3); + if (l != NULL) + { + vim_memset(l, ' ', added); + vim_strncpy(l + added, p_extra, n_used); + if (n_used < n_extra) + { + char_u *lp = l + added + n_used - 1; + + if (has_mbyte) + { + // change last character to '…' + lp -= (*mb_head_off)(l, lp); + STRCPY(lp, "…"); + n_used = lp - l + 3; + } + else + // change last character to '>' + *lp = '>'; + } + vim_free(p_extra_free); + p_extra = p_extra_free = l; + n_extra = n_used + added; + n_attr_skip = added; + } } } } @@ -1598,6 +1627,14 @@ win_line( text_prop_follows = other_tpi != -1; } } + else if (text_prop_next < text_prop_count + && text_props[text_prop_next].tp_col == MAXCOL + && *ptr != NUL + && ptr[mb_ptr2len(ptr)] == NUL) + // When at last-but-one character and a text property + // follows after it, we may need to flush the line after + // displaying that character. + text_prop_follows = TRUE; } #endif diff --git a/src/proto/charset.pro b/src/proto/charset.pro --- a/src/proto/charset.pro +++ b/src/proto/charset.pro @@ -33,6 +33,7 @@ void init_chartabsize_arg(chartabsize_T void clear_chartabsize_arg(chartabsize_T *cts); int lbr_chartabsize(chartabsize_T *cts); int lbr_chartabsize_adv(chartabsize_T *cts); +int textprop_size_after_trunc(win_T *wp, int below, int added, char_u *text, int *n_used_ptr); int win_lbr_chartabsize(chartabsize_T *cts, int *headp); void getvcol(win_T *wp, pos_T *pos, colnr_T *start, colnr_T *cursor, colnr_T *end); colnr_T getvcol_nolist(pos_T *posp); diff --git a/src/structs.h b/src/structs.h --- a/src/structs.h +++ b/src/structs.h @@ -815,6 +815,8 @@ typedef struct textprop_S #define TP_FLAG_WRAP 0x40 // virtual text wraps - when missing // text is truncated +#define PROP_TEXT_MIN_CELLS 4 // minimun number of cells to use for + // the text, even when truncating /* * Structure defining a property type. diff --git a/src/testdir/dumps/Test_prop_with_text_after_trunc_1.dump b/src/testdir/dumps/Test_prop_with_text_after_trunc_1.dump new file mode 100644 --- /dev/null +++ b/src/testdir/dumps/Test_prop_with_text_after_trunc_1.dump @@ -0,0 +1,9 @@ +|o+0&#ffffff0|n|e| |t|w|o| |t|h|r|e@1| |f|o|u|r| |f|i|v|e| |s|i|x| |s|e|v|e|n| +0&#ffff4012|O|N|E| |a|n|d| |T|W|O| |a|n|d| |T|H|R|E@1| |a|n|d|… +|o+0&#ffffff0|n|e| |t|w|o| |t|h|r|e@1| |f|o|u|r| |f|i|v|e| |s|i|x| |s|e|v|e|n| +0&#ffff4012|o|n|e| |A|N|D| |t|w|o| |A|N|D| |t|h|r|e@1| |A|N|D|… +|o+0&#ffffff0|n|e| |t|w|o| |t|h|r|e@1| |f|o|u|r| |f|i|v|e| |s|i|x| |s|e|v|e|n| @26 +| +0&#ffff4012|o|n|e| |A|N|D| |t|w|o| |A|N|D| |t|h|r|e@1| |A|N|D| |f|o|u|r| |A|N|D| |f|i|v|e| |l|e|t|s| |w|r|a|p| |a|f|t|e|r| |s|o|… +|c+0&#ffffff0|u|r|s|o|r| >h|e|r|e| @48 +|~+0#4040ff13&| @58 +|~| @58 +|~| @58 +| +0#0000000&@41|4|,|8| @10|A|l@1| diff --git a/src/testdir/dumps/Test_prop_with_text_after_trunc_2.dump b/src/testdir/dumps/Test_prop_with_text_after_trunc_2.dump new file mode 100644 --- /dev/null +++ b/src/testdir/dumps/Test_prop_with_text_after_trunc_2.dump @@ -0,0 +1,9 @@ +>o+0&#ffffff0|n|e| |t|w|o| |t|h|r|e@1| |f|o|u|r| |f|i|v|e| |s|i|x| |s|e|v|e|n| +0&#ffff4012|O|N|…||+1&#ffffff0|o+0&&|n|e| |t|w|o| |t|h|r|e@1| |f|o|u|r| |f|i|v +|o|n|e| |t|w|o| |t|h|r|e@1| |f|o|u|r| |f|i|v|e| |s|i|x| |s|e|v|e|n| +0&#ffff4012|o|n|…||+1&#ffffff0|e+0&&| |s|i|x| |s|e|v|e|n| +0&#ffff4012|o|n|e| |A|N|D| |t|… +|o+0&#ffffff0|n|e| |t|w|o| |t|h|r|e@1| |f|o|u|r| |f|i|v|e| |s|i|x| |s|e|v|e|n| @3||+1&&|o+0&&|n|e| |t|w|o| |t|h|r|e@1| |f|o|u|r| |f|i|v +| +0&#ffff4012|o|n|e| |A|N|D| |t|w|o| |A|N|D| |t|h|r|e@1| |A|N|D| |f|o|u|r| |A|N|D| |…||+1&#ffffff0|e+0&&| |s|i|x| |s|e|v|e|n| @10 +|c|u|r|s|o|r| |h|e|r|e| @25||+1&&| +0&#ffff4012|o|n|e| |A|N|D| |t|w|o| |A|N|D| |t|h|r|e|… +|~+0#4040ff13#ffffff0| @35||+1#0000000&|c+0&&|u|r|s|o|r| |h|e|r|e| @10 +|~+0#4040ff13&| @35||+1#0000000&|~+0#4040ff13&| @20 +|[+3#0000000&|N|o| |N|a|m|e|]| |[|+|]| @5|1|,|1| @11|A|l@1| |<+1&&|N|a|m|e|]| |[|+|]| |4|,|8| @4|B|o|t +|:+0&&|3|7|v|s|p| @53 diff --git a/src/testdir/dumps/Test_prop_with_text_after_trunc_3.dump b/src/testdir/dumps/Test_prop_with_text_after_trunc_3.dump new file mode 100644 --- /dev/null +++ b/src/testdir/dumps/Test_prop_with_text_after_trunc_3.dump @@ -0,0 +1,9 @@ +|o+0&#ffffff0|n|e| |t|w|o| |t|h|r|e@1| |f|o|u|r| |f|i|v|e| |s|i|x| |s|e|v|e|n| +0&#ffff4012|O|N||+1&#ffffff0|o+0&&|n|e| |t|w|o| |t|h|r|e@1| |f|o|u|r| |f|i|v|e +|E+0&#ffff4012| |a|n|d| |T|W|O| |a|n|d| |T|H|R|E@1| |a|n|d| |F|O|U|R| |a|n|d| |F|I|…||+1&#ffffff0| +0&&|s|i|x| |s|e|v|e|n| +0&#ffff4012|o|n|e| |A|N|D| |t|w|o|… +|o+0&#ffffff0|n|e| |t|w|o| |t|h|r|e@1| |f|o|u|r| |f|i|v|e| |s|i|x| |s|e|v|e>n| +0&#ffff4012|o|n||+1&#ffffff0|o+0&&|n|e| |t|w|o| |t|h|r|e@1| |f|o|u|r| |f|i|v|e +|e+0&#ffff4012| |A|N|D| |t|w|o| |A|N|D| |t|h|r|e@1| |A|N|D| |f|o|u|r| |A|N|D| |f|i|…||+1&#ffffff0| +0&&|s|i|x| |s|e|v|e|n| @12 +|o|n|e| |t|w|o| |t|h|r|e@1| |f|o|u|r| |f|i|v|e| |s|i|x| |s|e|v|e|n| @2||+1&&| +0&#ffff4012|o|n|e| |A|N|D| |t|w|o| |A|N|D| |t|h|r|e@1|… +| |o|n|e| |A|N|D| |t|w|o| |A|N|D| |t|h|r|e@1| |A|N|D| |f|o|u|r| |A|N|D|…||+1&#ffffff0|c+0&&|u|r|s|o|r| |h|e|r|e| @11 +|c|u|r|s|o|r| |h|e|r|e| @24||+1&&|~+0#4040ff13&| @21 +|[+3#0000000&|N|o| |N|a|m|e|]| |[|+|]| @4|2|,|3@1| @10|A|l@1| |<+1&&| |N|a|m|e|]| |[|+|]| |4|,|8| @4|B|o|t +|:+0&&|3|6|w|i|n|c|m|d| ||| @48 diff --git a/src/testdir/dumps/Test_prop_with_text_after_trunc_4.dump b/src/testdir/dumps/Test_prop_with_text_after_trunc_4.dump new file mode 100644 --- /dev/null +++ b/src/testdir/dumps/Test_prop_with_text_after_trunc_4.dump @@ -0,0 +1,9 @@ +|o+0&#ffffff0|n|e| |t|w|o| |t|h|r|e@1| |f|o|u|r| |f|i|v|e| |s|i|x| |s|e|v|e|n||+1&&|o+0&&|n|e| |t|w|o| |t|h|r|e@1| |f|o|u|r| |f|i|v|e| |s|i +| +0&#ffff4012|O|N|E| |a|n|d| |T|W|O| |a|n|d| |T|H|R|E@1| |a|n|d| |F|O|U|R| |…||+1&#ffffff0|x+0&&| |s|e|v|e|n| +0&#ffff4012|o|n|e| |A|N|D| |t|w|o| |A|N|D| |t|… +|o+0&#ffffff0|n|e| |t|w|o| |t|h|r|e@1| |f|o|u|r| |f|i|v|e| |s|i|x| |s|e|v|e>n||+1&&|o+0&&|n|e| |t|w|o| |t|h|r|e@1| |f|o|u|r| |f|i|v|e| |s|i +| +0&#ffff4012|o|n|e| |A|N|D| |t|w|o| |A|N|D| |t|h|r|e@1| |A|N|D| |f|o|u|r| |…||+1&#ffffff0|x+0&&| |s|e|v|e|n| @18 +|o|n|e| |t|w|o| |t|h|r|e@1| |f|o|u|r| |f|i|v|e| |s|i|x| |s|e|v|e|n||+1&&| +0&#ffff4012|o|n|e| |A|N|D| |t|w|o| |A|N|D| |t|h|r|e@1| |A|N|… +| |o|n|e| |A|N|D| |t|w|o| |A|N|D| |t|h|r|e@1| |A|N|D| |f|o|u|r| |…||+1&#ffffff0|c+0&&|u|r|s|o|r| |h|e|r|e| @14 +|c|u|r|s|o|r| |h|e|r|e| @21||+1&&|~+0#4040ff13&| @24 +|[+3#0000000&|N|o| |N|a|m|e|]| |[|+|]| @3|2|,|3@1| @8|A|l@1| |<+1&&|o| |N|a|m|e|]| |[|+|]| |4|,|8| @6|B|o|t +|:+0&&|3@1|w|i|n|c|m|d| ||| @48 diff --git a/src/testdir/dumps/Test_prop_with_text_after_trunc_5.dump b/src/testdir/dumps/Test_prop_with_text_after_trunc_5.dump new file mode 100644 --- /dev/null +++ b/src/testdir/dumps/Test_prop_with_text_after_trunc_5.dump @@ -0,0 +1,9 @@ +|o+0&#ffffff0|n|e| |t|w|o| |t|h|r|e@1| |f|o|u|r||+1&&|o+0&&|n|e| |t|w|o| |t|h|r|e@1| |f|o|u|r| |f|i|v|e| |s|i|x| |s|e|v|e|n| +0&#ffff4012|o|n|e| |A|N|… +| +0&#ffffff0|f|i|v|e| |s|i|x| |s|e|v|e|n| +0&#ffff4012|O|N||+1&#ffffff0|o+0&&|n|e| |t|w|o| |t|h|r|e@1| |f|o|u|r| |f|i|v|e| |s|i|x| |s|e|v|e|n| @7 +|E+0&#ffff4012| |a|n|d| |T|W|O| |a|n|d| |T|H|R|…||+1&#ffffff0| +0&#ffff4012|o|n|e| |A|N|D| |t|w|o| |A|N|D| |t|h|r|e@1| |A|N|D| |f|o|u|r| |A|N|D| |f|i|v|e|… +|o+0&#ffffff0|n|e| |t|w|o| |t|h|r|e@1| |f|o|u|r||+1&&|c+0&&|u|r|s|o|r| |h|e|r|e| @29 +@1|f|i|v|e| |s|i>x| |s|e|v|e|n| +0&#ffff4012|o|n||+1&#ffffff0|~+0#4040ff13&| @39 +|e+0#0000000#ffff4012| |A|N|D| |t|w|o| |A|N|D| |t|h|r|…||+1&#ffffff0|~+0#4040ff13&| @39 +|@@2| @14||+1#0000000&|~+0#4040ff13&| @39 +|<+3#0000000&|m|e|]| |[|+|]| |2|,|2|7| @1|T|o|p| |[+1&&|N|o| |N|a|m|e|]| |[|+|]| @9|4|,|8| @11|B|o|t +|:+0&&|1|8|w|i|n|c|m|d| ||| @48 diff --git a/src/testdir/dumps/Test_prop_with_text_after_wraps_1.dump b/src/testdir/dumps/Test_prop_with_text_after_wraps_1.dump new file mode 100644 --- /dev/null +++ b/src/testdir/dumps/Test_prop_with_text_after_wraps_1.dump @@ -0,0 +1,9 @@ +|o+0&#ffffff0|n|e| |t|w|o| |t|h|r|e@1| |f|o|u|r| |f|i|v|e| |s|i|x| |s|e|v|e|n| +0&#ffff4012|O|N|E| |a|n|d| |T|W|O| |a|n|d| |T|H|R|E@1| |a|n|d| +|F|O|U|R| |a|n|d| |F|I|V|E| +0&#ffffff0@46 +|o|n|e| |t|w|o| |t|h|r|e@1| |f|o|u|r| |f|i|v|e| |s|i|x| |s|e|v|e|n| +0&#ffff4012|o|n|e| |A|N|D| |t|w|o| |A|N|D| |t|h|r|e@1| |A|N|D| +|f|o|u|r| |A|N|D| |f|i|v|e| +0&#ffffff0@46 +|o|n|e| |t|w|o| |t|h|r|e@1| |f|o|u|r| |f|i|v|e| |s|i|x| |s|e|v|e|n| @26 +| +0&#ffff4012|o|n|e| |A|N|D| |t|w|o| |A|N|D| |t|h|r|e@1| |A|N|D| |f|o|u|r| |A|N|D| |f|i|v|e| |l|e|t|s| |w|r|a|p| |a|f|t|e|r| |s|o|m +|e| |m|o|r|e| |t|e|x|t| +0&#ffffff0@48 +|c|u|r|s|o|r| >h|e|r|e| @48 +@42|4|,|8| @10|A|l@1| diff --git a/src/testdir/test_textprop.vim b/src/testdir/test_textprop.vim --- a/src/testdir/test_textprop.vim +++ b/src/testdir/test_textprop.vim @@ -2286,6 +2286,70 @@ func Test_props_with_text_after_joined() call delete('XscriptPropsWithTextAfterJoined') endfunc +func Test_props_with_text_after_truncated() + CheckRunVimInTerminal + + let lines =<< trim END + call setline(1, ['one two three four five six seven']) + call prop_type_add('afterprop', #{highlight: 'Search'}) + call prop_add(1, 0, #{type: 'afterprop', text: ' ONE and TWO and THREE and FOUR and FIVE'}) + + call setline(2, ['one two three four five six seven']) + call prop_add(2, 0, #{type: 'afterprop', text: ' one AND two AND three AND four AND five', text_align: 'right'}) + + call setline(3, ['one two three four five six seven']) + call prop_add(3, 0, #{type: 'afterprop', text: ' one AND two AND three AND four AND five lets wrap after some more text', text_align: 'below'}) + + call setline(4, ['cursor here']) + normal 4Gfh + END + call writefile(lines, 'XscriptPropsWithTextAfterTrunc') + let buf = RunVimInTerminal('-S XscriptPropsWithTextAfterTrunc', #{rows: 9, cols: 60}) + call VerifyScreenDump(buf, 'Test_prop_with_text_after_trunc_1', {}) + + call term_sendkeys(buf, ":37vsp\gg") + call VerifyScreenDump(buf, 'Test_prop_with_text_after_trunc_2', {}) + + call term_sendkeys(buf, ":36wincmd |\") + call term_sendkeys(buf, "2G$") + call VerifyScreenDump(buf, 'Test_prop_with_text_after_trunc_3', {}) + + call term_sendkeys(buf, ":33wincmd |\") + call VerifyScreenDump(buf, 'Test_prop_with_text_after_trunc_4', {}) + + call term_sendkeys(buf, ":18wincmd |\") + call term_sendkeys(buf, "0fx") + call VerifyScreenDump(buf, 'Test_prop_with_text_after_trunc_5', {}) + + call StopVimInTerminal(buf) + call delete('XscriptPropsWithTextAfterTrunc') +endfunc + +func Test_props_with_text_after_wraps() + CheckRunVimInTerminal + + let lines =<< trim END + call setline(1, ['one two three four five six seven']) + call prop_type_add('afterprop', #{highlight: 'Search'}) + call prop_add(1, 0, #{type: 'afterprop', text: ' ONE and TWO and THREE and FOUR and FIVE', text_wrap: 'wrap'}) + + call setline(2, ['one two three four five six seven']) + call prop_add(2, 0, #{type: 'afterprop', text: ' one AND two AND three AND four AND five', text_align: 'right', text_wrap: 'wrap'}) + + call setline(3, ['one two three four five six seven']) + call prop_add(3, 0, #{type: 'afterprop', text: ' one AND two AND three AND four AND five lets wrap after some more text', text_align: 'below', text_wrap: 'wrap'}) + + call setline(4, ['cursor here']) + normal 4Gfh + END + call writefile(lines, 'XscriptPropsWithTextAfterWraps') + let buf = RunVimInTerminal('-S XscriptPropsWithTextAfterWraps', #{rows: 9, cols: 60}) + call VerifyScreenDump(buf, 'Test_prop_with_text_after_wraps_1', {}) + + call StopVimInTerminal(buf) + call delete('XscriptPropsWithTextAfterWraps') +endfunc + func Test_removed_prop_with_text_cleans_up_array() new call setline(1, 'some text here') diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -736,6 +736,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 139, +/**/ 138, /**/ 137,