1 " ==============================================================================
3 " (incorporated into latex-suite by Srinath Avadhanula)
4 " Last Change: Tue Dec 31 11:00 AM 2002 PST
6 " This ftplugin provides the following maps:
7 " . <M-b> encloses the previous character in \mathbf{}
8 " . <M-c> is polymorphic as follows:
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{}
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
18 " 1. (<M-l> \left(\right
20 " {<M-l> \left\{\right\}
21 " 2. <<M-l> \langle\rangle
22 " 3. q<M-l> \lefteqn{}
23 " otherwise insert \label{}
25 " These functions make it extremeley easy to do all the \left \right stuff in
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 " ==============================================================================
38 if exists('b:did_brackets')
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 " ==============================================================================
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
63 if !hasmapto('<Plug>Tex_MathCal', 'i') && mapcheck('<M-c>', 'i') == ''
64 imap <buffer> <silent> <M-c> <Plug>Tex_MathCal
66 if !hasmapto('<Plug>Tex_LeftRight', 'i') && mapcheck('<M-l>', 'i') == ''
67 imap <buffer> <silent> <M-l> <Plug>Tex_LeftRight
72 " ==============================================================================
73 " Visual/Normal Mode mappings.
74 " ==============================================================================
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>
83 " ==============================================================================
84 " Function definitions
85 " ==============================================================================
86 " define the funtions only once.
87 if exists('*Tex_MathBF')
90 " Tex_MathBF: encloses te previous letter/number in \mathbf{} {{{
92 function! Tex_MathBF()
93 return "\<Left>\\mathbf{\<Right>}\<Esc>hvUla"
95 " Tex_MathCal: enclose the previous letter/number in \mathcal {{{
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).'}'
105 return IMAP_PutTextWithMovement('\cite{<++>}<++>')
109 " Tex_LeftRight: maps <M-l> in insert mode. {{{
111 " This is a polymorphic function, which maps the behaviour of <M-l> in the
113 " If the character before typing <M-l> is one of '([{|<q', then do the
115 " 1. (<M-l> \left(<++>\right<++>
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 =~ '(\|\[\|{\||'
132 let rhs = matchstr(matchedbrackets, char.'\zs.\ze')
133 return "\<BS>".IMAP_PutTextWithMovement('\left'.add.char.'<++>\right'.add.rhs.'<++>')
135 return "\<BS>".IMAP_PutTextWithMovement('langle<++>\rangle<++>')
137 return "\<BS>".IMAP_PutTextWithMovement('\lefteqn{<++>}<++>')
139 return IMAP_PutTextWithMovement('\label{<++>}<++>')
142 " Tex_PutLeftRight: maps <M-l> in normal mode {{{
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]
150 exe "normal ileft\\\<Esc>l%iright\\\<Esc>l%"
152 exe "normal iright\\\<Esc>l%ileft\\\<Esc>l%"
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%"