Skip to content

Commit 2288b39

Browse files
committed
Merge remote-tracking branch 'vim/master'
2 parents 4b08a13 + 8c64a36 commit 2288b39

11 files changed

Lines changed: 169 additions & 3 deletions

File tree

runtime/doc/eval.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2463,6 +2463,7 @@ tolower({expr}) String the String {expr} switched to lowercase
24632463
toupper({expr}) String the String {expr} switched to uppercase
24642464
tr({src}, {fromstr}, {tostr}) String translate chars of {src} in {fromstr}
24652465
to chars in {tostr}
2466+
trim({text}[, {mask}]) String trim characters in {mask} from {text}
24662467
trunc({expr}) Float truncate Float {expr}
24672468
type({name}) Number type of variable {name}
24682469
undofile({name}) String undo file name for {name}
@@ -8659,6 +8660,22 @@ tr({src}, {fromstr}, {tostr}) *tr()*
86598660
echo tr("<blob>", "<>", "{}")
86608661
< returns "{blob}"
86618662

8663+
trim({text}[, {mask}]) *trim()*
8664+
Return {text} as a String where any character in {mask} is
8665+
removed from the beginning and end of {text}.
8666+
If {mask} is not given, {mask} is all characters up to 0x20,
8667+
which includes Tab, space, NL and CR, plus the non-breaking
8668+
space character 0xa0.
8669+
This code deals with multibyte characters properly.
8670+
8671+
Examples: >
8672+
echo trim(" \r\t\t\r RESERVE \t \t\n\x0B\x0B")."_TAIL"
8673+
< returns "RESERVE_TAIL" >
8674+
echo trim("needrmvRESERVEnnneeedddrrmmmmvv", "ednmrv")
8675+
< returns "RESERVE" >
8676+
echo trim("rm<blob1><blob2><any_chars>rrmm<blob1><blob2><blob2>", "rm<blob1><blob2>")
8677+
< returns "any_chas"
8678+
86628679
trunc({expr}) *trunc()*
86638680
Return the largest integral value with magnitude less than or
86648681
equal to {expr} as a |Float| (truncate towards zero).

src/buffer.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,13 @@ open_buffer(
291291
unchanged(curbuf, FALSE);
292292
save_file_ff(curbuf); /* keep this fileformat */
293293

294+
/* Set last_changedtick to avoid triggering a TextChanged autocommand right
295+
* after it was added. */
296+
curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
297+
#ifdef FEAT_INS_EXPAND
298+
curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
299+
#endif
300+
294301
/* require "!" to overwrite the file, because it wasn't read completely */
295302
#ifdef FEAT_EVAL
296303
if (aborting())

src/evalfunc.c

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
430430
static void f_tolower(typval_T *argvars, typval_T *rettv);
431431
static void f_toupper(typval_T *argvars, typval_T *rettv);
432432
static void f_tr(typval_T *argvars, typval_T *rettv);
433+
static void f_trim(typval_T *argvars, typval_T *rettv);
433434
#ifdef FEAT_FLOAT
434435
static void f_trunc(typval_T *argvars, typval_T *rettv);
435436
#endif
@@ -899,6 +900,7 @@ static struct fst
899900
{"tolower", 1, 1, f_tolower},
900901
{"toupper", 1, 1, f_toupper},
901902
{"tr", 3, 3, f_tr},
903+
{"trim", 1, 2, f_trim},
902904
#ifdef FEAT_FLOAT
903905
{"trunc", 1, 1, f_trunc},
904906
#endif
@@ -5546,7 +5548,7 @@ f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
55465548
return;
55475549
#ifdef FEAT_GUI
55485550
if (gui.in_use)
5549-
gui_mch_get_winpos(&x, &y);
5551+
(void)gui_mch_get_winpos(&x, &y);
55505552
# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
55515553
else
55525554
# endif
@@ -13230,6 +13232,72 @@ f_tr(typval_T *argvars, typval_T *rettv)
1323013232
rettv->vval.v_string = ga.ga_data;
1323113233
}
1323213234

13235+
/*
13236+
* "trim({expr})" function
13237+
*/
13238+
static void
13239+
f_trim(typval_T *argvars, typval_T *rettv)
13240+
{
13241+
char_u buf1[NUMBUFLEN];
13242+
char_u buf2[NUMBUFLEN];
13243+
char_u *head = get_tv_string_buf_chk(&argvars[0], buf1);
13244+
char_u *mask = NULL;
13245+
char_u *tail;
13246+
char_u *prev;
13247+
char_u *p;
13248+
int c1;
13249+
13250+
rettv->v_type = VAR_STRING;
13251+
if (head == NULL)
13252+
{
13253+
rettv->vval.v_string = NULL;
13254+
return;
13255+
}
13256+
13257+
if (argvars[1].v_type == VAR_STRING)
13258+
mask = get_tv_string_buf_chk(&argvars[1], buf2);
13259+
13260+
while (*head != NUL)
13261+
{
13262+
c1 = PTR2CHAR(head);
13263+
if (mask == NULL)
13264+
{
13265+
if (c1 > ' ' && c1 != 0xa0)
13266+
break;
13267+
}
13268+
else
13269+
{
13270+
for (p = mask; *p != NUL; MB_PTR_ADV(p))
13271+
if (c1 == PTR2CHAR(p))
13272+
break;
13273+
if (*p == NUL)
13274+
break;
13275+
}
13276+
MB_PTR_ADV(head);
13277+
}
13278+
13279+
for (tail = head + STRLEN(head); tail > head; tail = prev)
13280+
{
13281+
prev = tail;
13282+
MB_PTR_BACK(head, prev);
13283+
c1 = PTR2CHAR(prev);
13284+
if (mask == NULL)
13285+
{
13286+
if (c1 > ' ' && c1 != 0xa0)
13287+
break;
13288+
}
13289+
else
13290+
{
13291+
for (p = mask; *p != NUL; MB_PTR_ADV(p))
13292+
if (c1 == PTR2CHAR(p))
13293+
break;
13294+
if (*p == NUL)
13295+
break;
13296+
}
13297+
}
13298+
rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13299+
}
13300+
1323313301
#ifdef FEAT_FLOAT
1323413302
/*
1323513303
* "trunc({float})" function

src/libvterm/src/vterm_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include <stdarg.h>
77

8-
#if defined(__GNUC__)
8+
#if defined(__GNUC__) && !defined(__MINGW32__)
99
# define INTERNAL __attribute__((visibility("internal")))
1010
# define UNUSED __attribute__((unused))
1111
#else

src/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,9 @@ getout(int exitval)
14641464
win_T *wp;
14651465

14661466
exiting = TRUE;
1467+
#if defined(FEAT_JOB_CHANNEL)
1468+
ch_log(NULL, "Exiting...");
1469+
#endif
14671470

14681471
/* When running in Ex mode an error causes us to exit with a non-zero exit
14691472
* code. POSIX requires this, although it's not 100% clear from the

src/os_unix.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ mch_total_mem(int special UNUSED)
603603
# ifdef MAC_OS_X_VERSION_10_9
604604
+ vm_stat.compressor_page_count
605605
# endif
606-
) * getpagesize();
606+
) * sysconf(_SC_PAGESIZE);
607607
mach_port_deallocate(mach_task_self(), host);
608608
}
609609
# endif

src/terminal.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3401,6 +3401,15 @@ f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
34013401

34023402
for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
34033403
{
3404+
int c = cell.chars[i];
3405+
int pc = prev_cell.chars[i];
3406+
3407+
/* For the first character NUL is the same as space. */
3408+
if (i == 0)
3409+
{
3410+
c = (c == NUL) ? ' ' : c;
3411+
pc = (pc == NUL) ? ' ' : pc;
3412+
}
34043413
if (cell.chars[i] != prev_cell.chars[i])
34053414
same_chars = FALSE;
34063415
if (cell.chars[i] == NUL || prev_cell.chars[i] == NUL)

src/testdir/screendump.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ source shared.vim
2626
"
2727
" Options is a dictionary (not used yet).
2828
func RunVimInTerminal(arguments, options)
29+
" If Vim doesn't exit a swap file remains, causing other tests to fail.
30+
" Remove it here.
31+
call delete(".swp")
32+
2933
" Make a horizontal and vertical split, so that we can get exactly the right
3034
" size terminal window. Works only when we currently have one window.
3135
call assert_equal(1, winnr('$'))

src/testdir/test_autocmd.vim

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
" Tests for autocommands
22

3+
source shared.vim
4+
35
func! s:cleanup_buffers() abort
46
for bnr in range(1, bufnr('$'))
57
if bufloaded(bnr) && bufnr('%') != bnr
@@ -1304,3 +1306,22 @@ func Test_ChangedP()
13041306

13051307
bw!
13061308
endfunc
1309+
1310+
func Test_Changed_FirstTime()
1311+
if !has('terminal') || has('gui_running')
1312+
return
1313+
endif
1314+
" Prepare file for TextChanged event.
1315+
call writefile([''], 'Xchanged.txt')
1316+
let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
1317+
call assert_equal('running', term_getstatus(buf))
1318+
" It's only adding autocmd, so that no event occurs.
1319+
call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
1320+
call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
1321+
call WaitFor({-> term_getstatus(buf) == 'finished'})
1322+
call assert_equal([''], readfile('Xchanged.txt'))
1323+
1324+
" clean up
1325+
call delete('Xchanged.txt')
1326+
bwipe!
1327+
endfunc

src/testdir/test_functions.vim

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,3 +876,26 @@ func Test_shellescape()
876876

877877
let &shell = save_shell
878878
endfunc
879+
880+
func Test_trim()
881+
call assert_equal("Testing", trim(" \t\r\r\x0BTesting \t\n\r\n\t\x0B\x0B"))
882+
call assert_equal("Testing", trim(" \t \r\r\n\n\x0BTesting \t\n\r\n\t\x0B\x0B"))
883+
call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t"))
884+
call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww"))
885+
call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail"))
886+
call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " "))
887+
call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx"))
888+
call assert_equal("RESERVE", trim("你RESERVE好", "你好"))
889+
call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好"))
890+
call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", ))
891+
call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好"))
892+
call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes"))
893+
call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses"))
894+
call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要"))
895+
call assert_equal("", trim("", ""))
896+
call assert_equal("a", trim("a", ""))
897+
call assert_equal("", trim("", "a"))
898+
899+
let chars = join(map(range(1, 0x20) + [0xa0], {n -> nr2char(n)}), '')
900+
call assert_equal("x", trim(chars . "x" . chars))
901+
endfunc

0 commit comments

Comments
 (0)