add vim conf files
[arrow.git] / conf_slk120 / vim / _vim / ftplugin / tex / brackets.vim
blob426d7029d580f4be98b0aa2edad8dd28b9cc7c2a
1 " ==============================================================================
2 " Author: Carl Mueller
3 "                 (incorporated into latex-suite by Srinath Avadhanula)
4 " Last Change: Tue Dec 31 11:00 AM 2002 PST
5 " Description:
6 "       This ftplugin provides the following maps:
7 " . <M-b> encloses the previous character in \mathbf{}
8 " . <M-c> is polymorphic as follows:
9 "     Insert mode:
10 "     1. If the previous character is a letter or number, then capitalize it and
11 "        enclose it in \mathcal{}
12 "     2. otherwise insert \cite{}
13 "     Visual Mode:
14 "     1. Enclose selection in \mathcal{}
15 " . <M-l> is also polymorphic as follows:
16 "     If the character before typing <M-l> is one of '([{|<q', then do the
17 "     following:
18 "       1. (<M-l>       \left(\right
19 "               similarly for [, |
20 "          {<M-l>       \left\{\right\}
21 "       2. <<M-l>       \langle\rangle
22 "       3. q<M-l>       \lefteqn{}
23 "     otherwise insert  \label{}
24
25 " These functions make it extremeley easy to do all the \left \right stuff in
26 " latex.
28 " NOTE: The insert mode maps are created only if maps are no maps already to
29 " the relevant functions Tex_MathBF, Tex_MathCal and Tex_LeftRight. This is to
30 " enable people who might need the alt keys for typing to use some other
31 " keypress to trigger the same behavior. In order to use some other key, (say
32 " <C-c>) to use Tex_MathCal(), do the following
34 "       inoremap <buffer> <silent> <C-c> <C-r>=Tex_MathCal()<CR>
36 " ============================================================================== 
37 " Avoid reinclusion.
38 if exists('b:did_brackets')
39         finish
40 endif
41 let b:did_brackets = 1
43 " ==============================================================================
44 " Insert mode mappings
45 " All the insert mode mappings check to see if the function they are creating
46 " the map for already exists in the rhs of some map.
47 " ============================================================================== 
48 " {{{
50 " Provide <plug>'d mapping for easy user customization.
52 inoremap <silent> <Plug>Tex_MathBF      <C-r>=Tex_MathBF()<CR>
53 inoremap <silent> <Plug>Tex_MathCal     <C-r>=Tex_MathCal()<CR>
54 inoremap <silent> <Plug>Tex_LeftRight   <C-r>=Tex_LeftRight()<CR>
56 " Provide mappings only if the user hasn't provided a map already or if the
57 " target lhs doesn't have a mapping.
58 " TODO: These will be removed in future revisions. Alt mappings are a headache
59 "       for European users...
60 if !hasmapto('<Plug>Tex_MathBF', 'i') && mapcheck('<M-b>', 'i') == ''
61         imap <buffer> <silent> <M-b>        <Plug>Tex_MathBF
62 endif
63 if !hasmapto('<Plug>Tex_MathCal', 'i') && mapcheck('<M-c>', 'i') == ''
64         imap <buffer> <silent> <M-c>        <Plug>Tex_MathCal
65 endif
66 if !hasmapto('<Plug>Tex_LeftRight', 'i') && mapcheck('<M-l>', 'i') == ''
67         imap <buffer> <silent> <M-l>        <Plug>Tex_LeftRight
68 endif
70 " }}}
72 " ==============================================================================
73 " Visual/Normal Mode mappings.
74 " ==============================================================================
75 " {{{
77 vnoremap <buffer> <silent> <M-b> <C-C>`>a}<Esc>`<i\mathbf{<Esc>
78 vnoremap <buffer> <silent> <M-c> <C-C>`>a}<Esc>`<i\mathcal{<Esc>
79 nnoremap <buffer> <silent> <M-l> :call <SID>PutLeftRight()<CR>
81 " }}}
83 " ==============================================================================
84 " Function definitions
85 " ============================================================================== 
86 " define the funtions only once.
87 if exists('*Tex_MathBF')
88         finish
89 endif
90 " Tex_MathBF: encloses te previous letter/number in \mathbf{} {{{
91 " Description: 
92 function! Tex_MathBF()
93         return "\<Left>\\mathbf{\<Right>}\<Esc>hvUla"
94 endfunction " }}}
95 " Tex_MathCal: enclose the previous letter/number in \mathcal {{{
96 " Description:
97 "       if the last character is not a letter/number, then insert \cite{}
98 function! Tex_MathCal()
99         let line = getline(line("."))
100         let char = line[col(".")-2]
102         if char =~ '[a-zA-Z0-9]'
103                 return "\<BS>".'\mathcal{'.toupper(char).'}'
104         else
105                 return IMAP_PutTextWithMovement('\cite{<++>}<++>')
106         endif
107 endfunction
108 " }}}
109 " Tex_LeftRight: maps <M-l> in insert mode. {{{
110 " Description:
111 " This is a polymorphic function, which maps the behaviour of <M-l> in the
112 " following way:
113 " If the character before typing <M-l> is one of '([{|<q', then do the
114 " following:
115 "       1. (<M-l>               \left(<++>\right<++>
116 "               similarly for [, |
117 "          {<M-l>               \left\{<++>\right\}<++>
118 "       2. <<M-l>               \langle<++>\rangle<++>
119 "       3. q<M-l>               \lefteqn{<++>}<++>
120 " otherwise insert  \label{<++>}<++>
121 function! Tex_LeftRight()
122         let line = getline(line("."))
123         let char = line[col(".")-2]
124         let previous = line[col(".")-3]
126         let matchedbrackets = '()[]{}||'
127         if char =~ '(\|\[\|{\||'
128                 let add = ''
129                 if char =~ '{'
130                         let add = "\\"
131                 endif
132                 let rhs = matchstr(matchedbrackets, char.'\zs.\ze')
133                 return "\<BS>".IMAP_PutTextWithMovement('\left'.add.char.'<++>\right'.add.rhs.'<++>')
134         elseif char == '<'
135                 return "\<BS>".IMAP_PutTextWithMovement('langle<++>\rangle<++>')
136         elseif char == 'q'
137                 return "\<BS>".IMAP_PutTextWithMovement('\lefteqn{<++>}<++>')
138         else
139                 return IMAP_PutTextWithMovement('\label{<++>}<++>')
140         endif
141 endfunction " }}}
142 " Tex_PutLeftRight: maps <M-l> in normal mode {{{
143 " Description:
144 " Put \left...\right in front of the matched brackets.
145 function! Tex_PutLeftRight()
146         let previous = getline(line("."))[col(".") - 2]
147         let char = getline(line("."))[col(".") - 1]
148         if previous == '\'
149                 if char == '{'
150                         exe "normal ileft\\\<Esc>l%iright\\\<Esc>l%"
151                 elseif char == '}'
152                         exe "normal iright\\\<Esc>l%ileft\\\<Esc>l%"
153                 endif
154         elseif char =~ '\[\|('
155                 exe "normal i\\left\<Esc>l%i\\right\<Esc>l%"
156         elseif char =~ '\]\|)'
157                 exe "normal i\\right\<Esc>l%i\\left\<Esc>l%"
158         endif
159 endfunction " }}}
161 " vim:fdm=marker