@@ -601,8 +601,10 @@ popup_adjust_position(win_T *wp)
601601 wp -> w_topline = wp -> w_buffer -> b_ml .ml_line_count ;
602602
603603 // Compute width based on longest text line and the 'wrap' option.
604+ // Use a minimum width of one, so that something shows when there is no
605+ // text.
604606 // TODO: more accurate wrapping
605- wp -> w_width = 0 ;
607+ wp -> w_width = 1 ;
606608 for (lnum = wp -> w_topline ; lnum <= wp -> w_buffer -> b_ml .ml_line_count ; ++ lnum )
607609 {
608610 int len = vim_strsize (ml_get_buf (wp -> w_buffer , lnum , FALSE));
@@ -703,6 +705,48 @@ typedef enum
703705 TYPE_DIALOG
704706} create_type_T ;
705707
708+ /*
709+ * Make "buf" empty and set the contents to "text".
710+ * Used by popup_create() and popup_settext().
711+ */
712+ static void
713+ popup_set_buffer_text (buf_T * buf , typval_T text )
714+ {
715+ int lnum ;
716+
717+ // Clear the buffer, then replace the lines.
718+ curbuf = buf ;
719+ for (lnum = buf -> b_ml .ml_line_count ; lnum > 0 ; -- lnum )
720+ ml_delete (lnum , FALSE);
721+ curbuf = curwin -> w_buffer ;
722+
723+ // Add text to the buffer.
724+ if (text .v_type == VAR_STRING )
725+ {
726+ // just a string
727+ ml_append_buf (buf , 0 , text .vval .v_string , (colnr_T )0 , TRUE);
728+ }
729+ else
730+ {
731+ list_T * l = text .vval .v_list ;
732+
733+ if (l -> lv_len > 0 )
734+ {
735+ if (l -> lv_first -> li_tv .v_type == VAR_STRING )
736+ // list of strings
737+ add_popup_strings (buf , l );
738+ else
739+ // list of dictionaries
740+ add_popup_dicts (buf , l );
741+ }
742+ }
743+
744+ // delete the line that was in the empty buffer
745+ curbuf = buf ;
746+ ml_delete (buf -> b_ml .ml_line_count , FALSE);
747+ curbuf = curwin -> w_buffer ;
748+ }
749+
706750/*
707751 * popup_create({text}, {options})
708752 * popup_atcursor({text}, {options})
@@ -789,31 +833,7 @@ popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
789833 // TODO: find tab page "nr"
790834 emsg ("Not implemented yet" );
791835
792- // Add text to the buffer.
793- if (argvars [0 ].v_type == VAR_STRING )
794- {
795- // just a string
796- ml_append_buf (buf , 0 , argvars [0 ].vval .v_string , (colnr_T )0 , TRUE);
797- }
798- else
799- {
800- list_T * l = argvars [0 ].vval .v_list ;
801-
802- if (l -> lv_len > 0 )
803- {
804- if (l -> lv_first -> li_tv .v_type == VAR_STRING )
805- // list of strings
806- add_popup_strings (buf , l );
807- else
808- // list of dictionaries
809- add_popup_dicts (buf , l );
810- }
811- }
812-
813- // Delete the line of the empty buffer.
814- curbuf = buf ;
815- ml_delete (buf -> b_ml .ml_line_count , FALSE);
816- curbuf = curwin -> w_buffer ;
836+ popup_set_buffer_text (buf , argvars [0 ]);
817837
818838 if (type == TYPE_ATCURSOR )
819839 {
@@ -1112,6 +1132,22 @@ f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
11121132 }
11131133}
11141134
1135+ /*
1136+ * popup_settext({id}, {text})
1137+ */
1138+ void
1139+ f_popup_settext (typval_T * argvars , typval_T * rettv UNUSED )
1140+ {
1141+ int id = (int )tv_get_number (& argvars [0 ]);
1142+ win_T * wp = find_popup_win (id );
1143+
1144+ if (wp != NULL )
1145+ {
1146+ popup_set_buffer_text (wp -> w_buffer , argvars [1 ]);
1147+ popup_adjust_position (wp );
1148+ }
1149+ }
1150+
11151151 static void
11161152popup_free (win_T * wp )
11171153{
0 commit comments