#   PreProcessor 0.1
#   by Csaba Peterfalvi
#   peterfalvi.web.elte.hu
#   08-08-2014

#   Enables Forward Search from WinEdt or TeXnicCenter to YAP with the General File Preprocessor 'preprocessor.pl'


@parts = split(/\\/,$0);
$temp=$0 . 'preprocessor_subs.pl';
$temp=~s/$parts[-2]\\$parts[-1]//;
require $temp;


$tex = shift @ARGV;
$dvi = shift @ARGV;
$CommentChar = shift @ARGV;
$JobFile = shift @ARGV;
$JobFile=~s/\//\\/g;
print "$tex\n";

open($in, '<', $JobFile) or goto NOPP;

@parts = split(/\\/,$JobFile);
$Path=$JobFile;
$Path=~s/\\$parts[-1]//;

@jobs=();
while(<$in>){
	chomp($_);
	if(m/^$CommentChar#WRITEPP: *"(.+)"/){
		$temp=subs($1,$Path);
		push(@jobs,"$temp");};}
close($in);

foreach $job(@jobs){
	@parts = split(/\\/,$job);
	$job=$parts[-1];
	$job=~s/(\.\w+)$//;
	$jobpp=$job . '_pp';
	$tex=~s/$job/$jobpp/;}

NOPP:
$comm = 'yap -1 -s "' . $tex . '" "' . $dvi . '"';
system($comm);
