dwm.vim 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. "==============================================================================
  2. " Copyright: Copyright (C) 2012 Stanislas Polu an other Contributors
  3. " Permission is hereby granted to use and distribute this code,
  4. " with or without modifications, provided that this copyright
  5. " notice is copied with it. Like anything else that's free,
  6. " dwm.vim is provided *as is* and comes with no warranty of
  7. " any kind, either expressed or implied. In no event will the
  8. " copyright holder be liable for any damages resulting from
  9. " the use of this software.
  10. " Name Of File: dwm.vim
  11. " Description: Dynamic Window Manager behaviour for Vim
  12. " Maintainer: Stanislas Polu (polu.stanislas at gmail dot com)
  13. " Last Changed: Tuesday, 23 August 2012
  14. " Version: See g:dwm_version for version number.
  15. " Usage: This file should reside in the plugin directory and be
  16. " automatically sourced.
  17. "
  18. " For more help see supplied documentation.
  19. " History: See supplied documentation.
  20. "==============================================================================
  21. " Exit quickly if already running
  22. if exists("g:dwm_version") || &diff || &cp
  23. finish
  24. endif
  25. let g:dwm_version = "0.1.2"
  26. " Check for Vim version 700 or greater {{{1
  27. if v:version < 700
  28. echo "Sorry, dwm.vim ".g:dwm_version."\nONLY runs with Vim 7.0 and greater."
  29. finish
  30. endif
  31. " All layout transformations assume the layout contains one master pane on the
  32. " left and an arbitrary number of stacked panes on the right
  33. " +--------+--------+
  34. " | | S1 |
  35. " | +--------+
  36. " | M | S3 |
  37. " | +--------+
  38. " | | S3 |
  39. " +--------+--------+
  40. " Move the current master pane to the stack
  41. function! DWM_Stack(clockwise)
  42. 1wincmd w
  43. if a:clockwise
  44. " Move to the top of the stack
  45. wincmd K
  46. else
  47. " Move to the bottom of the stack
  48. wincmd J
  49. endif
  50. " At this point, the layout *should* be the following with the previous master
  51. " at the top.
  52. " +-----------------+
  53. " | M |
  54. " +-----------------+
  55. " | S1 |
  56. " +-----------------+
  57. " | S2 |
  58. " +-----------------+
  59. " | S3 |
  60. " +-----------------+
  61. endfunction
  62. " Add a new buffer
  63. function! DWM_New()
  64. " Move current master pane to the stack
  65. call DWM_Stack(1)
  66. " Create a vertical split
  67. vert topleft new
  68. call DWM_ResizeMasterPaneWidth()
  69. endfunction
  70. " Move the current window to the master pane (the previous master window is
  71. " added to the top of the stack). If current window is master already - switch
  72. " to stack top
  73. function! DWM_Focus()
  74. if winnr('$') == 1
  75. return
  76. endif
  77. if winnr() == 1
  78. wincmd w
  79. endif
  80. let l:curwin = winnr()
  81. call DWM_Stack(1)
  82. exec l:curwin . "wincmd w"
  83. wincmd H
  84. call DWM_ResizeMasterPaneWidth()
  85. endfunction
  86. " Handler for BufWinEnter autocommand
  87. " Recreate layout broken by new window
  88. function! DWM_AutoEnter()
  89. if winnr('$') == 1
  90. return
  91. endif
  92. " Skip buffers without filetype
  93. if !len(&l:filetype)
  94. return
  95. endif
  96. " Skip quickfix buffers
  97. if &l:buftype == 'quickfix'
  98. return
  99. endif
  100. " Move new window to stack top
  101. wincmd K
  102. " Focus new window (twice :)
  103. call DWM_Focus()
  104. call DWM_Focus()
  105. endfunction
  106. " Close the current window
  107. function! DWM_Close()
  108. if winnr() == 1
  109. " Close master panel.
  110. return 'close | wincmd H | call DWM_ResizeMasterPaneWidth()'
  111. else
  112. return 'close'
  113. end
  114. endfunction
  115. function! DWM_ResizeMasterPaneWidth()
  116. " Make all windows equally high and wide
  117. wincmd =
  118. " resize the master pane if user defined it
  119. if exists('g:dwm_master_pane_width')
  120. if type(g:dwm_master_pane_width) == type("")
  121. exec 'vertical resize ' . ((str2nr(g:dwm_master_pane_width)*&columns)/100)
  122. else
  123. exec 'vertical resize ' . g:dwm_master_pane_width
  124. endif
  125. endif
  126. endfunction
  127. function! DWM_GrowMaster()
  128. if winnr() == 1
  129. exec "vertical resize +1"
  130. else
  131. exec "vertical resize -1"
  132. endif
  133. if exists("g:dwm_master_pane_width") && g:dwm_master_pane_width
  134. let g:dwm_master_pane_width += 1
  135. else
  136. let g:dwm_master_pane_width = ((&columns)/2)+1
  137. endif
  138. endfunction
  139. function! DWM_ShrinkMaster()
  140. if winnr() == 1
  141. exec "vertical resize -1"
  142. else
  143. exec "vertical resize +1"
  144. endif
  145. if exists("g:dwm_master_pane_width") && g:dwm_master_pane_width
  146. let g:dwm_master_pane_width -= 1
  147. else
  148. let g:dwm_master_pane_width = ((&columns)/2)-1
  149. endif
  150. endfunction
  151. function! DWM_Rotate(clockwise)
  152. call DWM_Stack(a:clockwise)
  153. if a:clockwise
  154. wincmd W
  155. else
  156. wincmd w
  157. endif
  158. wincmd H
  159. call DWM_ResizeMasterPaneWidth()
  160. endfunction
  161. nnoremap <silent> <Plug>DWMRotateCounterclockwise :call DWM_Rotate(0)<CR>
  162. nnoremap <silent> <Plug>DWMRotateClockwise :call DWM_Rotate(1)<CR>
  163. nnoremap <silent> <Plug>DWMNew :call DWM_New()<CR>
  164. nnoremap <silent> <Plug>DWMClose :exec DWM_Close()<CR>
  165. nnoremap <silent> <Plug>DWMFocus :call DWM_Focus()<CR>
  166. nnoremap <silent> <Plug>DWMGrowMaster :call DWM_GrowMaster()<CR>
  167. nnoremap <silent> <Plug>DWMShrinkMaster :call DWM_ShrinkMaster()<CR>
  168. if !exists('g:dwm_map_keys')
  169. let g:dwm_map_keys = 1
  170. endif
  171. if g:dwm_map_keys
  172. nnoremap <C-J> <C-W>w
  173. nnoremap <C-K> <C-W>W
  174. if !hasmapto('<Plug>DWMRotateCounterclockwise')
  175. nmap <C-,> <Plug>DWMRotateCounterclockwise
  176. endif
  177. if !hasmapto('<Plug>DWMRotateClockwise')
  178. nmap <C-.> <Plug>DWMRotateClockwise
  179. endif
  180. if !hasmapto('<Plug>DWMNew')
  181. nmap <C-N> <Plug>DWMNew
  182. endif
  183. if !hasmapto('<Plug>DWMClose')
  184. nmap <C-C> <Plug>DWMClose
  185. endif
  186. if !hasmapto('<Plug>DWMFocus')
  187. nmap <C-@> <Plug>DWMFocus
  188. nmap <C-Space> <Plug>DWMFocus
  189. endif
  190. if !hasmapto('<Plug>DWMGrowMaster')
  191. nmap <C-L> <Plug>DWMGrowMaster
  192. endif
  193. if !hasmapto('<Plug>DWMShrinkMaster')
  194. nmap <C-H> <Plug>DWMShrinkMaster
  195. endif
  196. endif
  197. if has('autocmd')
  198. augroup dwm
  199. au!
  200. au BufWinEnter * if &l:buflisted || &l:filetype == 'help' | call DWM_AutoEnter() | endif
  201. augroup end
  202. endif