Pregunta

I've read vim-wiki about dynamic templates and I want similar, simple "template-system". I've created a function:

function! Read_template(file)
    execute '0r /home/zsolt/.vim/skeletons/'.a:file
    %substitute#\[:EVAL:\]\(.\{-\}\)\[:END:\]#\=eval(submatch(1))#ge
    %substitute#\[:READ:\]\(.\{-\}\)\[:END:\]#??????#ge
endfunction

I want to include a file from a template. The EVAL works well but how can I solve the READ function? It isn't important to eval the included file.

An example:

main.tex:

\documentclass[a4paper]{article}
....

exam.tex:

% Created [:EVAL:]strftime('%Y. %B. %d.')[:END:]
[:READ:]/path/of/main/main.tex[:READ:]

I exec Read_template("exam.tex") and want that exam.tex includes main.tex.

How can I do this?

¿Fue útil?

Solución

You'll need to read the file and insert its contents. As you cannot use :read (it will read entire lines and cannot be called from within a substitution), you have to use the lower-level readfile() Vimscript function, like this:

%substitute#\[:READ:\]\(.\{-\}\)\[:END:\]#\=join(readfile(submatch(1)),"\n")/#ge

Otros consejos

You'll have to parse each line imported and apply what needs be : - expression substitution - inclusion of other templates, etc. (which will mean that you'll have to remove and add lines on the fly. In the last version of mu-template template expansion engine, the expansion is done in-memory)

FYI, my work of mu-template already has this feature: http://code.google.com/p/lh-vim/wiki/muTemplate#Completely_useless_recursive_example

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top