[Vim-latex-devel] matching brackets
Brought to you by:
srinathava,
tmaas
From: Carl M. <cm...@ma...> - 2002-11-12 13:26:28
|
One of my biggest problems used to be mismatched brackets. Now I have macros which automatically type the matching bracket. For instance, "(" results in "()" with the cursor in between. Since I sometimes want only one bracket, I made it so that "((" results in "(". That is faster than typing "(" and then deleting the extra bracket. Not everyone may agree with this idea, so it could be an option. However, it has saved me an enormous amount of time. Also, the emacs package "ultratex" has automatic matching $$ and {}, so there is some precedent. I'm listing the code below. It applies to: () [] {} \{\} || \|\| $$ && (when within eqnarray or eqnarray* environments) Carl Mueller ========================================================================= " Bracket Completion Macros " Key Bindings " Typing the symbol a second time (for example, $$) will result in one " of the symbole (for instance, $). With {, typing \{ will result in \{\}. inoremap <buffer> ( <C-R>=<SID>Double("(",")")<CR> "inoremap <buffer> [ <C-R>=<SID>Double("[","]")<CR> inoremap <buffer> [ <C-R>=<SID>CompleteSlash("[","]")<CR> inoremap <buffer> $ <C-R>=<SID>Double("$","$")<CR> inoremap <buffer> & <C-R>=<SID>DoubleAmpersands()<CR> inoremap <buffer> { <C-R>=<SID>CompleteSlash("{","}")<CR> inoremap <buffer> \| <C-R>=<SID>CompleteSlash("\|","\|")<CR> " If you would rather insert $$ individually, the following macro by " Charles Campbell will make the cursor blink on the previous dollar sign, " if it is in the same line. " inoremap $ $<C-O>F$<C-O>:redraw!<CR><C-O>:sleep 500m<CR><C-O>f$<Right> " Functions " For () and $$ function! s:Double(left,right) if strpart(getline(line(".")),col(".")-2,2) == a:left . a:right return "\<Del>" else return a:left . a:right . "\<Left>" endif endfunction " Complete [, \[, {, \{, |, \| function! s:CompleteSlash(left,right) let column = col(".") let first = getline(line("."))[column-2] let second = getline(line("."))[column-1] if first == "\\" if a:left == "[" return "\[\<CR>\<CR>\\]\<Up>" else return a:left . "\\" . a:right . "\<Left>\<Left>" endif else if a:left =~ '\[\|{' \ && strpart(getline(line(".")),col(".")-2,2) == a:left . a:right return "\<Del>" else return a:left . a:right . "\<Left>" endif endif endfunction " Double ampersands, if you are in an eqnarray or eqnarray* environment. function! s:DoubleAmpersands() let stop = 0 let currentline = line(".") while stop == 0 let currentline = currentline - 1 let thisline = getline(currentline) if thisline =~ '\\begin' || currentline == 0 let stop = 1 endif endwhile if thisline =~ '\\begin{eqnarray\**}' return "&&\<Left>" elseif strpart(getline(line(".")),col(".")-2,2) == "&&" return "\<Del>" else return "&" endif endfunction |