From 37cbd8b00ae6e1a81b70aa6c2bd709a0a264a037 Mon Sep 17 00:00:00 2001 From: Hugo Musso Gualandi Date: Sat, 4 Jul 2020 12:42:41 -0300 Subject: [PATCH 1/6] Remove some dead code The calls to min() and max() ensure that these conditions are never true. --- plugin/move.vim | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/plugin/move.vim b/plugin/move.vim index 5089562..39071ae 100644 --- a/plugin/move.vim +++ b/plugin/move.vim @@ -52,11 +52,6 @@ function! s:MoveBlockDown(start, end, distance) let l:next_line = min([a:end + a:distance, line('$')]) - if l:next_line > line('$') - call s:ResetCursor() - return - endif - execute 'silent' a:start ',' a:end 'move ' l:next_line if (g:move_auto_indent == 1) call s:ResetCursor() @@ -72,11 +67,6 @@ function! s:MoveBlockUp(start, end, distance) let l:prev_line = max([a:start - a:distance, 1]) - if l:prev_line < 0 - call s:ResetCursor() - return - endif - execute 'silent' a:start ',' a:end 'move ' (l:prev_line - 1) if (g:move_auto_indent == 1) call s:ResetCursor() From c7f35c42b36ddf351aeed191f7191db7a3a5909e Mon Sep 17 00:00:00 2001 From: Hugo Musso Gualandi Date: Sun, 5 Jul 2020 11:01:56 -0300 Subject: [PATCH 2/6] Consolidate ModeLineUp and MoveLineDown Reduce code duplication by replacing MoveLineUp and MoveLineDown by a single function, MoveLineVertically. --- plugin/move.vim | 79 ++++++++++++++----------------------------------- 1 file changed, 23 insertions(+), 56 deletions(-) diff --git a/plugin/move.vim b/plugin/move.vim index 39071ae..77dcfb7 100644 --- a/plugin/move.vim +++ b/plugin/move.vim @@ -38,13 +38,6 @@ function! s:RestoreDefaultRegister() let @" = s:default_register_value endfunction -function! s:GetRelativeCursorVirtCol() - let l:cursor_col = virtcol('.') - silent normal! ^ - " cursor position relative line start taking into account of indentations - return l:cursor_col - virtcol('.') + 1 -endfunction - function! s:MoveBlockDown(start, end, distance) if !&modifiable return @@ -170,53 +163,32 @@ function! s:MoveBlockRight(distance) range let &virtualedit = l:old_virtualedit endfunction -function! s:MoveLineUp(distance) - if !&modifiable || line('.') == 1 - return - endif - - let l:relative_cursor_col = s:GetRelativeCursorVirtCol() - - if (line('.') - a:distance) < 0 - execute 'silent move 0' - if (g:move_auto_indent == 1) - normal! == - endif - return - endif - - execute 'silent m-' . (a:distance + 1) - - if (g:move_auto_indent == 1) - normal! == - endif - - " restore cursor column position - execute 'silent normal!' . max([1, (virtcol('.') + l:relative_cursor_col - 1)]) . '|' -endfunction - -function! s:MoveLineDown(distance) - if !&modifiable || line('.') == line('$') +function! s:MoveLineVertically(distance) + if !&modifiable return endif - let l:relative_cursor_col = s:GetRelativeCursorVirtCol() + " Remember the current cursor position. When we move or reindent a line + " Vim will move the cursor to the first non-blank character. + let l:old_cursor_col = virtcol('.') + silent normal! ^ + let l:old_indent = virtcol('.') - if (line('.') + a:distance) > line('$') - silent move $ - if (g:move_auto_indent == 1) - normal! == - endif - return + if a:distance <= 0 + let l:after = max([1, line('.') + a:distance]) - 1 + else + let l:after = min([line('$'), line('.') + a:distance]) endif + execute 'silent move' l:after - execute 'silent m+' . a:distance if (g:move_auto_indent == 1) - normal! == + silent normal! == endif - " restore cursor column position - execute 'silent normal!' . max([1, (virtcol('.') + l:relative_cursor_col - 1)]) . '|' + " Restore the cursor column, taking indentation changes into account. + let l:new_indent = virtcol('.') + let l:new_cursor_col = max([1, l:old_cursor_col - l:old_indent + l:new_indent]) + execute 'silent normal!' l:new_cursor_col . '|' endfunction function! s:MoveCharLeft(distance) @@ -273,14 +245,9 @@ function! s:MoveBlockHalfPageDown(count) range call s:MoveBlockDown(a:firstline, a:lastline, l:distance) endfunction -function! s:MoveLineHalfPageUp(count) - let l:distance = a:count * (winheight('.') / 2) - call s:MoveLineUp(l:distance) -endfunction - -function! s:MoveLineHalfPageDown(count) +function! s:MoveLineHalfPageVertically(count) let l:distance = a:count * (winheight('.') / 2) - call s:MoveLineDown(l:distance) + call s:MoveLineVertically(l:distance) endfunction function! s:MoveKey(key) @@ -299,10 +266,10 @@ vnoremap MoveBlockRight :call MoveBlockRight(v:coun " or characters. In the case of lines, it causes vim to complain with E16 " (Invalid adress) if we try to move out of bounds. In the case of characters, " it messes up the result of calling col(). -nnoremap MoveLineDown : call MoveLineDown(v:count1) -nnoremap MoveLineUp : call MoveLineUp(v:count1) -nnoremap MoveLineHalfPageDown : call MoveLineHalfPageDown(v:count1) -nnoremap MoveLineHalfPageUp : call MoveLineHalfPageUp(v:count1) +nnoremap MoveLineDown : call MoveLineVertically( v:count1) +nnoremap MoveLineUp : call MoveLineVertically(-v:count1) +nnoremap MoveLineHalfPageDown : call MoveLineHalfPageVertically( v:count1) +nnoremap MoveLineHalfPageUp : call MoveLineHalfPageVertically(-v:count1) nnoremap MoveCharLeft : call MoveCharLeft(v:count1) nnoremap MoveCharRight : call MoveCharRight(v:count1) From 1fcb8cfb85998e5d0a149e95167c74727fc170b0 Mon Sep 17 00:00:00 2001 From: Hugo Musso Gualandi Date: Sun, 5 Jul 2020 11:32:01 -0300 Subject: [PATCH 3/6] Consolidate MoveBlockUp and MoveBlockDown Reduce code duplication by replacing MoveBlockUp and MoveBlockDown by a single function, MoveBlockVertically. --- plugin/move.vim | 56 ++++++++++++++----------------------------------- 1 file changed, 16 insertions(+), 40 deletions(-) diff --git a/plugin/move.vim b/plugin/move.vim index 77dcfb7..7a8ed79 100644 --- a/plugin/move.vim +++ b/plugin/move.vim @@ -26,10 +26,6 @@ if !exists('g:move_past_end_of_line') let g:move_past_end_of_line = 1 endif -function! s:ResetCursor() - normal! gv=gv^ -endfunction - function! s:SaveDefaultRegister() let s:default_register_value = @" endfunction @@ -38,34 +34,23 @@ function! s:RestoreDefaultRegister() let @" = s:default_register_value endfunction -function! s:MoveBlockDown(start, end, distance) +function s:MoveBlockVertically(start, end, distance) if !&modifiable return endif - let l:next_line = min([a:end + a:distance, line('$')]) - - execute 'silent' a:start ',' a:end 'move ' l:next_line - if (g:move_auto_indent == 1) - call s:ResetCursor() + if a:distance <= 0 + let l:after = max([1, a:start + a:distance]) - 1 else - normal! gv - endif -endfunction - -function! s:MoveBlockUp(start, end, distance) - if !&modifiable - return + let l:after = min([line('$'), a:end + a:distance]) endif + execute 'silent' a:start ',' a:end 'move ' l:after - let l:prev_line = max([a:start - a:distance, 1]) - - execute 'silent' a:start ',' a:end 'move ' (l:prev_line - 1) if (g:move_auto_indent == 1) - call s:ResetCursor() - else - normal! gv + normal! gv= endif + + normal! gv endfunction function! s:MoveBlockLeft(distance) range @@ -227,22 +212,13 @@ function! s:MoveCharRight(distance) call s:RestoreDefaultRegister() endfunction -function! s:MoveBlockOneLineUp(count) range - call s:MoveBlockUp(a:firstline, a:lastline, a:count) -endfunction - -function! s:MoveBlockOneLineDown(count) range - call s:MoveBlockDown(a:firstline, a:lastline, a:count) -endfunction - -function! s:MoveBlockHalfPageUp(count) range - let l:distance = a:count * (winheight('.') / 2) - call s:MoveBlockUp(a:firstline, a:lastline, l:distance) +function! s:MoveBlockOneLineVertically(count) range + call s:MoveBlockVertically(a:firstline, a:lastline, a:count) endfunction -function! s:MoveBlockHalfPageDown(count) range +function! s:MoveBlockHalfPageVertically(count) range let l:distance = a:count * (winheight('.') / 2) - call s:MoveBlockDown(a:firstline, a:lastline, l:distance) + call s:MoveBlockVertically(a:firstline, a:lastline, l:distance) endfunction function! s:MoveLineHalfPageVertically(count) @@ -255,10 +231,10 @@ function! s:MoveKey(key) endfunction -vnoremap MoveBlockDown :call MoveBlockOneLineDown(v:count1) -vnoremap MoveBlockUp :call MoveBlockOneLineUp(v:count1) -vnoremap MoveBlockHalfPageDown :call MoveBlockHalfPageDown(v:count1) -vnoremap MoveBlockHalfPageUp :call MoveBlockHalfPageUp(v:count1) +vnoremap MoveBlockDown :call MoveBlockOneLineVertically( v:count1) +vnoremap MoveBlockUp :call MoveBlockOneLineVertically(-v:count1) +vnoremap MoveBlockHalfPageDown :call MoveBlockHalfPageVertically( v:count1) +vnoremap MoveBlockHalfPageUp :call MoveBlockHalfPageVertically(-v:count1) vnoremap MoveBlockLeft :call MoveBlockLeft(v:count1) vnoremap MoveBlockRight :call MoveBlockRight(v:count1) From 2067ce034eebbad2a160dc764bc1f000e01e3d54 Mon Sep 17 00:00:00 2001 From: Hugo Musso Gualandi Date: Sun, 5 Jul 2020 11:46:00 -0300 Subject: [PATCH 4/6] Consolidate the HalfPage functions Reduce code duplication by putting the computation of half page size in a separate function. --- plugin/move.vim | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/plugin/move.vim b/plugin/move.vim index 7a8ed79..5c6d955 100644 --- a/plugin/move.vim +++ b/plugin/move.vim @@ -212,18 +212,12 @@ function! s:MoveCharRight(distance) call s:RestoreDefaultRegister() endfunction -function! s:MoveBlockOneLineVertically(count) range - call s:MoveBlockVertically(a:firstline, a:lastline, a:count) -endfunction - -function! s:MoveBlockHalfPageVertically(count) range - let l:distance = a:count * (winheight('.') / 2) - call s:MoveBlockVertically(a:firstline, a:lastline, l:distance) +function! s:HalfPageSize() + return winheight('.') / 2 endfunction -function! s:MoveLineHalfPageVertically(count) - let l:distance = a:count * (winheight('.') / 2) - call s:MoveLineVertically(l:distance) +function! s:MoveRangeVertically(count) range + call s:MoveBlockVertically(a:firstline, a:lastline, a:count) endfunction function! s:MoveKey(key) @@ -231,10 +225,10 @@ function! s:MoveKey(key) endfunction -vnoremap MoveBlockDown :call MoveBlockOneLineVertically( v:count1) -vnoremap MoveBlockUp :call MoveBlockOneLineVertically(-v:count1) -vnoremap MoveBlockHalfPageDown :call MoveBlockHalfPageVertically( v:count1) -vnoremap MoveBlockHalfPageUp :call MoveBlockHalfPageVertically(-v:count1) +vnoremap MoveBlockDown :call MoveRangeVertically( v:count1) +vnoremap MoveBlockUp :call MoveRangeVertically(-v:count1) +vnoremap MoveBlockHalfPageDown :call MoveRangeVertically( v:count1 * HalfPageSize()) +vnoremap MoveBlockHalfPageUp :call MoveRangeVertically(-v:count1 * HalfPageSize()) vnoremap MoveBlockLeft :call MoveBlockLeft(v:count1) vnoremap MoveBlockRight :call MoveBlockRight(v:count1) @@ -244,8 +238,8 @@ vnoremap MoveBlockRight :call MoveBlockRight(v:coun " it messes up the result of calling col(). nnoremap MoveLineDown : call MoveLineVertically( v:count1) nnoremap MoveLineUp : call MoveLineVertically(-v:count1) -nnoremap MoveLineHalfPageDown : call MoveLineHalfPageVertically( v:count1) -nnoremap MoveLineHalfPageUp : call MoveLineHalfPageVertically(-v:count1) +nnoremap MoveLineHalfPageDown : call MoveLineVertically( v:count1 * HalfPageSize()) +nnoremap MoveLineHalfPageUp : call MoveLineVertically(-v:count1 * HalfPageSize()) nnoremap MoveCharLeft : call MoveCharLeft(v:count1) nnoremap MoveCharRight : call MoveCharRight(v:count1) From 82001af61ccb53d1ae3e6a7cc3ccf97bd40da093 Mon Sep 17 00:00:00 2001 From: Hugo Musso Gualandi Date: Sun, 5 Jul 2020 11:48:19 -0300 Subject: [PATCH 5/6] Test truthyness of g:move_auto_indent People might expect that any truthy value should work to enable auto indent, and not just 1. --- plugin/move.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/move.vim b/plugin/move.vim index 5c6d955..3785ec6 100644 --- a/plugin/move.vim +++ b/plugin/move.vim @@ -46,7 +46,7 @@ function s:MoveBlockVertically(start, end, distance) endif execute 'silent' a:start ',' a:end 'move ' l:after - if (g:move_auto_indent == 1) + if g:move_auto_indent normal! gv= endif @@ -166,7 +166,7 @@ function! s:MoveLineVertically(distance) endif execute 'silent move' l:after - if (g:move_auto_indent == 1) + if g:move_auto_indent silent normal! == endif From 741a643426eae641a4b29ebbd0a5b982ade7c614 Mon Sep 17 00:00:00 2001 From: Hugo Musso Gualandi Date: Sun, 5 Jul 2020 12:23:02 -0300 Subject: [PATCH 6/6] Get rid of the MoveRange function --- plugin/move.vim | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/plugin/move.vim b/plugin/move.vim index 3785ec6..dceda3d 100644 --- a/plugin/move.vim +++ b/plugin/move.vim @@ -34,17 +34,17 @@ function! s:RestoreDefaultRegister() let @" = s:default_register_value endfunction -function s:MoveBlockVertically(start, end, distance) +function s:MoveBlockVertically(distance) range if !&modifiable return endif if a:distance <= 0 - let l:after = max([1, a:start + a:distance]) - 1 + let l:after = max([1, a:firstline + a:distance]) - 1 else - let l:after = min([line('$'), a:end + a:distance]) + let l:after = min([line('$'), a:lastline + a:distance]) endif - execute 'silent' a:start ',' a:end 'move ' l:after + execute 'silent' a:firstline ',' a:lastline 'move ' l:after if g:move_auto_indent normal! gv= @@ -216,19 +216,15 @@ function! s:HalfPageSize() return winheight('.') / 2 endfunction -function! s:MoveRangeVertically(count) range - call s:MoveBlockVertically(a:firstline, a:lastline, a:count) -endfunction - function! s:MoveKey(key) return '<' . g:move_key_modifier . '-' . a:key . '>' endfunction -vnoremap MoveBlockDown :call MoveRangeVertically( v:count1) -vnoremap MoveBlockUp :call MoveRangeVertically(-v:count1) -vnoremap MoveBlockHalfPageDown :call MoveRangeVertically( v:count1 * HalfPageSize()) -vnoremap MoveBlockHalfPageUp :call MoveRangeVertically(-v:count1 * HalfPageSize()) +vnoremap MoveBlockDown :call MoveBlockVertically( v:count1) +vnoremap MoveBlockUp :call MoveBlockVertically(-v:count1) +vnoremap MoveBlockHalfPageDown :call MoveBlockVertically( v:count1 * HalfPageSize()) +vnoremap MoveBlockHalfPageUp :call MoveBlockVertically(-v:count1 * HalfPageSize()) vnoremap MoveBlockLeft :call MoveBlockLeft(v:count1) vnoremap MoveBlockRight :call MoveBlockRight(v:count1)