diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt index 97df49c1b3..506a7b8c6e 100644 --- a/runtime/doc/eval.txt +++ b/runtime/doc/eval.txt @@ -2463,6 +2463,7 @@ tolower({expr}) String the String {expr} switched to lowercase toupper({expr}) String the String {expr} switched to uppercase tr({src}, {fromstr}, {tostr}) String translate chars of {src} in {fromstr} to chars in {tostr} +trim({text}[, {mask}]) String trim characters in {mask} from {text} trunc({expr}) Float truncate Float {expr} type({name}) Number type of variable {name} undofile({name}) String undo file name for {name} @@ -8659,6 +8660,22 @@ tr({src}, {fromstr}, {tostr}) *tr()* echo tr("", "<>", "{}") < returns "{blob}" +trim({text}[, {mask}]) *trim()* + Return {text} as a String where any character in {mask} is + removed from the beginning and end of {text}. + If {mask} is not given, {mask} is all characters up to 0x20, + which includes Tab, space, NL and CR, plus the non-breaking + space character 0xa0. + This code deals with multibyte characters properly. + + Examples: > + echo trim(" \r\t\t\r RESERVE \t \t\n\x0B\x0B")."_TAIL" +< returns "RESERVE_TAIL" > + echo trim("needrmvRESERVEnnneeedddrrmmmmvv", "ednmrv") +< returns "RESERVE" > + echo trim("rmrrmm", "rm") +< returns "any_chas" + trunc({expr}) *trunc()* Return the largest integral value with magnitude less than or equal to {expr} as a |Float| (truncate towards zero). diff --git a/src/buffer.c b/src/buffer.c index af9d5b7ba9..69a8112ee8 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -291,6 +291,13 @@ open_buffer( unchanged(curbuf, FALSE); save_file_ff(curbuf); /* keep this fileformat */ + /* Set last_changedtick to avoid triggering a TextChanged autocommand right + * after it was added. */ + curbuf->b_last_changedtick = CHANGEDTICK(curbuf); +#ifdef FEAT_INS_EXPAND + curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf); +#endif + /* require "!" to overwrite the file, because it wasn't read completely */ #ifdef FEAT_EVAL if (aborting()) diff --git a/src/evalfunc.c b/src/evalfunc.c index 7740706509..3d01cc22a0 100644 --- a/src/evalfunc.c +++ b/src/evalfunc.c @@ -430,6 +430,7 @@ static void f_timer_stopall(typval_T *argvars, typval_T *rettv); static void f_tolower(typval_T *argvars, typval_T *rettv); static void f_toupper(typval_T *argvars, typval_T *rettv); static void f_tr(typval_T *argvars, typval_T *rettv); +static void f_trim(typval_T *argvars, typval_T *rettv); #ifdef FEAT_FLOAT static void f_trunc(typval_T *argvars, typval_T *rettv); #endif @@ -899,6 +900,7 @@ static struct fst {"tolower", 1, 1, f_tolower}, {"toupper", 1, 1, f_toupper}, {"tr", 3, 3, f_tr}, + {"trim", 1, 2, f_trim}, #ifdef FEAT_FLOAT {"trunc", 1, 1, f_trunc}, #endif @@ -5546,7 +5548,7 @@ f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv) return; #ifdef FEAT_GUI if (gui.in_use) - gui_mch_get_winpos(&x, &y); + (void)gui_mch_get_winpos(&x, &y); # if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE) else # endif @@ -13230,6 +13232,72 @@ error: rettv->vval.v_string = ga.ga_data; } +/* + * "trim({expr})" function + */ + static void +f_trim(typval_T *argvars, typval_T *rettv) +{ + char_u buf1[NUMBUFLEN]; + char_u buf2[NUMBUFLEN]; + char_u *head = get_tv_string_buf_chk(&argvars[0], buf1); + char_u *mask = NULL; + char_u *tail; + char_u *prev; + char_u *p; + int c1; + + rettv->v_type = VAR_STRING; + if (head == NULL) + { + rettv->vval.v_string = NULL; + return; + } + + if (argvars[1].v_type == VAR_STRING) + mask = get_tv_string_buf_chk(&argvars[1], buf2); + + while (*head != NUL) + { + c1 = PTR2CHAR(head); + if (mask == NULL) + { + if (c1 > ' ' && c1 != 0xa0) + break; + } + else + { + for (p = mask; *p != NUL; MB_PTR_ADV(p)) + if (c1 == PTR2CHAR(p)) + break; + if (*p == NUL) + break; + } + MB_PTR_ADV(head); + } + + for (tail = head + STRLEN(head); tail > head; tail = prev) + { + prev = tail; + MB_PTR_BACK(head, prev); + c1 = PTR2CHAR(prev); + if (mask == NULL) + { + if (c1 > ' ' && c1 != 0xa0) + break; + } + else + { + for (p = mask; *p != NUL; MB_PTR_ADV(p)) + if (c1 == PTR2CHAR(p)) + break; + if (*p == NUL) + break; + } + } + rettv->vval.v_string = vim_strnsave(head, (int)(tail - head)); +} + #ifdef FEAT_FLOAT /* * "trunc({float})" function diff --git a/src/libvterm/src/vterm_internal.h b/src/libvterm/src/vterm_internal.h index 3e7f1e5cb3..a06d57775c 100644 --- a/src/libvterm/src/vterm_internal.h +++ b/src/libvterm/src/vterm_internal.h @@ -5,7 +5,7 @@ #include -#if defined(__GNUC__) +#if defined(__GNUC__) && !defined(__MINGW32__) # define INTERNAL __attribute__((visibility("internal"))) # define UNUSED __attribute__((unused)) #else diff --git a/src/main.c b/src/main.c index 634e25b937..aa026e0e9c 100644 --- a/src/main.c +++ b/src/main.c @@ -1464,6 +1464,9 @@ getout(int exitval) win_T *wp; exiting = TRUE; +#if defined(FEAT_JOB_CHANNEL) + ch_log(NULL, "Exiting..."); +#endif /* When running in Ex mode an error causes us to exit with a non-zero exit * code. POSIX requires this, although it's not 100% clear from the diff --git a/src/os_unix.c b/src/os_unix.c index 150ca57aca..3dee1c7c32 100644 --- a/src/os_unix.c +++ b/src/os_unix.c @@ -603,7 +603,7 @@ mch_total_mem(int special UNUSED) # ifdef MAC_OS_X_VERSION_10_9 + vm_stat.compressor_page_count # endif - ) * getpagesize(); + ) * sysconf(_SC_PAGESIZE); mach_port_deallocate(mach_task_self(), host); } # endif diff --git a/src/terminal.c b/src/terminal.c index b406609599..8019e899c0 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -3401,6 +3401,15 @@ f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED) for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i) { + int c = cell.chars[i]; + int pc = prev_cell.chars[i]; + + /* For the first character NUL is the same as space. */ + if (i == 0) + { + c = (c == NUL) ? ' ' : c; + pc = (pc == NUL) ? ' ' : pc; + } if (cell.chars[i] != prev_cell.chars[i]) same_chars = FALSE; if (cell.chars[i] == NUL || prev_cell.chars[i] == NUL) diff --git a/src/testdir/screendump.vim b/src/testdir/screendump.vim index 1016646c5b..c84221580c 100644 --- a/src/testdir/screendump.vim +++ b/src/testdir/screendump.vim @@ -26,6 +26,10 @@ source shared.vim " " Options is a dictionary (not used yet). func RunVimInTerminal(arguments, options) + " If Vim doesn't exit a swap file remains, causing other tests to fail. + " Remove it here. + call delete(".swp") + " Make a horizontal and vertical split, so that we can get exactly the right " size terminal window. Works only when we currently have one window. call assert_equal(1, winnr('$')) diff --git a/src/testdir/test_autocmd.vim b/src/testdir/test_autocmd.vim index 7716340352..a8142ef55c 100644 --- a/src/testdir/test_autocmd.vim +++ b/src/testdir/test_autocmd.vim @@ -1,5 +1,7 @@ " Tests for autocommands +source shared.vim + func! s:cleanup_buffers() abort for bnr in range(1, bufnr('$')) if bufloaded(bnr) && bufnr('%') != bnr @@ -1304,3 +1306,22 @@ func Test_ChangedP() bw! endfunc + +func Test_Changed_FirstTime() + if !has('terminal') || has('gui_running') + return + endif + " Prepare file for TextChanged event. + call writefile([''], 'Xchanged.txt') + let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3}) + call assert_equal('running', term_getstatus(buf)) + " It's only adding autocmd, so that no event occurs. + call term_sendkeys(buf, ":au! TextChanged call writefile(['No'], 'Xchanged.txt')\") + call term_sendkeys(buf, "\\:qa!\") + call WaitFor({-> term_getstatus(buf) == 'finished'}) + call assert_equal([''], readfile('Xchanged.txt')) + + " clean up + call delete('Xchanged.txt') + bwipe! +endfunc diff --git a/src/testdir/test_functions.vim b/src/testdir/test_functions.vim index ffc3bc3785..49e5d1f1a6 100644 --- a/src/testdir/test_functions.vim +++ b/src/testdir/test_functions.vim @@ -876,3 +876,26 @@ func Test_shellescape() let &shell = save_shell endfunc + +func Test_trim() + call assert_equal("Testing", trim(" \t\r\r\x0BTesting \t\n\r\n\t\x0B\x0B")) + call assert_equal("Testing", trim(" \t \r\r\n\n\x0BTesting \t\n\r\n\t\x0B\x0B")) + call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t")) + call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww")) + call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail")) + call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " ")) + call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx")) + call assert_equal("RESERVE", trim("你RESERVE好", "你好")) + call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好")) + call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", )) + call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好")) + call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes")) + call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses")) + call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要")) + call assert_equal("", trim("", "")) + call assert_equal("a", trim("a", "")) + call assert_equal("", trim("", "a")) + + let chars = join(map(range(1, 0x20) + [0xa0], {n -> nr2char(n)}), '') + call assert_equal("x", trim(chars . "x" . chars)) +endfunc diff --git a/src/version.c b/src/version.c index 9777f32f9b..045c5f264c 100644 --- a/src/version.c +++ b/src/version.c @@ -781,6 +781,20 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ +/**/ + 1633, +/**/ + 1632, +/**/ + 1631, +/**/ + 1630, +/**/ + 1629, +/**/ + 1628, +/**/ + 1627, /**/ 1626, /**/