|
| 1 | +" Copyright (c) 2015 Junegunn Choi |
| 2 | +" |
| 3 | +" MIT License |
| 4 | +" |
| 5 | +" Permission is hereby granted, free of charge, to any person obtaining |
| 6 | +" a copy of this software and associated documentation files (the |
| 7 | +" "Software"), to deal in the Software without restriction, including |
| 8 | +" without limitation the rights to use, copy, modify, merge, publish, |
| 9 | +" distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | +" permit persons to whom the Software is furnished to do so, subject to |
| 11 | +" the following conditions: |
| 12 | +" |
| 13 | +" The above copyright notice and this permission notice shall be |
| 14 | +" included in all copies or substantial portions of the Software. |
| 15 | +" |
| 16 | +" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | +" EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | +" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | +" NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 20 | +" LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 21 | +" OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 22 | +" WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | + |
| 24 | +let s:cpo_save = &cpo |
| 25 | +set cpo&vim |
| 26 | + |
| 27 | +function! s:get_color(group, attr) |
| 28 | + return synIDattr(synIDtrans(hlID(a:group)), a:attr) |
| 29 | +endfunction |
| 30 | + |
| 31 | +function! s:set_color(group, attr, color) |
| 32 | + let gui = has('gui_running') |
| 33 | + execute printf("hi %s %s%s=%s", a:group, gui ? 'gui' : 'cterm', a:attr, a:color) |
| 34 | +endfunction |
| 35 | + |
| 36 | +function! s:blank(repel) |
| 37 | + if bufwinnr(t:goyo_pads.r) <= bufwinnr(t:goyo_pads.l) + 1 |
| 38 | + \ || bufwinnr(t:goyo_pads.b) <= bufwinnr(t:goyo_pads.t) + 3 |
| 39 | + call s:goyo_off() |
| 40 | + endif |
| 41 | + execute 'wincmd' a:repel |
| 42 | +endfunction |
| 43 | + |
| 44 | +function! s:init_pad(command) |
| 45 | + execute a:command |
| 46 | + |
| 47 | + setlocal buftype=nofile bufhidden=wipe nomodifiable nobuflisted noswapfile |
| 48 | + \ nonu nocursorline nocursorcolumn winfixwidth winfixheight statusline=\ |
| 49 | + if exists('&rnu') |
| 50 | + setlocal nornu |
| 51 | + endif |
| 52 | + if exists('&colorcolumn') |
| 53 | + setlocal colorcolumn= |
| 54 | + endif |
| 55 | + let bufnr = winbufnr(0) |
| 56 | + |
| 57 | + execute winnr('#') . 'wincmd w' |
| 58 | + return bufnr |
| 59 | +endfunction |
| 60 | + |
| 61 | +function! s:setup_pad(bufnr, vert, size, repel) |
| 62 | + let win = bufwinnr(a:bufnr) |
| 63 | + execute win . 'wincmd w' |
| 64 | + execute (a:vert ? 'vertical ' : '') . 'resize ' . max([0, a:size]) |
| 65 | + augroup goyop |
| 66 | + execute 'autocmd WinEnter,CursorMoved <buffer> nested call s:blank("'.a:repel.'")' |
| 67 | + autocmd WinLeave <buffer> call s:hide_statusline() |
| 68 | + augroup END |
| 69 | + |
| 70 | + " To hide scrollbars of pad windows in GVim |
| 71 | + let diff = winheight(0) - line('$') - (has('gui_running') ? 2 : 0) |
| 72 | + if diff > 0 |
| 73 | + setlocal modifiable |
| 74 | + call append(0, map(range(1, diff), '""')) |
| 75 | + normal! gg |
| 76 | + setlocal nomodifiable |
| 77 | + endif |
| 78 | + execute winnr('#') . 'wincmd w' |
| 79 | +endfunction |
| 80 | + |
| 81 | +function! s:hmargin() |
| 82 | + let nwidth = max([len(string(line('$'))) + 1, &numberwidth]) |
| 83 | + let width = t:goyo_width + (&number ? nwidth : 0) |
| 84 | + return (&columns - width) |
| 85 | +endfunction |
| 86 | + |
| 87 | +function! s:resize_pads() |
| 88 | + let t:goyo_width = max([2, t:goyo_width]) |
| 89 | + let t:goyo_margin_top = min([max([2, t:goyo_margin_top]), &lines / 2 - 1]) |
| 90 | + let t:goyo_margin_bottom = min([max([2, t:goyo_margin_bottom]), &lines / 2 - 1]) |
| 91 | + |
| 92 | + let hmargin = s:hmargin() |
| 93 | + |
| 94 | + augroup goyop |
| 95 | + autocmd! |
| 96 | + augroup END |
| 97 | + call s:setup_pad(t:goyo_pads.t, 0, t:goyo_margin_top - 1, 'j') |
| 98 | + call s:setup_pad(t:goyo_pads.b, 0, t:goyo_margin_bottom - 2, 'k') |
| 99 | + call s:setup_pad(t:goyo_pads.l, 1, hmargin / 2 - 1, 'l') |
| 100 | + call s:setup_pad(t:goyo_pads.r, 1, hmargin / 2 - 1, 'h') |
| 101 | + |
| 102 | + let t:goyo_width = winwidth(0) |
| 103 | +endfunction |
| 104 | + |
| 105 | +function! s:tranquilize() |
| 106 | + let bg = s:get_color('Normal', 'bg') |
| 107 | + for grp in ['NonText', 'FoldColumn', 'ColorColumn', 'VertSplit', |
| 108 | + \ 'StatusLine', 'StatusLineNC', 'SignColumn'] |
| 109 | + " -1 on Vim / '' on GVim |
| 110 | + if bg == -1 || empty(bg) |
| 111 | + call s:set_color(grp, 'fg', get(g:, 'goyo_bg', 'black')) |
| 112 | + call s:set_color(grp, 'bg', 'NONE') |
| 113 | + else |
| 114 | + call s:set_color(grp, 'fg', bg) |
| 115 | + call s:set_color(grp, 'bg', bg) |
| 116 | + endif |
| 117 | + call s:set_color(grp, '', 'NONE') |
| 118 | + endfor |
| 119 | +endfunction |
| 120 | + |
| 121 | +function! s:hide_statusline() |
| 122 | + let &l:statusline = repeat(' ', winwidth(0)) |
| 123 | +endfunction |
| 124 | + |
| 125 | +function! s:hide_linenr() |
| 126 | + if !get(g:, 'goyo_linenr', 0) |
| 127 | + setlocal nonu |
| 128 | + if exists('&rnu') |
| 129 | + setlocal nornu |
| 130 | + endif |
| 131 | + endif |
| 132 | + if exists('&colorcolumn') |
| 133 | + setlocal colorcolumn= |
| 134 | + endif |
| 135 | +endfunction |
| 136 | + |
| 137 | +function! s:maps_nop() |
| 138 | + let mapped = filter(['R', 'H', 'J', 'K', 'L', '|', '_'], |
| 139 | + \ "empty(maparg(\"\<c-w>\".v:val, 'n'))") |
| 140 | + for c in mapped |
| 141 | + execute 'nnoremap <c-w>'.escape(c, '|').' <nop>' |
| 142 | + endfor |
| 143 | + return mapped |
| 144 | +endfunction |
| 145 | + |
| 146 | +function! s:maps_resize() |
| 147 | + let commands = { |
| 148 | + \ '=': ':<c-u>let [t:goyo_width, t:goyo_margin_top, t:goyo_margin_bottom] = t:goyo_initial_dim <bar> call <sid>resize_pads()<cr>', |
| 149 | + \ '>': ':<c-u>let t:goyo_width = winwidth(0) + 2 * v:count1 <bar> call <sid>resize_pads()<cr>', |
| 150 | + \ '<': ':<c-u>let t:goyo_width = winwidth(0) - 2 * v:count1 <bar> call <sid>resize_pads()<cr>', |
| 151 | + \ '+': ':<c-u>let t:goyo_margin_top -= v:count1 <bar> let t:goyo_margin_bottom -= v:count1 <bar> call <sid>resize_pads()<cr>', |
| 152 | + \ '-': ':<c-u>let t:goyo_margin_top += v:count1 <bar> let t:goyo_margin_bottom += v:count1 <bar> call <sid>resize_pads()<cr>' |
| 153 | + \ } |
| 154 | + let mapped = filter(keys(commands), "empty(maparg(\"\<c-w>\".v:val, 'n'))") |
| 155 | + for c in mapped |
| 156 | + execute 'nnoremap <silent> <c-w>'.c.' '.commands[c] |
| 157 | + endfor |
| 158 | + return mapped |
| 159 | +endfunction |
| 160 | + |
| 161 | +function! s:goyo_on(width) |
| 162 | + let s:orig_tab = tabpagenr() |
| 163 | + |
| 164 | + " New tab |
| 165 | + tab split |
| 166 | + |
| 167 | + let t:goyo_master = winbufnr(0) |
| 168 | + let t:goyo_width = a:width |
| 169 | + let t:goyo_margin_top = get(g:, 'goyo_margin_top', 4) |
| 170 | + let t:goyo_margin_bottom = get(g:, 'goyo_margin_bottom', 4) |
| 171 | + let t:goyo_initial_dim = [t:goyo_width, t:goyo_margin_top, t:goyo_margin_bottom] |
| 172 | + let t:goyo_pads = {} |
| 173 | + let t:goyo_revert = |
| 174 | + \ { 'laststatus': &laststatus, |
| 175 | + \ 'showtabline': &showtabline, |
| 176 | + \ 'fillchars': &fillchars, |
| 177 | + \ 'winminwidth': &winminwidth, |
| 178 | + \ 'winwidth': &winwidth, |
| 179 | + \ 'winminheight': &winminheight, |
| 180 | + \ 'winheight': &winheight, |
| 181 | + \ 'ruler': &ruler, |
| 182 | + \ 'sidescroll': &sidescroll, |
| 183 | + \ 'sidescrolloff': &sidescrolloff |
| 184 | + \ } |
| 185 | + let t:goyo_maps = extend(s:maps_nop(), s:maps_resize()) |
| 186 | + if has('gui_running') |
| 187 | + let t:goyo_revert.guioptions = &guioptions |
| 188 | + endif |
| 189 | + |
| 190 | + " vim-gitgutter |
| 191 | + let t:goyo_disabled_gitgutter = get(g:, 'gitgutter_enabled', 0) |
| 192 | + if t:goyo_disabled_gitgutter |
| 193 | + silent! GitGutterDisable |
| 194 | + endif |
| 195 | + |
| 196 | + " vim-signify |
| 197 | + let t:goyo_disabled_signify = exists('b:sy') && b:sy.active |
| 198 | + if t:goyo_disabled_signify |
| 199 | + SignifyToggle |
| 200 | + endif |
| 201 | + |
| 202 | + " vim-airline |
| 203 | + let t:goyo_disabled_airline = exists("#airline") |
| 204 | + if t:goyo_disabled_airline |
| 205 | + AirlineToggle |
| 206 | + endif |
| 207 | + |
| 208 | + " vim-powerline |
| 209 | + let t:goyo_disabled_powerline = exists("#PowerlineMain") |
| 210 | + if t:goyo_disabled_powerline |
| 211 | + augroup PowerlineMain |
| 212 | + autocmd! |
| 213 | + augroup END |
| 214 | + augroup! PowerlineMain |
| 215 | + endif |
| 216 | + |
| 217 | + " lightline.vim |
| 218 | + let t:goyo_disabled_lightline = exists('#LightLine') |
| 219 | + if t:goyo_disabled_lightline |
| 220 | + silent! call lightline#disable() |
| 221 | + endif |
| 222 | + |
| 223 | + call s:hide_linenr() |
| 224 | + " Global options |
| 225 | + let &winheight = max([&winminheight, 1]) |
| 226 | + set winminheight=1 |
| 227 | + set winheight=1 |
| 228 | + set winminwidth=1 winwidth=1 |
| 229 | + set laststatus=0 |
| 230 | + set showtabline=0 |
| 231 | + set noruler |
| 232 | + set fillchars+=vert:\ |
| 233 | + set fillchars+=stl:. |
| 234 | + set fillchars+=stlnc:\ |
| 235 | + set sidescroll=1 |
| 236 | + set sidescrolloff=0 |
| 237 | + |
| 238 | + " Hide left-hand scrollbars |
| 239 | + if has('gui_running') |
| 240 | + set guioptions-=l |
| 241 | + set guioptions-=L |
| 242 | + endif |
| 243 | + |
| 244 | + let t:goyo_pads.l = s:init_pad('vertical topleft new') |
| 245 | + let t:goyo_pads.r = s:init_pad('vertical botright new') |
| 246 | + let t:goyo_pads.t = s:init_pad('topleft new') |
| 247 | + let t:goyo_pads.b = s:init_pad('botright new') |
| 248 | + |
| 249 | + call s:resize_pads() |
| 250 | + call s:tranquilize() |
| 251 | + |
| 252 | + augroup goyo |
| 253 | + autocmd! |
| 254 | + autocmd TabLeave * call s:goyo_off() |
| 255 | + autocmd VimResized * call s:resize_pads() |
| 256 | + autocmd ColorScheme * call s:tranquilize() |
| 257 | + autocmd BufWinEnter * call s:hide_linenr() | call s:hide_statusline() |
| 258 | + autocmd WinEnter,WinLeave * call s:hide_statusline() |
| 259 | + augroup END |
| 260 | + |
| 261 | + call s:hide_statusline() |
| 262 | + if exists('g:goyo_callbacks[0]') |
| 263 | + call g:goyo_callbacks[0]() |
| 264 | + endif |
| 265 | + silent! doautocmd User GoyoEnter |
| 266 | +endfunction |
| 267 | + |
| 268 | +function! s:goyo_off() |
| 269 | + if !exists('#goyo') |
| 270 | + return |
| 271 | + endif |
| 272 | + |
| 273 | + " Oops, not this tab |
| 274 | + if !exists('t:goyo_revert') |
| 275 | + return |
| 276 | + endif |
| 277 | + |
| 278 | + " Clear auto commands |
| 279 | + augroup goyo |
| 280 | + autocmd! |
| 281 | + augroup END |
| 282 | + augroup! goyo |
| 283 | + augroup goyop |
| 284 | + autocmd! |
| 285 | + augroup END |
| 286 | + augroup! goyop |
| 287 | + |
| 288 | + for c in t:goyo_maps |
| 289 | + execute 'nunmap <c-w>'.escape(c, '|') |
| 290 | + endfor |
| 291 | + |
| 292 | + let goyo_revert = t:goyo_revert |
| 293 | + let goyo_disabled_gitgutter = t:goyo_disabled_gitgutter |
| 294 | + let goyo_disabled_signify = t:goyo_disabled_signify |
| 295 | + let goyo_disabled_airline = t:goyo_disabled_airline |
| 296 | + let goyo_disabled_powerline = t:goyo_disabled_powerline |
| 297 | + let goyo_disabled_lightline = t:goyo_disabled_lightline |
| 298 | + let goyo_orig_buffer = t:goyo_master |
| 299 | + let [line, col] = [line('.'), col('.')] |
| 300 | + |
| 301 | + if tabpagenr() == 1 |
| 302 | + tabnew |
| 303 | + normal! gt |
| 304 | + bd |
| 305 | + endif |
| 306 | + tabclose |
| 307 | + execute 'normal! '.s:orig_tab.'gt' |
| 308 | + if winbufnr(0) == goyo_orig_buffer |
| 309 | + " Doesn't work if window closed with `q` |
| 310 | + execute printf('normal! %dG%d|', line, col) |
| 311 | + endif |
| 312 | + |
| 313 | + let wmw = remove(goyo_revert, 'winminwidth') |
| 314 | + let ww = remove(goyo_revert, 'winwidth') |
| 315 | + let &winwidth = ww |
| 316 | + let &winminwidth = wmw |
| 317 | + let wmh = remove(goyo_revert, 'winminheight') |
| 318 | + let wh = remove(goyo_revert, 'winheight') |
| 319 | + let &winheight = max([wmh, 1]) |
| 320 | + let &winminheight = wmh |
| 321 | + let &winheight = wh |
| 322 | + |
| 323 | + for [k, v] in items(goyo_revert) |
| 324 | + execute printf("let &%s = %s", k, string(v)) |
| 325 | + endfor |
| 326 | + execute 'colo '. get(g:, 'colors_name', 'default') |
| 327 | + |
| 328 | + if goyo_disabled_gitgutter |
| 329 | + silent! GitGutterEnable |
| 330 | + endif |
| 331 | + |
| 332 | + if goyo_disabled_signify |
| 333 | + silent! if !b:sy.active |
| 334 | + SignifyToggle |
| 335 | + endif |
| 336 | + endif |
| 337 | + |
| 338 | + if goyo_disabled_airline && !exists("#airline") |
| 339 | + AirlineToggle |
| 340 | + silent! AirlineRefresh |
| 341 | + endif |
| 342 | + |
| 343 | + if goyo_disabled_powerline && !exists("#PowerlineMain") |
| 344 | + doautocmd PowerlineStartup VimEnter |
| 345 | + silent! PowerlineReloadColorscheme |
| 346 | + endif |
| 347 | + |
| 348 | + if goyo_disabled_lightline |
| 349 | + silent! call lightline#enable() |
| 350 | + endif |
| 351 | + |
| 352 | + if exists('#Powerline') |
| 353 | + doautocmd Powerline ColorScheme |
| 354 | + endif |
| 355 | + |
| 356 | + if exists('g:goyo_callbacks[1]') |
| 357 | + call g:goyo_callbacks[1]() |
| 358 | + endif |
| 359 | + silent! doautocmd User GoyoLeave |
| 360 | +endfunction |
| 361 | + |
| 362 | +function! goyo#execute(bang, ...) |
| 363 | + let width = a:0 > 0 ? a:1 : get(g:, 'goyo_width', 80) |
| 364 | + |
| 365 | + if a:bang |
| 366 | + if exists('#goyo') |
| 367 | + call s:goyo_off() |
| 368 | + endif |
| 369 | + else |
| 370 | + if exists('#goyo') == 0 |
| 371 | + call s:goyo_on(width) |
| 372 | + elseif a:0 > 0 |
| 373 | + let t:goyo_width = width |
| 374 | + call s:resize_pads() |
| 375 | + else |
| 376 | + call s:goyo_off() |
| 377 | + end |
| 378 | + end |
| 379 | +endfunction |
| 380 | + |
| 381 | +let &cpo = s:cpo_save |
| 382 | +unlet s:cpo_save |
| 383 | + |
0 commit comments