| 1 |
eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}' && eval 'exec perl -S $0 $argv:q' |
| 2 |
if 0; |
| 3 |
use strict; |
| 4 |
# |
| 5 |
# vpe.pl |
| 6 |
# |
| 7 |
# Copyright (C) 2000, 2012 Heiko Oberdiek. |
| 8 |
# |
| 9 |
# This program may be distributed and/or modified under the |
| 10 |
# conditions of the LaTeX Project Public License, either version 1.2 |
| 11 |
# of this license or (at your option) any later version. |
| 12 |
# The latest version of this license is in |
| 13 |
# http://www.latex-project.org/lppl.txt |
| 14 |
# and version 1.2 or later is part of all distributions of LaTeX |
| 15 |
# version 1999/12/01 or later. |
| 16 |
# |
| 17 |
# See file "vpe.txt" for a list of files that belong to this project. |
| 18 |
# |
| 19 |
# This file "vpe.pl" may be renamed to "vpe" |
| 20 |
# for installation purposes. |
| 21 |
# |
| 22 |
my $prj = 'vpe'; |
| 23 |
my $file = "$prj.pl"; |
| 24 |
my $program = uc($&) if $file =~ /^\w+/; |
| 25 |
my $version = "0.2"; |
| 26 |
my $date = "2012/04/18"; |
| 27 |
my $author = "Heiko Oberdiek"; |
| 28 |
my $copyright = "Copyright (c) 2000, 2012 by $author."; |
| 29 |
# |
| 30 |
# History: |
| 31 |
# 2000/09/15 v0.1: First release. |
| 32 |
# 2012/04/18 v0.2: Option --version added. |
| 33 |
# |
| 34 |
|
| 35 |
### program identification |
| 36 |
my $title = "$program $version, $date - $copyright\n"; |
| 37 |
|
| 38 |
### editor call |
| 39 |
# %F: file name |
| 40 |
# %L: line number |
| 41 |
my $EditorCall = "xterm -e joe +%d %s"; |
| 42 |
$EditorCall = $ENV{TEXEDIT} if $ENV{TEXEDIT}; |
| 43 |
$EditorCall = $ENV{VPE} if $ENV{VPE}; |
| 44 |
|
| 45 |
### error strings |
| 46 |
my $Error = "!!! Error:"; # error prefix |
| 47 |
|
| 48 |
my $usage = <<"END_OF_USAGE"; |
| 49 |
${title} |
| 50 |
Depending on the name of the script `vpe' works in four modes: |
| 51 |
|
| 52 |
* [vpe] Syntax: vpe <pdf file>[.pdf] |
| 53 |
The pdf file is scanned for actions created by VTeX, |
| 54 |
that start an editor with the source file at the |
| 55 |
specified line under Windows. |
| 56 |
A symbol link is made from the source file name |
| 57 |
extended by the extension `.vpe' to this script. |
| 58 |
The line number is encoded in the path and the |
| 59 |
action is changed to start this script. |
| 60 |
|
| 61 |
* [sty] Internal for vpe.sty: |
| 62 |
Syntax: vpe --sty [--progname=...] <latex file> <vpe file> |
| 63 |
|
| 64 |
* [system] Internal for vpe.sty: |
| 65 |
Syntax: vpe --system <vpe file> |
| 66 |
|
| 67 |
* [launch] Syntax: <source file name>.vpe |
| 68 |
The script decodes the line number in the path of |
| 69 |
the source file name and starts an editor with |
| 70 |
this file at that line number. |
| 71 |
|
| 72 |
Options: |
| 73 |
--help: print usage |
| 74 |
--version print version number |
| 75 |
--verbose: print additional informations during running |
| 76 |
--force: force symbol links |
| 77 |
--delete: delete symbol links |
| 78 |
--sty: internal for `vpe.sty' (get absolute file name and lines) |
| 79 |
--system: internal for `vpe.sty' (get system info linux or win) |
| 80 |
--progname: latex, pdflatex, elatex, pdfelatex |
| 81 |
END_OF_USAGE |
| 82 |
|
| 83 |
### options |
| 84 |
$::opt_verbose = 0; |
| 85 |
$::opt_help = 0; |
| 86 |
$::opt_version = 0; |
| 87 |
$::opt_force = 0; |
| 88 |
$::opt_delete = 0; |
| 89 |
$::opt_sty = 0; |
| 90 |
$::opt_system = 0; |
| 91 |
$::opt_progname = "latex"; |
| 92 |
use Getopt::Long; |
| 93 |
GetOptions( |
| 94 |
"help!", |
| 95 |
"version!", |
| 96 |
"verbose!", |
| 97 |
"force!", |
| 98 |
"delete!", |
| 99 |
"sty!", |
| 100 |
"system!", |
| 101 |
"progname=s", |
| 102 |
) or die $usage; |
| 103 |
if ($::opt_help) { |
| 104 |
die $usage; |
| 105 |
} |
| 106 |
if ($::opt_version) { |
| 107 |
print "$prj $date v$version\n"; |
| 108 |
exit(0); |
| 109 |
} |
| 110 |
|
| 111 |
if ($::opt_sty and $::opt_system) { |
| 112 |
die "$usage" . |
| 113 |
"$Error Options --sty and --system cannot used together!\n"; |
| 114 |
} |
| 115 |
|
| 116 |
################### |
| 117 |
### launch mode ### |
| 118 |
################### |
| 119 |
if ($0 =~ /\.vpe/) { |
| 120 |
|
| 121 |
@ARGV == 0 or |
| 122 |
die "$usage$Error Too many arguments [launch mode]!\n"; |
| 123 |
|
| 124 |
my $file = ""; |
| 125 |
$0 =~ m|(^[\./]+\./)(.+)\.vpe$| or |
| 126 |
die "$Error Cannot extract line number ($0)!\n"; |
| 127 |
my $str = $1; |
| 128 |
$file = "/$2"; |
| 129 |
|
| 130 |
my $line = ""; |
| 131 |
while ($str ne "") { |
| 132 |
$str =~ m|^(/*)(\.?)\./(.*)$| or |
| 133 |
die "$Error Parse error!\n"; |
| 134 |
$line .= length($1) + (($2 eq ".") ? 5 : 0); |
| 135 |
$str = $3; |
| 136 |
} |
| 137 |
|
| 138 |
my $callstr = $EditorCall; |
| 139 |
$EditorCall =~ s/%s/$file/; |
| 140 |
$EditorCall =~ s/%d/$line/; |
| 141 |
print "File: $file, line: $line\n" if $::opt_verbose; |
| 142 |
exec($EditorCall); |
| 143 |
exit 1; |
| 144 |
} |
| 145 |
|
| 146 |
### |
| 147 |
### used by both sty and system mode: |
| 148 |
### |
| 149 |
my $system = "linux"; |
| 150 |
$system = "" if $^O =~ /os2/i; |
| 151 |
$system = "" if $^O =~ /mac/i; |
| 152 |
$system = "win" if $^O =~ /dos/i; |
| 153 |
$system = "win" if $^O =~ /win/i; |
| 154 |
|
| 155 |
################ |
| 156 |
### sty mode ### |
| 157 |
################ |
| 158 |
if ($::opt_sty) { |
| 159 |
|
| 160 |
@ARGV == 2 or |
| 161 |
die "$usage$Error Wrong arguments [sty mode]!\n"; |
| 162 |
|
| 163 |
my $vpefile = $ARGV[1]; |
| 164 |
print "VPE file: $vpefile\n" if $::opt_verbose; |
| 165 |
|
| 166 |
my $file = `kpsewhich -progname=$::opt_progname $ARGV[0]`; |
| 167 |
chomp $file; |
| 168 |
if (!($file =~ m|^/| or $file =~ m|^\w:|)) { |
| 169 |
use Cwd; |
| 170 |
$file = cwd() . "/" . $file; |
| 171 |
$file =~ s|/[^/]+/\.\./|/|g; |
| 172 |
$file =~ s|/\./|/|g; |
| 173 |
} |
| 174 |
-f $file or |
| 175 |
die "$Error Cannot find file `$file' [sty mode]!\n"; |
| 176 |
print "File: $file\n" if $::opt_verbose; |
| 177 |
|
| 178 |
my $last = 0; |
| 179 |
if (open(IN, $file)) { |
| 180 |
while (<IN>) { |
| 181 |
$last++; |
| 182 |
} |
| 183 |
} |
| 184 |
if ($last == 0) { |
| 185 |
$last = 10000; |
| 186 |
} |
| 187 |
print "Last line: $last\n" if $::opt_verbose; |
| 188 |
|
| 189 |
open(OUT, ">>$vpefile") or |
| 190 |
die "$Error Cannot open file `$vpefile`!\n"; |
| 191 |
print OUT "\\vpeentry{$file}{$last}\n"; |
| 192 |
|
| 193 |
exit 1; |
| 194 |
} |
| 195 |
|
| 196 |
################### |
| 197 |
### system mode ### |
| 198 |
################### |
| 199 |
if ($::opt_system) { |
| 200 |
|
| 201 |
@ARGV == 1 or |
| 202 |
die "$usage$Error Wrong arguments [system mode]!\n"; |
| 203 |
|
| 204 |
my $vpefile = $ARGV[0]; |
| 205 |
print "VPE file: $vpefile\n" if $::opt_verbose; |
| 206 |
|
| 207 |
open(OUT, ">>$vpefile") or |
| 208 |
die "$Error Cannot open file `$vpefile`!\n"; |
| 209 |
print OUT "\\vpesystem{$system}\n"; |
| 210 |
|
| 211 |
exit 1; |
| 212 |
} |
| 213 |
|
| 214 |
################ |
| 215 |
### vpe mode ### |
| 216 |
################ |
| 217 |
if (@ARGV < 1) { |
| 218 |
die "$usage$Error Missing pdf file [vpe mode]!\n"; |
| 219 |
} |
| 220 |
if (@ARGV > 2) { |
| 221 |
die "$usage$Error Too many arguments [vpe mode]!\n"; |
| 222 |
} |
| 223 |
|
| 224 |
my $pdffile = $ARGV[0]; |
| 225 |
if (!-f $pdffile) { |
| 226 |
my $name = $pdffile; |
| 227 |
$pdffile .= ".pdf"; |
| 228 |
-f $pdffile or |
| 229 |
die "$Error File `$name' not found [vpe mode]!\n"; |
| 230 |
} |
| 231 |
|
| 232 |
open(IN, "+<$pdffile") or |
| 233 |
die "$Error Cannot open `$pdffile' [vpe mode]!\n"; |
| 234 |
binmode(IN); |
| 235 |
my %symlinks = (); |
| 236 |
while (<IN>) { |
| 237 |
|
| 238 |
if (m|/F\([\./]*(/.*)\.vpe\)|) { |
| 239 |
$symlinks{$1} = 1; |
| 240 |
next; |
| 241 |
} |
| 242 |
|
| 243 |
my $do = 0; |
| 244 |
my ($action, $file, $line, $type); |
| 245 |
# my $color; # only for debugging |
| 246 |
|
| 247 |
if (m|^ |
| 248 |
/A \s* << \s* |
| 249 |
/Type \s* /Action \s* |
| 250 |
/S \s* /Launch \s* |
| 251 |
/Win \s* << \s* |
| 252 |
/F \s* \(aftcomp.exe\) \s* |
| 253 |
/P \s* \("(.*)\" \s+ \d+\-(\d+)\) \s* |
| 254 |
>> \s* |
| 255 |
>> \s* |
| 256 |
$ |
| 257 |
|x |
| 258 |
) { |
| 259 |
$action = $_; |
| 260 |
$file = $1; |
| 261 |
$line = $2; |
| 262 |
# $color = "/C[0 0 1]"; # only for debugging |
| 263 |
$type = "aftcomp"; |
| 264 |
$do = 1; |
| 265 |
} |
| 266 |
|
| 267 |
if (m|^ |
| 268 |
/A \s* << \s* |
| 269 |
/Type \s* /Action \s* |
| 270 |
/S \s* /Launch \s* |
| 271 |
/Win \s* << \s* |
| 272 |
/F \s* \(repos.exe\) \s* |
| 273 |
/P \s* \( |
| 274 |
"(.*)" \s* |
| 275 |
"(\d+)" \s* |
| 276 |
"(.*)" \s* |
| 277 |
"(\d+)" |
| 278 |
\) \s* |
| 279 |
>> \s* |
| 280 |
>> \s* |
| 281 |
$ |
| 282 |
|x |
| 283 |
) { |
| 284 |
$action = $_; |
| 285 |
if ($3 eq "") { |
| 286 |
$file = $1; |
| 287 |
$line = $2; |
| 288 |
} |
| 289 |
else { |
| 290 |
# ??? |
| 291 |
$file = $1; |
| 292 |
$line = $2; |
| 293 |
} |
| 294 |
# $color = "/C[1 0 0]"; # only for debugging |
| 295 |
$type = "repos"; |
| 296 |
$do = 1; |
| 297 |
} |
| 298 |
|
| 299 |
if ($do) { |
| 300 |
my $length = length($action); |
| 301 |
print "* File: $file, line: $line, type: $type\n" if $::opt_verbose; |
| 302 |
|
| 303 |
if (!($file =~ m|^/|)) { |
| 304 |
print STDERR "$Error File `$file' lacks of absolute path!\n"; |
| 305 |
next; |
| 306 |
} |
| 307 |
|
| 308 |
if (!$::opt_delete) { |
| 309 |
if ($line <= 0) { |
| 310 |
$line = 1; |
| 311 |
} |
| 312 |
|
| 313 |
my $newaction = ""; |
| 314 |
{ |
| 315 |
my $digit = substr($line, 0, 1); |
| 316 |
if ($digit <= 5) { |
| 317 |
$newaction .= "/" x $digit; |
| 318 |
} |
| 319 |
else { |
| 320 |
$newaction .= "/" x ($digit - 5) . "."; |
| 321 |
} |
| 322 |
$newaction .= "./"; |
| 323 |
my $rest = $line; |
| 324 |
while (($rest = substr($rest, 1)) ne "") { |
| 325 |
$digit = substr($rest, 0, 1); |
| 326 |
if ($digit < 5) { |
| 327 |
$newaction .= "/" x $digit; |
| 328 |
} |
| 329 |
else { |
| 330 |
$newaction .= "/" x ($digit - 5) . "."; |
| 331 |
} |
| 332 |
$newaction .= "./"; |
| 333 |
} |
| 334 |
} |
| 335 |
$newaction .= substr($file, 1) . ".vpe"; |
| 336 |
$newaction = # $color . # only for debugging |
| 337 |
"/A<</Type/Action/S/Launch/F($newaction)>>"; |
| 338 |
$newaction .= " " x ($length - length($newaction) - 1); |
| 339 |
if (length($newaction) > $length) { |
| 340 |
print STDERR "$Error Action too long!\n"; |
| 341 |
next; |
| 342 |
} |
| 343 |
|
| 344 |
seek(IN, -length($action), 1); |
| 345 |
print IN $newaction; |
| 346 |
} |
| 347 |
|
| 348 |
$symlinks{$file} = 1; |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
if (keys(%symlinks)) { |
| 353 |
|
| 354 |
my $this = $0; |
| 355 |
if (!$::opt_delete) { |
| 356 |
if (!-f $0 or !-x $0) { |
| 357 |
$this = `which $0`; |
| 358 |
($this ne "") or die "$Error Cannot find this script!\n"; |
| 359 |
} |
| 360 |
if (!($this =~ m|^/|)) { |
| 361 |
use Cwd; |
| 362 |
$this = cwd() . "/" . $this; |
| 363 |
} |
| 364 |
$this =~ s|/[^/]+/\.\./|/|g; |
| 365 |
$this =~ s|/\./|/|g; |
| 366 |
} |
| 367 |
|
| 368 |
if ($::opt_delete) { |
| 369 |
print "Delete symlinks:\n"; |
| 370 |
} |
| 371 |
else { |
| 372 |
if ($::opt_force) { |
| 373 |
print "Forced symlinks to $this:\n"; |
| 374 |
} |
| 375 |
else { |
| 376 |
print "Symlinks to $this:\n"; |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
foreach (keys(%symlinks)) { |
| 381 |
my $sym = $_ . ".vpe"; |
| 382 |
print " $sym ["; |
| 383 |
|
| 384 |
if ($::opt_delete) { |
| 385 |
if (!-l $sym) { |
| 386 |
print "ok, not existing]\n"; |
| 387 |
next; |
| 388 |
} |
| 389 |
unlink($sym); |
| 390 |
if (!-l $sym) { |
| 391 |
print "ok, deleted]\n"; |
| 392 |
next; |
| 393 |
} |
| 394 |
print "failed]\n"; |
| 395 |
next; |
| 396 |
} |
| 397 |
if ($::opt_force) { |
| 398 |
if (-l $sym) { |
| 399 |
unlink($sym); |
| 400 |
if (-l $sym) { |
| 401 |
print "deletion failed]\n"; |
| 402 |
next; |
| 403 |
} |
| 404 |
if (symlink($this, $sym)) { |
| 405 |
print "ok, deleted and created]\n"; |
| 406 |
next; |
| 407 |
} |
| 408 |
print "deleted, creation failed]\n"; |
| 409 |
next; |
| 410 |
} |
| 411 |
if (symlink($this, $sym)) { |
| 412 |
print "ok, created]\n"; |
| 413 |
next; |
| 414 |
} |
| 415 |
print "creation failed]\n"; |
| 416 |
next; |
| 417 |
} |
| 418 |
if (-f $sym) { |
| 419 |
print "exists]\n"; |
| 420 |
next; |
| 421 |
} |
| 422 |
if (symlink($this, $sym)) { |
| 423 |
print "ok, created]\n"; |
| 424 |
next; |
| 425 |
} |
| 426 |
print "failed]\n"; |
| 427 |
next; |
| 428 |
} |
| 429 |
} |
| 430 |
__END__ |