如何使窗口分割与当前大小一样保持大小一致,而其他大小一样

问题描述 投票:0回答:1

想法是做类似于Golden Ratio(vim插件)的操作。但是,我不想将大小调整为“黄金比例”,而是要为未选择的窗口设置特定的大小。

Ex:

# Window 1 selected
----------------------
|        |   |   |   |
|        |   |   |   |
|   1    | 2 | 3 | 4 |
|        |   |   |   |
|        |   |   |   |
----------------------

# Window 3 selected
----------------------
|   |   |        |   |
|   |   |        |   |
| 1 | 2 |    3   | 4 |
|   |   |        |   |
|   |   |        |   |
----------------------

这是我到目前为止所写的(只是一个在制品):

function g:ResizeWindow()
  let tabs = gettabinfo()
  let current_tabnr = tabpagenr()
  let current_window = win_getid()

  let tab = filter(tabs, 'v:val.tabnr == current_tabnr')[0]

  for window in tab.windows
    if window != current_window
      call win_gotoid(window)
      exe 'vertical resize' 20
    endif
  endfor

  call win_gotoid(current_window)
  let current_window_size = &columns - ((len(tab.windows) - 1) * 20)
  exe 'vertical resize' current_window_size
endfunction

autocmd WinNew,WinEnter * :call g:ResizeWindow()

要测试,您可以打开一个缓冲区,然后仅将其:vsp多次。然后,当您浏览窗口时,它似乎在大多数情况下都可以工作,但有时其中一个窗口会以不一致的方式崩溃。它比其余的要小得多。通常,这是在我从左向右导航...而从右向左导航时发生的。

关于此问题怎么解决的任何想法?

vim neovim
1个回答
0
投票

超级有趣的功能!

这里是工作版本:

function g:ResizeWindow()
  let tabs = gettabinfo()
  let current_tabnr = tabpagenr()
  let current_window = win_getid()

  let tab = filter(tabs, 'v:val.tabnr == current_tabnr')[0]

  let small_size = 5

  for window in tab.windows
    if window == current_window
      let size = &columns - ((len(tab.windows) - 1) * small_size) - (len(tab.windows) - 1)
    else
      let size = small_size
    endif
    noautocmd call win_gotoid(window)
    exe 'noautocmd vertical resize ' . size
  endfor

  call win_gotoid(current_window)
endfunction

set winwidth=1
set winminwidth=1
autocmd WinNew,WinEnter * :call g:ResizeWindow()

说明

我对您的初始WIP进行了很多更改,这是您的代码遇到的主要问题:

  • 该函数被递归调用:函数win_gotoid的调用触发了autocmd。所以这弄乱了所有尺寸

  • 默认最小窗口大小(minwinwidth)和默认窗口大小(winwidth)与您应用的大小搞混了

  • 您最后一次调整了当前窗口的大小,从而将右侧的窗口压缩了

  • 您的主窗口大小计算未考虑窗口分隔符


免责声明

如果一个窗口上有水平分割,此功能将中断!

© www.soinside.com 2019 - 2024. All rights reserved.