diff options
author | John Ankarström <john@ankarstrom.se> | 2021-06-17 20:14:53 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2021-06-17 20:14:53 +0200 |
commit | 5b4358819a1af8dec70722491d32c8863ba65423 (patch) | |
tree | 4a71983f0bf14dbb482181e0822c8b6fcca89de1 | |
parent | 21be8e78d7a629f57f28620dc4445bc230818ef4 (diff) | |
download | xutil-5b4358819a1af8dec70722491d32c8863ba65423.tar.gz |
wpdf: Rewrite in Perl
-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; |