@@ -328,6 +328,7 @@ static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
328328static void f_searchpos (typval_T * argvars , typval_T * rettv );
329329static void f_server2client (typval_T * argvars , typval_T * rettv );
330330static void f_serverlist (typval_T * argvars , typval_T * rettv );
331+ static void f_setbufline (typval_T * argvars , typval_T * rettv );
331332static void f_setbufvar (typval_T * argvars , typval_T * rettv );
332333static void f_setcharsearch (typval_T * argvars , typval_T * rettv );
333334static void f_setcmdpos (typval_T * argvars , typval_T * rettv );
@@ -764,6 +765,7 @@ static struct fst
764765 {"searchpos" , 1 , 4 , f_searchpos },
765766 {"server2client" , 2 , 2 , f_server2client },
766767 {"serverlist" , 0 , 0 , f_serverlist },
768+ {"setbufline" , 3 , 3 , f_setbufline },
767769 {"setbufvar" , 3 , 3 , f_setbufvar },
768770 {"setcharsearch" , 1 , 1 , f_setcharsearch },
769771 {"setcmdpos" , 1 , 1 , f_setcmdpos },
@@ -9867,6 +9869,115 @@ f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
98679869 rettv -> vval .v_string = r ;
98689870}
98699871
9872+ /*
9873+ * Set line or list of lines in buffer "buf".
9874+ */
9875+ static void
9876+ set_buffer_lines (buf_T * buf , linenr_T lnum , typval_T * lines , typval_T * rettv )
9877+ {
9878+ char_u * line = NULL ;
9879+ list_T * l = NULL ;
9880+ listitem_T * li = NULL ;
9881+ long added = 0 ;
9882+ linenr_T lcount ;
9883+ buf_T * curbuf_save ;
9884+ int is_curbuf = buf == curbuf ;
9885+
9886+ if (buf == NULL || buf -> b_ml .ml_mfp == NULL || lnum < 1 )
9887+ {
9888+ rettv -> vval .v_number = 1 ; /* FAIL */
9889+ return ;
9890+ }
9891+
9892+ curbuf_save = curbuf ;
9893+ curbuf = buf ;
9894+
9895+ lcount = curbuf -> b_ml .ml_line_count ;
9896+
9897+ if (lines -> v_type == VAR_LIST )
9898+ {
9899+ l = lines -> vval .v_list ;
9900+ li = l -> lv_first ;
9901+ }
9902+ else
9903+ line = get_tv_string_chk (lines );
9904+
9905+ /* default result is zero == OK */
9906+ for (;;)
9907+ {
9908+ if (l != NULL )
9909+ {
9910+ /* list argument, get next string */
9911+ if (li == NULL )
9912+ break ;
9913+ line = get_tv_string_chk (& li -> li_tv );
9914+ li = li -> li_next ;
9915+ }
9916+
9917+ rettv -> vval .v_number = 1 ; /* FAIL */
9918+ if (line == NULL || lnum < 1 || lnum > curbuf -> b_ml .ml_line_count + 1 )
9919+ break ;
9920+
9921+ /* When coming here from Insert mode, sync undo, so that this can be
9922+ * undone separately from what was previously inserted. */
9923+ if (u_sync_once == 2 )
9924+ {
9925+ u_sync_once = 1 ; /* notify that u_sync() was called */
9926+ u_sync (TRUE);
9927+ }
9928+
9929+ if (lnum <= curbuf -> b_ml .ml_line_count )
9930+ {
9931+ /* existing line, replace it */
9932+ if (u_savesub (lnum ) == OK && ml_replace (lnum , line , TRUE) == OK )
9933+ {
9934+ changed_bytes (lnum , 0 );
9935+ if (is_curbuf && lnum == curwin -> w_cursor .lnum )
9936+ check_cursor_col ();
9937+ rettv -> vval .v_number = 0 ; /* OK */
9938+ }
9939+ }
9940+ else if (added > 0 || u_save (lnum - 1 , lnum ) == OK )
9941+ {
9942+ /* lnum is one past the last line, append the line */
9943+ ++ added ;
9944+ if (ml_append (lnum - 1 , line , (colnr_T )0 , FALSE) == OK )
9945+ rettv -> vval .v_number = 0 ; /* OK */
9946+ }
9947+
9948+ if (l == NULL ) /* only one string argument */
9949+ break ;
9950+ ++ lnum ;
9951+ }
9952+
9953+ if (added > 0 )
9954+ appended_lines_mark (lcount , added );
9955+
9956+ curbuf = curbuf_save ;
9957+ }
9958+
9959+ /*
9960+ * "setbufline()" function
9961+ */
9962+ static void
9963+ f_setbufline (argvars , rettv )
9964+ typval_T * argvars ;
9965+ typval_T * rettv ;
9966+ {
9967+ linenr_T lnum ;
9968+ buf_T * buf ;
9969+
9970+ buf = get_buf_tv (& argvars [0 ], FALSE);
9971+ if (buf == NULL )
9972+ rettv -> vval .v_number = 1 ; /* FAIL */
9973+ else
9974+ {
9975+ lnum = get_tv_lnum_buf (& argvars [1 ], buf );
9976+
9977+ set_buffer_lines (buf , lnum , & argvars [2 ], rettv );
9978+ }
9979+ }
9980+
98709981/*
98719982 * "setbufvar()" function
98729983 */
@@ -10021,72 +10132,9 @@ f_setfperm(typval_T *argvars, typval_T *rettv)
1002110132 static void
1002210133f_setline (typval_T * argvars , typval_T * rettv )
1002310134{
10024- linenr_T lnum ;
10025- char_u * line = NULL ;
10026- list_T * l = NULL ;
10027- listitem_T * li = NULL ;
10028- long added = 0 ;
10029- linenr_T lcount = curbuf -> b_ml .ml_line_count ;
10030-
10031- lnum = get_tv_lnum (& argvars [0 ]);
10032- if (argvars [1 ].v_type == VAR_LIST )
10033- {
10034- l = argvars [1 ].vval .v_list ;
10035- li = l -> lv_first ;
10036- }
10037- else
10038- line = get_tv_string_chk (& argvars [1 ]);
10039-
10040- /* default result is zero == OK */
10041- for (;;)
10042- {
10043- if (l != NULL )
10044- {
10045- /* list argument, get next string */
10046- if (li == NULL )
10047- break ;
10048- line = get_tv_string_chk (& li -> li_tv );
10049- li = li -> li_next ;
10050- }
10135+ linenr_T lnum = get_tv_lnum (& argvars [0 ]);
1005110136
10052- rettv -> vval .v_number = 1 ; /* FAIL */
10053- if (line == NULL || lnum < 1 || lnum > curbuf -> b_ml .ml_line_count + 1 )
10054- break ;
10055-
10056- /* When coming here from Insert mode, sync undo, so that this can be
10057- * undone separately from what was previously inserted. */
10058- if (u_sync_once == 2 )
10059- {
10060- u_sync_once = 1 ; /* notify that u_sync() was called */
10061- u_sync (TRUE);
10062- }
10063-
10064- if (lnum <= curbuf -> b_ml .ml_line_count )
10065- {
10066- /* existing line, replace it */
10067- if (u_savesub (lnum ) == OK && ml_replace (lnum , line , TRUE) == OK )
10068- {
10069- changed_bytes (lnum , 0 );
10070- if (lnum == curwin -> w_cursor .lnum )
10071- check_cursor_col ();
10072- rettv -> vval .v_number = 0 ; /* OK */
10073- }
10074- }
10075- else if (added > 0 || u_save (lnum - 1 , lnum ) == OK )
10076- {
10077- /* lnum is one past the last line, append the line */
10078- ++ added ;
10079- if (ml_append (lnum - 1 , line , (colnr_T )0 , FALSE) == OK )
10080- rettv -> vval .v_number = 0 ; /* OK */
10081- }
10082-
10083- if (l == NULL ) /* only one string argument */
10084- break ;
10085- ++ lnum ;
10086- }
10087-
10088- if (added > 0 )
10089- appended_lines_mark (lcount , added );
10137+ set_buffer_lines (curbuf , lnum , & argvars [1 ], rettv );
1009010138}
1009110139
1009210140static void set_qf_ll_list (win_T * wp , typval_T * list_arg , typval_T * action_arg , typval_T * what_arg , typval_T * rettv );
0 commit comments