Advertisement
Guest User

Untitled

a guest
Nov 24th, 2019
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 13.40 KB | None | 0 0
  1. let g:WincentColorColumnBufferNameBlacklist = ['__LanguageClient__']
  2. let g:WincentColorColumnFileTypeBlacklist = ['command-t', 'diff', 'fugitiveblame', 'undotree', 'nerdtree', 'qf']
  3. let g:WincentCursorlineBlacklist = ['command-t']
  4. let g:WincentMkviewFiletypeBlacklist = ['diff', 'hgcommit', 'gitcommit']
  5.  
  6. function! wincent#autocmds#attempt_select_last_file() abort
  7.   let l:previous=expand('#:t')
  8.   if l:previous !=# ''
  9.     call search('\v<' . l:previous . '>')
  10.   endif
  11. endfunction
  12.  
  13. function! wincent#autocmds#should_colorcolumn() abort
  14.   if index(g:WincentColorColumnBufferNameBlacklist, bufname(bufnr('%'))) != -1
  15.     return 0
  16.   endif
  17.   return index(g:WincentColorColumnFileTypeBlacklist, &filetype) == -1
  18. endfunction
  19.  
  20. function! wincent#autocmds#should_cursorline() abort
  21.   return index(g:WincentCursorlineBlacklist, &filetype) == -1
  22. endfunction
  23.  
  24. " Loosely based on: http://vim.wikia.com/wiki/Make_views_automatic
  25. function! wincent#autocmds#should_mkview() abort
  26.   return
  27.         \ &buftype ==# '' &&
  28.         \ index(g:WincentMkviewFiletypeBlacklist, &filetype) == -1 &&
  29.         \ !exists('$SUDO_USER') " Don't create root-owned files.
  30. endfunction
  31.  
  32. function! wincent#autocmds#mkview() abort
  33.   try
  34.     if exists('*haslocaldir') && haslocaldir()
  35.       " We never want to save an :lcd command, so hack around it...
  36.       cd -
  37.       mkview
  38.       lcd -
  39.     else
  40.       mkview
  41.     endif
  42.   catch /\<E186\>/
  43.     " No previous directory: probably a `git` operation.
  44.   catch /\<E190\>/
  45.     " Could be name or path length exceeding NAME_MAX or PATH_MAX.
  46.   endtry
  47. endfunction
  48.  
  49. function! s:get_spell_settings() abort
  50.   return {
  51.         \   'spell': &l:spell,
  52.         \   'spellcapcheck': &l:spellcapcheck,
  53.         \   'spellfile': &l:spellfile,
  54.         \   'spelllang': &l:spelllang
  55.         \ }
  56. endfunction
  57.  
  58. function! s:set_spell_settings(settings) abort
  59.   let &l:spell=a:settings.spell
  60.   let &l:spellcapcheck=a:settings.spellcapcheck
  61.   let &l:spellfile=a:settings.spellfile
  62.   let &l:spelllang=a:settings.spelllang
  63. endfunction
  64.  
  65. function! wincent#autocmds#blur_window() abort
  66.   if wincent#autocmds#should_colorcolumn()
  67.     let l:settings=s:get_spell_settings()
  68.     ownsyntax off
  69.     set nolist
  70.     if has('conceal')
  71.       set conceallevel=0
  72.     endif
  73.     call s:set_spell_settings(l:settings)
  74.   endif
  75. endfunction
  76.  
  77. function! wincent#autocmds#focus_window() abort
  78.   if wincent#autocmds#should_colorcolumn()
  79.     if !empty(&ft)
  80.       let l:settings=s:get_spell_settings()
  81.       ownsyntax on
  82.       set list
  83.       let l:conceal_exclusions=get(g:, 'indentLine_fileTypeExclude', [])
  84.       if has('conceal') && index(l:conceal_exclusions, &ft) == -1
  85.         set conceallevel=1
  86.       endif
  87.       call s:set_spell_settings(l:settings)
  88.     endif
  89.   endif
  90. endfunction
  91.  
  92. function! wincent#autocmds#blur_statusline() abort
  93.   " Default blurred statusline (buffer number: filename).
  94.   let l:blurred='%{wincent#statusline#gutterpadding()}'
  95.   let l:blurred.='\ ' " space
  96.   let l:blurred.='\ ' " space
  97.   let l:blurred.='\ ' " space
  98.   let l:blurred.='\ ' " space
  99.   let l:blurred.='%<' " truncation point
  100.   let l:blurred.='%f' " filename
  101.   let l:blurred.='%=' " split left/right halves (makes background cover whole)
  102.   call s:update_statusline(l:blurred, 'blur')
  103. endfunction
  104.  
  105. function! wincent#autocmds#focus_statusline() abort
  106.   " `setlocal statusline=` will revert to global 'statusline' setting.
  107.   call s:update_statusline('', 'focus')
  108. endfunction
  109.  
  110. function! s:update_statusline(default, action) abort
  111.   let l:statusline = s:get_custom_statusline(a:action)
  112.   if type(l:statusline) == type('')
  113.     " Apply custom statusline.
  114.     execute 'setlocal statusline=' . l:statusline
  115.   elseif l:statusline == 0
  116.     " Do nothing.
  117.     "
  118.     " Note that order matters here because of Vimscript's insane coercion rules:
  119.     " when comparing a string to a number, the string gets coerced to 0, which
  120.     " means that all strings `== 0`. So, we must check for string-ness first,
  121.     " above.
  122.     return
  123.   else
  124.     execute 'setlocal statusline=' . a:default
  125.   endif
  126. endfunction
  127.  
  128. function! s:get_custom_statusline(action) abort
  129.   if &ft ==# 'command-t'
  130.     " Will use Command-T-provided buffer name, but need to escape spaces.
  131.     return '\ \ ' . substitute(bufname('%'), ' ', '\\ ', 'g')
  132.   elseif &ft ==# 'diff' && exists('t:diffpanel') && t:diffpanel.bufname ==# bufname('%')
  133.     return 'Undotree\ preview' " Less ugly, and nothing really useful to show.
  134.   elseif &ft ==# 'undotree'
  135.     return 0 " Don't override; undotree does its own thing.
  136.   elseif &ft ==# 'nerdtree'
  137.     return 0 " Don't override; NERDTree does its own thing.
  138.   elseif &ft ==# 'qf'
  139.     if a:action ==# 'blur'
  140.       return
  141.             \ '%{wincent#statusline#gutterpadding()}'
  142.             \ . '\ '
  143.             \ . '\ '
  144.             \ . '\ '
  145.             \ . '\ '
  146.             \ . '%<'
  147.             \ . '%q'
  148.             \ . '\ '
  149.             \ . '%{get(w:,\"quickfix_title\",\"\")}'
  150.             \ . '%='
  151.     else
  152.       return g:WincentQuickfixStatusline
  153.     endif
  154.   endif
  155.  
  156.   return 1 " Use default.
  157. endfunction
  158.  
  159. function! wincent#autocmds#idleboot() abort
  160.   " Make sure we automatically call wincent#autocmds#idleboot() only once.
  161.   augroup WincentIdleboot
  162.     autocmd!
  163.   augroup END
  164.  
  165.   " Make sure we run deferred tasks exactly once.
  166.   doautocmd User WincentDefer
  167.   autocmd! User WincentDefer
  168. endfunction
  169.  
  170. " Directories where we want to perform auto-encryption on save.
  171. let s:encrypted={}
  172. let s:encrypted[expand('~/code/ansible-configs')]='vendor/git-cipher/bin/git-cipher'
  173. let s:encrypted[expand('~/code/wincent')]='vendor/git-cipher/bin/git-cipher'
  174.  
  175. " Update encryptable files after saving.
  176. function! wincent#autocmds#encrypt(file) abort
  177.   let l:base=fnamemodify(a:file, ':h')
  178.   let l:directories=keys(s:encrypted)
  179.   for l:directory in l:directories
  180.     if stridx(a:file, l:directory) == 0
  181.       let l:encrypted=l:base . '/.' . fnamemodify(a:file, ':t') . '.encrypted'
  182.       if filewritable(l:encrypted) == 1
  183.         let l:executable=l:directory . '/' . s:encrypted[l:directory]
  184.         if executable(l:executable)
  185.           call system(
  186.                 \   fnamemodify(l:executable, ':S') .
  187.                 \   ' encrypt ' .
  188.                 \   shellescape(a:file)
  189.                 \ )
  190.         endif
  191.       endif
  192.       break
  193.     endif
  194.   endfor
  195. endfunction
  196.  
  197. " Filetypes that we might want to apply directory-specific overrides to.
  198. let s:wincent_override_filetypes=[
  199.       \   'bnd',
  200.       \   'conf',
  201.       \   'groovy',
  202.       \   'html',
  203.       \   'java',
  204.       \   'javascript',
  205.       \   'jproperties',
  206.       \   'json',
  207.       \   'jsp',
  208.       \   'ignore',
  209.       \   'npmbundler',
  210.       \   'scss',
  211.       \   'soy',
  212.       \   'tsx',
  213.       \   'typescript',
  214.       \   'xml'
  215.       \ ]
  216.  
  217.  
  218. function! wincent#autocmds#apply_overrides(file, type) abort
  219.   let l:pattern=join(s:wincent_override_filetypes, '\|')
  220.   if match(a:type, '\<\(' . l:pattern . '\)\>') != -1
  221.     let l:detected=wincent#liferay#detect(a:file)
  222.     if l:detected
  223.       setlocal noexpandtab
  224.       setlocal shiftwidth=4
  225.       setlocal tabstop=4
  226.       setlocal noshiftround
  227.  
  228.       if match(&formatprg, '^par ') != -1
  229.         " "T", turns tabs to spaces, and I can't seem to turn it off, but I can
  230.         " at least make it use the right number of them...
  231.         let &l:formatprg=substitute(&formatprg, 'T\d*', 'T4', '')
  232.  
  233.         " ... and then override the |gq| operator to do a |:retab!| after
  234.         " applying.
  235.         map <buffer> gq <Plug>(operator-format-and-retab)
  236.         call operator#user#define('format-and-retab', 'wincent#autocmds#format')
  237.       endif
  238.  
  239.       if l:detected == 2
  240.         " Additional settings for main liferay-portal repo, but not for *.js or
  241.         " *.scss.
  242.         if match(a:type, '\<\(javascript\|scss\)\>') == -1
  243.           setlocal noendofline
  244.           setlocal nofixendofline
  245.         endif
  246.       endif
  247.     endif
  248.   endif
  249. endfunction
  250.  
  251. function! wincent#autocmds#format(motion) abort
  252.   if has('ex_extra')
  253.     let l:v=operator#user#visual_command_from_wise_name(a:motion)
  254.     silent execute 'normal!' '`[' . l:v . '`]gq'
  255.     '[,']retab!
  256.   endif
  257. endfunction
  258.  
  259. if has('autocmd')
  260.   function! s:WincentAutocmds()
  261.     augroup WincentAutocmds
  262.       autocmd!
  263.  
  264.       autocmd VimResized * execute "normal! \<c-w>="
  265.  
  266.       " http://vim.wikia.com/wiki/Detect_window_creation_with_WinEnter
  267.       autocmd VimEnter * autocmd WinEnter * let w:created=1
  268.       autocmd VimEnter * let w:created=1
  269.  
  270.       " Disable paste mode on leaving insert mode.
  271.       autocmd InsertLeave * set nopaste
  272.  
  273.       " Make current window more obvious by turning off/adjusting some features in non-current
  274.       " windows.
  275.       if exists('+winhighlight')
  276.         autocmd BufEnter,FocusGained,VimEnter,WinEnter * set winhighlight=
  277.         autocmd FocusLost,WinLeave * set winhighlight=CursorLineNr:LineNr,EndOfBuffer:ColorColumn,IncSearch:ColorColumn,Normal:ColorColumn,NormalNC:ColorColumn,SignColumn:ColorColumn
  278.         if exists('+colorcolumn')
  279.           autocmd BufEnter,FocusGained,VimEnter,WinEnter * if wincent#autocmds#should_colorcolumn() | let &l:colorcolumn='+' . join(range(0, 254), ',+') | endif
  280.         endif
  281.       elseif exists('+colorcolumn')
  282.         autocmd BufEnter,FocusGained,VimEnter,WinEnter * if wincent#autocmds#should_colorcolumn() | let &l:colorcolumn='+' . join(range(0, 254), ',+') | endif
  283.         autocmd FocusLost,WinLeave * if wincent#autocmds#should_colorcolumn() | let &l:colorcolumn=join(range(1, 255), ',') | endif
  284.       endif
  285.       autocmd InsertLeave,VimEnter,WinEnter * if wincent#autocmds#should_cursorline() | setlocal cursorline | endif
  286.       autocmd InsertEnter,WinLeave * if wincent#autocmds#should_cursorline() | setlocal nocursorline | endif
  287.       if has('statusline')
  288.         " TODO: move this into statusline.vim? or move autocmd stuff in statusline.vim
  289.         " here?
  290.         autocmd BufEnter,FocusGained,VimEnter,WinEnter * call wincent#autocmds#focus_statusline()
  291.         autocmd FocusLost,WinLeave * call wincent#autocmds#blur_statusline()
  292.       endif
  293.       autocmd BufEnter,FocusGained,VimEnter,WinEnter * call wincent#autocmds#focus_window()
  294.       autocmd FocusLost,WinLeave * call wincent#autocmds#blur_window()
  295.  
  296.       if has('mksession')
  297.         " Save/restore folds and cursor position.
  298.         autocmd BufWritePost,BufLeave,WinLeave ?* if wincent#autocmds#should_mkview() | call wincent#autocmds#mkview() | endif
  299.         if has('folding')
  300.           autocmd BufWinEnter ?* if wincent#autocmds#should_mkview() | silent! loadview | execute 'silent! ' . line('.') . 'foldopen!' | endif
  301.         else
  302.           autocmd BufWinEnter ?* if wincent#autocmds#should_mkview() | silent! loadview | endif
  303.         endif
  304.       elseif has('folding')
  305.         " Like the autocmd described in `:h last-position-jump` but we add `:foldopen!`.
  306.         autocmd BufWinEnter * if line("'\"") > 1 && line("'\"") <= line('$') | execute "normal! g`\"" | execute 'silent! ' . line("'\"") . 'foldopen!' | endif
  307.       else
  308.         autocmd BufWinEnter * if line("'\"") > 1 && line("'\"") <= line('$') | execute "normal! g`\"" | endif
  309.       endif
  310.  
  311.       autocmd BufWritePost */spell/*.add silent! :mkspell! %
  312.  
  313.       autocmd BufWritePost * call wincent#autocmds#encrypt(expand('<afile>:p'))
  314.  
  315.       " Beware, if no path yet, <aname> will be the filetype (eg. `javascript`).
  316.       " When we later expand it with ":p", that will make it look like a
  317.       " file (eg. "javascript") in the current directory. Fortunately, this
  318.       " serves our purposes adequately.
  319.       autocmd FileType * call wincent#autocmds#apply_overrides(expand('<afile>'), expand('<amatch>'))
  320.     augroup END
  321.   endfunction
  322.  
  323.   call s:WincentAutocmds()
  324.  
  325.   " Wait until idle to run additional "boot" commands.
  326.   augroup WincentIdleboot
  327.     autocmd!
  328.     if has('vim_starting')
  329.       autocmd CursorHold,CursorHoldI * call wincent#autocmds#idleboot()
  330.     endif
  331.   augroup END
  332.  
  333.  
  334.   "
  335.   " Goyo
  336.   "
  337.  
  338.   let s:settings={}
  339.  
  340.   function! s:goyo_enter()
  341.     augroup WincentAutocmds
  342.       autocmd!
  343.     augroup END
  344.     augroup! WincentAutocmds
  345.  
  346.     augroup WincentAutocolor
  347.       autocmd!
  348.     augroup END
  349.     augroup! WincentAutocolor
  350.  
  351.     let s:settings = {
  352.           \   'showbreak': &showbreak,
  353.           \   'statusline': &statusline,
  354.           \   'cursorline': &cursorline,
  355.           \   'showmode': &showmode
  356.           \ }
  357.  
  358.     set showbreak=
  359.     set statusline=\
  360.     set nocursorline
  361.     set noshowmode
  362.  
  363.     highlight! NonText ctermbg=bg ctermfg=bg guibg=bg guifg=bg
  364.  
  365.     if exists('$TMUX')
  366.       silent !tmux set status off
  367.     endif
  368.  
  369.     let b:quitting=0
  370.     let b:quitting_bang=0
  371.     if has('patch-7.3.544')
  372.       autocmd QuitPre <buffer> let b:quitting=1
  373.       cabbrev <buffer> q! let b:quitting_bang = 1 <bar> q!
  374.     endif
  375.   endfunction
  376.  
  377.   function! s:goyo_leave()
  378.     let l:is_last_buffer=len(filter(range(1, bufnr('$')), 'buflisted(v:val)')) == 1
  379.     if b:quitting && l:is_last_buffer
  380.       if b:quitting_bang
  381.         qa!
  382.       else
  383.         qa
  384.       endif
  385.     endif
  386.  
  387.     for [k, v] in items(s:settings)
  388.       execute 'let &' . k . '=' . string(v)
  389.     endfor
  390.  
  391.     highlight clear NonText
  392.     highlight link NonText Conceal
  393.  
  394.     if exists('$TMUX')
  395.       silent !tmux set status on
  396.     endif
  397.  
  398.     call s:WincentAutocmds()
  399.   endfunction
  400.  
  401.   autocmd! User GoyoEnter nested call <SID>goyo_enter()
  402.   autocmd! User GoyoLeave nested call <SID>goyo_leave()
  403. endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement