Skip to content

Instantly share code, notes, and snippets.

@mattn
Last active March 14, 2016 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattn/23c1f50999084992ca98 to your computer and use it in GitHub Desktop.
Save mattn/23c1f50999084992ca98 to your computer and use it in GitHub Desktop.
diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt
index 1460731..35deba2 100644
--- a/runtime/doc/eval.txt
+++ b/runtime/doc/eval.txt
@@ -2067,6 +2067,9 @@ searchpos( {pattern} [, {flags} [, {stopline} [, {timeout}]]])
server2client( {clientid}, {string})
Number send reply string
serverlist() String get a list of available servers
+setbufline( {expr}, {lnum}, {line})
+ Number set line {lnum} to {line} in buffer
+ {expr}
setbufvar( {expr}, {varname}, {val}) set {varname} in buffer {expr} to {val}
setcharsearch( {dict}) Dict set character search from {dict}
setcmdpos( {pos}) Number set cursor position in command-line
@@ -5977,6 +5980,15 @@ serverlist() *serverlist()*
Example: >
:echo serverlist()
<
+setbufline({expr}, {lnum}, {text}) *setbufline()*
+ Set line {lnum} to {text} in buffer {expr}. To insert
+ lines use |append()|.
+
+ For the use of {expr}, see |bufname()| above.
+
+ {lnum} is used like with |setline()|.
+ This works like |setline()| but works also hidden buffer.
+
setbufvar({expr}, {varname}, {val}) *setbufvar()*
Set option or local variable {varname} in buffer {expr} to
{val}.
diff --git a/runtime/syntax/vim.vim b/runtime/syntax/vim.vim
index 0206a61..b808591 100644
--- a/runtime/syntax/vim.vim
+++ b/runtime/syntax/vim.vim
@@ -77,7 +77,7 @@ syn case match
" Function Names {{{2
syn keyword vimFuncName contained abs alloc_fail argc argv assert_exception assert_true browse buflisted bufnr byteidx ceil cindent complete confirm cos cscope_connection delete diff_hlID eval exepath expand feedkeys filter float2nr fnameescape foldclosedend foldtextresult garbagecollect getbufvar getcharsearch getcmdtype getcwd getfsize getline getpid getreg gettabwinvar getwinvar globpath haslocaldir histdel hlexists iconv input inputrestore insert islocked jsondecode len line localtime luaeval mapcheck matchaddpos matchend max mode nr2char perleval printf pyeval reltime remote_foreground remote_send repeat round screencol searchdecl searchpos server2client setcharsearch setloclist setqflist settabwinvar shellescape sin soundfold split str2nr strftime strlen strtrans substitute synIDattr system tabpagenr taglist tanh tolower tr type undotree values visualmode winbufnr winheight winnr winrestview winwidth writefile
syn keyword vimFuncName contained acos and argidx asin assert_fails atan browsedir bufloaded bufwinnr byteidxcomp changenr clearmatches complete_add connect cosh cursor did_filetype empty eventhandler exists expr8 filereadable finddir floor fnamemodify foldlevel foreground get getchar getcmdline getcmdwintype getfontname getftime getloclist getpos getregtype getwinposx glob has hasmapto histget hlID indent inputdialog inputsave invert items jsonencode libcall line2byte log map match matcharg matchlist min mzeval or pow pumvisible range reltimestr remote_peek remove resolve screenattr screenrow searchpair sendexpr serverlist setcmdpos setmatches setreg setwinvar shiftwidth sinh spellbadword sqrt strchars stridx strpart strwidth synconcealed synIDtrans systemlist tabpagewinnr tan tempname toupper trunc undofile uniq virtcol wildmenumode wincol winline winrestcmd winsaveview wordcount xor
-syn keyword vimFuncName contained add append arglistid assert_equal assert_false atan2 bufexists bufname byte2line call char2nr col complete_check copy count deepcopy diff_filler escape executable exp extend filewritable findfile fmod foldclosed foldtext function getbufline getcharmod getcmdpos getcurpos getfperm getftype getmatches getqflist gettabvar getwinposy glob2regpat has_key histadd histnr hostname index inputlist inputsecret isdirectory join keys libcallnr lispindent log10 maparg matchadd matchdelete matchstr mkdir nextnonblank pathshorten prevnonblank py3eval readfile remote_expr remote_read rename reverse screenchar search searchpairpos sendraw setbufvar setline setpos settabvar sha256 simplify sort spellsuggest str2float strdisplaywidth string strridx submatch synID synstack tabpagebuflist tagfiles
+syn keyword vimFuncName contained add append arglistid assert_equal assert_false atan2 bufexists bufname byte2line call char2nr col complete_check copy count deepcopy diff_filler escape executable exp extend filewritable findfile fmod foldclosed foldtext function getbufline getcharmod getcmdpos getcurpos getfperm getftype getmatches getqflist gettabvar getwinposy glob2regpat has_key histadd histnr hostname index inputlist inputsecret isdirectory join keys libcallnr lispindent log10 maparg matchadd matchdelete matchstr mkdir nextnonblank pathshorten prevnonblank py3eval readfile remote_expr remote_read rename reverse screenchar search searchpairpos sendraw setbufline setbufvar setline setpos settabvar sha256 simplify sort spellsuggest str2float strdisplaywidth string strridx submatch synID synstack tabpagebuflist tagfiles
"--- syntax here and above generated by mkvimvim ---
" Special Vim Highlighting (not automatic) {{{1
diff --git a/src/eval.c b/src/eval.c
index e045a67..9610189 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -728,6 +728,7 @@ static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
static void f_searchpos(typval_T *argvars, typval_T *rettv);
static void f_server2client(typval_T *argvars, typval_T *rettv);
static void f_serverlist(typval_T *argvars, typval_T *rettv);
+static void f_setbufline(typval_T *argvars, typval_T *rettv);
static void f_setbufvar(typval_T *argvars, typval_T *rettv);
static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
@@ -8310,6 +8311,7 @@ static struct fst
{"searchpos", 1, 4, f_searchpos},
{"server2client", 2, 2, f_server2client},
{"serverlist", 0, 0, f_serverlist},
+ {"setbufline", 3, 3, f_setbufline},
{"setbufvar", 3, 3, f_setbufvar},
{"setcharsearch", 1, 1, f_setcharsearch},
{"setcmdpos", 1, 1, f_setcmdpos},
@@ -17511,26 +17513,34 @@ f_setfperm(typval_T *argvars, typval_T *rettv)
}
/*
- * "setline()" function
+ * Set line or list of lines to buffer "buf".
*/
static void
-f_setline(typval_T *argvars, typval_T *rettv)
+set_buffer_lines(buf_T *buf, linenr_T lnum, typval_T *lines, typval_T *rettv)
{
- linenr_T lnum;
char_u *line = NULL;
list_T *l = NULL;
listitem_T *li = NULL;
long added = 0;
- linenr_T lcount = curbuf->b_ml.ml_line_count;
+ buf_T *curbuf_save = NULL;
+ int is_curbuf;
- lnum = get_tv_lnum(&argvars[0]);
- if (argvars[1].v_type == VAR_LIST)
+ rettv->vval.v_number = 1; /* FAIL */
+
+ if (buf == NULL || buf->b_ml.ml_mfp == NULL || lnum < 0)
+ return;
+
+ if (lines->v_type == VAR_LIST)
{
- l = argvars[1].vval.v_list;
+ l = lines->vval.v_list;
li = l->lv_first;
}
else
- line = get_tv_string_chk(&argvars[1]);
+ line = get_tv_string_chk(lines);
+
+ is_curbuf = buf == curbuf;
+ curbuf_save = curbuf;
+ curbuf = buf;
/* default result is zero == OK */
for (;;)
@@ -17562,7 +17572,7 @@ f_setline(typval_T *argvars, typval_T *rettv)
if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
{
changed_bytes(lnum, 0);
- if (lnum == curwin->w_cursor.lnum)
+ if (is_curbuf && lnum == curwin->w_cursor.lnum)
check_cursor_col();
rettv->vval.v_number = 0; /* OK */
}
@@ -17581,7 +17591,39 @@ f_setline(typval_T *argvars, typval_T *rettv)
}
if (added > 0)
- appended_lines_mark(lcount, added);
+ appended_lines_mark(curbuf->b_ml.ml_line_count, added);
+
+ curbuf = curbuf_save;
+}
+
+/*
+ * "setbufline()" function
+ */
+ static void
+f_setbufline(typval_T *argvars, typval_T *rettv)
+{
+ linenr_T lnum;
+ buf_T *buf;
+
+ (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
+ ++emsg_off;
+ buf = get_buf_tv(&argvars[0], FALSE);
+ --emsg_off;
+
+ lnum = get_tv_lnum_buf(&argvars[1], buf);
+
+ set_buffer_lines(buf, lnum, &argvars[2], rettv);
+}
+
+/*
+ * "setline()" function
+ */
+ static void
+f_setline(typval_T *argvars, typval_T *rettv)
+{
+ linenr_T lnum = get_tv_lnum(&argvars[0]);
+
+ set_buffer_lines(curbuf, lnum, &argvars[1], rettv);
}
static void set_qf_ll_list(win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv);
diff --git a/src/testdir/test_bufline.vim b/src/testdir/test_bufline.vim
new file mode 100644
index 0000000..575a830
--- /dev/null
+++ b/src/testdir/test_bufline.vim
@@ -0,0 +1,12 @@
+" Tests for setbufline() and getbufline()
+func Test_setbufline_getbufline()
+ new
+ let b = bufnr('%')
+ hide
+ call setbufline(b, 1, ['foo', 'bar'])
+ call assert_equal(['foo'], getbufline(b, 1))
+ call assert_equal(['bar'], getbufline(b, 2))
+ call assert_equal(['foo', 'bar'], getbufline(b, 1, 2))
+ exe "bd!" b
+ call assert_equal([], getbufline(b, 1, 2))
+endfunc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment