From da64ea0801afdb20292aca0ca1e41e11e3c64e89 Mon Sep 17 00:00:00 2001 From: Alexander Karle Date: Sun, 9 Dec 2018 12:13:07 -0500 Subject: [PATCH] vim: move functions to autoload/ This improves startup performance by not sourcing the functions until they are called. See :h autoload-functions for more info --- vim/autoload/utils.vim | 23 +++++++++++++++++++++++ vim/autoload/vimrc.vim | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ vim/vimrc | 94 +++++----------------------------------------------------------------------------------------- 3 files changed, 94 insertions(+), 89 deletions(-) create mode 100644 vim/autoload/utils.vim create mode 100644 vim/autoload/vimrc.vim diff --git a/vim/autoload/utils.vim b/vim/autoload/utils.vim new file mode 100644 index 0000000..a3e4122 --- /dev/null +++ b/vim/autoload/utils.vim @@ -0,0 +1,23 @@ +" autoload/utils.vim -- utility functions to support vimrc +" +" See `:h autoload-functions` for explanation of autoloading and performance + +" A function to execute a command and return to the old position +" CREDIT: http://vimcasts.org/episodes/tidying-whitespace/ +function! utils#CMDPreserve(command) abort + " Preparation: save last search, and cursor position. + let _s=@/ + let l = line(".") + let c = col(".") + " Do the business: + execute a:command + " Clean up: restore previous search history, and cursor position + let @/=_s + call cursor(l, c) +endfunction + +function! utils#warn(msg) abort + echohl WarningMsg + echo 'vimrc: ' . a:msg + echohl None +endfunction diff --git a/vim/autoload/vimrc.vim b/vim/autoload/vimrc.vim new file mode 100644 index 0000000..5e69993 --- /dev/null +++ b/vim/autoload/vimrc.vim @@ -0,0 +1,66 @@ +" autoload/vimrc.vim -- functions to be called in vimrc +" +" See `:h autoload-functions` for explanation of autoloading and performance + + +function! vimrc#ToggleColorColumn() abort + if &colorcolumn == "" + set colorcolumn=80 + else + set colorcolumn="" + endif +endfunction + +" Searches all of project for word under cursor +function! vimrc#GitGrep(word) abort + if s:InGitRepo() + let raw = system('git grep -n ' . a:word) + let matches = split(raw, '\n') + let qfitems = [] + for m in matches + let items = split(m, ':') + " Rare case that we match a binary file + if len(items) < 3 + continue + endif + let d = { + \'filename': items[0], + \'lnum': items[1], + \'text': join(items[2:], '') + \} + + let qfitems += [d] + endfor + call setqflist(qfitems, 'r') + else + call utils#warn("called GitGrepWordUnderCursor outside of git repo") + endif +endfunction + +function! vimrc#GitGrepWordUnderCursor() abort + let word = substitute(expand(''), '#', '\\#', 'g') + call vimrc#GitGrep(word) +endfunction + +function! s:InGitRepo() abort + if has('win64') || has('win32') + let fsep = '\' + else + let fsep = '/' + endif + let dir_list = split(getcwd(), fsep) + let running_path = '' + for dir in dir_list + let running_path = join([running_path, dir], fsep) + if isdirectory(running_path . fsep . '.git') + return 1 + endif + endfor + return 0 +endfunction + +function! vimrc#SetPasteAndPasteFromClipboard() abort + set paste + normal! "*p + set nopaste +endfunction diff --git a/vim/vimrc b/vim/vimrc index 7175484..70aaede 100644 --- a/vim/vimrc +++ b/vim/vimrc @@ -117,90 +117,6 @@ if has('autocmd') endif " }}} -" FUNCTIONS {{{ -" A function to execute a command and return to the old position -" CREDIT: http://vimcasts.org/episodes/tidying-whitespace/ -function! CMDPreserve(command) abort - " Preparation: save last search, and cursor position. - let _s=@/ - let l = line(".") - let c = col(".") - " Do the business: - execute a:command - " Clean up: restore previous search history, and cursor position - let @/=_s - call cursor(l, c) -endfunction - -function! ToggleColorColumn() abort - if &colorcolumn == "" - set colorcolumn=80 - else - set colorcolumn="" - endif -endfunction - -" Searches all of project for word under cursor -function! GitGrep(word) abort - if s:InGitRepo() - let raw = system('git grep -n ' . a:word) - let matches = split(raw, '\n') - let qfitems = [] - for m in matches - let items = split(m, ':') - " Rare case that we match a binary file - if len(items) < 3 - continue - endif - let d = { - \'filename': items[0], - \'lnum': items[1], - \'text': join(items[2:], '') - \} - - let qfitems += [d] - endfor - call setqflist(qfitems, 'r') - else - call s:warn("called GitGrepWordUnderCursor outside of git repo") - endif -endfunction - -function! GitGrepWordUnderCursor() abort - let word = substitute(expand(''), '#', '\\#', 'g') - call GitGrep(word) -endfunction - -function! s:InGitRepo() abort - if has('win64') || has('win32') - let fsep = '\' - else - let fsep = '/' - endif - let dir_list = split(getcwd(), fsep) - let running_path = '' - for dir in dir_list - let running_path = join([running_path, dir], fsep) - if isdirectory(running_path . fsep . '.git') - return 1 - endif - endfor - return 0 -endfunction - -function! SetPasteAndPasteFromClipboard() abort - set paste - normal! "*p - set nopaste -endfunction - -function! s:warn(msg) abort - echohl WarningMsg - echo 'vimrc: ' . a:msg - echohl None -endfunction -" }}} - " KEY MAPPINGS {{{ " set leader to be spacebar let mapleader = " " @@ -222,10 +138,10 @@ if has('terminal') || has('nvim') endif " Paste from clipboard without any formatting issues -nnoremap :call SetPasteAndPasteFromClipboard() +nnoremap :call vimrc#SetPasteAndPasteFromClipboard() " Clear trailing whitespace (through regexp) -nnoremap :call CMDPreserve("%s/\\s\\+$//e") +nnoremap :call utils#CMDPreserve("%s/\\s\\+$//e") " edit in current buffer dir nnoremap e :e %:h/ @@ -237,7 +153,7 @@ nnoremap L :set background=light nnoremap D :set background=dark " toggle color column (to ensure short lines) -nnoremap C ToggleColorColumn() +nnoremap C vimrc#ToggleColorColumn() " Quick open current directory of file being edited if isdirectory(s:vimdir . '/bundle/vim-dirvish') @@ -267,7 +183,7 @@ nnoremap :nohlsearch " Get greppin quick! --> search word under cursor (escape # for VimL autoload) nnoremap g :exe "grep " . substitute(expand(''), '#', '\\#', 'g') -nnoremap G :call GitGrepWordUnderCursor():copen +nnoremap G :call vimrc#GitGrepWordUnderCursor():copen " Toggle whitespace nnoremap w :set list! @@ -280,7 +196,7 @@ nnoremap V :edit $MYVIMRC " }}} " COMMANDS {{{ -command! -nargs=1 GitGrep call GitGrep('') | copen +command! -nargs=1 GitGrep call vimrc#GitGrep('') | copen command! CD exe 'cd ' . expand('%:h') " }}} -- libgit2 0.28.4