Skip to content
This repository has been archived by the owner on Oct 16, 2019. It is now read-only.

Commit

Permalink
Simpler configuration, no default FileTypes, less defaults
Browse files Browse the repository at this point in the history
This is a response to #21. Introduces two new configuration variables (g:AutoClosePairs_add / _del) and simpler customization of g: or b:AutoClosePairs (which now can be set to a string, which is parsed by the plugin when necessary).
  • Loading branch information
Artem Baguinski committed Feb 14, 2012
1 parent 1e5eb35 commit 2b5dd85
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 21 deletions.
56 changes: 42 additions & 14 deletions doc/AutoClose.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,40 @@ combination with |FileType| |autocommand|s.
3.1 Defining characters to auto close~

The default auto-close pair repertoire can be overridden by setting either
g:AutoClosePairs or b:AutoClosePairs.
|g:AutoClosePairs| or |b:AutoClosePairs|, which should contain a string of
space separated pairs of open / close characters. In case of twin pairs (the
same character used as opener and closer) it doesn't have to be repeated:

>
let g:AutoClosePairs = "() {} \""
<

For slight modification of the default repertoire the |g:AutoClosePairs_add|
and |g:AutoClosePairs_del| provide a simplified interface. Note These variables
only take effect if |g:AutoClosePairs| isn't set.

*g:AutoClosePairs_add*

|g:AutoClosePairs_add| has the same format as |g:AutoClosePairs| (see
above). Example:
>
" add <angular brackets> and |pipes|
let g:AutoClosePairs_add = "<> |"
<
*g:AutoClosePairs_del*

|g:AutoClosePairs_del| should contain a string of opener characters to be
removed from default repertoire:
>
" don't close apostrophes
let g:AutoClosePairs_del = "'"
<

3.2 Configuration helper functions~

Note The following functions aren't defined when the user's .vimrc is being
sources, because at that point plugin wasn't loaded yet. They can be used in
autocommands which run after loading the plugins (e.g. FileType)

*AutoClose#ParsePairs()*

Expand All @@ -151,17 +184,18 @@ these variables easier. The function expects a string of space separated
character pairs or single characters. Single characters are treated as twin
pairs (where opening and closing symbols are identical). Examples:
>
" override the global default
let g:AutoClosePairs = AutoClose#ParsePairs("() [] {} <> «» ` \" '")
" override the defaults for a particular FileType
autocommand FileType lisp
\ let b:AutoClosePairs = AutoClose#ParsePairs("\" ()")
<
The default pair repertoire is equivalent to the call:
>
AutoClose#ParsePairs("() [] {} <> «» ` \" '")a
AutoClose#ParsePairs("() [] {} ` \" '")
<
Note Most of the time you don't need to use this function, as variables
|b:AutoClosePairs| and |g:AutoClosePairs| are automatically parsed if they
contain strings instead of dictionaries.

*AutoClose#DefaultPairs()*

A copy of the default pair repertoire can be retrieved using the function
Expand Down Expand Up @@ -189,16 +223,10 @@ the default pairs dictionary.
" auto-close || pair in ruby files
autocmd FileType ruby
\ let b:AutoClosePairs = AutoClose#DefaultPairsModified("|", "")
" don't auto-close <> pair in shell files
autocmd FileType typoscript,zsh,sh
\ let b:AutoClosePairs = AutoClose#DefaultPairsModified("", "<")
<

Note the above two autocommands are actually installed by the plugin.

*ac_protectedregions* *acProtectedRegions*
3.2 Defining protected regions~
3.3 Defining protected regions~

Auto-close behavior is disabled in certain syntax regions: "Comment",
"String" and "Character". This feature is controlled by the setting
Expand All @@ -213,7 +241,7 @@ Both variables should be assigned a list of region names. Example:
<

*ac_turnon* *ac_turnoff* *ac_toggle*
3.3 Turning AutoClose on and off~
3.4 Turning AutoClose on and off~

As AutoClose change the buffer you are editing, sometimes you just want
to turn this feature off for a while and than turn it on again.
Expand All @@ -229,7 +257,7 @@ want to do that task:

*ac_wrapPrefix* *AutoCloseSelectionWrapPrefix*

3.4 Selection wrap prefix~
3.5 Selection wrap prefix~

As described above (see |ac_wrapSelection|), when in visual mode, key
combination <Leader>a followed by a configured open or close symbol causes the
Expand Down
30 changes: 23 additions & 7 deletions plugin/AutoClose.vim
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,19 @@ endfunction

" this function is made visible for the sake of users
function! AutoClose#DefaultPairs()
return AutoClose#ParsePairs("() {} [] <> ` \" '")
return AutoClose#ParsePairs(g:AutoClosePairs)
endfunction

function! AutoClose#DefaultPairsModified(pairsToAdd,openersToRemove)
function! s:ModifyPairsList(list, pairsToAdd, openersToRemove)
return filter(
\ extend(AutoClose#DefaultPairs(), AutoClose#ParsePairs(a:pairsToAdd), "force"),
\ extend(a:list, AutoClose#ParsePairs(a:pairsToAdd), "force"),
\ "stridx(a:openersToRemove,v:key)<0")
endfunction

function! AutoClose#DefaultPairsModified(pairsToAdd,openersToRemove)
return s:ModifyPairsList(AutoClose#DefaultPairs(), a:pairsToAdd, a:openersToRemove)
endfunction

" Define variables (in the buffer namespace).
function! s:DefineVariables()
" All the following variables can be set per buffer or global.
Expand All @@ -350,6 +354,12 @@ function! s:DefineVariables()
let defaults['AutoClosePumvisible'.key] = '<'.key.'>'
endfor

if exists ('b:AutoClosePairs') && type('b:AutoClosePairs') == type("")
let tmp = AutoClose#ParsePairs(b:AutoClosePairs)
unlet b:AutoClosePairs
let b:AutoClosePairs = tmp
endif

" Now handle/assign values
for key in keys(defaults)
if exists('b:'.key) && type(eval('b:'.key)) == type(defaults[key])
Expand Down Expand Up @@ -440,6 +450,16 @@ endfunction
" Configuration
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

let s:AutoClosePairs_FactoryDefaults = AutoClose#ParsePairs("() {} [] ` \" '")
if !exists("g:AutoClosePairs_add") | let g:AutoClosePairs_add = "" | endif
if !exists("g:AutoClosePairs_del") | let g:AutoClosePairs_del = "" | endif
if !exists("g:AutoClosePairs")
let g:AutoClosePairs = s:ModifyPairsList(
\ s:AutoClosePairs_FactoryDefaults,
\ g:AutoCloseDefaultPairs_add,
\ g:AutoCloseDefaultPairs_del )
endif

let s:movementKeys = split('Esc Up Down Left Right Home End PageUp PageDown')
" list of keys that get mapped to themselves for pumvisible()
let s:pumMovementKeys = split('Up Down PageUp PageDown')
Expand All @@ -453,10 +473,6 @@ au!
autocmd BufNewFile,BufRead,BufEnter * if !<SID>IsLoadedOnBuffer() | call <SID>CreateMaps() | endif
autocmd InsertEnter * call <SID>EmptyBuffer()
autocmd BufEnter * if mode() == 'i' | call <SID>EmptyBuffer() | endif
autocmd FileType ruby
\ let b:AutoClosePairs = AutoClose#DefaultPairsModified("|", "")
autocmd FileType typoscript,zsh,sh
\ let b:AutoClosePairs = AutoClose#DefaultPairsModified("", "<")
augroup END

" Define convenient commands
Expand Down

0 comments on commit 2b5dd85

Please sign in to comment.