diff --git a/doc/AutoClose.txt b/doc/AutoClose.txt new file mode 100644 index 0000000..3903fc5 --- /dev/null +++ b/doc/AutoClose.txt @@ -0,0 +1,290 @@ +*AutoClose.txt* Documentation for script AutoClose.vim + + Script : AutoClose.vim + Author : Thiago Alves + Email : + Changed: 25 October 2011 + +============================================================================== + *ac_intro* +1. Overview~ + + This plugin was born as a personal need to get some little special +features from other editors into my preferred one. The Eclipse IDE editor has +an auto-complete for open-close pair of characters feature that I always +wanted into Vim. + + As soon as you type a character that could have a matching (closing) +counterpart, Eclipse automatically puts its counterpart in front of your +cursor, and it is smart enough to ignore the closing character afterwards, if +typed, but just moves your cursor one character forward. + + But the Eclipse editor does not stop there. If you type an opening +character on any part of your code that should not have a pair completion, it +will not insert the closing character for you. + + The AutoClose plugin is an attempt to reproduce this behavior for Vim. + + The following is covered in this document: + + 1. Overview |ac_intro| + 2. Features |ac_features| + 3. Configuring |ac_config| + 3.1 Defining characters to auto close |ac_charstoclose| + 3.2 Defining protected regions |ac_protectedregions| + 3.3 Turning AutoClose on and off |ac_turnon| + 3.4 Selection wrap prefix |ac_wrapPrefix| + 4. Under the hood |ac_details| + 4.1 Mappings |ac_mappings| + +============================================================================== + *ac_operation* +2. Features~ + + Plugin provides the following features: + + 1. Auto-closing: when opening symbol is typed (e.g. an opening +parenthesis) corresponding closing simbol is inserted after the cursor. + + You type: + + sin(123*(Pi*2 + + Result: + + sin(123*(Pi*2)) + + 2. Typing over the closing characters: when cursor is immediatelly before +a closing character and a closing character is typed, no new character is +inserted. This is often handier than moving past closing symbols with cursor +keys. + + You type: + + sin(Pi*2.0+theta)*A + + Result: + + sin(Pi*2.0+theta)*A + + This might seem pointless, but in fact gives more freedom to the user. +While auto-closing is a big help when typing complex expressions (or simpler +expressions in a lisp dialect), it shouldn't get in the way of typing a simple +one. + + 3. Erasing empty pairs at once: when is typed inside an empty +pair, the closing symbol is erased together with the opening one. + + Situation before pressing : + + print(|) + + Here bar symbol represents cursor position. After pressing : + + print + + On the other hand, in the middle of an empty pair only erases a +closing symbol and provides a way to cancel the effect of auto-closing when +undesired. Thus, if in the same situation you'd pressed , the result +would be: + + print( + + *ac_wrapSelection* + + 4. Wrapping a selection in a pair: in visual mode extended mappings are +provided: a( etc for all opening characters, which wrap a selection in +a corresponding opening/closing pair. + + Situation: + + print "Some text" + + Position cursor in normal mode (shown as underscore below) on the space: + + print_"Some text" + + Assuming your symbol is a backslash (see ||): + + You type: + + v$\a( + + Result: + + print("Some text") + + Explanation: + + removes the space which isn't necessary after wrapping + v starts visual mode + $ extends selection until the end of line + \a( causes selection to be wrapped in a pair of parentheses + + Note the a preifx is configurable (down to omitting altogether), +see |ac_wrapPrefix|. + +============================================================================== + *ac_config* +3. Configuring~ + + It is possible to personalize the AutoClose plugin in two ways: + + 1. Setting which character will have which pair to close + 2. Defining regions where the auto close will not happen + + Both settings are made by defining a special variable for each case. +Setting can be overridden either globally or on a per-buffer basis, by setting +g: or b: version of a variable. The latter is especially useful in a +combination with |FileType| |autocommand|s. + + *ac_charstoclose* *AutoClosePairs* +3.1 Definning characters to auto close~ + + The default auto-close pair repertoire can be overridden by setting either +g:AutoClosePairs or b:AutoClosePairs. + + *AutoClose#ParsePairs()* + + A utility function |AutoClose#ParsePairs()| is provided to make setting +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#DefaultPairs()* + + A copy of the default pair repertoir can be retreived using the funcion +|AutoClose#DefaultPairs()|. The function returns a dictionary, mapping opening +symbols to corresponding closing symbols. + + *AutoClose#DefaultPairsModified()* + + Often you would like to only slightly modify the default (add or remove +pairs), for this case utility function |AutoClose#DefaultPairsModified()| is +provided, with the following prototype: +> + AutoClose#DefaultPairsModified(pairsToAdd, openersToRemove) +< + The function returns a pair repertoire with pairs from the 'pairsToAdd' +argument inserted and pairs corresponding to opener symbols from the +'openersToRemove' argument removed. The actual default repertoire isn't +modified. The format of the 'pairsToAdd' argument is the same as expected by +the |AutoClose#ParsePairs()| described above, the 'openersToRemove' is a +simple string of characters treated as open symbols to remove from a copy of +the default pairs dictionary. + + Examples: +> + " 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 Definning protected regions~ + + Auto-close behavior is disabled in certain syntax regions: "Comment", +"String" and "Character". This feature is controlled by the setting +AutoCloseProtectedRegions, which can be overridden globally (via +g:AutoCloseProtectedRegions) or per buffer (via b:AutoCloseProtectedRegions). +Both variables should be assigned a list of region names. Example: + +> + let g:AutoCloseProtectedRegions = ["Comment", "String", "Character"] + autocmd FileType exoticLanguage + \ let b:AutoCloseProtectedRegions = ["Dialog", "ThoughBubble"] +< + + *ac_turnon* *ac_turnoff* *ac_toggle* +3.3 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. + + AutoClose provide three commands that can be mapped to any key as you +want to do that task: + + 1. AutoCloseOn + 2. AutoCloseOff + 3. AutoCloseToggle + + And if you don't want to map anything, just use them on the command mode. + + *ac_wrapPrefix* *AutoCloseSelectionWrapPrefix* + +3.4 Selection wrap prefix~ + + As described above (see |ac_wrapSelection|), when in visual mode, key +combination a followed by a configured open or close symbol causes the +plugin to wrap visual selection in a corresponding symbol pair. The a +portion of the mapping can be overridden by setting g: or +b:|AutoCloseSelectionWrapPrefix| variable. Some simple souls, such as the +author of this variable, even set it to an empty string, so that to wrap +selection it is enough to just type an opening or closing symbol when in +visual mode. Examples: +> + " oh, blasphemy! + let g:AutoCloseSelectionWrapPrefix="" + + " hide the sacrilege from the gods of vim: + autocmd FileType vim let b:AutoCloseSelectionWrapPrefix="a" +< +============================================================================== + *ac_details* +4. Under the hood~ + + This section will describe what is involved on AutoClose plugin to make +it work. The intent of this section is to let users know what this plugin is +doing in case of any strange behavior on your system. + + Before version 1.1, AutoClose used to use mappings and autocommands, but +now only mapping of the pairs and the backspace is used. This change simplify +drastically the whole work of the plugin. + + *ac_mappings* +4.1 Mappings~ + + Every character that is defined to have a closing counterpart is mapped to +one of three possible functions: + + 1. InsertPair + 2. ClosePair + 3. OpenOrCloseTwinPair + + The first two functions are meant to characters that it pair is different +from it self like ( and ). Their behavior depending on which character is +typed, open or close one. If the open character is typed than the +InsertPair function check if there is a possibility to automatically insert +the close pair (e.g. the cursor is not inside some area that is not allow to +have auto completion) and if a close character is typed than ClosePair +just check if the next character is the close one and if it can be skipped. + + OpenOrCloseTwinPair is a little bit trickier. It is used to characters +that has itself as the closing pair (e.g the " character). Its behavior will +be the sum of InsertPair and ClosePair functions plus the verification of the +existance of an already typed char. + + +============================================================================== + + If you have questions or suggestions, please send me an email. + +------------------------------------------------------------------------------ + vim:tw=78:fo=tcq2:ai:isk=!-~,^*,^\|,^\":ts=8:ft=help:norl: diff --git a/doc/autoclose.txt b/doc/autoclose.txt deleted file mode 100644 index 2993f33..0000000 --- a/doc/autoclose.txt +++ /dev/null @@ -1,188 +0,0 @@ -*AutoClose.txt* Documentation for script AutoClose.vim -> - Script : AutoClose.vim - Author : Thiago Alves - Email : - Changed: 08/11/2008 - -============================================================================== - *ac_intro* -1. Overview~ - - This plugin was born as a personal need to get some little special -features from other editors into my preferred one. The Eclipse IDE editor has -an auto-complete for open-close pair of characters feature that I always -wanted into Vim. - - As soon as you type a character that could have a matching (closing) -counterpart, Eclipse automatically puts its counterpart in front of your -cursor, and it is smart enough to ignore the closing character afterwards, if -typed, but just moves your cursor one character forward. - - But the Eclipse editor does not stop there. If you type an opening -character on any part of your code that should not have a pair completion, it -will not insert the closing character for you. - - The AutoClose plugin is an attempt to reproduce this behavior for Vim. - - The following is covered in this document: - - 1. Overview |ac_intro| - 2. Under the hood |ac_details| - 2.1 Mappings |ac_mappings| - 3. Configuring |ac_config| - 3.1 Defining characters to auto close |ac_charstoclose| - 3.2 Defining protected regions |ac_protectedregions| - 3.3 Turning AutoComplete on and off |ac_turnon| - 4. Known problems |ac_problems| - -============================================================================== - *ac_details* -2. Under the hood~ - - This section will describe what is involved on AutoComplete plugin to make -it work. The intent of this section is to let users know what this plugin is -doing in case of any strange behavior on your system. - - Before version 1.1, AutoClose used to use mappings and autocommands, but -now only mapping of the pairs and the backspace is used. This change simplify -drastically the whole work of the plugin. - - *ac_mappings* -2.1 Mappings~ - - Every character that is defined to have a closing counterpart is mapped to -one of three possible functions: - - 1. OpenChar - 2. CloseChar - 3. CheckChar - - The first two functions are meant to characters that it pair is different -from it self like ( and ). Their behavior depending on which character is -typed, open or close one. If the open character is typed than the OpenChar -function check if there is a possibility to automatically insert the close -pair (e.g. the cursor is not inside some area that is not allow to have auto -completion) and if a close character is typed than CloseChar just check -if the next character is the close one and if it can be skipped. - - CheckChar is a little bit trickier. It is used to characters that has -itself as the closing pair (e.g the " character). Its behavior will be the sum -of OpenChar and CloseChar functions plus the verification of the existance of -an already typed char. - - -============================================================================== - *ac_config* -3. Configuring~ - - It is possible to personalize the AutoClose plugin in two ways: - - 1. Setting which character will have which pair to close - 2. Defining regions where the auto close will not happen - - Both settings are made by defining a special variable for each case. - - *ac_charstoclose* *AutoClosePairs* -3.1 Definning characters to auto close~ - - If you are not sattisfied with the default characters to close you can -define your own by setting the global variable g:AutoClosePairs. - - The only thing to be careful is that this variable must be a dictionary on -the following format: -> - {"": ""} -< - Otherwise the plugin will not recognize the settings and will use the -default values. - - Following is the default declaration for auto close chars. If this -configuration is enought, you don't need to configure nothing: -> - let g:AutoClosePairs = {'(': ')', '{': '}', '[': ']', '"': '"', "'": "'", - \ '<': '>', '`': '`', '«': '»'} -< - - *ac_protectedregions* *acProtectedRegions* -3.2 Definning protected regions~ - - Inside some special regions the auto close feature is far from util, -actually we can say that on those regions, this feature is worst than no -feature at all. This is the case for "Comments" and "Strings". - - If you edit files with this characteristic and there is more than comments -and strings where you would like to protect from auto close, just define the -global variable g:AutoCloseProtectedRegions. - - This variable must be a list of strings or the plugin will not recognize -it and will use the defaults parameters. The format of this variable should -be: -> - ["", "", ...] -< - Following is the default declaration to the protected regions. If you are -satisfied with this, you don't have to set this setting: -> - let g:AutoCloseProtectedRegions. = ["Comment", "String", "Character"] -< - - *ac_turnon* *ac_turnoff* *ac_toggle* -3.3 Turning AutoComplete on and off~ - - As AutoComplete change the buffer you are editing, sometimes you just want -to turn this feature off for a while and than turn it on again. - - AutoComplete provide three commands that can be mapped to any key as you -want to do that task: - - 1. AutoCloseOn - 2. AutoCloseOff - 3. AutoCloseToggle - - And if you don't want to map anything, just use them on the command mode. - -============================================================================== - *ac_problems* -4. Known problems~ - - The reinsertion of the text with the "." character in normal mode is not -working properly. Now, after an auto completion, if you go to normal mode and -try to reinsert your previously entered text, only what was inserted between -the auto completed characters is reinserted. See an example (note that a | is -used to represent cursor position here): - - 1. Go to insert mode and type: (123 -> - 1 (123|) - 2 - 3 - 4 - ~ -< - 2. Go to normal mode - 3. Move the cursor to somewhere else -> - 1 (123) - 2 - 3 | - 4 - ~ -< - 4. Press "." key and you'll have: -> - 1 (123) - 2 - 3 123 - 4 - ~ -< - I'm still not sure if there is a solution to this problem because this is -a normal behavior of Vim that usually caches what you are entering in insert -mode until a movement key (\, \, \, \ and possibly some -others) is pressed and you start typing again. - - If you have questions or suggestions, please send me an email. - ------------------------------------------------------------------------------- - vim:tw=78:fo=tcq2:ai:isk=!-~,^*,^\|,^\":ts=8:ft=help:norl: diff --git a/plugin/autoclose.vim b/plugin/AutoClose.vim similarity index 64% rename from plugin/autoclose.vim rename to plugin/AutoClose.vim index 6b31825..87d0aae 100644 --- a/plugin/autoclose.vim +++ b/plugin/AutoClose.vim @@ -48,6 +48,8 @@ function! s:GetPrevChar() return s:GetCharBehind(1) endfunction +" used to implement automatic delection of closing character when opening +" counterpart is deleted function! s:IsEmptyPair() let l:prev = s:GetPrevChar() let l:next = s:GetNextChar() @@ -109,7 +111,10 @@ function! s:CountQuotes(char) let l:result = 0 if l:currPos >= 0 - for q in b:AutoCloseQuotes + for [q,closer] in items(b:AutoClosePairs) + " only consider twin pairs + if q != closer | continue | endif + if b:AutoCloseSmartQuote != 0 let l:regex = q . '[ˆ\\' . q . ']*(\\.[ˆ\\' . q . ']*)*' . q else @@ -133,6 +138,13 @@ function! s:CountQuotes(char) return l:result endfunction +" The auto-close buffer is used in a fix of the redo functionality. +" As we insert characters after cursor, we remember them and at the moment +" that vim would normally collect the last entered string into dot register +" (:help ".) - i.e. when esc or a motion key is typed in insert mode - we +" erase the inserted symbols and pretend that we have just now typed them. +" This way vim picks them up into dot register as well and user can repeat the +" typed bit with . command. function! s:PushBuffer(char) if !exists("b:AutoCloseBuffer") let b:AutoCloseBuffer = [] @@ -159,13 +171,13 @@ function! s:FlushBuffer() if l:len > 0 let l:result = join(b:AutoCloseBuffer, '') . repeat("\", l:len) let b:AutoCloseBuffer = [] - call s:EraseCharsOnLine(l:len) + call s:EraseNCharsAtCursor(l:len) endif endif return l:result endfunction -function! s:InsertCharsOnLine(str) +function! s:InsertStringAtCursor(str) let l:line = getline('.') let l:column = col('.')-2 @@ -176,7 +188,7 @@ function! s:InsertCharsOnLine(str) endif endfunction -function! s:EraseCharsOnLine(len) +function! s:EraseNCharsAtCursor(len) let l:line = getline('.') let l:column = col('.')-2 @@ -187,53 +199,54 @@ function! s:EraseCharsOnLine(len) endif endfunction -function! s:InsertPair(char) - if ! b:AutoCloseOn || ! has_key(b:AutoClosePairs, a:char) || s:IsForbidden(a:char) - return a:char +" returns the opener, after having inserted its closer if necessary +function! s:InsertPair(opener) + if ! b:AutoCloseOn || ! has_key(b:AutoClosePairs, a:opener) || s:IsForbidden(a:opener) + return a:opener endif let l:save_ve = &ve set ve=all let l:next = s:GetNextChar() - let l:result = a:char " only add closing pair before space or any of the closepair chars let close_before = '\s\|\V\[,.;' . escape(join(keys(b:AutoClosePairs) + values(b:AutoClosePairs), ''), ']').']' - if (l:next == "\0" || l:next =~ close_before) && s:AllowQuote(a:char, 0) - call s:InsertCharsOnLine(b:AutoClosePairs[a:char]) - call s:PushBuffer(b:AutoClosePairs[a:char]) + if (l:next == "\0" || l:next =~ close_before) && s:AllowQuote(a:opener, 0) + call s:InsertStringAtCursor(b:AutoClosePairs[a:opener]) + call s:PushBuffer(b:AutoClosePairs[a:opener]) endif exec "set ve=" . l:save_ve - return l:result + return a:opener endfunction -function! s:ClosePair(char) +" returns the closer, after having eaten identical one if necessary +function! s:ClosePair(closer) let l:save_ve = &ve set ve=all - let l:result = a:char - if b:AutoCloseOn && s:GetNextChar() == a:char - call s:EraseCharsOnLine(1) + if b:AutoCloseOn && s:GetNextChar() == a:closer + call s:EraseNCharsAtCursor(1) call s:PopBuffer() endif exec "set ve=" . l:save_ve - return l:result + return a:closer endfunction -function! s:CheckPair(char) - let l:occur = s:CountQuotes(a:char) - - if l:occur == 0 || l:occur%2 == 0 - " Opening char +" in case closer is identical with its opener - heuristically decide which one +" is being typed and act accordingly +function! s:OpenOrCloseTwinPair(char) + if s:CountQuotes(a:char) % 2 == 0 + " act as opening char return s:InsertPair(a:char) else - " Closing char + " act as closing char return s:ClosePair(a:char) endif endfunction +" maintain auto-close buffer when delete key is pressed function! s:Delete() let l:save_ve = &ve set ve=all @@ -246,6 +259,9 @@ function! s:Delete() return "\" endfunction +" when backspace is pressed: +" - erase an empty pair if backspacing from inside one +" - maintain auto-close buffer function! s:Backspace() let l:save_ve = &ve let l:prev = s:GetPrevChar() @@ -253,7 +269,7 @@ function! s:Backspace() set ve=all if b:AutoCloseOn && s:IsEmptyPair() && (l:prev != l:next || s:AllowQuote(l:prev, 1)) - call s:EraseCharsOnLine(1) + call s:EraseNCharsAtCursor(1) call s:PopBuffer() endif @@ -270,30 +286,59 @@ function! s:ToggleAutoClose() endif endfunction +" Parse a whitespace separated line of pairs +" single characters are assumed to be twin pairs (closer identical to +" opener) +function! AutoClose#ParsePairs(string) + if type(a:string) == type({}) + return a:string + elseif type(a:string) != type("") + echoerr "AutoClose#ParsePairs(): Argument not a dictionary or a string" + return {} + endif + + let l:dict = {} + for pair in split(a:string) + " strlen is length in bytes, we want in (wide) characters + let l:pairLen = strlen(substitute(pair,'.','x','g')) + if l:pairLen == 1 + " assume a twin pair + let l:dict[pair] = pair + elseif l:pairLen == 2 + let l:dict[pair[0]] = pair[1] + else + echoerr "AutoClose: Bad pair string - a pair longer then two character" + echoerr " `- String: " . a:sring + echoerr " `- Pair: " . pair . " Pair len: " . l:pairLen + endif + endfor + return l:dict +endfunction + +" this function is made visible for the sake of users +function! AutoClose#DefaultPairs() + return AutoClose#ParsePairs("() {} [] <> «» ` \" '") +endfunction + +function! AutoClose#DefaultPairsModified(pairsToAdd,openersToRemove) + return filter( + \ extend(AutoClose#DefaultPairs(), AutoClose#ParsePairs(a:pairsToAdd), "force"), + \ "stridx(a:openersToRemove,v:key)<0") +endfunction + " Define variables (in the buffer namespace). -" If reset is true, the variables get reset. This is used on FileType changes. -function! s:DefineVariables(reset) +function! s:DefineVariables() " All the following variables can be set per buffer or global. - " The buffer namespace is used internally, and gets reset on FileType - " events. + " The buffer namespace is used internally let defaults = { - \ 'AutoClosePairs': {'(': ')', '{': '}', '[': ']', '"': '"', "'": "'", - \ '<': '>', '`': '`', '«': '»'}, - \ 'AutoCloseQuotes': [], + \ 'AutoClosePairs': AutoClose#DefaultPairs(), \ 'AutoCloseProtectedRegions': ["Comment", "String", "Character"], \ 'AutoCloseSmartQuote': 1, \ 'AutoCloseOn': 1, - \ 'AutoClosePreservDotReg': 1 + \ 'AutoClosePreservDotReg': 1, + \ 'AutoCloseSelectionWrapPrefix': 'a', \ } - let filetypes = split(&ft, '\.') - if index(filetypes, 'ruby') != -1 - let defaults['AutoClosePairs']['|'] = '|' - endif - if index(filetypes, 'typoscript') != -1 || index(filetypes, 'zsh') != -1 || index(filetypes, 'sh') != -1 - unlet defaults['AutoClosePairs']['<'] - endif - " Let the user define if he/she wants the plugin to do special actions when the " popup menu is visible and a movement key is pressed. " Movement keys used in the menu get mapped to themselves @@ -307,18 +352,10 @@ function! s:DefineVariables(reset) " Now handle/assign values for key in keys(defaults) - if exists('l:var') | unlet l:var | endif - if ! a:reset && exists('b:'.key) - exec 'let l:var = b:' . key - if type(l:var) == type(defaults[key]) - continue - endif - endif - if exists('g:' . key) - exec 'let l:var = g:' . key - if type(l:var) == type(defaults[key]) - exec 'let b:' . key . ' = g:' . key - endif + if exists('b:'.key) && type(eval('b:'.key)) == type(defaults[key]) + continue + elseif exists('g:'.key) && type(eval('g:'.key)) == type(defaults[key]) + exec 'let b:' . key . ' = g:' . key else exec 'let b:' . key . ' = ' . string(defaults[key]) endif @@ -326,28 +363,27 @@ function! s:DefineVariables(reset) endfunction function! s:CreatePairsMaps() - let l:appendQuote = (len(b:AutoCloseQuotes) == 0) " create appropriate maps to defined open/close characters for key in keys(b:AutoClosePairs) - let map_open = ( has_key(s:mapRemap, key) ? s:mapRemap[key] : key ) - let map_close = ( has_key(s:mapRemap, b:AutoClosePairs[key]) ? s:mapRemap[b:AutoClosePairs[key]] : b:AutoClosePairs[key] ) - - let open_func_arg = ( has_key(s:argRemap, map_open) ? '"' . s:argRemap[map_open] . '"' : '"' . map_open . '"' ) - let close_func_arg = ( has_key(s:argRemap, map_close) ? '"' . s:argRemap[map_close] . '"' : '"' . map_close . '"' ) - - exec "vnoremap a" . map_open . " `>a" . map_close . "`" - exec "vnoremap a" . map_close . " `>a" . map_close . "`" + let opener = s:keyName(key) + let closer = s:keyName(b:AutoClosePairs[key]) + let quoted_opener = s:quoteAndEscape(opener) + let quoted_closer = s:quoteAndEscape(closer) + + exec "vnoremap ". b:AutoCloseSelectionWrapPrefix + \ . opener . " `>a" . closer . "`" + exec "vnoremap ". b:AutoCloseSelectionWrapPrefix + \ . closer . " `>a" . closer . "`" if key == b:AutoClosePairs[key] - if l:appendQuote - call add(b:AutoCloseQuotes, key) - endif - exec "inoremap " . map_open . " =CheckPair(" . open_func_arg . ")" + exec "inoremap " . opener + \ . " =OpenOrCloseTwinPair(" . quoted_opener . ")" else - exec "inoremap " . map_open . " =InsertPair(" . open_func_arg . ")" - exec "inoremap " . map_close . " =ClosePair(" . close_func_arg . ")" + exec "inoremap " . opener + \ . " =InsertPair(" . quoted_opener . ")" + exec "inoremap " . closer + \ . " =ClosePair(" . quoted_closer . ")" endif endfor - endfunction function! s:CreateExtraMaps() @@ -363,7 +399,7 @@ function! s:CreateExtraMaps() endif exe 'let l:pvisiblemap = b:AutoClosePumvisible' . key if len(l:pvisiblemap) - exec "inoremap <" . key . "> pumvisible() ? \"" . l:pvisiblemap . "\" : \"\\=FlushBuffer()\\\\<" . key . ">\"" + exec "inoremap <" . key . "> pumvisible() ? '" . l:pvisiblemap . "' : '=FlushBuffer()<" . key . ">'" else exec "inoremap <" . key . "> =FlushBuffer()<" . key . ">" endif @@ -375,8 +411,9 @@ function! s:CreateExtraMaps() endif endfunction -function! s:CreateMaps(reset) - call s:DefineVariables(a:reset) +function! s:CreateMaps() + silent doautocmd FileType + call s:DefineVariables() call s:CreatePairsMaps() call s:CreateExtraMaps() @@ -387,15 +424,25 @@ function! s:IsLoadedOnBuffer() return (exists("b:loaded_AutoClose") && b:loaded_AutoClose) endfunction +" map some characters to their key names +function! s:keyName(char) + let s:keyNames = {'|': '', ' ': ''} + return get(s:keyNames,a:char,a:char) +endfunction + +" escape some characters for use in strings +function! s:quoteAndEscape(char) + let s:escapedChars = {'"': '\"'} + return '"' . get(s:escapedChars,a:char,a:char) . '"' +endfunction + """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " Configuration """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" -" here is a dictionary of characters that need to be converted before being used as map -let s:mapRemap = {'|': '', ' ': ''} -let s:argRemap = {'"': '\"'} -let s:movementKeys = ['Esc', 'Up', 'Down', 'Left', 'Right', 'Home', 'End', 'PageUp', 'PageDown'] -let s:pumMovementKeys = ['Up', 'Down', 'PageUp', 'PageDown'] " list of keys that get mapped to themselves for pumvisible() +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') if s:needspecialkeyhandling " map s:movementKeys to xterm equivalent let s:movementKeysXterm = {'Esc': '', 'Up': 'OA', 'Down': 'OB', 'Left': 'OD', 'Right': 'OC', 'Home': 'OH', 'End': 'OF', 'PageUp': '[5~', 'PageDown': '[6~'} @@ -403,13 +450,17 @@ endif augroup (autoclose) au! -autocmd FileType * call CreateMaps(1) -autocmd BufNewFile,BufRead,BufEnter * if !IsLoadedOnBuffer() | call CreateMaps(0) | endif +autocmd BufNewFile,BufRead,BufEnter * if !IsLoadedOnBuffer() | call CreateMaps() | endif autocmd InsertEnter * call EmptyBuffer() autocmd BufEnter * if mode() == 'i' | call 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 command! AutoCloseOn :let b:AutoCloseOn = 1 command! AutoCloseOff :let b:AutoCloseOn = 0 command! AutoCloseToggle :call s:ToggleAutoClose() +" vim:sw=4:sts=4: