patch 8.1.1773: the preview popup window may be too far to the right

Problem:    The preview popup window may be too far to the right.
Solution:   Keep it inside the screen.  Also keep the close button and
            scrollbar visible if possible.
diff --git a/src/testdir/dumps/Test_popupwin_previewpopup_1.dump b/src/testdir/dumps/Test_popupwin_previewpopup_1.dump
index d23dd67..06ac4cb 100644
--- a/src/testdir/dumps/Test_popupwin_previewpopup_1.dump
+++ b/src/testdir/dumps/Test_popupwin_previewpopup_1.dump
@@ -8,7 +8,7 @@
 |f|i|n|d| >t|h|e|w|o|r|d| |s|o|m|e|w|h|e|r|e| @52
 |n|i|n|e| @70
 |t|h|i|s| |i|s| |a|n|o|t|h|e|r| |w|o|r|d| @54
+|v|e|r|y| |l|o|n|g| |l|i|n|e| |w|h|e|r|e| |t|h|e| |w|o|r|d| |i|s| |a|l|s|o| |a|n|o|t|h|e|r| @29
 |~+0#4040ff13&| @73
 |~| @73
-|~| @73
 |:+0#0000000&| @55|8|,|6| @10|A|l@1| 
diff --git a/src/testdir/dumps/Test_popupwin_previewpopup_2.dump b/src/testdir/dumps/Test_popupwin_previewpopup_2.dump
index 4d0e49c..aa53bb3 100644
--- a/src/testdir/dumps/Test_popupwin_previewpopup_2.dump
+++ b/src/testdir/dumps/Test_popupwin_previewpopup_2.dump
@@ -8,7 +8,7 @@
 |f|i|n|d| |t|h|e|║+0#0000001#ffd7ff255|3|0| @37| +0#0000000#a8a8a8255|║+0#0000001#ffd7ff255| +0#0000000#ffffff0@23
 |n|i|n|e| @3|╚+0#0000001#ffd7ff255|═@40|╝| +0#0000000#ffffff0@23
 |t|h|i|s| |i|s| >a|n|o|t|h|e|r| |w|o|r|d| @54
+|v|e|r|y| |l|o|n|g| |l|i|n|e| |w|h|e|r|e| |t|h|e| |w|o|r|d| |i|s| |a|l|s|o| |a|n|o|t|h|e|r| @29
 |~+0#4040ff13&| @73
 |~| @73
-|~| @73
 |/+0#0000000&|a|n|o|t|h|e|r| @48|1|0|,|9| @9|A|l@1| 
diff --git a/src/testdir/dumps/Test_popupwin_previewpopup_3.dump b/src/testdir/dumps/Test_popupwin_previewpopup_3.dump
index 27a6caf..c4ae417 100644
--- a/src/testdir/dumps/Test_popupwin_previewpopup_3.dump
+++ b/src/testdir/dumps/Test_popupwin_previewpopup_3.dump
@@ -8,7 +8,7 @@
 |f|i|n|d| |t|h|e|w|o|r|d| |s|║+0#0000001#ffd7ff255|3|0| @37| +0#0000000#a8a8a8255|║+0#0000001#ffd7ff255| +0#0000000#ffffff0@17
 |n|i|n|e| @9|╚+0#0000001#ffd7ff255|═@40|╝| +0#0000000#ffffff0@17
 |t|h|i|s| |i|s| >a|n|o|t|h|e|r| |w|o|r|d| @54
+|v|e|r|y| |l|o|n|g| |l|i|n|e| |w|h|e|r|e| |t|h|e| |w|o|r|d| |i|s| |a|l|s|o| |a|n|o|t|h|e|r| @29
 |~+0#4040ff13&| @73
 |~| @73
-|~| @73
 |:+0#0000000&| @55|1|0|,|9| @9|A|l@1| 
diff --git a/src/testdir/dumps/Test_popupwin_previewpopup_4.dump b/src/testdir/dumps/Test_popupwin_previewpopup_4.dump
new file mode 100644
index 0000000..fc63507
--- /dev/null
+++ b/src/testdir/dumps/Test_popupwin_previewpopup_4.dump
@@ -0,0 +1,14 @@
+|o+0&#ffffff0|n|e| @71
+|t|w|o| @71
+|t|h|r|e@1| @69
+|f|o|u|r| @70
+|f|i|v|e| @27|╔+0#0000001#ffd7ff255|═@40|X
+|s+0#0000000#ffffff0|i|x| @28|║+0#0000001#ffd7ff255|2|7| @37| +0#0000000#a8a8a8255|║+0#0000001#ffd7ff255
+|s+0#0000000#ffffff0|e|v|e|n| @26|║+0#0000001#ffd7ff255|t|h|i|s| |i|s| |a|n|o|t|h|e|r| |p|l|a|c|e| @18| +0#0000000#a8a8a8255|║+0#0000001#ffd7ff255
+|f+0#0000000#ffffff0|i|n|d| |t|h|e|w|o|r|d| |s|o|m|e|w|h|e|r|e| @9|║+0#0000001#ffd7ff255|2|9| @37| +0#0000000#0000001|║+0#0000001#ffd7ff255
+|n+0#0000000#ffffff0|i|n|e| @27|║+0#0000001#ffd7ff255|3|0| @37| +0#0000000#a8a8a8255|║+0#0000001#ffd7ff255
+|t+0#0000000#ffffff0|h|i|s| |i|s| |a|n|o|t|h|e|r| |w|o|r|d| @11|╚+0#0000001#ffd7ff255|═@40|╝
+|v+0#0000000#ffffff0|e|r|y| |l|o|n|g| |l|i|n|e| |w|h|e|r|e| |t|h|e| |w|o|r|d| |i|s| |a|l|s|o| >a|n|o|t|h|e|r| @29
+|~+0#4040ff13&| @73
+|~| @73
+|/+0#0000000&|a|n|o|t|h|e|r| @48|1@1|,|3|9| @8|A|l@1| 
diff --git a/src/testdir/test_popupwin.vim b/src/testdir/test_popupwin.vim
index 20c175a..6033be6 100644
--- a/src/testdir/test_popupwin.vim
+++ b/src/testdir/test_popupwin.vim
@@ -1009,9 +1009,9 @@
   call cursor(1, 1)
   redraw
   let winid = popup_create('vim', #{
-        \ line: 'cursor+2',
-        \ col: 'cursor+1',
-        \})
+	\ line: 'cursor+2',
+	\ col: 'cursor+1',
+	\})
   redraw
   let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
   call assert_equal('xvimxxxxxxxxxxxxx', line)
@@ -1020,9 +1020,9 @@
   call cursor(3, 3)
   redraw
   let winid = popup_create('vim', #{
-        \ line: 'cursor-2',
-        \ col: 'cursor-1',
-        \})
+	\ line: 'cursor-2',
+	\ col: 'cursor-1',
+	\})
   redraw
   let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
   call assert_equal('xvimxxxxxxxxxxxxx', line)
@@ -1347,11 +1347,11 @@
   call delete('XtestPopupBehind')
 endfunc
 
-func s:VerifyPosition( p, msg, line, col, width, height )
-  call assert_equal( a:line,   popup_getpos( a:p ).line,   a:msg . ' (l)' )
-  call assert_equal( a:col,    popup_getpos( a:p ).col,    a:msg . ' (c)' )
-  call assert_equal( a:width,  popup_getpos( a:p ).width,  a:msg . ' (w)' )
-  call assert_equal( a:height, popup_getpos( a:p ).height, a:msg . ' (h)' )
+func s:VerifyPosition(p, msg, line, col, width, height)
+  call assert_equal(a:line,   popup_getpos(a:p).line,   a:msg . ' (l)')
+  call assert_equal(a:col,    popup_getpos(a:p).col,    a:msg . ' (c)')
+  call assert_equal(a:width,  popup_getpos(a:p).width,  a:msg . ' (w)')
+  call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
 endfunc
 
 func Test_popup_position_adjust()
@@ -1364,24 +1364,24 @@
   " Entries for cases which don't vary based on wrapping.
   " Format is per tests described below
   let both_wrap_tests = [
-        \       [ 'a', 5, &columns,        5, &columns - 2, 1, 1 ],
-        \       [ 'b', 5, &columns + 1,    5, &columns - 2, 1, 1 ],
-        \       [ 'c', 5, &columns - 1,    5, &columns - 2, 1, 1 ],
-        \       [ 'd', 5, &columns - 2,    5, &columns - 2, 1, 1 ],
-        \       [ 'e', 5, &columns - 3,    5, &columns - 3, 1, 1 ],
-        \
-        \       [ 'aa', 5, &columns,        5, &columns - 2, 2, 1 ],
-        \       [ 'bb', 5, &columns + 1,    5, &columns - 2, 2, 1 ],
-        \       [ 'cc', 5, &columns - 1,    5, &columns - 2, 2, 1 ],
-        \       [ 'dd', 5, &columns - 2,    5, &columns - 2, 2, 1 ],
-        \       [ 'ee', 5, &columns - 3,    5, &columns - 3, 2, 1 ],
-        \
-        \       [ 'aaa', 5, &columns,        5, &columns - 2, 3, 1 ],
-        \       [ 'bbb', 5, &columns + 1,    5, &columns - 2, 3, 1 ],
-        \       [ 'ccc', 5, &columns - 1,    5, &columns - 2, 3, 1 ],
-        \       [ 'ddd', 5, &columns - 2,    5, &columns - 2, 3, 1 ],
-        \       [ 'eee', 5, &columns - 3,    5, &columns - 3, 3, 1 ],
-        \ ]
+	\       ['a', 5, &columns,        5, &columns - 2, 1, 1],
+	\       ['b', 5, &columns + 1,    5, &columns - 2, 1, 1],
+	\       ['c', 5, &columns - 1,    5, &columns - 2, 1, 1],
+	\       ['d', 5, &columns - 2,    5, &columns - 2, 1, 1],
+	\       ['e', 5, &columns - 3,    5, &columns - 3, 1, 1],
+	\
+	\       ['aa', 5, &columns,        5, &columns - 2, 2, 1],
+	\       ['bb', 5, &columns + 1,    5, &columns - 2, 2, 1],
+	\       ['cc', 5, &columns - 1,    5, &columns - 2, 2, 1],
+	\       ['dd', 5, &columns - 2,    5, &columns - 2, 2, 1],
+	\       ['ee', 5, &columns - 3,    5, &columns - 3, 2, 1],
+	\
+	\       ['aaa', 5, &columns,        5, &columns - 2, 3, 1],
+	\       ['bbb', 5, &columns + 1,    5, &columns - 2, 3, 1],
+	\       ['ccc', 5, &columns - 1,    5, &columns - 2, 3, 1],
+	\       ['ddd', 5, &columns - 2,    5, &columns - 2, 3, 1],
+	\       ['eee', 5, &columns - 3,    5, &columns - 3, 3, 1],
+	\ ]
 
   " these test groups are dicts with:
   "  - comment: something to identify the group of tests by
@@ -1395,61 +1395,61 @@
   "     - expected width
   "     - expected height
   let tests = [
-        \ #{
-        \   comment: 'left-aligned with wrapping',
-        \   options: #{
-        \     wrap: 1,
-        \     pos: 'botleft',
-        \   },
-        \   tests: both_wrap_tests + [
-        \       [ 'aaaa', 5, &columns,        4, &columns - 2, 3, 2 ],
-        \       [ 'bbbb', 5, &columns + 1,    4, &columns - 2, 3, 2 ],
-        \       [ 'cccc', 5, &columns - 1,    4, &columns - 2, 3, 2 ],
-        \       [ 'dddd', 5, &columns - 2,    4, &columns - 2, 3, 2 ],
-        \       [ 'eeee', 5, &columns - 3,    5, &columns - 3, 4, 1 ],
-        \   ],
-        \ },
-        \ #{
-        \   comment: 'left aligned without wrapping',
-        \   options: #{
-        \     wrap: 0,
-        \     pos: 'botleft',
-        \   },
-        \   tests: both_wrap_tests + [
-        \       [ 'aaaa', 5, &columns,        5, &columns - 3, 4, 1 ],
-        \       [ 'bbbb', 5, &columns + 1,    5, &columns - 3, 4, 1 ],
-        \       [ 'cccc', 5, &columns - 1,    5, &columns - 3, 4, 1 ],
-        \       [ 'dddd', 5, &columns - 2,    5, &columns - 3, 4, 1 ],
-        \       [ 'eeee', 5, &columns - 3,    5, &columns - 3, 4, 1 ],
-        \   ],
-        \ },
-        \ #{
-        \   comment: 'left aligned with fixed position',
-        \   options: #{
-        \     wrap: 0,
-        \     fixed: 1,
-        \     pos: 'botleft',
-        \   },
-        \   tests: both_wrap_tests + [
-        \       [ 'aaaa', 5, &columns,        5, &columns - 2, 3, 1 ],
-        \       [ 'bbbb', 5, &columns + 1,    5, &columns - 2, 3, 1 ],
-        \       [ 'cccc', 5, &columns - 1,    5, &columns - 2, 3, 1 ],
-        \       [ 'dddd', 5, &columns - 2,    5, &columns - 2, 3, 1 ],
-        \       [ 'eeee', 5, &columns - 3,    5, &columns - 3, 4, 1 ],
-        \   ],
-        \ },
-      \ ]
+	\ #{
+	\   comment: 'left-aligned with wrapping',
+	\   options: #{
+	\     wrap: 1,
+	\     pos: 'botleft',
+	\   },
+	\   tests: both_wrap_tests + [
+	\       ['aaaa', 5, &columns,        4, &columns - 2, 3, 2],
+	\       ['bbbb', 5, &columns + 1,    4, &columns - 2, 3, 2],
+	\       ['cccc', 5, &columns - 1,    4, &columns - 2, 3, 2],
+	\       ['dddd', 5, &columns - 2,    4, &columns - 2, 3, 2],
+	\       ['eeee', 5, &columns - 3,    5, &columns - 3, 4, 1],
+	\   ],
+	\ },
+	\ #{
+	\   comment: 'left aligned without wrapping',
+	\   options: #{
+	\     wrap: 0,
+	\     pos: 'botleft',
+	\   },
+	\   tests: both_wrap_tests + [
+	\       ['aaaa', 5, &columns,        5, &columns - 3, 4, 1],
+	\       ['bbbb', 5, &columns + 1,    5, &columns - 3, 4, 1],
+	\       ['cccc', 5, &columns - 1,    5, &columns - 3, 4, 1],
+	\       ['dddd', 5, &columns - 2,    5, &columns - 3, 4, 1],
+	\       ['eeee', 5, &columns - 3,    5, &columns - 3, 4, 1],
+	\   ],
+	\ },
+	\ #{
+	\   comment: 'left aligned with fixed position',
+	\   options: #{
+	\     wrap: 0,
+	\     fixed: 1,
+	\     pos: 'botleft',
+	\   },
+	\   tests: both_wrap_tests + [
+	\       ['aaaa', 5, &columns,        5, &columns - 2, 3, 1],
+	\       ['bbbb', 5, &columns + 1,    5, &columns - 2, 3, 1],
+	\       ['cccc', 5, &columns - 1,    5, &columns - 2, 3, 1],
+	\       ['dddd', 5, &columns - 2,    5, &columns - 2, 3, 1],
+	\       ['eeee', 5, &columns - 3,    5, &columns - 3, 4, 1],
+	\   ],
+	\ },
+	\ ]
 
   for test_group in tests
     for test in test_group.tests
       let [ text, line, col, e_line, e_col, e_width, e_height ] = test
       let options = #{
-            \ line: line,
-            \ col: col,
-            \ }
-      call extend( options, test_group.options )
+	    \ line: line,
+	    \ col: col,
+	    \ }
+      call extend(options, test_group.options)
 
-      let p = popup_create( text, options )
+      let p = popup_create(text, options)
 
       let msg = string(extend(options, #{text: text}))
       call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
@@ -1465,62 +1465,62 @@
   " width of screen
   let X = join(map(range(&columns), {->'X'}), '')
 
-  let p = popup_create( X, #{line: 1, col: 1, wrap: 0})
-  call s:VerifyPosition( p, 'full width topleft', 1, 1, &columns, 1 )
+  let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
+  call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
 
   redraw
   let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
   call assert_equal(X, line)
 
-  call popup_close( p )
+  call popup_close(p)
   redraw
 
   " Same if placed on the right hand side
-  let p = popup_create( X, #{line: 1, col: &columns, wrap: 0})
-  call s:VerifyPosition( p, 'full width topright', 1, 1, &columns, 1 )
+  let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
+  call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
 
   redraw
   let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
   call assert_equal(X, line)
 
-  call popup_close( p )
+  call popup_close(p)
   redraw
 
   " Extend so > window width
   let X .= 'x'
 
-  let p = popup_create( X, #{line: 1, col: 1, wrap: 0})
-  call s:VerifyPosition( p, 'full width +  1 topleft', 1, 1, &columns, 1 )
+  let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
+  call s:VerifyPosition(p, 'full width +  1 topleft', 1, 1, &columns, 1)
 
   redraw
   let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
   call assert_equal(X[ : -2 ], line)
 
-  call popup_close( p )
+  call popup_close(p)
   redraw
 
   " Shifted then truncated (the x is not visible)
-  let p = popup_create( X, #{line: 1, col: &columns - 3, wrap: 0})
-  call s:VerifyPosition( p, 'full width + 1 topright', 1, 1, &columns, 1 )
+  let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
+  call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
 
   redraw
   let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
   call assert_equal(X[ : -2 ], line)
 
-  call popup_close( p )
+  call popup_close(p)
   redraw
 
   " Not shifted, just truncated
-  let p = popup_create( X,
-        \ #{line: 1, col: 2, wrap: 0, fixed: 1})
-  call s:VerifyPosition( p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
+  let p = popup_create(X,
+	\ #{line: 1, col: 2, wrap: 0, fixed: 1})
+  call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
 
   redraw
   let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
   let e_line = ' ' . X[ 1 : -2 ]
   call assert_equal(e_line, line)
 
-  call popup_close( p )
+  call popup_close(p)
   redraw
 
   call popup_clear()
@@ -1715,7 +1715,7 @@
     call popup_settext(p, 'this is a text')
   END
 
-  call writefile( lines, 'XtestPopupSetText' )
+  call writefile(lines, 'XtestPopupSetText')
   let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
   call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
 
@@ -2155,16 +2155,17 @@
   let lines =<< trim END
         set tags=Xtags
 	call setline(1, [
-              \ 'one',
-              \ 'two',
-              \ 'three',
-              \ 'four',
-              \ 'five',
-              \ 'six',
-              \ 'seven',
-              \ 'find theword somewhere',
-              \ 'nine',
-              \ 'this is another word'])
+	      \ 'one',
+	      \ 'two',
+	      \ 'three',
+	      \ 'four',
+	      \ 'five',
+	      \ 'six',
+	      \ 'seven',
+	      \ 'find theword somewhere',
+	      \ 'nine',
+	      \ 'this is another word',
+	      \ 'very long line where the word is also another'])
         set previewpopup=height:4,width:40
   END
   call writefile(lines, 'XtestPreviewPopup')
@@ -2177,6 +2178,13 @@
   call term_sendkeys(buf, "/another\<CR>\<C-W>}")
   call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
 
+  call term_sendkeys(buf, ":call popup_move(popup_getpreview(), #{col: 15})\<CR>")
+  call term_sendkeys(buf, ":\<CR>")
+  call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
+
+  call term_sendkeys(buf, "/another\<CR>\<C-W>}")
+  call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
+
   call StopVimInTerminal(buf)
   call delete('Xtags')
   call delete('Xtagfile')