vim/vimrc.functions.vim

332 lines
9.2 KiB
VimL
Raw Normal View History

2018-05-07 16:44:18 +00:00
function! SetColor(name, fg, bg, fg_l, bg_l, style) abort
2018-05-07 22:49:02 +00:00
"{{{
2018-05-07 16:44:18 +00:00
if &background ==? 'dark'
if a:fg ==? 'normal'
let l:guifg = ' guifg=' . g:d_normal_fg
let l:ctermfg = 'ctermfg=black'
" let l:termfg = 'termfg=black'
elseif a:fg ==? 'none'
let l:guifg = ' guifg=' . g:d_normal_bg
let l:ctermfg = ' ctermfg=white'
" let l:termfg = ' termfg=white'
elseif a:fg ==? ''
let l:guifg = ''
let l:ctermfg = ''
" let l:termfg = ''
else
let l:guifg = ' guifg=' . a:fg
let l:ctermfg = ' ctermfg=black'
" let l:termfg = ' termfg=black'
endif
if a:bg ==? 'none'
let l:guibg = ' guibg=NONE'
let l:ctermbg = ' ctermbg=none'
" let l:termbg = ' termbg=none'
elseif a:bg ==? ''
let l:guibg = ''
let l:ctermbg = ''
" let l:termbg = ''
else
let l:guibg = ' guibg=' . a:bg
let l:ctermbg = ' ctermbg=black'
endif
else " light background
if a:fg_l ==? 'normal'
let l:guifg = ' guifg=' . g:l_normal_fg
let l:ctermfg = ' ctermfg=black'
" let l:termfg = ' termfg=black'
elseif a:fg_l ==? 'none'
let l:guifg = ' guifg=' . g:l_normal_bg
let l:ctermfg = ' ctermfg=white'
" let l:termfg = ' termfg=white'
elseif a:fg_l ==? ''
let l:guifg = ''
let l:ctermfg = ''
let l:termfg = ''
else
let l:guifg = ' guifg=' . a:fg_l
let l:ctermfg = ' ctermfg=black'
" let l:termfg = ' termfg=black'
endif
if a:bg_l ==? 'none'
let l:guibg = ' guibg=NONE'
let l:ctermbg = ' ctermbg=none'
" let l:termbg = ' termbg=none'
elseif a:bg_l ==? ''
let l:guibg = ''
let l:ctermbg = ''
" let l:termbg = ''
else
let l:guibg = ' guibg=' . a:bg_l
let l:ctermbg = ' ctermbg=white'
" let l:termbg = ' termbg=white'
endif
2018-05-06 23:32:11 +00:00
endif
2018-05-07 16:44:18 +00:00
if a:style ==? ''
let l:style = ' term=none cterm=none gui=NONE'
2018-05-06 23:32:11 +00:00
else
2018-05-07 16:44:18 +00:00
let l:style = ' term=' . a:style . ' cterm=' . a:style . ' gui=' . a:style
2018-05-06 23:32:11 +00:00
endif
2018-05-07 16:44:18 +00:00
let l:histring = 'hi! ' . a:name . l:guifg . l:guibg . l:style
let l:histring .= l:ctermfg . l:ctermbg
" let l:histring .= l:termfg . l:termbg
2018-05-06 23:32:11 +00:00
2018-05-07 16:44:18 +00:00
execute 'hi clear ' . a:name
2018-05-06 23:32:11 +00:00
execute l:histring
2018-05-07 22:49:02 +00:00
endfunction"}}}
2018-05-06 23:32:11 +00:00
2018-03-17 23:59:01 +00:00
" TODO better name
2018-03-30 22:43:04 +00:00
function! GetStatusFrag(condition, colorname, conditionprefix, text) abort
let l:frag='%#' . a:colorname . '#'"{{{
2018-03-17 23:59:01 +00:00
let l:frag.=a:conditionprefix
let l:frag.='%{(' . a:condition . ")?'" . a:text . "':''}"
return l:frag
2018-03-30 22:43:04 +00:00
endfunction
"}}}
2018-03-17 23:59:01 +00:00
function! MyFoldText()
"{{{
let l:linecount = v:foldend - v:foldstart - 1
let l:line = getline(v:foldstart)
let l:prefix = '|+' . l:linecount . v:folddashes . '| '
let l:subexp = '^ \{,' . len(l:prefix) . '}'
2018-05-08 21:28:12 +00:00
let l:foldtext = substitute(l:line, l:subexp, l:prefix, '')
let l:foldtext = substitute(l:foldtext, '"{\{3}', '', '')
return l:foldtext
endfunction
"}}}
2018-03-30 22:43:04 +00:00
function! CheckLineEnding() abort
2018-05-08 21:28:12 +00:00
"{{{
:normal $
2018-03-17 23:59:01 +00:00
if getline('.')[col('.')-1] == ';' || getline('.')[col('.')-1] == ','
:startinsert
else
:startinsert!
endif
2018-03-30 22:43:04 +00:00
endfunction
"}}}
2018-03-17 23:59:01 +00:00
" get name of syntax item
2018-03-30 22:43:04 +00:00
function! SyntaxItem() abort
"{{{
2018-03-17 23:59:01 +00:00
return synIDattr(synID(line('.'),col('.'),1),'name') . ' -> ' . synIDattr(synIDtrans(synID(line('.'),col('.'),1)), 'name' )
endfunction
2018-04-04 18:55:23 +00:00
nnoremap <space>pp :echom SyntaxItem()<CR>
2018-03-30 22:43:04 +00:00
"}}}
2018-03-17 23:59:01 +00:00
" devdocs DD
" https://gist.github.com/romainl/8d3b73428b4366f75a19be2dad2f0987#file-devdocs-vim
2018-03-30 22:43:04 +00:00
function! s:Get_env() abort
if has('win64') || has('win32') || has('win16')"{{{
2018-03-17 23:59:01 +00:00
return 'WINDOWS'
else
return toupper(substitute(system('uname'), '\n', '', ''))
endif
endfunction
" What command to use on what system
let s:cmds = {'DARWIN': 'open', 'LINUX': 'qutebrowser', 'WINDOWS': 'start'}
" Build the URL stub
let s:stub = s:cmds[<SID>Get_env()] . " 'http://devdocs.io/?q="
command! -nargs=* DD silent! call system(len(split(<q-args>, ' ')) == 0 ?
\ s:stub . &ft . ' ' . expand('<cword>') . "'" : len(split(<q-args>, ' ')) == 1 ?
\ s:stub . &ft . ' ' . <q-args> . "'" : s:stub . <q-args> . "'")
"}}}
" use ranger as file manager
2018-03-30 22:43:04 +00:00
if !exists('*RangerExplorer')
function RangerExplorer() abort"{{{
2018-03-17 23:59:01 +00:00
exec 'silent !ranger --choosefile=/tmp/vim_ranger_current_file ' . expand('%:p:h')
if filereadable('/tmp/vim_ranger_current_file')
exec 'edit ' . system('cat /tmp/vim_ranger_current_file')
call system('rm /tmp/vim_ranger_current_file')
endif
redraw!
endfun
map <space>ra :call RangerExplorer()<CR>
2018-03-30 22:43:04 +00:00
endif
"}}}
2018-03-17 23:59:01 +00:00
2018-03-30 22:43:04 +00:00
function! SetColorColumn() abort
if &buftype == ''"{{{
2018-03-18 16:25:44 +00:00
setlocal colorcolumn=80
endif
endfunction
"}}}
2018-03-30 22:43:04 +00:00
function! GetLinterStatus(key) abort
"{{{
let l:statuscount = 0
if exists('b:ale_linted')
let l:linter = ale#statusline#Count(bufnr(''))
else
let l:linter = GetDiagnosticCountsFromSigns(bufnr(''))
endif
2018-03-17 23:59:01 +00:00
if has_key(l:linter, a:key)
let l:statuscount = l:linter[a:key]
endif
return l:statuscount
2018-03-18 16:25:44 +00:00
endfunction
"}}}
2018-03-29 20:41:18 +00:00
" Shell command
" http://vim.wikia.com/wiki/VimTip1599
function! s:RunShellCommand(cmdline) abort
"{{{
let l:expanded_cmdline = a:cmdline
for l:part in split(a:cmdline, ' ')
if l:part[0] =~ '\v[%#<]'
let l:expanded_part = fnameescape(expand(l:part))
let l:expanded_cmdline = substitute(l:expanded_cmdline, l:part, l:expanded_part, '')
endif
endfor
if g:shell_scratch_buffer_nr > -1
let l:win_nr = bufwinnr(g:shell_scratch_buffer_nr)
if l:win_nr < 0
execute 'bdelete' g:shell_scratch_buffer_nr
top new
let g:shell_scratch_buffer_nr = bufnr('%')
else
execute l:win_nr. ' wincmd w'
setlocal modifiable
%delete _
endif
else
top new
let g:shell_scratch_buffer_nr = bufnr('%')
endif
setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
nnoremap <buffer> q :bdelete<CR>
augroup ResetShellBufferNr
autocmd! * <buffer>
autocmd BufUnload <buffer> let g:shell_scratch_buffer_nr = -1
augroup END
" call setline(1, 'You entered: ' . a:cmdline)
" call setline(2, 'Expanded Form: ' .l:expanded_cmdline)
" call setline(3,substitute(getline(2),'.','=','g'))
execute '$read !'. l:expanded_cmdline
1
setlocal nomodifiable
if !exists('b:shell_line_count')
let b:shell_line_count = line('$')
if b:shell_line_count > 25
let b:shell_line_count = 20
endif
execute 'resize' b:shell_line_count + 1
endif
wincmd p
endfunction
2018-03-30 22:43:04 +00:00
command! -complete=shellcmd -nargs=+ Shell call s:RunShellCommand(<q-args>)
let g:shell_scratch_buffer_nr = -1
2018-03-29 20:41:18 +00:00
"}}}
2018-03-18 16:25:44 +00:00
function! GetDiagnosticCountsFromSigns(buffer) abort
let l:error = 0
let l:warn = 0
let l:info = 0
let l:hint = 0
redir => l:result
silent exec 'sign place buffer=' . a:buffer
redir end
let l:lines = split(l:result, '\n')
for l:line in l:lines
if l:line =~? 'Error'
let l:error += 1
endif
if l:line =~? 'Warning'
let l:warn += 1
endif
if l:line =~? 'Info'
let l:info += 1
endif
if l:line =~? 'Hint'
let l:hint += 1
endif
endfor
return {'error': l:error, 'warning': l:warn, 'info': l:info, 'hint': l:hint}
endfunction
2018-03-30 22:43:04 +00:00
2018-03-30 22:31:10 +00:00
" https://stackoverflow.com/a/40195855
2018-03-30 22:43:04 +00:00
" ex_command: command to run to execute file
function! SaveAndExecute(ex_command) abort
" SOURCE [reusable window]: https://github.com/fatih/vim-go/blob/master/autoload/go/ui.vim{{{
2018-03-30 22:31:10 +00:00
" save and reload current file
silent execute "update | edit"
" get file path of current file
let s:current_buffer_file_path = expand("%")
let s:output_buffer_name = "Python"
let s:output_buffer_filetype = "output"
" reuse existing buffer window if it exists otherwise create a new one
if !exists("s:buf_nr") || !bufexists(s:buf_nr)
silent execute 'top new ' . s:output_buffer_name
let s:buf_nr = bufnr('%')
elseif bufwinnr(s:buf_nr) == -1
silent execute 'top new'
silent execute s:buf_nr . 'buffer'
elseif bufwinnr(s:buf_nr) != bufwinnr('%')
silent execute bufwinnr(s:buf_nr) . 'wincmd w'
endif
2018-03-18 16:25:44 +00:00
2018-03-30 22:31:10 +00:00
silent execute "setlocal filetype=" . s:output_buffer_filetype
setlocal bufhidden=delete
setlocal buftype=nofile
setlocal noswapfile
setlocal nobuflisted
setlocal winfixheight
setlocal cursorline " make it easy to distinguish
setlocal nonumber
setlocal norelativenumber
setlocal showbreak=""
nnoremap <silent> <buffer> q :bdelete<CR>
2018-03-30 22:31:10 +00:00
" clear the buffer
setlocal noreadonly
" setlocal modifiable
%delete _
" add the console output
2018-03-30 22:43:04 +00:00
silent execute '.!'. a:ex_command . ' ' . shellescape(s:current_buffer_file_path, 1)
2018-03-30 22:31:10 +00:00
" resize window to content length
" Note: This is annoying because if you print a lot of lines then your code buffer is forced to a height of one line every time you run this function.
" However without this line the buffer starts off as a default size and if you resize the buffer then it keeps that custom size after repeated runs of this function.
" But if you close the output buffer then it returns to using the default size when its recreated
"execute 'resize' . line('$')
" make the buffer non modifiable
setlocal readonly
" setlocal nomodifiable
endfunction
2018-03-30 22:43:04 +00:00
"}}}
2018-03-18 16:25:44 +00:00
2018-03-17 23:59:01 +00:00
" vim: foldmethod=marker