Vim Tips Wiki
(Category: A.T.I.)
No edit summary
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=710
 
|id=710
  +
|previous=709
|title=Save time by typing and running templates instead of routine code.
 
  +
|next=711
|created=May 4, 2004 1:37
 
  +
|created=May 4, 2004
 
|complexity=basic
 
|complexity=basic
|author=akeru at tilling dot com
+
|author=akeru
 
|version=6.0
 
|version=6.0
 
|rating=0/2
 
|rating=0/2
  +
|category1=Automated Text Insertion
|text=
 
  +
|category2=
The idea is pretty simple.
 
  +
}}
  +
There's a template script written in perl (see sources below).
   
  +
Use this mapping in visual mode:
1.There's template script written in perl (see sources below.)
 
   
  +
<pre>
2. and mapping in visual mode:
 
  +
vnoremap <F6> :!perl E:\\Devtools\\vim\\vimfiles\\template\truler.pl<CR>
  +
</pre>
   
  +
The template markers are:
vnoremap &lt;F6&gt; :!perl E:\\Devtools\\vim\\vimfiles\\template\truler.pl&lt;CR&gt;
 
   
  +
<pre>
  +
TS:<Single string template here>
  +
TB:
  +
<multistring template here>
  +
TE:
  +
TF: <template file name >
  +
</pre>
   
  +
So now we are typing following templates and get them transformed.
   
  +
Typing this:
The template markers are:
 
   
  +
<pre>
TS:&lt;Single string template here&gt;
 
  +
TS:public final static int PROXY_TYPE_{$p2}={$p1};
  +
1;HTTP
  +
2;SOCKS
  +
3;SSH
  +
4;HTTPS
  +
5;NOPROXY
  +
</pre>
   
  +
Now select this template with values below, press magic button and 'ta-da', we have this code :
TB:
 
   
  +
<pre>
&lt;multistring template here&gt;
 
  +
public final static int PROXY_TYPE_HTTP=1;
  +
public final static int PROXY_TYPE_SOCKS=2;
  +
public final static int PROXY_TYPE_SSH=3;
  +
public final static int PROXY_TYPE_HTTPS=4;
  +
public final static int PROXY_TYPE_NOPROXY=5;
  +
</pre>
   
  +
Here's the same with multiline template, type following:
TE:
 
   
  +
<pre>
TF: &lt;template file name &gt;
 
  +
TB:
  +
public {$p1} is{$p2}Established() \{
  +
return m{$p2};
  +
\}
  +
TE:
  +
boolean;DirectConnection
  +
boolean;SockConnection
  +
boolean;HTTPConnection
  +
</pre>
   
  +
and after transforming we'll got this :
   
  +
<pre>
  +
protected boolean isDirectConnectionEstablished() {
  +
return m_DirectConnection;
  +
}
   
  +
protected boolean isSockConnectionEstablished() {
So now we are typing following templates and get them transformed :-).
 
  +
return m_SockConnection;
  +
}
   
  +
protected boolean isHTTPConnectionEstablished() {
  +
return m_HTTPConnection;
  +
}
  +
</pre>
   
  +
I know it's stupid 'copy-paste' and I don't like it, but sometimes It's required
  +
and that simple script saving my time.
   
  +
Text::Template you can download it from http://search.cpan.org.
Typing this :
 
   
TS:public final static int PROXY_TYPE_{$p2}={$p1};
 
   
  +
<pre>
1;HTTP
 
  +
# truler.pl
  +
use Text::Template;
  +
$tb=0; $te=0;$template_str="";
  +
$template_dir="E:\\Devtools\\vim\\vimfiles\\template\\";
  +
while (<STDIN>){
  +
if (/^TF:(.*)$/){#file
  +
$template_file=$1;
  +
$template = Text::Template->new(SOURCE => $template_dir.$template_file)
  +
or die "Couldn't construct template: $Text::Template::ERROR";
   
  +
}elsif (/^TS:(.*)$/){#line
2;SOCKS
 
  +
$template_str=$1;
  +
$template = Text::Template->new(TYPE=> STRING ,SOURCE => "$template_str\n")
  +
or die "Couldn't construct template: $Text::Template::ERROR";
   
  +
}elsif (/^TB:/){#line
3;SSH
 
  +
$tb=1;
  +
}elsif (/^TE:/){#line
  +
$te=1;
  +
$template = Text::Template->new(TYPE=> STRING ,SOURCE => "$template_str\n")
  +
or die "Couldn't construct template: $Text::Template::ERROR";
  +
}elsif ($tb==1 && $te==0 ) {
  +
$template_str.=$_;
  +
}else {
   
  +
if (defined $template) {
4;HTTPS
 
  +
chomp;
  +
@p=split /;/;
  +
#if ($p > 0) { print "$_\n";
   
  +
my %vars = (
5;NOPROXY
 
  +
p1 => $p[0],
  +
p2 => $p[1],
  +
p3 => $p[2],
  +
p4 => $p[3],
  +
p5 => $p[4],
  +
p6 => $p[5],
  +
p7 => $p[6],
  +
p8 => $p[7],
  +
p9 => $p[8]
  +
);
  +
my $result = $template->fill_in(HASH => \%vars);
  +
if (defined $result) { print $result }
  +
#}
  +
} #template_file defined
  +
}#else of TF
  +
}#end while
  +
</pre>
   
  +
==Comments==
  +
I have something similar here http://www.ophinity.com/papers/wrangling/index.html#multiply
   
  +
written in python, so it's inherently superior.
   
Now select this template with values below, press magic button and 'ta-da',
 
 
we have this code :
 
 
 
 
public final static int PROXY_TYPE_HTTP=1;
 
 
public final static int PROXY_TYPE_SOCKS=2;
 
 
public final static int PROXY_TYPE_SSH=3;
 
 
public final static int PROXY_TYPE_HTTPS=4;
 
 
public final static int PROXY_TYPE_NOPROXY=5;
 
 
 
 
Here's the same with multiline template, type following:
 
 
TB:
 
 
public {$p1} is{$p2}Established() \{
 
 
return m{$p2};
 
 
\}
 
 
TE:
 
 
boolean;DirectConnection
 
 
boolean;SockConnection
 
 
boolean;HTTPConnection
 
 
 
 
and after transforming we'll got this :
 
 
protected boolean isDirectConnectionEstablished() {
 
 
return m_DirectConnection;
 
 
}
 
 
 
 
protected boolean isSockConnectionEstablished() {
 
 
return m_SockConnection;
 
 
}
 
 
 
 
protected boolean isHTTPConnectionEstablished() {
 
 
return m_HTTPConnection;
 
 
}
 
 
 
 
I know it's stupid 'copy-paste' and I don't like it, but sometimes It's required
 
 
and that simple script saving my time. Everthing working under Windoze.
 
 
Text::Template you can download it from http://search.cpan.org.
 
 
 
 
Source below:
 
 
---truler.pl-------
 
 
use Text::Template;
 
 
$tb=0; $te=0;$template_str="";
 
 
$template_dir="E:\\Devtools\\vim\\vimfiles\\template\\";
 
 
while (&lt;STDIN&gt;){
 
 
 
 
if (/^TF:.*/){&#35;file
 
 
 
 
$_=~/^TF:(.*?)$/;
 
 
$template_file=$1;
 
 
$template = Text::Template-&gt;new(SOURCE =&gt; $template_dir.$template_file)
 
 
or die "Couldn't construct template: $Text::Template::ERROR";
 
 
 
 
}elsif (/^TS:.*/){&#35;line
 
 
$_=~/^TS:(.*?)$/;
 
 
$template_str=$1;
 
 
$template = Text::Template-&gt;new(TYPE=&gt; STRING ,SOURCE =&gt; "$template_str\n")
 
 
or die "Couldn't construct template: $Text::Template::ERROR";
 
 
 
 
}elsif (/^TB:.*/){&#35;line
 
 
$tb=1;
 
 
}elsif (/^TE:.*/){&#35;line
 
 
$te=1;
 
 
$template = Text::Template-&gt;new(TYPE=&gt; STRING ,SOURCE =&gt; "$template_str\n")
 
 
or die "Couldn't construct template: $Text::Template::ERROR";
 
 
}elsif ($tb==1 &amp;&amp; $te==0 ) {
 
 
$template_str.="$_";
 
 
}else {
 
 
 
 
if (defined $template) {
 
 
chomp($_);
 
 
@p=split /;/;
 
 
&#35;if ($p &gt; 0) { print "$_\n";
 
 
 
 
my %vars = (
 
 
p1 =&gt; $p[0],
 
 
p2 =&gt; $p[1],
 
 
p3 =&gt; $p[2],
 
 
p4 =&gt; $p[3],
 
 
p5 =&gt; $p[4],
 
 
p6 =&gt; $p[5],
 
 
p7 =&gt; $p[6],
 
 
p8 =&gt; $p[7],
 
 
p9 =&gt; $p[8]
 
 
);
 
 
my $result = $template-&gt;fill_in(HASH =&gt; \%vars);
 
 
if (defined $result) { print $result }
 
 
&#35;}
 
 
} &#35;template_file defined
 
 
}&#35;else of TF
 
 
}&#35;end while
 
 
--- truler.pl-------
 
 
 
 
 
}}
 
 
== Comments ==
 
Guys,sorry for confusing explanation,I hope you catch the idea.
 
Contact me by akeru--AT--tulling.N0SPAM.com if you have any questions , ideas .
 
 
 
akeru--AT--tulling.N0SPAM.com
 
, May 6, 2004 2:29
 
----
 
___shameless self promotion____
 
i have something similar here:
 
http://www.ophinity.com/papers/wrangling/index.html&#35;multiply
 
 
written in python, so it's inherently superior ;)
 
 
 
 
demian0311--AT--yahoo.com
 
, May 7, 2004 14:54
 
----
 
&gt;&gt;&gt;___shameless self promotion____
 
It's was because of lack of communication with other vim fans ;-)
 
I think the power of such tools is in template engine.
 
Does multiply.py allow to pick-up templates from file or you have to type common used templates
 
every time ? Can you put some python code into template and run it during the template transforming ? :-)
 
 
akeru
 
, May 9, 2004 4:34
 
 
----
 
----
<!-- parsed by vimtips.py in 0.515612 seconds-->
 
[[Category:Automated Text Insertion]]
 

Latest revision as of 05:30, 15 July 2010

Tip 710 Printable Monobook Previous Next

created May 4, 2004 · complexity basic · author akeru · version 6.0


There's a template script written in perl (see sources below).

Use this mapping in visual mode:

vnoremap <F6> :!perl E:\\Devtools\\vim\\vimfiles\\template\truler.pl<CR>

The template markers are:

TS:<Single string template here>
TB:
<multistring template here>
TE:
TF: <template file name >

So now we are typing following templates and get them transformed.

Typing this:

TS:public final static int PROXY_TYPE_{$p2}={$p1};
1;HTTP
2;SOCKS
3;SSH
4;HTTPS
5;NOPROXY

Now select this template with values below, press magic button and 'ta-da', we have this code :

public final static int PROXY_TYPE_HTTP=1;
public final static int PROXY_TYPE_SOCKS=2;
public final static int PROXY_TYPE_SSH=3;
public final static int PROXY_TYPE_HTTPS=4;
public final static int PROXY_TYPE_NOPROXY=5;

Here's the same with multiline template, type following:

TB:
public {$p1} is{$p2}Established() \{
 return m{$p2};
\}
TE:
boolean;DirectConnection
boolean;SockConnection
boolean;HTTPConnection

and after transforming we'll got this :

protected boolean isDirectConnectionEstablished() {
  return m_DirectConnection;
}

protected boolean isSockConnectionEstablished() {
  return m_SockConnection;
}

protected boolean isHTTPConnectionEstablished() {
  return m_HTTPConnection;
}

I know it's stupid 'copy-paste' and I don't like it, but sometimes It's required and that simple script saving my time.

Text::Template you can download it from http://search.cpan.org.


# truler.pl
use Text::Template;
$tb=0; $te=0;$template_str="";
$template_dir="E:\\Devtools\\vim\\vimfiles\\template\\";
while (<STDIN>){
 if (/^TF:(.*)$/){#file
 $template_file=$1;
 $template = Text::Template->new(SOURCE => $template_dir.$template_file)
 or die "Couldn't construct template: $Text::Template::ERROR";

 }elsif (/^TS:(.*)$/){#line
 $template_str=$1;
 $template = Text::Template->new(TYPE=> STRING ,SOURCE => "$template_str\n")
 or die "Couldn't construct template: $Text::Template::ERROR";

 }elsif (/^TB:/){#line
 $tb=1;
 }elsif (/^TE:/){#line
 $te=1;
 $template = Text::Template->new(TYPE=> STRING ,SOURCE => "$template_str\n")
 or die "Couldn't construct template: $Text::Template::ERROR";
 }elsif ($tb==1 && $te==0 ) {
 $template_str.=$_;
 }else {

 if (defined $template) {
 chomp;
 @p=split /;/;
 #if ($p > 0) { print "$_\n";

 my %vars = (
 p1 => $p[0],
 p2 => $p[1],
 p3 => $p[2],
 p4 => $p[3],
 p5 => $p[4],
 p6 => $p[5],
 p7 => $p[6],
 p8 => $p[7],
 p9 => $p[8]
 );
 my $result = $template->fill_in(HASH => \%vars);
 if (defined $result) { print $result }
 #}
 } #template_file defined
 }#else of TF
}#end while

Comments[]

I have something similar here http://www.ophinity.com/papers/wrangling/index.html#multiply

written in python, so it's inherently superior.