patch 9.1.0934: hard to view an existing buffer in the preview window

Problem:  hard to view an existing buffer in the preview window
Solution: add the :pbuffer command (Yinzuo Jiang)

Similar as `:pedit` and `:buffer` command. `:pbuffer` edits buffer [N]
from the buffer list in the preview window.

`:pbuffer` can also open special buffer, for example terminal buffer.

closes: #16222

Signed-off-by: Yinzuo Jiang <jiangyinzuo@foxmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/testdir/dumps/Test_popup_and_previewwindow_pbuffer.dump b/src/testdir/dumps/Test_popup_and_previewwindow_pbuffer.dump
new file mode 100644
index 0000000..0eab2a6
--- /dev/null
+++ b/src/testdir/dumps/Test_popup_and_previewwindow_pbuffer.dump
@@ -0,0 +1,20 @@
+|a+0&#ffffff0|b|0| @71
+|a|b|1| @71
+|a|b|2| @71
+|a|b|3| @71
+|a|b|4| @71
+|a|b|5| @71
+|a|b|6| @71
+|a|b|7| @71
+|a|b|8| @71
+|a+0#0000001#e0e0e08|b|0| @11| +0#0000000#0000001|e+1&#ffffff0|w|]|[|+|]| @34|1|,|1| @11|T|o|p
+|a+0#0000001#ffd7ff255|b|1| @11| +0#0000000#0000001| +0&#ffffff0@58
+|a+0#0000001#ffd7ff255|b|2| @11| +0#0000000#0000001| +0&#ffffff0@58
+|a+0#0000001#ffd7ff255|b|3| @11| +0#0000000#a8a8a8255| +0&#ffffff0@58
+|a+0#0000001#ffd7ff255|b|4| @11| +0#0000000#a8a8a8255| +0&#ffffff0@58
+|a+0#0000001#ffd7ff255|b|5| @11| +0#0000000#a8a8a8255| +0&#ffffff0@58
+|a|b|0> @71
+|~+0#4040ff13&| @73
+|~| @73
+|[+3#0000000&|N|o| |N|a|m|e|]| |[|+|]| @43|1@1|,|1| @10|B|o|t
+|-+2&&@1| |K|e|y|w|o|r|d| |L|o|c|a|l| |c|o|m|p|l|e|t|i|o|n| |(|^|N|^|P|)| |m+0#00e0003&|a|t|c|h| |1| |o|f| |1|0| +0#0000000&@26
diff --git a/src/testdir/dumps/Test_popup_and_previewwindow_01.dump b/src/testdir/dumps/Test_popup_and_previewwindow_pedit.dump
similarity index 100%
rename from src/testdir/dumps/Test_popup_and_previewwindow_01.dump
rename to src/testdir/dumps/Test_popup_and_previewwindow_pedit.dump
diff --git a/src/testdir/dumps/Test_pum_preview_pbuffer_1.dump b/src/testdir/dumps/Test_pum_preview_pbuffer_1.dump
new file mode 100644
index 0000000..ae02ce5
--- /dev/null
+++ b/src/testdir/dumps/Test_pum_preview_pbuffer_1.dump
@@ -0,0 +1,12 @@
+|o+0&#ffffff0|n|e| |o|t|h|e|r> @65
+|╔+0#0000001#ffd7ff255|═@1| +0&#e0e0e08|o|t|h|e|r| @9|═+0&#ffd7ff255@21|X| +0#0000000#ffffff0@32
+|║+0#0000001#ffd7ff255|l+0#af5f00255&|e| +0#0000001&|o|n|c|e| @32|║| +0#0000000#ffffff0@32
+|║+0#0000001#ffd7ff255|l+0#af5f00255&|e| +0#0000001&|o|n|l|y| @32|║| +0#0000000#ffffff0@32
+|║+0#0000001#ffd7ff255|e+0#af5f00255&|c| +0#0000001&|o|f@1| @33|║| +0#0000000#ffffff0@32
+|║+0#0000001#ffd7ff255|e+0#af5f00255&|c| +0#0000001&|o|n|e| @33|║| +0#0000000#ffffff0@32
+|║+0#0000001#ffd7ff255|c+0#af5f00255&|a|l@1| +0#0000001&|s+0#00e0e07&|y|s|t|e|m|(+0#e000e06&|'+0#e000002&|e|c|h|o| |h|e|l@1|o|'|)+0#e000e06&| +0#0000001&@14|║| +0#0000000#ffffff0@32
+|║+0#0000001#ffd7ff255|"+0#0000e05&| |t|h|e| |e|n|d| +0#0000001&@30|║| +0#4040ff13#ffffff0@32
+|╚+0#0000001#ffd7ff255|═@39|⇲| +0#4040ff13#ffffff0@32
+|~| @73
+|~| @73
+|-+2#0000000&@1| |K|e|y|w|o|r|d| |c|o|m|p|l|e|t|i|o|n| |(|^|N|^|P|)| |m+0#00e0003&|a|t|c|h| |1| |o|f| |5| +0#0000000&@33
diff --git a/src/testdir/dumps/Test_pum_preview_pbuffer_2.dump b/src/testdir/dumps/Test_pum_preview_pbuffer_2.dump
new file mode 100644
index 0000000..e83929f
--- /dev/null
+++ b/src/testdir/dumps/Test_pum_preview_pbuffer_2.dump
@@ -0,0 +1,12 @@
+|o+0&#ffffff0|n|e| |o|n|c|e> @66
+|╔+0#0000001#ffd7ff255|═@1| |o|t|h|e|r| @9|═@21|X| +0#0000000#ffffff0@32
+|║+0#0000001#ffd7ff255|l+0#af5f00255&|e| +0#0000001#e0e0e08|o|n|c|e| @10| +0&#ffd7ff255@21|║| +0#0000000#ffffff0@32
+|║+0#0000001#ffd7ff255|l+0#af5f00255&|e| +0#0000001&|o|n|l|y| @32|║| +0#0000000#ffffff0@32
+|║+0#0000001#ffd7ff255|e+0#af5f00255&|c| +0#0000001&|o|f@1| @33|║| +0#0000000#ffffff0@32
+|║+0#0000001#ffd7ff255|e+0#af5f00255&|c| +0#0000001&|o|n|e| @33|║| +0#0000000#ffffff0@32
+|║+0#0000001#ffd7ff255|c+0#af5f00255&|a|l@1| +0#0000001&|s+0#00e0e07&|y|s|t|e|m|(+0#e000e06&|'+0#e000002&|e|c|h|o| |h|e|l@1|o|'|)+0#e000e06&| +0#0000001&@14|║| +0#0000000#ffffff0@32
+|║+0#0000001#ffd7ff255|"+0#0000e05&| |t|h|e| |e|n|d| +0#0000001&@30|║| +0#4040ff13#ffffff0@32
+|╚+0#0000001#ffd7ff255|═@39|⇲| +0#4040ff13#ffffff0@32
+|~| @73
+|~| @73
+|-+2#0000000&@1| |K|e|y|w|o|r|d| |c|o|m|p|l|e|t|i|o|n| |(|^|N|^|P|)| |m+0#00e0003&|a|t|c|h| |2| |o|f| |5| +0#0000000&@33
diff --git a/src/testdir/dumps/Test_pum_preview_pbuffer_3.dump b/src/testdir/dumps/Test_pum_preview_pbuffer_3.dump
new file mode 100644
index 0000000..fde2104
--- /dev/null
+++ b/src/testdir/dumps/Test_pum_preview_pbuffer_3.dump
@@ -0,0 +1,12 @@
+|o+0&#ffffff0|n|e| |o|n|l|y> @66
+|╔+0#0000001#ffd7ff255|═@1| |o|t|h|e|r| @9|═@21|X| +0#0000000#ffffff0@32
+|║+0#0000001#ffd7ff255|l+0#af5f00255&|e| +0#0000001&|o|n|c|e| @32|║| +0#0000000#ffffff0@32
+|║+0#0000001#ffd7ff255|l+0#af5f00255&|e| +0#0000001#e0e0e08|o|n|l|y| @10| +0&#ffd7ff255@21|║| +0#0000000#ffffff0@32
+|║+0#0000001#ffd7ff255|e+0#af5f00255&|c| +0#0000001&|o|f@1| @33|║| +0#0000000#ffffff0@32
+|║+0#0000001#ffd7ff255|e+0#af5f00255&|c| +0#0000001&|o|n|e| @33|║| +0#0000000#ffffff0@32
+|║+0#0000001#ffd7ff255|c+0#af5f00255&|a|l@1| +0#0000001&|s+0#00e0e07&|y|s|t|e|m|(+0#e000e06&|'+0#e000002&|e|c|h|o| |h|e|l@1|o|'|)+0#e000e06&| +0#0000001&@14|║| +0#0000000#ffffff0@32
+|║+0#0000001#ffd7ff255|"+0#0000e05&| |t|h|e| |e|n|d| +0#0000001&@30|║| +0#4040ff13#ffffff0@32
+|╚+0#0000001#ffd7ff255|═@39|⇲| +0#4040ff13#ffffff0@32
+|~| @73
+|~| @73
+|-+2#0000000&@1| |K|e|y|w|o|r|d| |c|o|m|p|l|e|t|i|o|n| |(|^|N|^|P|)| |m+0#00e0003&|a|t|c|h| |3| |o|f| |5| +0#0000000&@33
diff --git a/src/testdir/dumps/Test_pum_preview_pbuffer_4.dump b/src/testdir/dumps/Test_pum_preview_pbuffer_4.dump
new file mode 100644
index 0000000..dee2b55
--- /dev/null
+++ b/src/testdir/dumps/Test_pum_preview_pbuffer_4.dump
@@ -0,0 +1,12 @@
+|o+0&#ffffff0|n|e| |o|f@1> @67
+|╔+0#0000001#ffd7ff255|═@1| |o|t|h|e|r| @9|═@21|X| +0#0000000#ffffff0@32
+|║+0#0000001#ffd7ff255|l+0#af5f00255&|e| +0#0000001&|o|n|c|e| @32|║| +0#0000000#ffffff0@32
+|║+0#0000001#ffd7ff255|l+0#af5f00255&|e| +0#0000001&|o|n|l|y| @32|║| +0#0000000#ffffff0@32
+|║+0#0000001#ffd7ff255|e+0#af5f00255&|c| +0#0000001#e0e0e08|o|f@1| @11| +0&#ffd7ff255@21|║| +0#0000000#ffffff0@32
+|║+0#0000001#ffd7ff255|e+0#af5f00255&|c| +0#0000001&|o|n|e| @33|║| +0#0000000#ffffff0@32
+|║+0#0000001#ffd7ff255|c+0#af5f00255&|a|l@1| +0#0000001&|s+0#00e0e07&|y|s|t|e|m|(+0#e000e06&|'+0#e000002&|e|c|h|o| |h|e|l@1|o|'|)+0#e000e06&| +0#0000001&@14|║| +0#0000000#ffffff0@32
+|║+0#0000001#ffd7ff255|"+0#0000e05&| |t|h|e| |e|n|d| +0#0000001&@30|║| +0#4040ff13#ffffff0@32
+|╚+0#0000001#ffd7ff255|═@39|⇲| +0#4040ff13#ffffff0@32
+|~| @73
+|~| @73
+|-+2#0000000&@1| |K|e|y|w|o|r|d| |c|o|m|p|l|e|t|i|o|n| |(|^|N|^|P|)| |m+0#00e0003&|a|t|c|h| |4| |o|f| |5| +0#0000000&@33
diff --git a/src/testdir/dumps/Test_pum_preview_1.dump b/src/testdir/dumps/Test_pum_preview_pedit_1.dump
similarity index 100%
rename from src/testdir/dumps/Test_pum_preview_1.dump
rename to src/testdir/dumps/Test_pum_preview_pedit_1.dump
diff --git a/src/testdir/dumps/Test_pum_preview_2.dump b/src/testdir/dumps/Test_pum_preview_pedit_2.dump
similarity index 100%
rename from src/testdir/dumps/Test_pum_preview_2.dump
rename to src/testdir/dumps/Test_pum_preview_pedit_2.dump
diff --git a/src/testdir/dumps/Test_pum_preview_3.dump b/src/testdir/dumps/Test_pum_preview_pedit_3.dump
similarity index 100%
rename from src/testdir/dumps/Test_pum_preview_3.dump
rename to src/testdir/dumps/Test_pum_preview_pedit_3.dump
diff --git a/src/testdir/dumps/Test_pum_preview_4.dump b/src/testdir/dumps/Test_pum_preview_pedit_4.dump
similarity index 100%
rename from src/testdir/dumps/Test_pum_preview_4.dump
rename to src/testdir/dumps/Test_pum_preview_pedit_4.dump
diff --git a/src/testdir/test_popup.vim b/src/testdir/test_popup.vim
index d5e6e31..69228e1 100644
--- a/src/testdir/test_popup.vim
+++ b/src/testdir/test_popup.vim
@@ -748,17 +748,11 @@
   bw!
 endfunc
 
-func Test_popup_and_previewwindow_dump()
+func s:run_popup_and_previewwindow_dump(lines, dumpfile)
   CheckScreendump
   CheckFeature quickfix
 
-  let lines =<< trim END
-    set previewheight=9
-    silent! pedit
-    call setline(1, map(repeat(["ab"], 10), "v:val .. v:key"))
-    exec "norm! G\<C-E>\<C-E>"
-  END
-  call writefile(lines, 'Xscript', 'D')
+  call writefile(a:lines, 'Xscript', 'D')
   let buf = RunVimInTerminal('-S Xscript', {})
 
   " wait for the script to finish
@@ -768,12 +762,32 @@
   call term_sendkeys(buf, "o")
   call TermWait(buf, 50)
   call term_sendkeys(buf, "\<C-X>\<C-N>")
-  call VerifyScreenDump(buf, 'Test_popup_and_previewwindow_01', {})
+  call VerifyScreenDump(buf, a:dumpfile, {})
 
   call term_sendkeys(buf, "\<Esc>u")
   call StopVimInTerminal(buf)
 endfunc
 
+func Test_popup_and_previewwindow_dump_pedit()
+  let lines =<< trim END
+    set previewheight=9
+    silent! pedit
+    call setline(1, map(repeat(["ab"], 10), "v:val .. v:key"))
+    exec "norm! G\<C-E>\<C-E>"
+  END
+  call s:run_popup_and_previewwindow_dump(lines, 'Test_popup_and_previewwindow_pedit')
+endfunc
+
+func Test_popup_and_previewwindow_dump_pbuffer()
+  let lines =<< trim END
+    set previewheight=9
+    silent! pbuffer
+    call setline(1, map(repeat(["ab"], 10), "v:val .. v:key"))
+    exec "norm! G\<C-E>\<C-E>\<C-E>"
+  END
+  call s:run_popup_and_previewwindow_dump(lines, 'Test_popup_and_previewwindow_pbuffer')
+endfunc
+
 func Test_balloon_split()
   CheckFunction balloon_split
 
diff --git a/src/testdir/test_popupwin.vim b/src/testdir/test_popupwin.vim
index 23f318f..d345b3e 100644
--- a/src/testdir/test_popupwin.vim
+++ b/src/testdir/test_popupwin.vim
@@ -1106,6 +1106,7 @@
   call assert_fails('call win_execute(winid, "next")', 'E994:')
   call assert_fails('call win_execute(winid, "rewind")', 'E994:')
   call assert_fails('call win_execute(winid, "pedit filename")', 'E994:')
+  call assert_fails('call win_execute(winid, "pbuffer filename")', 'E994:')
   call assert_fails('call win_execute(winid, "buf")', 'E994:')
   call assert_fails('call win_execute(winid, "bnext")', 'E994:')
   call assert_fails('call win_execute(winid, "bprev")', 'E994:')
@@ -3457,7 +3458,7 @@
   call StopVimInTerminal(buf)
 endfunc
 
-func Test_previewpopup_pum()
+func s:run_preview_popuppum(preview_lines, dumpfile_name)
   CheckScreendump
   CheckFeature quickfix
 
@@ -3471,30 +3472,43 @@
   END
   call writefile(lines, 'XpreviewText.vim', 'D')
 
-  let lines =<< trim END
-      call setline(1, ['one', 'two', 'three', 'other', 'once', 'only', 'off'])
-      set previewpopup=height:6,width:40
-      pedit XpreviewText.vim
-  END
-  call writefile(lines, 'XtestPreviewPum', 'D')
+  call writefile(a:preview_lines, 'XtestPreviewPum', 'D')
   let buf = RunVimInTerminal('-S XtestPreviewPum', #{rows: 12})
 
   call term_sendkeys(buf, "A o\<C-N>")
-  call VerifyScreenDump(buf, 'Test_pum_preview_1', {})
+  call VerifyScreenDump(buf, 'Test_pum_preview_' . a:dumpfile_name . '_1', {})
 
   call term_sendkeys(buf, "\<C-N>")
-  call VerifyScreenDump(buf, 'Test_pum_preview_2', {})
+  call VerifyScreenDump(buf, 'Test_pum_preview_' . a:dumpfile_name . '_2', {})
 
   call term_sendkeys(buf, "\<C-N>")
-  call VerifyScreenDump(buf, 'Test_pum_preview_3', {})
+  call VerifyScreenDump(buf, 'Test_pum_preview_' . a:dumpfile_name . '_3', {})
 
   call term_sendkeys(buf, "\<C-N>")
-  call VerifyScreenDump(buf, 'Test_pum_preview_4', {})
+  call VerifyScreenDump(buf, 'Test_pum_preview_' . a:dumpfile_name . '_4', {})
 
   call term_sendkeys(buf, "\<Esc>")
   call StopVimInTerminal(buf)
 endfunc
 
+func Test_previewpopup_pum_pedit()
+  let lines =<< trim END
+      call setline(1, ['one', 'two', 'three', 'other', 'once', 'only', 'off'])
+      set previewpopup=height:6,width:40
+      pedit XpreviewText.vim
+  END
+  call s:run_preview_popuppum(lines, 'pedit')
+endfunc
+
+func Test_previewpopup_pum_pbuffer()
+  let lines =<< trim END
+      call setline(1, ['one', 'two', 'three', 'other', 'once', 'only', 'off'])
+      set previewpopup=height:6,width:40
+      badd XpreviewText.vim
+      exe bufnr('$') . 'pbuffer'
+  END
+  call s:run_preview_popuppum(lines, 'pbuffer')
+endfunc
 
 func Get_popupmenu_lines()
   let lines =<< trim END
diff --git a/src/testdir/test_preview.vim b/src/testdir/test_preview.vim
index 3d13d99..0df1a90 100644
--- a/src/testdir/test_preview.vim
+++ b/src/testdir/test_preview.vim
@@ -15,14 +15,7 @@
   bwipe
 endfunc
 
-func Test_window_preview()
-  CheckFeature quickfix
-
-  " Open a preview window
-  pedit Xa
-  call assert_equal(2, winnr('$'))
-  call assert_equal(0, &previewwindow)
-
+func s:goto_preview_and_close()
   " Go to the preview window
   wincmd P
   call assert_equal(1, &previewwindow)
@@ -36,6 +29,46 @@
   call assert_fails('wincmd P', 'E441:')
 endfunc
 
+func Test_window_preview()
+  CheckFeature quickfix
+
+  " Open a preview window
+  pedit Xa
+  call assert_equal(2, winnr('$'))
+  call assert_equal(0, &previewwindow)
+
+  call s:goto_preview_and_close()
+endfunc
+
+func Test_window_preview_from_pbuffer()
+  CheckFeature quickfix
+
+  call writefile(['/* some C code */'], 'Xpreview.c', 'D')
+  edit Xpreview.c
+  const buf_num = bufnr('%')
+  enew
+  call assert_equal(1, winnr('$'))
+  exe 'pbuffer ' .  buf_num
+  call assert_equal(2, winnr('$'))
+  call assert_equal(0, &previewwindow)
+
+  call s:goto_preview_and_close()
+endfunc
+
+func Test_window_preview_terminal()
+  CheckFeature quickfix
+  CheckFeature terminal
+
+  term ++curwin
+  const buf_num = bufnr('$')
+  call assert_equal(1, winnr('$'))
+  exe 'pbuffer' . buf_num
+  call assert_equal(2, winnr('$'))
+  call assert_equal(0, &previewwindow)
+
+  call s:goto_preview_and_close()
+endfunc
+
 func Test_window_preview_from_help()
   CheckFeature quickfix
 
diff --git a/src/testdir/test_statusline.vim b/src/testdir/test_statusline.vim
index 22fd78c..73c3bee 100644
--- a/src/testdir/test_statusline.vim
+++ b/src/testdir/test_statusline.vim
@@ -220,6 +220,10 @@
   wincmd j
   call assert_match('^\[Preview\],PRV\s*$', s:get_statusline())
   pclose
+  pbuffer
+  wincmd j
+  call assert_match('^\[Preview\],PRV\s*$', s:get_statusline())
+  pclose
 
   " %y: Type of file in the buffer, e.g., "[vim]". See 'filetype'.
   " %Y: Type of file in the buffer, e.g., ",VIM". See 'filetype'.
diff --git a/src/testdir/test_vim9_cmd.vim b/src/testdir/test_vim9_cmd.vim
index 51ae7e6..900bf8a 100644
--- a/src/testdir/test_vim9_cmd.vim
+++ b/src/testdir/test_vim9_cmd.vim
@@ -2044,16 +2044,27 @@
   v9.CheckScriptFailureList(lines, ["E1356:", "E1405:"])
 enddef
 
+def s:check_previewpopup(expected_title: string)
+  var id = popup_findpreview()
+  assert_notequal(id, 0)
+  assert_match(expected_title, popup_getoptions(id).title)
+  popup_clear()
+  bw Xppfile
+  set previewpopup&
+enddef
+
 " Test for the 'previewpopup' option
 def Test_previewpopup()
   set previewpopup=height:10,width:60
   pedit Xppfile
-  var id = popup_findpreview()
-  assert_notequal(id, 0)
-  assert_match('Xppfile', popup_getoptions(id).title)
-  popup_clear()
-  bw Xppfile
-  set previewpopup&
+  s:check_previewpopup('Xppfile')
+enddef
+
+def Test_previewpopup_pbuffer()
+  set previewpopup=height:10,width:60
+  edit Xppfile
+  pbuffer
+  s:check_previewpopup('')
 enddef
 
 def Test_syntax_enable_clear()
diff --git a/src/testdir/test_window_cmd.vim b/src/testdir/test_window_cmd.vim
index 75b37ec..b6381a3 100644
--- a/src/testdir/test_window_cmd.vim
+++ b/src/testdir/test_window_cmd.vim
@@ -1258,6 +1258,7 @@
 
     " Preview window
     call assert_fails(dir .. 'pedit Xnorfile2', 'E36:')
+    call assert_fails(dir .. 'pbuffer', 'E36:')
     call setline(1, 'abc')
     call assert_fails(dir .. 'psearch abc', 'E36:')
   endif
diff --git a/src/testdir/test_winfixbuf.vim b/src/testdir/test_winfixbuf.vim
index 3cec4ed..2d4eaf6 100644
--- a/src/testdir/test_winfixbuf.vim
+++ b/src/testdir/test_winfixbuf.vim
@@ -2545,6 +2545,18 @@
   call assert_equal(l:other, bufnr())
 endfunc
 
+" Allow :pbuffer because, unlike :buffer, it uses a separate window
+func Test_pbuffer()
+  call s:reset_all_buffers()
+
+  let l:other = s:make_buffer_pairs()
+
+  exe 'pbuffer ' . l:other
+
+  execute "normal \<C-w>w"
+  call assert_equal(l:other, bufnr())
+endfunc
+
 " Fail :pop but :pop! is allowed
 func Test_pop()
   call s:reset_all_buffers()