Thread: [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 |
From: Luc H. <her...@fr...> - 2002-11-12 19:57:26
|
* On Tue, Nov 12, 2002 at 08:26:48AM -0500, Carl Mueller <cm...@ma...> wrote: > 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. Hum ... nice idea. If you don't mind, may be I will use in my common_brackets.vim. As the only times I need a single parenthesis are within comments or string contexts, I use <C-V> for the remaining rare times. Personnaly, I'd rather see this as a plugin. Then from any file in ftplugin/tex, the corresponding pairs are activated. and the same from ftplugin/vim, ftplugin/c, etc. These bracketing macros are not interresting in LaTeX documents only. > I'm listing the code below. It applies to: > () [] {} \{\} $$ It reminds me I need to extend my version of CompleteSlash to support '\%(\)' -- for vim > || \|\| > && (when within eqnarray or eqnarray* environments) cheers, -- Luc Hermitte |
From: Carl M. <cm...@ma...> - 2002-11-12 20:02:57
|
Dear Luc, On Tue, Nov 12, 2002 at 06:29:08PM +0100, Luc Hermitte wrote: > * On Tue, Nov 12, 2002 at 08:26:48AM -0500, Carl Mueller <cm...@ma...> wrote: > > 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. > > Hum ... nice idea. If you don't mind, may be I will use in my > common_brackets.vim. As the only times I need a single parenthesis are > within comments or string contexts, I use <C-V> for the remaining rare > times. > Please feel free to copy anything I've come up with; this applies to anyone else who might read these posts. Best wishes, Carl |