Move functions to autoload script

Rewrite example commands with new autoload functions.
This commit is contained in:
Ingo Karkat
2012-03-02 00:00:00 +01:00
parent cdba0fad67
commit 2a53dbaf15
+11 -44
View File
@@ -1,6 +1,7 @@
" ShowTrailingWhitespace.vim: Detect and delete unwanted whitespace at the end of lines.
"
" DEPENDENCIES:
" - ShowTrailingWhitespace.vim autoload script.
"
" Copyright: (C) 2012 Ingo Karkat
" The VIM LICENSE applies to this script; see ':help copyright'.
@@ -8,6 +9,9 @@
" Maintainer: Ingo Karkat <ingo@karkat.de>
"
" REVISION DATE REMARKS
" 002 26-Feb-2012 Move functions to autoload script.
" Rewrite example commands with new autoload
" functions.
" 001 25-Feb-2012 file creation
" Avoid installing twice or when in unsupported Vim version.
@@ -20,55 +24,18 @@ if ! exists('g:ShowTrailingWhitespace')
let g:ShowTrailingWhitespace = 1
endif
function! s:UpdateMatch( isInsertMode )
let l:pattern = (a:isInsertMode ? '\s\+\%#\@<!$' : '\s\+$')
if exists('w:ShowTrailingWhitespace_Match')
call matchdelete(w:ShowTrailingWhitespace_Match)
call matchadd('ShowTrailingWhitespace', pattern, -1, w:ShowTrailingWhitespace_Match)
else
let w:ShowTrailingWhitespace_Match = matchadd('ShowTrailingWhitespace', pattern)
endif
endfunction
function! s:DeleteMatch()
if exists('w:ShowTrailingWhitespace_Match')
silent! call matchdelete(w:ShowTrailingWhitespace_Match)
unlet w:ShowTrailingWhitespace_Match
endif
endfunction
function! s:DetectAll()
let l:currentWinNr = winnr()
" By entering a window, its height is potentially increased from 0 to 1 (the
" minimum for the current window). To avoid any modification, save the window
" sizes and restore them after visiting all windows.
let l:originalWindowLayout = winrestcmd()
noautocmd windo call s:Detect(0)
execute l:currentWinNr . 'wincmd w'
silent! execute l:originalWindowLayout
endfunction
function! s:Detect( isInsertMode )
if (exists('b:ShowTrailingWhitespace') ? b:ShowTrailingWhitespace : g:ShowTrailingWhitespace)
call s:UpdateMatch(a:isInsertMode)
else
call s:DeleteMatch()
endif
endfunction
augroup ShowTrailingWhitespace
autocmd!
autocmd BufWinEnter,InsertLeave * call <SID>Detect(0)
autocmd InsertEnter * call <SID>Detect(1)
autocmd BufWinEnter,InsertLeave * call ShowTrailingWhitespace#Detect(0)
autocmd InsertEnter * call ShowTrailingWhitespace#Detect(1)
augroup END
highlight def link ShowTrailingWhitespace Error
"command! -bar ShowTrailingWhitespaceOn let g:ShowTrailingWhitespace = 1 | call <SID>DetectAll()
"command! -bar ShowTrailingWhitespaceOff let g:ShowTrailingWhitespace = 0 | call <SID>DetectAll()
"command! -bar ShowTrailingWhitespaceBufferOn let b:ShowTrailingWhitespace = 1 | call <SID>UpdateMatch(0)
"command! -bar ShowTrailingWhitespaceBufferOff let b:ShowTrailingWhitespace = 0 | call <SID>DeleteMatch()
"command! -bar ShowTrailingWhitespaceBufferClear unlet! b:ShowTrailingWhitespace | call <SID>Detect(0)
command! -bar ShowTrailingWhitespaceOn call ShowTrailingWhitespace#Set(1,1)
command! -bar ShowTrailingWhitespaceOff call ShowTrailingWhitespace#Set(0,1)
command! -bar ShowTrailingWhitespaceBufferOn call ShowTrailingWhitespace#Set(1,0)
command! -bar ShowTrailingWhitespaceBufferOff call ShowTrailingWhitespace#Set(0,0)
command! -bar ShowTrailingWhitespaceBufferClear call ShowTrailingWhitespace#Reset()
" vim: set ts=8 sts=4 sw=4 noexpandtab ff=unix fdm=syntax :