test_selectmode.vim 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. " Test for Select-mode
  2. source check.vim
  3. " CheckNotGui
  4. " CheckUnix
  5. source shared.vim
  6. source mouse.vim
  7. " Test for select mode
  8. func Test_selectmode_basic()
  9. new
  10. call setline(1, range(1,100))
  11. 50
  12. norm! gHy
  13. call assert_equal('y51', getline('.'))
  14. call setline(1, range(1,100))
  15. 50
  16. exe ":norm! V9jo\<c-g>y"
  17. call assert_equal('y60', getline('.'))
  18. call setline(1, range(1,100))
  19. 50
  20. " call feedkeys(":set im\n\<c-o>gHc\<c-o>:set noim\n", 'tx')
  21. call feedkeys("i\<c-o>gHc\<esc>", 'tx')
  22. call assert_equal('c51', getline('.'))
  23. " clean up
  24. bw!
  25. endfunc
  26. " Test for starting selectmode
  27. func Test_selectmode_start()
  28. new
  29. set selectmode=key keymodel=startsel
  30. call setline(1, ['abc', 'def', 'ghi'])
  31. call cursor(1, 4)
  32. call feedkeys("A\<s-home>start\<esc>", 'txin')
  33. call assert_equal(['startdef', 'ghi'], getline(1, '$'))
  34. " start select mode again with gv
  35. set selectmode=cmd
  36. call feedkeys('gvabc', 'xt')
  37. call assert_equal('abctdef', getline(1))
  38. " arrow keys without shift should not start selection
  39. call feedkeys("A\<Home>\<Right>\<Left>ro", 'xt')
  40. call assert_equal('roabctdef', getline(1))
  41. set selectmode= keymodel=
  42. bw!
  43. endfunc
  44. " Test for characterwise select mode
  45. func Test_characterwise_select_mode()
  46. new
  47. " Select mode maps
  48. snoremap <lt>End> <End>
  49. snoremap <lt>Down> <Down>
  50. snoremap <lt>Del> <Del>
  51. " characterwise select mode: delete middle line
  52. call deletebufline('', 1, '$')
  53. call append('$', ['a', 'b', 'c'])
  54. exe "normal Gkkgh\<End>\<Del>"
  55. call assert_equal(['', 'b', 'c'], getline(1, '$'))
  56. " characterwise select mode: delete middle two lines
  57. call deletebufline('', 1, '$')
  58. call append('$', ['a', 'b', 'c'])
  59. exe "normal Gkkgh\<Down>\<End>\<Del>"
  60. call assert_equal(['', 'c'], getline(1, '$'))
  61. " characterwise select mode: delete last line
  62. call deletebufline('', 1, '$')
  63. call append('$', ['a', 'b', 'c'])
  64. exe "normal Ggh\<End>\<Del>"
  65. call assert_equal(['', 'a', 'b', ''], getline(1, '$'))
  66. " characterwise select mode: delete last two lines
  67. call deletebufline('', 1, '$')
  68. call append('$', ['a', 'b', 'c'])
  69. exe "normal Gkgh\<Down>\<End>\<Del>"
  70. call assert_equal(['', 'a', ''], getline(1, '$'))
  71. " CTRL-H in select mode behaves like 'x'
  72. call setline(1, 'abcdef')
  73. exe "normal! gggh\<Right>\<Right>\<Right>\<C-H>"
  74. call assert_equal('ef', getline(1))
  75. " CTRL-O in select mode switches to visual mode for one command
  76. call setline(1, 'abcdef')
  77. exe "normal! gggh\<C-O>3lm"
  78. call assert_equal('mef', getline(1))
  79. sunmap <lt>End>
  80. sunmap <lt>Down>
  81. sunmap <lt>Del>
  82. bwipe!
  83. endfunc
  84. " Test for linewise select mode
  85. func Test_linewise_select_mode()
  86. new
  87. " linewise select mode: delete middle line
  88. call append('$', ['a', 'b', 'c'])
  89. exe "normal GkkgH\<Del>"
  90. call assert_equal(['', 'b', 'c'], getline(1, '$'))
  91. " linewise select mode: delete middle two lines
  92. call deletebufline('', 1, '$')
  93. call append('$', ['a', 'b', 'c'])
  94. exe "normal GkkgH\<Down>\<Del>"
  95. call assert_equal(['', 'c'], getline(1, '$'))
  96. " linewise select mode: delete last line
  97. call deletebufline('', 1, '$')
  98. call append('$', ['a', 'b', 'c'])
  99. exe "normal GgH\<Del>"
  100. call assert_equal(['', 'a', 'b'], getline(1, '$'))
  101. " linewise select mode: delete last two lines
  102. call deletebufline('', 1, '$')
  103. call append('$', ['a', 'b', 'c'])
  104. exe "normal GkgH\<Down>\<Del>"
  105. call assert_equal(['', 'a'], getline(1, '$'))
  106. bwipe!
  107. endfunc
  108. " Test for blockwise select mode (g CTRL-H)
  109. func Test_blockwise_select_mode()
  110. new
  111. call setline(1, ['foo', 'bar'])
  112. call feedkeys("g\<BS>\<Right>\<Down>mm", 'xt')
  113. call assert_equal(['mmo', 'mmr'], getline(1, '$'))
  114. close!
  115. endfunc
  116. " Test for using visual mode maps in select mode
  117. func Test_select_mode_map()
  118. new
  119. vmap <buffer> <F2> 3l
  120. call setline(1, 'Test line')
  121. call feedkeys("gh\<F2>map", 'xt')
  122. call assert_equal('map line', getline(1))
  123. vmap <buffer> <F2> ygV
  124. call feedkeys("0gh\<Right>\<Right>\<F2>cwabc", 'xt')
  125. call assert_equal('abc line', getline(1))
  126. vmap <buffer> <F2> :<C-U>let v=100<CR>
  127. call feedkeys("gggh\<Right>\<Right>\<F2>foo", 'xt')
  128. call assert_equal('foo line', getline(1))
  129. " reselect the select mode using gv from a visual mode map
  130. vmap <buffer> <F2> gv
  131. set selectmode=cmd
  132. call feedkeys("0gh\<F2>map", 'xt')
  133. call assert_equal('map line', getline(1))
  134. set selectmode&
  135. close!
  136. endfunc
  137. " Test double/triple/quadruple click to start 'select' mode
  138. func Test_term_mouse_multiple_clicks_to_select_mode()
  139. let save_mouse = &mouse
  140. let save_term = &term
  141. " let save_ttymouse = &ttymouse
  142. " call test_override('no_query_mouse', 1)
  143. " 'mousetime' must be sufficiently large, or else the test is flaky when
  144. " using a ssh connection with X forwarding; i.e. ssh -X.
  145. " set mouse=a term=xterm mousetime=1000
  146. set mouse=a mousetime=1000
  147. set selectmode=mouse
  148. new
  149. for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
  150. let msg = 'ttymouse=' .. ttymouse_val
  151. " exe 'set ttymouse=' .. ttymouse_val
  152. " Single-click and drag should 'select' the characters
  153. call setline(1, ['foo [foo bar] foo', 'foo'])
  154. call MouseLeftClick(1, 3)
  155. call assert_equal(0, getcharmod(), msg)
  156. call MouseLeftDrag(1, 13)
  157. call MouseLeftRelease(1, 13)
  158. norm! o
  159. call assert_equal(['foo foo', 'foo'], getline(1, '$'), msg)
  160. " Double-click on word should visually 'select' the word.
  161. call setline(1, ['foo [foo bar] foo', 'foo'])
  162. call MouseLeftClick(1, 2)
  163. call assert_equal(0, getcharmod(), msg)
  164. call MouseLeftRelease(1, 2)
  165. call MouseLeftClick(1, 2)
  166. call assert_equal(32, getcharmod(), msg) " double-click
  167. call MouseLeftRelease(1, 2)
  168. call assert_equal('s', mode(), msg)
  169. norm! bar
  170. call assert_equal(['bar [foo bar] foo', 'foo'], getline(1, '$'), msg)
  171. " Double-click on opening square bracket should visually
  172. " 'select' the whole [foo bar].
  173. call setline(1, ['foo [foo bar] foo', 'foo'])
  174. call MouseLeftClick(1, 5)
  175. call assert_equal(0, getcharmod(), msg)
  176. call MouseLeftRelease(1, 5)
  177. call MouseLeftClick(1, 5)
  178. call assert_equal(32, getcharmod(), msg) " double-click
  179. call MouseLeftRelease(1, 5)
  180. call assert_equal('s', mode(), msg)
  181. norm! bar
  182. call assert_equal(['foo bar foo', 'foo'], getline(1, '$'), msg)
  183. " To guarantee that the next click is not counted as a triple click
  184. call MouseRightClick(1, 1)
  185. call MouseRightRelease(1, 1)
  186. " Triple-click should visually 'select' the whole line.
  187. call setline(1, ['foo [foo bar] foo', 'foo'])
  188. call MouseLeftClick(1, 3)
  189. call assert_equal(0, getcharmod(), msg)
  190. call MouseLeftRelease(1, 3)
  191. call MouseLeftClick(1, 3)
  192. call assert_equal(32, getcharmod(), msg) " double-click
  193. call MouseLeftRelease(1, 3)
  194. call MouseLeftClick(1, 3)
  195. call assert_equal(64, getcharmod(), msg) " triple-click
  196. call MouseLeftRelease(1, 3)
  197. call assert_equal('S', mode(), msg)
  198. norm! baz
  199. call assert_equal(['bazfoo'], getline(1, '$'), msg)
  200. " Quadruple-click should start visual block 'select'.
  201. call setline(1, ['aaaaaa', 'bbbbbb'])
  202. call MouseLeftClick(1, 2)
  203. call assert_equal(0, getcharmod(), msg)
  204. call MouseLeftRelease(1, 2)
  205. call MouseLeftClick(1, 2)
  206. call assert_equal(32, getcharmod(), msg) " double-click
  207. call MouseLeftRelease(1, 2)
  208. call MouseLeftClick(1, 2)
  209. call assert_equal(64, getcharmod(), msg) " triple-click
  210. call MouseLeftRelease(1, 2)
  211. call MouseLeftClick(1, 2)
  212. call assert_equal(96, getcharmod(), msg) " quadruple-click
  213. call MouseLeftDrag(2, 4)
  214. call MouseLeftRelease(2, 4)
  215. call assert_equal("\<c-s>", mode(), msg)
  216. norm! x
  217. call assert_equal(['axaa', 'bxbb'], getline(1, '$'), msg)
  218. endfor
  219. let &mouse = save_mouse
  220. " let &term = save_term
  221. " let &ttymouse = save_ttymouse
  222. set mousetime&
  223. set selectmode&
  224. " call test_override('no_query_mouse', 0)
  225. bwipe!
  226. endfunc
  227. " Test for selecting a register with CTRL-R
  228. func Test_selectmode_register()
  229. new
  230. " Default behavior: use unnamed register
  231. call setline(1, 'foo')
  232. call setreg('"', 'bar')
  233. call setreg('a', 'baz')
  234. exe ":norm! v\<c-g>a"
  235. call assert_equal(getline('.'), 'aoo')
  236. call assert_equal('f', getreg('"'))
  237. call assert_equal('baz', getreg('a'))
  238. " Use the black hole register
  239. call setline(1, 'foo')
  240. call setreg('"', 'bar')
  241. call setreg('a', 'baz')
  242. exe ":norm! v\<c-g>\<c-r>_a"
  243. call assert_equal(getline('.'), 'aoo')
  244. call assert_equal('bar', getreg('"'))
  245. call assert_equal('baz', getreg('a'))
  246. " Invalid register: use unnamed register
  247. call setline(1, 'foo')
  248. call setreg('"', 'bar')
  249. call setreg('a', 'baz')
  250. exe ":norm! v\<c-g>\<c-r>?a"
  251. call assert_equal(getline('.'), 'aoo')
  252. call assert_equal('f', getreg('"'))
  253. call assert_equal('baz', getreg('a'))
  254. " Use unnamed register
  255. call setline(1, 'foo')
  256. call setreg('"', 'bar')
  257. call setreg('a', 'baz')
  258. exe ":norm! v\<c-g>\<c-r>\"a"
  259. call assert_equal(getline('.'), 'aoo')
  260. call assert_equal('f', getreg('"'))
  261. call assert_equal('baz', getreg('a'))
  262. " use specicifed register, unnamed register is also written
  263. call setline(1, 'foo')
  264. call setreg('"', 'bar')
  265. call setreg('a', 'baz')
  266. exe ":norm! v\<c-g>\<c-r>aa"
  267. call assert_equal(getline('.'), 'aoo')
  268. call assert_equal('f', getreg('"'))
  269. call assert_equal('f', getreg('a'))
  270. bw!
  271. endfunc
  272. func Test_ins_ctrl_o_in_insert_mode_resets_selectmode()
  273. new
  274. " ctrl-o in insert mode resets restart_VIsual_select
  275. call setline(1, 'abcdef')
  276. call cursor(1, 1)
  277. exe "norm! \<c-v>\<c-g>\<c-o>c\<c-o>\<c-v>\<right>\<right>IABC"
  278. call assert_equal('ABCbcdef', getline(1))
  279. bwipe!
  280. endfunc
  281. " Test that an :lmap mapping for a printable keypad key is applied when typing
  282. " it in Select mode.
  283. func Test_selectmode_keypad_lmap()
  284. new
  285. lnoremap <buffer> <kPoint> ???
  286. lnoremap <buffer> <kEnter> !!!
  287. setlocal iminsert=1
  288. call setline(1, 'abcdef')
  289. call feedkeys("gH\<kPoint>\<Esc>", 'tx')
  290. call assert_equal(['???'], getline(1, '$'))
  291. call feedkeys("gH\<kEnter>\<Esc>", 'tx')
  292. call assert_equal(['!!!'], getline(1, '$'))
  293. bwipe!
  294. endfunc
  295. " vim: shiftwidth=2 sts=2 expandtab