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/ex_cmdidxs.h b/src/ex_cmdidxs.h
index 658f05e..0b13a6c 100644
--- a/src/ex_cmdidxs.h
+++ b/src/ex_cmdidxs.h
@@ -21,16 +21,16 @@
   /* n */ 308,
   /* o */ 328,
   /* p */ 340,
-  /* q */ 380,
-  /* r */ 383,
-  /* s */ 403,
-  /* t */ 473,
-  /* u */ 520,
-  /* v */ 531,
-  /* w */ 552,
-  /* x */ 566,
-  /* y */ 576,
-  /* z */ 577
+  /* q */ 381,
+  /* r */ 384,
+  /* s */ 404,
+  /* t */ 474,
+  /* u */ 521,
+  /* v */ 532,
+  /* w */ 553,
+  /* x */ 567,
+  /* y */ 577,
+  /* z */ 578
 };
 
 /*
@@ -56,7 +56,7 @@
   /* m */ {  1,  0,  0,  0,  7,  0,  0,  0,  0,  0, 10,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 16 },
   /* n */ {  0,  1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  5,  8, 10,  0,  0,  0,  0,  0, 17,  0,  0,  0,  0,  0 },
   /* o */ {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  2,  5,  0,  0,  0,  0,  0,  0,  9,  0, 11,  0,  0,  0 },
-  /* p */ {  1,  0,  3,  0,  4,  0,  0,  0,  0,  0,  0,  0,  0,  0,  7,  9,  0,  0, 16, 17, 26,  0, 28,  0, 29,  0 },
+  /* p */ {  1,  3,  4,  0,  5,  0,  0,  0,  0,  0,  0,  0,  0,  0,  8, 10,  0,  0, 17, 18, 27,  0, 29,  0, 30,  0 },
   /* q */ {  2,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0 },
   /* r */ {  0,  0,  0,  0,  0,  0,  0,  0, 12,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 14, 19,  0,  0,  0,  0 },
   /* s */ {  2,  6, 15,  0, 19, 23,  0, 25, 26,  0,  0, 29, 31, 35, 39, 41,  0, 50,  0, 51,  0, 64, 65,  0, 66,  0 },
@@ -69,4 +69,4 @@
   /* z */ {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0 }
 };
 
-static const int command_count = 594;
+static const int command_count = 595;
diff --git a/src/ex_cmds.h b/src/ex_cmds.h
index 25f6914..dc5da0a 100644
--- a/src/ex_cmds.h
+++ b/src/ex_cmds.h
@@ -1145,6 +1145,9 @@
 EXCMD(CMD_packloadall,	"packloadall",	ex_packloadall,
 	EX_BANG|EX_TRLBAR|EX_SBOXOK|EX_CMDWIN|EX_LOCK_OK,
 	ADDR_NONE),
+EXCMD(CMD_pbuffer,	"pbuffer",	ex_pbuffer,
+	EX_BANG|EX_RANGE|EX_BUFNAME|EX_BUFUNL|EX_COUNT|EX_EXTRA|EX_CMDARG|EX_TRLBAR,
+	ADDR_BUFFERS),
 EXCMD(CMD_pclose,	"pclose",	ex_pclose,
 	EX_BANG|EX_TRLBAR,
 	ADDR_NONE),
diff --git a/src/ex_docmd.c b/src/ex_docmd.c
index 859dc85..d516755 100644
--- a/src/ex_docmd.c
+++ b/src/ex_docmd.c
@@ -36,6 +36,7 @@
 static void	ex_doautocmd(exarg_T *eap);
 static void	ex_bunload(exarg_T *eap);
 static void	ex_buffer(exarg_T *eap);
+static void	do_exbuffer(exarg_T *eap);
 static void	ex_bmodified(exarg_T *eap);
 static void	ex_bnext(exarg_T *eap);
 static void	ex_bprevious(exarg_T *eap);
@@ -101,10 +102,14 @@
 static void	ex_pclose(exarg_T *eap);
 static void	ex_ptag(exarg_T *eap);
 static void	ex_pedit(exarg_T *eap);
+static void	ex_pbuffer(exarg_T *eap);
+static void	prepare_preview_window(void);
+static void	back_to_current_window(win_T *curwin_save);
 #else
 # define ex_pclose		ex_ni
 # define ex_ptag		ex_ni
 # define ex_pedit		ex_ni
+# define ex_pbuffer		ex_ni
 #endif
 static void	ex_hide(exarg_T *eap);
 static void	ex_exit(exarg_T *eap);
@@ -5734,6 +5739,15 @@
 {
     if (ERROR_IF_ANY_POPUP_WINDOW)
 	return;
+    do_exbuffer(eap);
+}
+
+/*
+ * ":buffer" command and alike.
+ */
+    static void
+do_exbuffer(exarg_T *eap)
+{
     if (*eap->arg)
 	eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
     else
@@ -9437,17 +9451,43 @@
 ex_pedit(exarg_T *eap)
 {
     win_T	*curwin_save = curwin;
+    prepare_preview_window();
 
+    // Edit the file.
+    do_exedit(eap, NULL);
+
+    back_to_current_window(curwin_save);
+}
+
+/*
+ * ":pbuffer"
+ */
+    static void
+ex_pbuffer(exarg_T *eap)
+{
+    win_T	*curwin_save = curwin;
+    prepare_preview_window();
+
+    // Go to the buffer.
+    do_exbuffer(eap);
+
+    back_to_current_window(curwin_save);
+}
+
+    static void
+prepare_preview_window(void)
+{
     if (ERROR_IF_ANY_POPUP_WINDOW)
 	return;
 
     // Open the preview window or popup and make it the current window.
     g_do_tagpreview = p_pvh;
     prepare_tagpreview(TRUE, TRUE, FALSE);
+}
 
-    // Edit the file.
-    do_exedit(eap, NULL);
-
+    static void
+back_to_current_window(win_T *curwin_save)
+{
     if (curwin != curwin_save && win_valid(curwin_save))
     {
 	// Return cursor to where we were
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()
diff --git a/src/version.c b/src/version.c
index 1cfe99d..f8cee6d 100644
--- a/src/version.c
+++ b/src/version.c
@@ -705,6 +705,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    934,
+/**/
     933,
 /**/
     932,