Initial import of my home directory
[home/.git] / .vim / ftplugin / latex-suite / templates.vim
1 "=============================================================================
2 "            File: templates.vim
3 "      Author: Gergely Kontra
4 "              (minor modifications by Srinath Avadhanula)
5 "              (plus other modifications by Mikolaj Machowski) 
6 "         Version: 1.0 
7 "     Created: Tue Apr 23 05:00 PM 2002 PST
8 "         CVS: $Id: templates.vim 1002 2006-03-23 04:02:21Z srinathava $ 
9
10 "  Description: functions for handling templates in latex-suite/templates
11 "               directory.
12 "=============================================================================
13
14 let s:path = expand("<sfile>:p:h")
15
16 " SetTemplateMenu: sets up the menu for templates {{{
17 function! <SID>SetTemplateMenu()
18         let flist = Tex_FindInRtp('', 'templates')
19         let i = 1
20         while 1
21                 let fname = Tex_Strntok(flist, ',', i)
22                 if fname == ''
23                         break
24                 endif
25                 exe "amenu ".g:Tex_TemplatesMenuLocation."&".i.":<Tab>".fname." ".
26                         \":call <SID>ReadTemplate('".fname."')<CR>"
27                 let i = i + 1
28         endwhile
29 endfunction 
30
31 if g:Tex_Menus
32         call <SID>SetTemplateMenu()
33 endif
34
35 " }}}
36 " ReadTemplate: reads in the template file from the template directory. {{{
37 function! <SID>ReadTemplate(...)
38         if a:0 > 0
39                 let filename = a:1
40         else
41                 let filelist = Tex_FindInRtp('', 'templates')
42                 let filename = 
43                                         \ Tex_ChooseFromPrompt("Choose a template file:\n" . 
44                                         \ Tex_CreatePrompt(filelist, 2, ',') . 
45                                         \ "\nEnter number or name of file :", 
46                                         \ filelist, ',')
47         endif
48
49         let fname = Tex_FindInRtp(filename.'.tex', 'templates', ':p')
50         call Tex_Debug("0read ".fname, 'templates')
51
52         silent! exe "0read ".fname
53
54         " The first line of the file contains the specifications of what the
55         " placeholder characters and the other special characters are.
56         let pattern = '\v(\S+)\t(\S+)\t(\S+)\t(\S+)'
57
58         let s:phsTemp = substitute(getline(1), pattern, '\1', '')
59         let s:pheTemp = substitute(getline(1), pattern, '\2', '')
60         let s:exeTemp = substitute(getline(1), pattern, '\3', '')
61         let s:comTemp = substitute(getline(1), pattern, '\4', '')
62
63         0 d_
64
65         call s:ProcessTemplate()
66         call Tex_pack_updateall(1)
67
68         " Do not handle the placeholders here. Let IMAP_PutTextWithMovement do it
69         " because it handles UTF-8 character substitutions etc. Therefore delete
70         " the text into @a and paste it using IMAP_PutTextWithMovement().
71         let _a = @a
72         normal! ggVG"ax
73         
74         let _fo = &fo
75         " Since IMAP_PutTextWithMovement simulates the key-presses, leading
76         " indendatation can get duplicated in strange ways if ``fo`` is non-empty.
77         " NOTE: the indentexpr thingie is still respected with an empty fo so that
78         "           environments etc are properly indented.
79         set fo=
80
81         call Tex_Debug("normal! i\<C-r>=IMAP_PutTextWithMovement(@a, '".s:phsTemp."', '".s:pheTemp."')\<CR>", 'templates')
82         exec "normal! i\<C-r>=IMAP_PutTextWithMovement(@a, '".s:phsTemp."', '".s:pheTemp."')\<CR>"
83
84         let &fo = _fo
85         let @a = _a
86
87         call Tex_Debug('phs = '.s:phsTemp.', phe = '.s:pheTemp.', exe = '.s:exeTemp.', com = '.s:comTemp, 'templates')
88
89 endfunction
90
91 " }}}
92 " ProcessTemplate: processes the special characters in template file. {{{
93 "                  This implementation follows from Gergely Kontra's
94 "                  mu-template.vim
95 "                  http://vim.sourceforge.net/scripts/script.php?script_id=222
96 function! <SID>ProcessTemplate()
97         if exists('s:phsTemp') && s:phsTemp != ''
98
99                 exec 'silent! %s/^'.s:comTemp.'\(\_.\{-}\)'.s:comTemp.'$/\=<SID>Compute(submatch(1))/ge'
100                 exec 'silent! %s/'.s:exeTemp.'\(.\{-}\)'.s:exeTemp.'/\=<SID>Exec(submatch(1))/ge'
101                 exec 'silent! g/'.s:comTemp.s:comTemp.'/d'
102                 
103                 " A function only puts one item into the search history...
104                 call Tex_CleanSearchHistory()
105         endif
106 endfunction
107
108 function! <SID>Exec(what)
109         exec 'return '.a:what
110 endfunction
111
112 " Back-Door to trojans !!!
113 function! <SID>Compute(what)
114         exe a:what
115         if exists('s:comTemp')
116                 return s:comTemp.s:comTemp
117         else
118                 return ''
119         endif
120 endfunction
121
122 " }}}
123 " Command definitions {{{
124 if v:version >= 602
125         com! -complete=custom,Tex_CompleteTemplateName -nargs=? TTemplate :call <SID>ReadTemplate(<f-args>)
126                 \| :startinsert
127
128         " Tex_CompleteTemplateName: for completing names in TTemplate command {{{
129         "       Description: get list of template names with Tex_FindInRtp(), remove full path
130         "       and return list of names separated with newlines.
131         "
132         function! Tex_CompleteTemplateName(A,P,L)
133                 " Get name of macros from all runtimepath directories
134                 let tmplnames = Tex_FindInRtp('', 'templates')
135                 " Separate names with \n not ,
136                 let tmplnames = substitute(tmplnames,',','\n','g')
137                 return tmplnames
138         endfunction
139         " }}}
140         
141 else
142         com! -nargs=? TTemplate :call <SID>ReadTemplate(<f-args>)
143                 \| :startinsert
144
145 endif
146
147 " }}}
148
149 " vim:fdm=marker:ff=unix:noet:ts=4:sw=4