Skip to content

Commit

Permalink
Use win_move_* functions for resizing windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
dstein64 committed Aug 7, 2022
1 parent 00a31b4 commit ce95c78
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 20 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ below).
## Requirements

* Full functionality
- `vim>=8.1.2269` or `nvim>=0.4.0`
* Limited functionality (no window labels)
- `vim>=8.1.1140`
- `vim>=8.2.4052` or `nvim>=0.7.0`

## Installation

Expand Down
84 changes: 72 additions & 12 deletions autoload/win.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
let s:popupwin = exists('*popup_create') && exists('*popup_close')
let s:floatwin = exists('*nvim_open_win') && exists('*nvim_win_close')

let s:winmove = exists('*win_move_separator') && exists('*win_move_statusline')

let s:code0 = char2nr('0')
let s:code1 = char2nr('1')
let s:code9 = char2nr('9')
Expand All @@ -13,9 +15,18 @@ let s:down_chars = ['j', "\<down>"]
let s:up_chars = ['k', "\<up>"]
let s:right_chars = ['l', "\<right>"]
let s:shift_left_chars = ['H', "\<s-left>"]
let s:ctrl_left_chars = ["\<c-h>", "\<c-left>"]
let s:shift_down_chars = ['J', "\<s-down>"]
let s:ctrl_down_chars = ["\<c-j>", "\<c-down>"]
let s:shift_up_chars = ['K', "\<s-up>"]
let s:ctrl_up_chars = ["\<c-k>", "\<c-up>"]
let s:shift_right_chars = ['L', "\<s-right>"]
let s:ctrl_right_chars = ["\<c-l>", "\<c-right>"]

let s:resize_chars = s:shift_left_chars + s:ctrl_left_chars
\ + s:shift_down_chars + s:ctrl_down_chars
\ + s:shift_up_chars + s:ctrl_up_chars
\ + s:shift_right_chars + s:ctrl_right_chars

" Returns window count, with special handling to exclude floating and external
" windows in neovim. The windows with numbers less than or equal to the value
Expand Down Expand Up @@ -273,14 +284,29 @@ function! s:ShowHelp()
\ ' - Use movement keys to move to neighboring windows.',
\ ' - Enter a window number (where applicable, press `<enter>` to submit).',
\ ' - Use `w` or `W` to sequentially move to the next or previous window.',
\ '* Hold `<shift>` and use movement keys to resize the active window.',
\ ' - Left movements decrease width and right movements increase width.',
\ ' - Down movements decrease height and up movements increase height.',
\ ]
if s:winmove
call extend(l:help_lines, [
\ '* Hold `<shift>` and use movement keys to resize the active window.',
\ ' - Left and right movements shift the right border.',
\ ' - Down and up movements shift the bottom border.',
\ '* Hold `<control>` and use movement keys to resize the active window.',
\ ' - Left and right movements shift the left border.',
\ ' - Down and up movements shift the top border.',
\ ])
else
call extend(l:help_lines, [
\ '* Hold `<shift>` and use movement keys to resize the active window.',
\ ' - Left movements decrease width and right movements increase width.',
\ ' - Down movements decrease height and up movements increase height.',
\ ])
endif
call extend(l:help_lines, [
\ '* Press `s` or `S` followed by a movement key or window number, to swap'
\ . ' buffers.',
\ ' - The active window changes with `s` and is retained with `S`.',
\ '* Press `<esc>` to leave vim-win (or go back, where applicable).',
\ ]
\ ])
let l:echo_list = []
call add(l:echo_list, ['Title', "vim-win help\n"])
let l:help_text = join(l:help_lines, "\n")
Expand Down Expand Up @@ -441,14 +467,48 @@ function! win#Win(...)
wincmd k
elseif s:Contains(s:right_chars, l:char)
wincmd l
elseif s:Contains(s:shift_left_chars, l:char)
execute g:win_resize_width ' wincmd <'
elseif s:Contains(s:shift_right_chars, l:char)
execute g:win_resize_width ' wincmd >'
elseif s:Contains(s:shift_up_chars, l:char)
execute g:win_resize_height ' wincmd +'
elseif s:Contains(s:shift_down_chars, l:char)
execute g:win_resize_height ' wincmd -'
elseif s:Contains(s:resize_chars, l:char)
if s:winmove
if s:Contains(s:shift_left_chars, l:char)
call win_move_separator(0, -g:win_resize_width)
elseif s:Contains(s:ctrl_left_chars, l:char)
if winnr('h') !=# winnr()
call win_move_separator(winnr('h'), -g:win_resize_width)
endif
elseif s:Contains(s:shift_right_chars, l:char)
call win_move_separator(0, g:win_resize_width)
elseif s:Contains(s:ctrl_right_chars, l:char)
if winnr('h') !=# winnr()
call win_move_separator(winnr('h'), g:win_resize_width)
endif
elseif s:Contains(s:shift_up_chars, l:char)
" Even though this operates on the current window, make sure there is
" a window below, to prevent resizing the command line.
if winnr('j') !=# winnr()
call win_move_statusline(0, -g:win_resize_width)
endif
elseif s:Contains(s:ctrl_up_chars, l:char)
if winnr('k') !=# winnr()
call win_move_statusline(winnr('k'), -g:win_resize_width)
endif
elseif s:Contains(s:shift_down_chars, l:char)
call win_move_statusline(0, g:win_resize_width)
elseif s:Contains(s:ctrl_down_chars, l:char)
if winnr('k') !=# winnr()
call win_move_statusline(winnr('k'), g:win_resize_width)
endif
endif
else
if s:Contains(s:shift_left_chars, l:char)
execute g:win_resize_width . ' wincmd <'
elseif s:Contains(s:shift_right_chars, l:char)
execute g:win_resize_width . ' wincmd >'
elseif s:Contains(s:shift_up_chars, l:char)
execute g:win_resize_width . ' wincmd +'
elseif s:Contains(s:shift_down_chars, l:char)
execute g:win_resize_width . ' wincmd -'
endif
endif
endif
catch
call s:Beep()
Expand Down
15 changes: 10 additions & 5 deletions doc/win.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ extensible, allowing additional functionality to be added (see
1. Requirements *win-requirements*

* Full functionality
- `vim>=8.1.2269` or `nvim>=0.4.0`
* Limited functionality (no window labels)
- `vim>=8.1.1140`
- `vim>=8.2.4052` or `nvim>=0.7.0`

============================================================================
2. Installation *win-installation*
Expand All @@ -39,8 +37,15 @@ These can be customized (see |win-configuration| below).
- Enter a window number (where applicable, press <enter> to submit).
- Use `w` or `W` to sequentially move to the next or previous window.
* Hold <shift> and use movement keys to resize the active window.
- Left movements decrease width and right movements increase width.
- Down movements decrease height and up movements increase height.
- Left and right movements shift the right border.
- Down and up movements shift the bottom border.
- For `vim<8.2.4052` and `nvim<0.7.0`:
* Left movements decrease width and right movements increase width.
* Down movements decrease height and up movements increase height.
* Hold <control> and use movement keys to resize the active window.
- Left and right movements shift the left border.
- Down and up movements shift the top border.
- Not available for `vim<8.2.4052` and `nvim<0.7.0`:
* Press `s` or `S` followed by a movement key or window number, to swap buffers.
- The active window changes with `s` and is retained with `S`.
* Press `?` to show a help message.
Expand Down

0 comments on commit ce95c78

Please sign in to comment.