From 807a65b2da5390aa930ea11c12df2e60174420f1 Mon Sep 17 00:00:00 2001 From: Shadman Date: Wed, 30 Jul 2025 07:36:01 +0600 Subject: [PATCH] fix(prompt): cursor on prompt line, disallow ":edit" #34736 * fix: ensure :edit can not be use on prompt buffer * fix: starting editing on prompt-line doesn't put cursor at the end --- src/nvim/edit.c | 5 ++- src/nvim/ex_docmd.c | 6 +++ test/functional/legacy/prompt_buffer_spec.lua | 45 +++++++++++++++++++ test/old/testdir/test_autocmd.vim | 4 +- 4 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/nvim/edit.c b/src/nvim/edit.c index 781b6f0c23..3c189e74c8 100644 --- a/src/nvim/edit.c +++ b/src/nvim/edit.c @@ -1532,7 +1532,10 @@ static void init_prompt(int cmdchar_todo) { char *prompt = prompt_text(); - if (curwin->w_cursor.lnum < curbuf->b_prompt_start.mark.lnum) { + if (curwin->w_cursor.lnum < curbuf->b_prompt_start.mark.lnum + || (cmdchar_todo != 'O' + && curwin->w_cursor.lnum == curbuf->b_prompt_start.mark.lnum + && (curwin->w_cursor.col < (int)strlen(prompt_text())))) { curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; coladvance(curwin, MAXCOL); } diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index 864baf5c06..1be6c622e8 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -5718,6 +5718,12 @@ static void ex_edit(exarg_T *eap) return; } + // prevent use of :edit on prompt-buffers + if (bt_prompt(curbuf) && eap->cmdidx == CMD_edit && *eap->arg == NUL) { + emsg("cannot :edit a prompt buffer"); + return; + } + do_exedit(eap, NULL); } diff --git a/test/functional/legacy/prompt_buffer_spec.lua b/test/functional/legacy/prompt_buffer_spec.lua index 176470f248..25e4c1329f 100644 --- a/test/functional/legacy/prompt_buffer_spec.lua +++ b/test/functional/legacy/prompt_buffer_spec.lua @@ -130,6 +130,10 @@ describe('prompt buffer', function() {1:~ }|*3 {5:-- INSERT --} | ]]) + + -- :edit doesn't apply on prompt buffer + eq('Vim(edit):cannot :edit a prompt buffer', t.pcall_err(api.nvim_command, 'edit')) + feed('exit\n') screen:expect([[ ^other buffer | @@ -297,6 +301,47 @@ describe('prompt buffer', function() {1:~ }|*6 {5:-- INSERT --} | ]]) + + -- ensure cursor gets adjusted to end of user-text to prompt when insert mode + -- is entered from readonly region of prompt buffer + local prompt_pos = api.nvim_buf_get_mark(0, ':') + feed('') + -- works before prompt + api.nvim_win_set_cursor(0, { prompt_pos[1] - 1, 0 }) + screen:expect([[ + ^other buffer | + % line1 | + line2 | + {1:~ }|*6 + | + ]]) + feed('a') + feed('') + screen:expect([[ + other buffer | + % line1 | + line^2 | + {1:~ }|*6 + | + ]]) + -- works on prompt + api.nvim_win_set_cursor(0, { prompt_pos[1], 0 }) + screen:expect([[ + other buffer | + ^% line1 | + line2 | + {1:~ }|*6 + | + ]]) + feed('a') + feed('') + screen:expect([[ + other buffer | + % line1 | + line^2 | + {1:~ }|*6 + | + ]]) end) it('can put (p) multiline text', function() diff --git a/test/old/testdir/test_autocmd.vim b/test/old/testdir/test_autocmd.vim index 39d03c2d52..129b17b54f 100644 --- a/test/old/testdir/test_autocmd.vim +++ b/test/old/testdir/test_autocmd.vim @@ -859,7 +859,7 @@ func Test_BufReadCmdNofile() \ 'quickfix', \ 'help', "\ 'terminal', - \ 'prompt', + "\ 'prompt', "\ 'popup', \ ] new somefile @@ -977,7 +977,7 @@ func Test_BufEnter() \ 'quickfix', \ 'help', "\ 'terminal', - \ 'prompt', + "\ 'prompt', "\ 'popup', \ ] new somefile