Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Own simple template system in vim - include file

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?

like image 500
uzsolt Avatar asked Jun 18 '26 04:06

uzsolt


2 Answers

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
like image 53
Ingo Karkat Avatar answered Jun 21 '26 03:06

Ingo Karkat


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

like image 25
Luc Hermitte Avatar answered Jun 21 '26 03:06

Luc Hermitte



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!