diff options
-rwxr-xr-x | wpdf | 52 |
1 files changed, 32 insertions, 20 deletions
@@ -1,25 +1,37 @@ -#!/bin/sh +#!/usr/bin/perl -# wpdf -- view, watch and remake pdf +# wpdf -- view, watch and remake pdf based on Makefile -IFS=' -' +use strict; +use warnings; -[ -z "$*" ] && { echo usage: $0 source-file ... 1>&2; exit 1; } +my %deps; -success= -for source in "$@"; do - case $source in - *.pdf) echo skipping $source: already a pdf 1>&2 ;; - *) success=1 - xpdf -remote wpdf-$source ${source%.*}.pdf & ;; - esac -done +# collect dependencies for given pdf files +for (@ARGV) { + if (/\.pdf$/) { + (my $name = $_) =~ s/\.pdf$//; + open my $f, '<', 'Makefile' + or die "could not open Makefile: $!\n"; + while (<$f>) { + if (/^\Q$name.pdf\E:\s*(.*)/) { + $deps{$_} = $name for split /\s/, $1; + last; + } + } + close $f; + exec('xpdf', '-remote', "wpdf-$name", "$name.pdf") if fork == 0; + } else { + warn "skipping $_: not a pdf\n"; + } +} -[ -z "$success" ] && exit 1 -xdotool search --sync --onlyvisible --class xpdf 1>&- -sleep 0.2 || sleep 1 - -watch -i "$@" | while read source; do - make ${source%.*}.pdf && xpdf -remote wpdf-$source -reload -done +# make pdf on demand +open my $p, '-|', 'watch', keys %deps + or die "could not start watch: $!\n"; +while (<$p>) { + chomp; + system('make', "$deps{$_}.pdf") == 0 + && system('xpdf', '-remote', "wpdf-$deps{$_}", '-reload'); +} +close $p; |