1 "=============================================================================
3 " Author: Gergely Kontra
4 " (minor modifications by Srinath Avadhanula)
5 " (plus other modifications by Mikolaj Machowski)
7 " Created: Tue Apr 23 05:00 PM 2002 PST
8 " CVS: $Id: templates.vim 1002 2006-03-23 04:02:21Z srinathava $
10 " Description: functions for handling templates in latex-suite/templates
12 "=============================================================================
14 let s:path = expand("<sfile>:p:h")
16 " SetTemplateMenu: sets up the menu for templates {{{
17 function! <SID>SetTemplateMenu()
18 let flist = Tex_FindInRtp('', 'templates')
21 let fname = Tex_Strntok(flist, ',', i)
25 exe "amenu ".g:Tex_TemplatesMenuLocation."&".i.":<Tab>".fname." ".
26 \":call <SID>ReadTemplate('".fname."')<CR>"
32 call <SID>SetTemplateMenu()
36 " ReadTemplate: reads in the template file from the template directory. {{{
37 function! <SID>ReadTemplate(...)
41 let filelist = Tex_FindInRtp('', 'templates')
43 \ Tex_ChooseFromPrompt("Choose a template file:\n" .
44 \ Tex_CreatePrompt(filelist, 2, ',') .
45 \ "\nEnter number or name of file :",
49 let fname = Tex_FindInRtp(filename.'.tex', 'templates', ':p')
50 call Tex_Debug("0read ".fname, 'templates')
52 silent! exe "0read ".fname
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+)'
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', '')
65 call s:ProcessTemplate()
66 call Tex_pack_updateall(1)
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().
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.
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>"
87 call Tex_Debug('phs = '.s:phsTemp.', phe = '.s:pheTemp.', exe = '.s:exeTemp.', com = '.s:comTemp, 'templates')
92 " ProcessTemplate: processes the special characters in template file. {{{
93 " This implementation follows from Gergely Kontra's
95 " http://vim.sourceforge.net/scripts/script.php?script_id=222
96 function! <SID>ProcessTemplate()
97 if exists('s:phsTemp') && s:phsTemp != ''
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'
103 " A function only puts one item into the search history...
104 call Tex_CleanSearchHistory()
108 function! <SID>Exec(what)
109 exec 'return '.a:what
112 " Back-Door to trojans !!!
113 function! <SID>Compute(what)
115 if exists('s:comTemp')
116 return s:comTemp.s:comTemp
123 " Command definitions {{{
125 com! -complete=custom,Tex_CompleteTemplateName -nargs=? TTemplate :call <SID>ReadTemplate(<f-args>)
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.
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')
142 com! -nargs=? TTemplate :call <SID>ReadTemplate(<f-args>)
149 " vim:fdm=marker:ff=unix:noet:ts=4:sw=4