From 09f8635dcd7d5a459b7d4b38482b2e1fece607b0 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 4 Nov 2017 22:40:03 +1300 Subject: Simplify shell linting code with single vars Put the entire command line for the determined check and lint into the variable, so it can just be directly executed. --- vim/ftplugin/sh.vim | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/vim/ftplugin/sh.vim b/vim/ftplugin/sh.vim index ae1974a0..d13f34da 100644 --- a/vim/ftplugin/sh.vim +++ b/vim/ftplugin/sh.vim @@ -27,24 +27,24 @@ endif " Map checker based on shell family if exists('b:is_bash') && b:is_bash - let b:check = 'bash -n' + let b:check = 'write !bash -n' elseif exists('b:is_ksh') && b:is_ksh - let b:check = 'ksh -n' + let b:check = 'write !ksh -n' else - let b:check = 'sh -n' + let b:check = 'write !sh -n' endif nnoremap \ c - \ :execute ':write !' . b:check + \ :execute b:check " Map linter based on shell family if exists('b:is_bash') && b:is_bash - let b:lint = 'shellcheck -s bash -' + let b:lint = 'write shellcheck -s bash -' elseif exists('b:is_ksh') && b:is_ksh - let b:lint = 'shellcheck -s ksh -' + let b:lint = 'write !shellcheck -s ksh -' else - let b:lint = 'shellcheck -s sh -' + let b:lint = 'write !shellcheck -s sh -' endif nnoremap \ l - \ :execute ':write !' . b:lint + \ :execute b:lint -- cgit v1.2.3 From 1175a6d4338b43b8521617606f44a3c29a2ec0ff Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 4 Nov 2017 22:41:15 +1300 Subject: Add short-circuit boilerplate to plugins Set a g:loaded_* flag to prevent repeated reloads, and refuse to load at all if &compatible is set or if required features are missing. Some more accommodating plugins avoid the problems 'compatible' causes by saving its value at startup into a script variable, setting the option to the Vim default, and then restoring it when the plugin is done, to prevent any of its flags from interfering in the plugin code: let s:save_cpo = &cpo set cpo&vim ... let &cpo = s:save_cpo unlet s:save_cpo I don't want this boilerplate, so I'm going to do what Tim Pope's modules seem to, and just have the plugin refuse to do a single thing if 'compatible' is set. --- vim/autoload/detect_background.vim | 7 +++ vim/plugin/big_file_options.vim | 97 ++++++++++++++++---------------- vim/plugin/command_typos.vim | 63 +++++++++++---------- vim/plugin/copy_linebreak.vim | 50 ++++++++-------- vim/plugin/fixed_join.vim | 37 ++++++------ vim/plugin/strip_trailing_whitespace.vim | 89 +++++++++++++++-------------- vim/plugin/toggle_option_flag.vim | 84 ++++++++++++++------------- 7 files changed, 228 insertions(+), 199 deletions(-) diff --git a/vim/autoload/detect_background.vim b/vim/autoload/detect_background.vim index 5dd9218c..168640c3 100644 --- a/vim/autoload/detect_background.vim +++ b/vim/autoload/detect_background.vim @@ -6,6 +6,13 @@ " Author: Tom Ryder " License: Same as Vim itself " +if exists('g:loaded_detect_background') + \ || &compatible + finish +endif +let g:loaded_detect_background = 1 + +" Declare autoload function for 'background' set function! detect_background#DetectBackground() " Split up the value of $COLORFGBG (if any) by semicolons diff --git a/vim/plugin/big_file_options.vim b/vim/plugin/big_file_options.vim index fd686fd8..3d239048 100644 --- a/vim/plugin/big_file_options.vim +++ b/vim/plugin/big_file_options.vim @@ -5,58 +5,61 @@ " Author: Tom Ryder " License: Same as Vim itself " -if has('eval') && has('autocmd') +if exists('g:loaded_big_file_options') + \ || !has('autocmd') + \ || &compatible + finish +endif +let g:loaded_big_file_options = 1 + +" Default threshold is 10 MiB +if !exists('g:big_file_size') + let g:big_file_size = 10 * 1024 * 1024 +endif - " Default threshold is 10 MiB - if !exists('g:big_file_size') - let g:big_file_size = 10 * 1024 * 1024 +" Default to leaving syntax highlighting off +if !exists('g:big_file_syntax') + let g:big_file_syntax = 0 +endif + +" Cut 'synmaxcol' down to this or smaller for big files +if !exists('g:big_file_synmaxcol') + let g:big_file_synmaxcol = 256 +endif + +" Declare function for turning off slow options +function! s:BigFileOptions() + + " Don't do anything if the file is under the threshold + if getfsize(expand('')) <= g:big_file_size + return endif - " Default to leaving syntax highlighting off - if !exists('g:big_file_syntax') - let g:big_file_syntax = 0 + " Turn off backups, swap files, and undo files + setlocal nobackup + setlocal nowritebackup + setlocal noswapfile + if has('persistent_undo') + setlocal noundofile endif - " Cut 'synmaxcol' down to this or smaller for big files - if !exists('g:big_file_synmaxcol') - let g:big_file_synmaxcol = 256 + " Limit the number of columns of syntax highlighting + if exists('&synmaxcol') + \ && &synmaxcol > g:big_file_synmaxcol + execute 'setlocal synmaxcol=' . g:big_file_synmaxcol endif - " Declare function for turning off slow options - function! s:BigFileOptions() - - " Don't do anything if the file is under the threshold - if getfsize(expand('')) <= g:big_file_size - return - endif - - " Turn off backups, swap files, and undo files - setlocal nobackup - setlocal nowritebackup - setlocal noswapfile - if has('persistent_undo') - setlocal noundofile - endif - - " Limit the number of columns of syntax highlighting - if exists('&synmaxcol') - \ && &synmaxcol > g:big_file_synmaxcol - execute 'setlocal synmaxcol=' . g:big_file_synmaxcol - endif - - " Disable syntax highlighting if configured to do so - if !g:big_file_syntax - setlocal syntax=OFF - endif - - endfunction - - " Define autocmd for calling to check filesize - augroup big_file_options_bufreadpre - autocmd! - autocmd BufReadPre - \ * - \ call s:BigFileOptions() - augroup end + " Disable syntax highlighting if configured to do so + if !g:big_file_syntax + setlocal syntax=OFF + endif -endif +endfunction + +" Define autocmd for calling to check filesize +augroup big_file_options_bufreadpre + autocmd! + autocmd BufReadPre + \ * + \ call s:BigFileOptions() +augroup end diff --git a/vim/plugin/command_typos.vim b/vim/plugin/command_typos.vim index 6f46b115..16ba654d 100644 --- a/vim/plugin/command_typos.vim +++ b/vim/plugin/command_typos.vim @@ -6,33 +6,38 @@ " Author: Tom Ryder " License: Same as Vim itself " -if has('eval') && has('user_commands') - - command! -bang -complete=file -nargs=? - \ E - \ edit - command! -bang -complete=file -nargs=? - \ W - \ write - command! -bang -complete=file -nargs=? - \ WQ - \ wq - command! -bang -complete=file -nargs=? - \ Wq - \ wq - command! -bang - \ Q - \ quit - command! -bang - \ Qa - \ qall - command! -bang - \ QA - \ qall - command! -bang - \ Wa - \ wall - command! -bang - \ WA - \ wa +if exists('g:loaded_command_typos') + \ || !has('user_commands') + \ || &compatible + finish endif +let g:loaded_command_typos = 1 + +" Define commands +command! -bang -complete=file -nargs=? + \ E + \ edit +command! -bang -complete=file -nargs=? + \ W + \ write +command! -bang -complete=file -nargs=? + \ WQ + \ wq +command! -bang -complete=file -nargs=? + \ Wq + \ wq +command! -bang + \ Q + \ quit +command! -bang + \ Qa + \ qall +command! -bang + \ QA + \ qall +command! -bang + \ Wa + \ wall +command! -bang + \ WA + \ wa diff --git a/vim/plugin/copy_linebreak.vim b/vim/plugin/copy_linebreak.vim index 5c8d5f77..faeb1617 100644 --- a/vim/plugin/copy_linebreak.vim +++ b/vim/plugin/copy_linebreak.vim @@ -6,32 +6,36 @@ " Author: Tom Ryder " License: Same as Vim itself " -if has('eval') +if exists('g:loaded_copy_linebreak') + \ || !has('linebreak') + \ || &compatible + finish +endif +let g:loaded_copy_linebreak = 1 - " Define function - function! s:CopyLinebreak() +" Define function +function! s:CopyLinebreak() - " If linebreak is on, turn it off - if &l:linebreak - setlocal nolinebreak linebreak? - setlocal showbreak= - if exists('&breakindent') - setlocal nobreakindent - endif + " If linebreak is on, turn it off + if &l:linebreak + setlocal nolinebreak linebreak? + setlocal showbreak= + if exists('&breakindent') + setlocal nobreakindent + endif - " If it's off, turn it on - else - setlocal linebreak linebreak? - setlocal showbreak< - if exists('&breakindent') - setlocal breakindent - endif + " If it's off, turn it on + else + setlocal linebreak linebreak? + setlocal showbreak< + if exists('&breakindent') + setlocal breakindent endif + endif - endfunction +endfunction - " Provide mapping proxy to the function just defined - noremap - \ CopyLinebreak - \ :call CopyLinebreak() -endif +" Provide mapping proxy to the function just defined +noremap + \ CopyLinebreak + \ :call CopyLinebreak() diff --git a/vim/plugin/fixed_join.vim b/vim/plugin/fixed_join.vim index 5e3a5c2b..18f563f3 100644 --- a/vim/plugin/fixed_join.vim +++ b/vim/plugin/fixed_join.vim @@ -5,26 +5,29 @@ " Author: Tom Ryder " License: Same as Vim itself " -if has('eval') +if exists('g:loaded_fixed_join') + \ || &compatible + finish +endif +let g:loaded_fixed_join = 1 - " Declare function - function! s:FixedJoin() +" Declare function +function! s:FixedJoin() - " Save current cursor position - let l:lc = line('.') - let l:cc = col('.') + " Save current cursor position + let l:lc = line('.') + let l:cc = col('.') - " Build and execute join command - let l:command = '.,+' . v:count1 . 'join' - execute l:command + " Build and execute join command + let l:command = '.,+' . v:count1 . 'join' + execute l:command - " Restore cursor position - call cursor(l:lc, l:cc) + " Restore cursor position + call cursor(l:lc, l:cc) - endfunction +endfunction - " Create mapping proxy to the function just defined - noremap - \ FixedJoin - \ :call FixedJoin() -endif +" Create mapping proxy to the function just defined +noremap + \ FixedJoin + \ :call FixedJoin() diff --git a/vim/plugin/strip_trailing_whitespace.vim b/vim/plugin/strip_trailing_whitespace.vim index b5079d28..9a9d3d95 100644 --- a/vim/plugin/strip_trailing_whitespace.vim +++ b/vim/plugin/strip_trailing_whitespace.vim @@ -4,62 +4,65 @@ " Author: Tom Ryder " License: Same as Vim itself " -if has('eval') +if exists('g:loaded_strip_trailing_whitespace') + \ || &compatible + finish +endif +let g:loaded_strip_trailing_whitespace = 1 - " Define function for stripping whitespace - function! s:StripTrailingWhitespace() +" Define function for stripping whitespace +function! s:StripTrailingWhitespace() - " Iterating line number - let l:li = 1 + " Iterating line number + let l:li = 1 - " Line number of last line that had non-whitespace characters on it - let l:lw = 0 + " Line number of last line that had non-whitespace characters on it + let l:lw = 0 - " Line number of the file's last line - let l:ll = line('$') + " Line number of the file's last line + let l:ll = line('$') - " Iterate over the lines - while l:li <= l:ll + " Iterate over the lines + while l:li <= l:ll - " Get the line text - let l:line = getline(l:li) + " Get the line text + let l:line = getline(l:li) - " Replace the line with a subsitution of its text stripping extraneous - " whitespace - call setline(l:li, substitute(l:line, '\m\C\s\+$', '', 'g')) + " Replace the line with a subsitution of its text stripping extraneous + " whitespace + call setline(l:li, substitute(l:line, '\m\C\s\+$', '', 'g')) - " If this line has any non-whitespace characters on it, update l:lw with - " its index - if l:line =~# '\m\C\S' - let l:lw = l:li - endif + " If this line has any non-whitespace characters on it, update l:lw with + " its index + if l:line =~# '\m\C\S' + let l:lw = l:li + endif - " Increment the line counter for the next iteration - let l:li = l:li + 1 + " Increment the line counter for the next iteration + let l:li = l:li + 1 - endwhile + endwhile - " If the last non-whitespace line was before the last line proper, we can - " delete all lines after it - if l:lw < l:ll + " If the last non-whitespace line was before the last line proper, we can + " delete all lines after it + if l:lw < l:ll - " Get the current line and column so we can return to it - " (Yes I know about winsaveview() and winrestview(); I want this to work - " even on very old versions of Vim if possible) - let l:lc = line('.') - let l:cc = col('.') + " Get the current line and column so we can return to it + " (Yes I know about winsaveview() and winrestview(); I want this to work + " even on very old versions of Vim if possible) + let l:lc = line('.') + let l:cc = col('.') - " Delete the lines, which will move the cursor - execute l:lw + 1 . ',$ delete' + " Delete the lines, which will move the cursor + execute l:lw + 1 . ',$ delete' - " Return the cursor to the saved position - call cursor(l:lc, l:cc) - endif + " Return the cursor to the saved position + call cursor(l:lc, l:cc) + endif - endfunction +endfunction - " Create mapping proxy to the function just defined - noremap - \ StripTrailingWhitespace - \ :call StripTrailingWhitespace() -endif +" Create mapping proxy to the function just defined +noremap + \ StripTrailingWhitespace + \ :call StripTrailingWhitespace() diff --git a/vim/plugin/toggle_option_flag.vim b/vim/plugin/toggle_option_flag.vim index f83af1cc..ad89d080 100644 --- a/vim/plugin/toggle_option_flag.vim +++ b/vim/plugin/toggle_option_flag.vim @@ -7,44 +7,48 @@ " Author: Tom Ryder " License: Same as Vim itself " -if has('eval') && has('user_commands') - - " Internal function to do the toggling - function! s:Toggle(option, flag, local) - - " Check for weird options, we don't want to eval() anything funny - if a:option =~# '[^a-z]' - echoerr 'Illegal option name' - return - endif - - " Weird flags, too; should be a single inoffensive char - if a:flag !~# '^[a-z0-9.]$' - echoerr 'Illegal flag' - return - endif - - " Choose which set command to use - let l:set = a:local - \ ? 'setlocal' - \ : 'set' - - " eval() to assign -= or += to l:op for the option toggle - " (I couldn't get {curly braces} indirection to work) - let l:op = '' - execute 'let l:op = &' . a:option . ' =~# a:flag ? "-=" : "+="' - - " eval() to perform the option toggle and then print the value - execute l:set . ' ' . a:option . l:op . a:flag - execute l:set . ' ' . a:option . '?' - - endfunction - - " User commands wrapping around calls to the above function - command! -nargs=+ - \ ToggleOptionFlag - \ call Toggle(, 0) - command! -nargs=+ - \ ToggleOptionFlagLocal - \ call Toggle(, 1) +if exists('g:loaded_toggle_option_flag') + \ || !has('user_commands') + \ || &compatible + finish endif +let g:loaded_toggle_option_flag = 1 + +" Internal function to do the toggling +function! s:Toggle(option, flag, local) + + " Check for weird options, we don't want to eval() anything funny + if a:option =~# '[^a-z]' + echoerr 'Illegal option name' + return + endif + + " Weird flags, too; should be a single inoffensive char + if a:flag !~# '^[a-z0-9.]$' + echoerr 'Illegal flag' + return + endif + + " Choose which set command to use + let l:set = a:local + \ ? 'setlocal' + \ : 'set' + + " eval() to assign -= or += to l:op for the option toggle + " (I couldn't get {curly braces} indirection to work) + let l:op = '' + execute 'let l:op = &' . a:option . ' =~# a:flag ? "-=" : "+="' + + " eval() to perform the option toggle and then print the value + execute l:set . ' ' . a:option . l:op . a:flag + execute l:set . ' ' . a:option . '?' + +endfunction + +" User commands wrapping around calls to the above function +command! -nargs=+ + \ ToggleOptionFlag + \ call Toggle(, 0) +command! -nargs=+ + \ ToggleOptionFlagLocal + \ call Toggle(, 1) -- cgit v1.2.3