[nvim] add bangs and aborts to every Vimscript function

This commit is contained in:
Dmytro Meleshko 2021-06-24 17:51:28 +03:00
parent 4d1703c0fe
commit d27aa86af0
6 changed files with 23 additions and 23 deletions

View file

@ -9,7 +9,7 @@ let s:palette = {
\ } \ }
let s:colors = g:dotfiles_colorscheme_base16_colors let s:colors = g:dotfiles_colorscheme_base16_colors
function! s:base16_color(fg, bg) function! s:base16_color(fg, bg) abort
let fg = s:colors[a:fg] let fg = s:colors[a:fg]
let bg = s:colors[a:bg] let bg = s:colors[a:bg]
return [fg.gui, bg.gui, fg.cterm, bg.cterm] return [fg.gui, bg.gui, fg.cterm, bg.cterm]

View file

@ -21,12 +21,12 @@
" }}} " }}}
" The highlighting function {{{ " The highlighting function {{{
function s:is_number(value) function! s:is_number(value) abort
return type(a:value) == v:t_number return type(a:value) == v:t_number
endfunction endfunction
let s:colors = g:dotfiles_colorscheme_base16_colors let s:colors = g:dotfiles_colorscheme_base16_colors
function s:hi(group, fg, bg, attr, sp) function! s:hi(group, fg, bg, attr, sp) abort
let fg = {} let fg = {}
let bg = {} let bg = {}
let attr = 'NONE' let attr = 'NONE'

View file

@ -6,7 +6,7 @@
" }}} " }}}
if !g:vim_ide if !g:vim_ide
function IsCocEnabled() function! IsCocEnabled() abort
return 0 return 0
endfunction endfunction
finish finish
@ -17,7 +17,7 @@ endif
" coc mappings are enabled " coc mappings are enabled
let g:coc_filetypes = [] let g:coc_filetypes = []
function IsCocEnabled() function! IsCocEnabled() abort
return index(g:coc_filetypes, &filetype) >= 0 return index(g:coc_filetypes, &filetype) >= 0
endfunction endfunction
@ -64,7 +64,7 @@ endif
" }}} " }}}
" CocFormat {{{ " CocFormat {{{
function s:CocFormat(range, line1, line2) abort function! s:CocFormat(range, line1, line2) abort
if a:range == 0 if a:range == 0
call CocAction('format') call CocAction('format')
else else

View file

@ -12,7 +12,7 @@ set commentstring=//%s
" Indentination {{{ " Indentination {{{
function SetIndent(expandtab, shiftwidth) function! SetIndent(expandtab, shiftwidth) abort
let &l:expandtab = a:expandtab let &l:expandtab = a:expandtab
let &l:shiftwidth = str2nr(a:shiftwidth) let &l:shiftwidth = str2nr(a:shiftwidth)
let &l:tabstop = &l:shiftwidth let &l:tabstop = &l:shiftwidth
@ -78,7 +78,7 @@ set commentstring=//%s
nmap <silent> <leader>] m'yygccp`'j nmap <silent> <leader>] m'yygccp`'j
nmap <silent> <leader>[ m'yygccP`'k nmap <silent> <leader>[ m'yygccP`'k
function! PutOutput(cmd) function! PutOutput(cmd) abort
let output = execute(a:cmd) let output = execute(a:cmd)
execute "noswapfile pedit" "+" . fnameescape("setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile") fnameescape("preview://" . a:cmd) execute "noswapfile pedit" "+" . fnameescape("setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile") fnameescape("preview://" . a:cmd)
wincmd P wincmd P
@ -179,7 +179,7 @@ set commentstring=//%s
xnoremap ? <Esc>?\%><C-R>=line("'<")-1<CR>l\%<<C-R>=line("'>")+1<CR>l xnoremap ? <Esc>?\%><C-R>=line("'<")-1<CR>l\%<<C-R>=line("'>")+1<CR>l
" * and # in the Visual mode will search the selected text " * and # in the Visual mode will search the selected text
function! s:VisualStarSearch(search_cmd) function! s:VisualStarSearch(search_cmd) abort
let tmp = @" let tmp = @"
normal! y normal! y
let @/ = '\V' . substitute(escape(@", a:search_cmd . '\'), '\n', '\\n', 'g') let @/ = '\V' . substitute(escape(@", a:search_cmd . '\'), '\n', '\\n', 'g')

View file

@ -23,13 +23,13 @@ nnoremap <silent><expr> <CR> empty(&buftype) ? ":write<bar>wall\<CR>" : "\<CR>"
nnoremap <leader>/ :<C-u>grep<space> nnoremap <leader>/ :<C-u>grep<space>
function! s:grep_mapping_star_normal() function! s:grep_mapping_star_normal() abort
let word = expand("<cword>") let word = expand("<cword>")
if !empty(word) if !empty(word)
call feedkeys(":\<C-u>grep " . shellescape('\b' . word . '\b', 1), 'n') call feedkeys(":\<C-u>grep " . shellescape('\b' . word . '\b', 1), 'n')
endif endif
endfunction endfunction
function! s:grep_mapping_star_visual() function! s:grep_mapping_star_visual() abort
let tmp = @" let tmp = @"
normal! y normal! y
call feedkeys(":\<C-u>grep " . shellescape(@", 1), 'n') call feedkeys(":\<C-u>grep " . shellescape(@", 1), 'n')
@ -71,7 +71,7 @@ nnoremap <silent><expr> <CR> empty(&buftype) ? ":write<bar>wall\<CR>" : "\<CR>"
" DiffWithSaved {{{ " DiffWithSaved {{{
" Compare current buffer with the actual (saved) file on disk " Compare current buffer with the actual (saved) file on disk
function s:DiffWithSaved() function! s:DiffWithSaved() abort
let filetype = &filetype let filetype = &filetype
diffthis diffthis
vnew | read # | normal! ggdd vnew | read # | normal! ggdd
@ -84,7 +84,7 @@ nnoremap <silent><expr> <CR> empty(&buftype) ? ":write<bar>wall\<CR>" : "\<CR>"
" Reveal {{{ " Reveal {{{
" Reveal file in the system file explorer " Reveal file in the system file explorer
function s:Reveal(path) function! s:Reveal(path) abort
if has('macunix') if has('macunix')
" only macOS has functionality to really 'reveal' a file, that is, to open " only macOS has functionality to really 'reveal' a file, that is, to open
" its parent directory in Finder and select this file " its parent directory in Finder and select this file
@ -101,7 +101,7 @@ nnoremap <silent><expr> <CR> empty(&buftype) ? ":write<bar>wall\<CR>" : "\<CR>"
" Open {{{ " Open {{{
" opens file or URL with a system program " opens file or URL with a system program
function s:Open(path) function! s:Open(path) abort
" HACK: 2nd parameter of this function is called 'remote', it tells " HACK: 2nd parameter of this function is called 'remote', it tells
" whether to open a remote (1) or local (0) file. However, it doesn't work " whether to open a remote (1) or local (0) file. However, it doesn't work
" as expected in this context, because it uses the 'gf' command if it's " as expected in this context, because it uses the 'gf' command if it's
@ -119,7 +119,7 @@ nnoremap <silent><expr> <CR> empty(&buftype) ? ":write<bar>wall\<CR>" : "\<CR>"
" Yes, I know about the existence of :args, however it modifies the " Yes, I know about the existence of :args, however it modifies the
" argument list, so it doesn't play well with Obsession.vim because it " argument list, so it doesn't play well with Obsession.vim because it
" saves the argument list in the session file. " saves the argument list in the session file.
function s:EditGlob(...) function! s:EditGlob(...) abort
for glob in a:000 for glob in a:000
for name in glob(glob, 0, 1) for name in glob(glob, 0, 1)
execute 'edit' fnameescape(name) execute 'edit' fnameescape(name)
@ -131,7 +131,7 @@ nnoremap <silent><expr> <CR> empty(&buftype) ? ":write<bar>wall\<CR>" : "\<CR>"
" DragOut {{{ " DragOut {{{
" Shows a window for draging (-and-dropping) the currently opened file out. " Shows a window for draging (-and-dropping) the currently opened file out.
function s:DragOut(path) function! s:DragOut(path) abort
if empty(a:path) | return | endif if empty(a:path) | return | endif
if !executable('dragon-drag-and-drop') if !executable('dragon-drag-and-drop')
echoerr "Please install <https://github.com/mwh/dragon> for the DragOut command to work." echoerr "Please install <https://github.com/mwh/dragon> for the DragOut command to work."
@ -148,13 +148,13 @@ nnoremap <silent><expr> <CR> empty(&buftype) ? ":write<bar>wall\<CR>" : "\<CR>"
" on save (BufWritePre) {{{ " on save (BufWritePre) {{{
function s:IsUrl(str) function! s:IsUrl(str) abort
return a:str =~# '\v^\w+://' return a:str =~# '\v^\w+://'
endfunction endfunction
" create directory {{{ " create directory {{{
" Creates the parent directory of the file if it doesn't exist " Creates the parent directory of the file if it doesn't exist
function s:CreateDirOnSave() function! s:CreateDirOnSave() abort
let file = expand('<afile>') let file = expand('<afile>')
" check if this is a regular file and its path is not a URL " check if this is a regular file and its path is not a URL
if empty(&buftype) && !s:IsUrl(file) if empty(&buftype) && !s:IsUrl(file)
@ -165,7 +165,7 @@ nnoremap <silent><expr> <CR> empty(&buftype) ? ":write<bar>wall\<CR>" : "\<CR>"
" }}} " }}}
" fix whitespace {{{ " fix whitespace {{{
function s:FixWhitespaceOnSave() function! s:FixWhitespaceOnSave() abort
let pos = getpos('.') let pos = getpos('.')
" remove trailing whitespace " remove trailing whitespace
keeppatterns %s/\s\+$//e keeppatterns %s/\s\+$//e
@ -177,7 +177,7 @@ nnoremap <silent><expr> <CR> empty(&buftype) ? ":write<bar>wall\<CR>" : "\<CR>"
" auto-format with Coc.nvim {{{ " auto-format with Coc.nvim {{{
let g:coc_format_on_save_ignore = [] let g:coc_format_on_save_ignore = []
function s:FormatOnSave() function! s:FormatOnSave() abort
let file = expand('<afile>') let file = expand('<afile>')
if IsCocEnabled() && !s:IsUrl(file) && index(g:coc_format_on_save_ignore, &filetype) < 0 if IsCocEnabled() && !s:IsUrl(file) && index(g:coc_format_on_save_ignore, &filetype) < 0
silent CocFormat silent CocFormat
@ -185,7 +185,7 @@ nnoremap <silent><expr> <CR> empty(&buftype) ? ":write<bar>wall\<CR>" : "\<CR>"
endfunction endfunction
" }}} " }}}
function s:OnSave() function! s:OnSave() abort
call s:FixWhitespaceOnSave() call s:FixWhitespaceOnSave()
call s:FormatOnSave() call s:FormatOnSave()
call s:CreateDirOnSave() call s:CreateDirOnSave()

View file

@ -40,7 +40,7 @@ endif
set confirm set confirm
" Bbye with confirmation, or fancy buffer closer {{{ " Bbye with confirmation, or fancy buffer closer {{{
function s:CloseBuffer(cmd) abort function! s:CloseBuffer(cmd) abort
let cmd = a:cmd let cmd = a:cmd
if &modified if &modified
" <https://github.com/neovim/neovim/blob/a282a177d3320db25fa8f854cbcdbe0bc6abde7f/src/nvim/ex_cmds2.c#L1400> " <https://github.com/neovim/neovim/blob/a282a177d3320db25fa8f854cbcdbe0bc6abde7f/src/nvim/ex_cmds2.c#L1400>
@ -141,7 +141,7 @@ endif
" Based on <https://stackoverflow.com/a/1330556/12005228>, inspired by " Based on <https://stackoverflow.com/a/1330556/12005228>, inspired by
" <https://gist.github.com/romainl/f7e2e506dc4d7827004e4994f1be2df6>. " <https://gist.github.com/romainl/f7e2e506dc4d7827004e4994f1be2df6>.
" But apparently `vimgrep /pattern/ %` can be used instead? " But apparently `vimgrep /pattern/ %` can be used instead?
function! s:CmdGlobal(pattern, bang) function! s:CmdGlobal(pattern, bang) abort
let pattern = substitute(a:pattern, "/.*$", "", "") let pattern = substitute(a:pattern, "/.*$", "", "")
let matches = [] let matches = []
execute "g" . (a:bang ? "!" : "") . "/" . pattern . "/call add(matches, expand(\"%\").\":\".line(\".\").\":\".col(\".\").\":\".getline(\".\"))" execute "g" . (a:bang ? "!" : "") . "/" . pattern . "/call add(matches, expand(\"%\").\":\".line(\".\").\":\".col(\".\").\":\".getline(\".\"))"