aboutsummaryrefslogtreecommitdiff
path: root/autoload
diff options
context:
space:
mode:
Diffstat (limited to 'autoload')
-rw-r--r--autoload/utils.vim45
1 files changed, 45 insertions, 0 deletions
diff --git a/autoload/utils.vim b/autoload/utils.vim
index 0f9ee19..44125ac 100644
--- a/autoload/utils.vim
+++ b/autoload/utils.vim
@@ -85,3 +85,48 @@ function! utils#ToggleCursorCol() abort
echo 'cursorcolumn: ON'
endif
endfunction
+
+function! utils#SwitchLine(src_line_idx, direction) abort
+ if a:direction ==# 'up'
+ if a:src_line_idx == 1
+ return
+ endif
+ move-2
+ elseif a:direction ==# 'down'
+ if a:src_line_idx == line('$')
+ return
+ endif
+ move+1
+ endif
+endfunction
+
+function! utils#MoveSelection(direction) abort
+ " only do this if previous mode is visual line mode. Once we press some keys in
+ " visual line mode, we will leave this mode. So the output of `mode()` will be
+ " `n` instead of `V`. We can use `visualmode()` instead to check the previous
+ " mode, see also https://stackoverflow.com/a/61486601/6064933
+ if visualmode() !=# 'V'
+ return
+ endif
+
+ let l:start_line = line("'<")
+ let l:end_line = line("'>")
+ let l:num_line = l:end_line - l:start_line + 1
+
+ if a:direction ==# 'up'
+ if l:start_line == 1
+ " we can also directly use `normal gv`, see https://stackoverflow.com/q/9724123/6064933
+ normal gv
+ return
+ endif
+ silent execute printf('%s,%smove-2', l:start_line, l:end_line)
+ normal gv
+ elseif a:direction ==# 'down'
+ if l:end_line == line('$')
+ normal gv
+ return
+ endif
+ silent execute printf('%s,%smove+%s', l:start_line, l:end_line, l:num_line)
+ normal gv
+ endif
+endfunction