To: vim_dev@googlegroups.com Subject: Patch 9.0.0916 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 9.0.0916 Problem: getbufline() is inefficient for getting a single line. Solution: Add getbufoneline(). Files: runtime/doc/builtin.txt, runtime/doc/usr_41.txt, src/evalbuffer.c, src/proto/evalbuffer.pro, src/evalfunc.c, src/testdir/test_bufline.vim, src/testdir/test_vim9_builtin.vim *** ../vim-9.0.0915/runtime/doc/builtin.txt 2022-11-14 19:49:09.662298086 +0000 --- runtime/doc/builtin.txt 2022-11-21 19:47:24.338905162 +0000 *************** *** 208,213 **** --- 208,214 ---- getbufinfo([{buf}]) List information about buffers getbufline({buf}, {lnum} [, {end}]) List lines {lnum} to {end} of buffer {buf} + getbufoneline({buf}, {lnum}) String line {lnum} of buffer {buf} getbufvar({buf}, {varname} [, {def}]) any variable {varname} in buffer {buf} getchangelist([{buf}]) List list of change list items *************** *** 3204,3210 **** getbufline({buf}, {lnum} [, {end}]) Return a |List| with the lines starting from {lnum} to {end} (inclusive) in the buffer {buf}. If {end} is omitted, a ! |List| with only the line {lnum} is returned. For the use of {buf}, see |bufname()| above. --- 3205,3212 ---- getbufline({buf}, {lnum} [, {end}]) Return a |List| with the lines starting from {lnum} to {end} (inclusive) in the buffer {buf}. If {end} is omitted, a ! |List| with only the line {lnum} is returned. See ! `getbufoneline()` for only getting the line. For the use of {buf}, see |bufname()| above. *************** *** 3227,3232 **** --- 3229,3239 ---- < Can also be used as a |method|: > GetBufnr()->getbufline(lnum) + < + *getbufoneline()* + getbufoneline({buf}, {lnum}) + Just like `getbufline()` but only get one line and return it + as a string. getbufvar({buf}, {varname} [, {def}]) *getbufvar()* The result is the value of option or local buffer variable *************** *** 3771,3777 **** < Can also be used as a |method|: > ComputeLnum()->getline() ! < To get lines from another buffer see |getbufline()| getloclist({nr} [, {what}]) *getloclist()* Returns a |List| with all the entries in the location list for --- 3778,3785 ---- < Can also be used as a |method|: > ComputeLnum()->getline() ! < To get lines from another buffer see |getbufline()| and ! |getbufoneline()| getloclist({nr} [, {what}]) *getloclist()* Returns a |List| with all the entries in the location list for *** ../vim-9.0.0915/runtime/doc/usr_41.txt 2022-11-14 19:49:09.662298086 +0000 --- runtime/doc/usr_41.txt 2022-11-21 19:47:48.170932619 +0000 *************** *** 934,939 **** --- 935,941 ---- Working with text in another buffer: getbufline() get a list of lines from the specified buffer + getbufoneline() get a one line from the specified buffer setbufline() replace a line in the specified buffer appendbufline() append a list of lines in the specified buffer deletebufline() delete lines from a specified buffer *** ../vim-9.0.0915/src/evalbuffer.c 2022-11-20 11:13:12.116192314 +0000 --- src/evalbuffer.c 2022-11-21 19:24:36.372710591 +0000 *************** *** 814,823 **** } /* ! * "getbufline()" function */ ! void ! f_getbufline(typval_T *argvars, typval_T *rettv) { linenr_T lnum = 1; linenr_T end = 1; --- 814,824 ---- } /* ! * "retlist" TRUE: "getbufline()" function ! * "retlist" FALSE: "getbufoneline()" function */ ! static void ! getbufline(typval_T *argvars, typval_T *rettv, int retlist) { linenr_T lnum = 1; linenr_T end = 1; *************** *** 842,848 **** end = tv_get_lnum_buf(&argvars[2], buf); } ! get_buffer_lines(buf, lnum, end, TRUE, rettv); } /* --- 843,867 ---- end = tv_get_lnum_buf(&argvars[2], buf); } ! get_buffer_lines(buf, lnum, end, retlist, rettv); ! } ! ! /* ! * "getbufline()" function ! */ ! void ! f_getbufline(typval_T *argvars, typval_T *rettv) ! { ! getbufline(argvars, rettv, TRUE); ! } ! ! /* ! * "getbufoneline()" function ! */ ! void ! f_getbufoneline(typval_T *argvars, typval_T *rettv) ! { ! getbufline(argvars, rettv, FALSE); } /* *** ../vim-9.0.0915/src/proto/evalbuffer.pro 2022-06-27 23:15:02.000000000 +0100 --- src/proto/evalbuffer.pro 2022-11-21 19:24:45.412707561 +0000 *************** *** 16,21 **** --- 16,22 ---- void f_deletebufline(typval_T *argvars, typval_T *rettv); void f_getbufinfo(typval_T *argvars, typval_T *rettv); void f_getbufline(typval_T *argvars, typval_T *rettv); + void f_getbufoneline(typval_T *argvars, typval_T *rettv); void f_getline(typval_T *argvars, typval_T *rettv); void f_setbufline(typval_T *argvars, typval_T *rettv); void f_setline(typval_T *argvars, typval_T *rettv); *** ../vim-9.0.0915/src/evalfunc.c 2022-11-14 19:49:09.662298086 +0000 --- src/evalfunc.c 2022-11-21 19:21:24.192785345 +0000 *************** *** 1923,1928 **** --- 1923,1930 ---- ret_list_dict_any, f_getbufinfo}, {"getbufline", 2, 3, FEARG_1, arg3_buffer_lnum_lnum, ret_list_string, f_getbufline}, + {"getbufoneline", 2, 2, FEARG_1, arg2_buffer_lnum, + ret_string, f_getbufoneline}, {"getbufvar", 2, 3, FEARG_1, arg3_buffer_string_any, ret_any, f_getbufvar}, {"getchangelist", 0, 1, FEARG_1, arg1_buffer, *** ../vim-9.0.0915/src/testdir/test_bufline.vim 2022-11-20 11:13:12.116192314 +0000 --- src/testdir/test_bufline.vim 2022-11-21 19:33:27.013009787 +0000 *************** *** 11,17 **** --- 11,19 ---- hide call assert_equal(0, setbufline(b, 1, ['foo', 'bar'])) call assert_equal(['foo'], getbufline(b, 1)) + call assert_equal('foo', getbufoneline(b, 1)) call assert_equal(['bar'], getbufline(b, '$')) + call assert_equal('bar', getbufoneline(b, '$')) call assert_equal(['foo', 'bar'], getbufline(b, 1, 2)) exe "bd!" b call assert_equal([], getbufline(b, 1, 2)) *************** *** 35,42 **** --- 37,47 ---- call assert_equal(0, setbufline(b, 4, ['d', 'e'])) call assert_equal(['c'], b->getbufline(3)) + call assert_equal('c', b->getbufoneline(3)) call assert_equal(['d'], getbufline(b, 4)) + call assert_equal('d', getbufoneline(b, 4)) call assert_equal(['e'], getbufline(b, 5)) + call assert_equal('e', getbufoneline(b, 5)) call assert_equal([], getbufline(b, 6)) call assert_equal([], getbufline(b, 2, 1)) *** ../vim-9.0.0915/src/testdir/test_vim9_builtin.vim 2022-11-12 16:07:01.777944369 +0000 --- src/testdir/test_vim9_builtin.vim 2022-11-21 19:38:18.917960653 +0000 *************** *** 1724,1738 **** --- 1724,1746 ---- getbufline(-1, '$', '$')->assert_equal([]) getbufline(-1, 1, '$')->assert_equal([]) + getbufoneline('#', 1)->assert_equal(lines[0]) + assert_equal([7, 7, 7], getbufline('#', 1, '$')->map((_, _) => 7)) assert_fails('getbufline("", "$a", "$b")', ['E1030: Using a String as a Number: "$a"', 'E1030: Using a String as a Number: "$a"']) assert_fails('getbufline("", "$", "$b")', ['E1030: Using a String as a Number: "$b"', 'E1030: Using a String as a Number: "$b"']) bwipe! + assert_fails('getbufoneline("", "$a")', ['E1030: Using a String as a Number: "$a"', 'E1030: Using a String as a Number: "$a"']) + bwipe! + v9.CheckDefAndScriptFailure(['getbufline([], 2)'], ['E1013: Argument 1: type mismatch, expected string but got list', 'E1220: String or Number required for argument 1']) v9.CheckDefAndScriptFailure(['getbufline("a", [])'], ['E1013: Argument 2: type mismatch, expected string but got list', 'E1220: String or Number required for argument 2']) v9.CheckDefAndScriptFailure(['getbufline("a", 2, 0z10)'], ['E1013: Argument 3: type mismatch, expected string but got blob', 'E1220: String or Number required for argument 3']) + + v9.CheckDefAndScriptFailure(['getbufoneline([], 2)'], ['E1013: Argument 1: type mismatch, expected string but got list', 'E1220: String or Number required for argument 1']) + v9.CheckDefAndScriptFailure(['getbufoneline("a", [])'], ['E1013: Argument 2: type mismatch, expected string but got list', 'E1220: String or Number required for argument 2']) enddef def Test_getbufvar() *** ../vim-9.0.0915/src/version.c 2022-11-20 12:11:22.924741577 +0000 --- src/version.c 2022-11-21 19:55:53.911366859 +0000 *************** *** 697,698 **** --- 697,700 ---- { /* Add new patch number below this line */ + /**/ + 916, /**/ -- hundred-and-one symptoms of being an internet addict: 101. U can read htis w/o ny porblm and cant figur eout Y its evn listd. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// \\\ \\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///