Skip to content

Commit df46115

Browse files
glepnirchrisbra
authored andcommitted
patch 9.1.0265: console dialog cannot save unnamed buffers
Problem: console dialog cannot save unnamed buffers Solution: set bufname before save (glepnir). Define dialog_con_gui to test for GUI+Console dialog support, use it to skip the test when the GUI feature has been defined. Note: The dialog_changed() function will also try to call the browse_save_fname() function, when FEAT_BROWSE is defined (which is only defined in a GUI build of Vim). This will eventually lead to a call of do_browse(), which causes an error message if a GUI is not currently running (see the TODO: in do_browse()) and will then lead to a failure in Test_goto_buf_with_onfirm(). Therefore, we must disable the Test_goto_buf_with_onfirm(), when the dialog_con_gui feature is enabled (which basically means dialog feature for GUI and Console builds, in contrast to the dialog_con and dialog_gui feature). (Previously this wasn't a problem, because the test aborted in the YES case for the :confirm :b XgotoConf case and did therefore not run into the browse function call) closes: #14398 Signed-off-by: glepnir <glephunter@gmail.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 9574022 commit df46115

5 files changed

Lines changed: 49 additions & 12 deletions

File tree

runtime/doc/builtin.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*builtin.txt* For Vim version 9.1. Last change: 2024 Apr 03
1+
*builtin.txt* For Vim version 9.1. Last change: 2024 Apr 04
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -1771,7 +1771,7 @@ confirm({msg} [, {choices} [, {default} [, {type}]]])
17711771
made. It returns the number of the choice. For the first
17721772
choice this is 1.
17731773
Note: confirm() is only supported when compiled with dialog
1774-
support, see |+dialog_con| and |+dialog_gui|.
1774+
support, see |+dialog_con| |+dialog_con_gui| and |+dialog_gui|.
17751775

17761776
{msg} is displayed in a |dialog| with {choices} as the
17771777
alternatives. When {choices} is missing or empty, "&OK" is
@@ -11200,6 +11200,7 @@ cscope Compiled with |cscope| support.
1120011200
cursorbind Compiled with |'cursorbind'| (always true)
1120111201
debug Compiled with "DEBUG" defined.
1120211202
dialog_con Compiled with console dialog support.
11203+
dialog_con_gui Compiled with console and GUI dialog support.
1120311204
dialog_gui Compiled with GUI dialog support.
1120411205
diff Compiled with |vimdiff| and 'diff' support.
1120511206
digraphs Compiled with support for digraphs.

src/evalfunc.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6127,6 +6127,13 @@ f_has(typval_T *argvars, typval_T *rettv)
61276127
1
61286128
#else
61296129
0
6130+
#endif
6131+
},
6132+
{"dialog_con_gui",
6133+
#if defined(FEAT_CON_DIALOG) && defined(FEAT_GUI_DIALOG)
6134+
1
6135+
#else
6136+
0
61306137
#endif
61316138
},
61326139
{"dialog_gui",

src/ex_cmds2.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ dialog_changed(
163163
char_u buff[DIALOG_MSG_SIZE];
164164
int ret;
165165
buf_T *buf2;
166-
exarg_T ea;
166+
exarg_T ea;
167+
int empty_buf = buf->b_fname == NULL ? TRUE : FALSE;
167168

168169
dialog_msg(buff, _("Save changes to \"%s\"?"), buf->b_fname);
169170
if (checkall)
@@ -181,10 +182,27 @@ dialog_changed(
181182
// May get file name, when there is none
182183
browse_save_fname(buf);
183184
#endif
184-
if (buf->b_fname != NULL && check_overwrite(&ea, buf,
185-
buf->b_fname, buf->b_ffname, FALSE) == OK)
185+
if (empty_buf)
186+
buf_set_name(buf->b_fnum, (char_u *)"Untitled");
187+
188+
if (check_overwrite(&ea, buf, buf->b_fname, buf->b_ffname, FALSE) == OK)
189+
{
186190
// didn't hit Cancel
187-
(void)buf_write_all(buf, FALSE);
191+
if (buf_write_all(buf, FALSE) == OK)
192+
return;
193+
}
194+
195+
// restore to empty when write failed
196+
if (empty_buf)
197+
{
198+
vim_free(buf->b_fname);
199+
buf->b_fname = NULL;
200+
vim_free(buf->b_ffname);
201+
buf->b_ffname = NULL;
202+
vim_free(buf->b_sfname);
203+
buf->b_sfname = NULL;
204+
unchanged(buf, TRUE, FALSE);
205+
}
188206
}
189207
else if (ret == VIM_NO)
190208
{

src/testdir/test_buffer.vim

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,21 +252,30 @@ func Test_goto_buf_with_confirm()
252252
CheckUnix
253253
CheckNotGui
254254
CheckFeature dialog_con
255+
" When dialog_con_gui is defined, Vim is compiled with GUI support
256+
" and FEAT_BROWSE will be defined, which causes :confirm :b to
257+
" call do_browse(), which will try to use a GUI file browser,
258+
" which aborts if a GUI is not available.
259+
CheckNotFeature dialog_con_gui
255260
new XgotoConf
256261
enew
257262
call setline(1, 'test')
258263
call assert_fails('b XgotoConf', 'E37:')
259264
call feedkeys('c', 'L')
260265
call assert_fails('confirm b XgotoConf', 'E37:')
261-
call assert_equal(1, &modified)
262-
call assert_equal('', @%)
266+
call assert_true(&modified)
267+
call assert_true(empty(bufname('%')))
263268
call feedkeys('y', 'L')
264-
call assert_fails('confirm b XgotoConf', ['', 'E37:'])
265-
call assert_equal(1, &modified)
266-
call assert_equal('', @%)
269+
confirm b XgotoConf
270+
call assert_equal('XgotoConf', bufname('%'))
271+
call assert_equal(['test'], readfile('Untitled'))
272+
e Untitled
273+
call setline(2, 'test2')
267274
call feedkeys('n', 'L')
268275
confirm b XgotoConf
269-
call assert_equal('XgotoConf', @%)
276+
call assert_equal('XgotoConf', bufname('%'))
277+
call assert_equal(['test'], readfile('Untitled'))
278+
call delete('Untitled')
270279
close!
271280
endfunc
272281

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,8 @@ static char *(features[]) =
704704

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
265,
707709
/**/
708710
264,
709711
/**/

0 commit comments

Comments
 (0)