3 # Script to make a LaTeX file for Lilypond
5 # Written by Jan Arne Fagertun <Jan.A.Fagertun@energy.sintef.no>
6 # Sat Nov 22 22:26:43 CET 1997
8 # Original LaTeX file made by Mats Bengtsson, 17/8 1997
13 IDENTIFICATION="$NAME $VERSION"
15 echo "$IDENTIFICATION" 1>&2
19 # should detect crashing lilypond
24 # - LILYINCLUDE update
27 # - handle LILYINCLUDE
31 # - Source rc-files, if present. Files are:
32 # /usr/local/share/lilyrc /etc/lilyrc $HOME/.lilyrc ./.lilyrc
33 # - tee output from Lilypond
34 # - Handles margins for A4 paper (equal on both sides)
35 # - new option -s (--separate) for one LaTeX run per file,
36 # else all files are run together
39 # - More useful ("two-level") debug.
40 # - The Q&D hack to find file names and not handling \include
41 # is replaced by grabbing output file names from Lilypond.
42 # = Detects multiple output files - adds them when running
44 # - Works with multiple input files - no matter if they are
45 # (a mix of) input to or output from Lilypond.
48 # - Still no margins handling.
49 # - We have to discuss how to handle multiple output files
50 # from Lilypond - 'ly2dvi standchen' gives a rather odd
54 # - Always exit after printing help info
56 # - Changes to ensure for more strict grep'ing of parameters
57 # Thanks to from G.B.Stott@bolton.ac.uk
58 # - More efficient use of sed -e 's///' -e 's///'
59 # Thanks to Johan Vromans <jvromans@squirrel.nl> and GBS
60 # - Ask tex for location of titledefs.tex (Thanks to JV)
61 # - Accept only exact match of "\def\mudelacomposer{"
62 # (or whatever mudela* defined in titledefs.tex)
63 # - Even more efficient use of sed (Thanks to JV)
64 # - Default file name for single output file implemented.
65 # - Moved help into function - finally included from 0.1.jcn1
68 # - Still doesn't handle \include
69 # - The Q&D for finding output file name from the sequence of
70 # \paper \output \midi \output really needs to be looked at.
71 # I have improved it a lot, but it's only capable of finding
72 # one (the last) file name.
73 # Well, I have to rewrite this entirely to handle \include,
75 # - Still no margins handling.
78 # - Some lines of output from lilypond do NOT start
79 # at first character position, therefore I have removed "^"
80 # in sed'ing and grep'ing.
83 # - add "Creator: " line to output
86 # - multiple input files to make score from several files
87 # (extra files assumed to be Lilypond output)
88 # - cp dvi-file instead of mv, to allow for xdvi reload
89 # - check for illegal long options
90 # - put in pt in text width, if not given
91 # - put in \nonstopmode in LaTeX file
92 # - restored LaTeX exit value check
95 # - temporarily omit LaTeX exit value check
96 # - remove ALL temporary files
99 # - fix for solaris - included from 0.1.jcn1
100 # - long option hack - included from 0.1.jcn1 - modified
101 # - moved help into function - NOT included from 0.1.jcn1 yet
102 # - various improvements - included from 0.1.jcn1
103 # - find mudela definitions from titledefs.tex
104 # - find papersize from lilypond output file (mudelapapersize),
105 # overridden by option '-p size' or '--papersize=size'
106 # - option -l lang or --language=lang overrides
107 # lilypond output file definition (mudelalanguage)
108 # - textwidth from lilypond output file (mudelapaperlinewidth)
113 # - moved help into function
120 Generate dvi file from mudela or lilypond output
121 Usage: $0 [options] file[s]
124 -D, --debug set debug mode
125 -o, --output set output directory
126 -h, --help this help text
127 -k, --keep keep LaTeX file
128 -l, --language= give LaTeX language (babel)
129 -p, --papersize= give LaTeX papersize (eg. a4paper)
130 -s, --separate run all files separately through LaTeX
132 files may be (a mix of) input to or output from lilypond(1)
139 for D in /usr/local/share/ /etc/ $HOME/. ./.
142 [ -f $RCfile ] && . $RCfile
146 # Keywords defined in titledefs.tex
148 TF=`kpsewhich -n tex tex titledefs.tex`
151 TF=/usr/lib/texmf/tex/lilypond/titledefs.tex
156 MU_DEF=`egrep "^.newcommand...mudela" $TF | \\
157 sed -e 's/^.newcommand...//' -e 's/\\}.*$//'`
162 MU_DEF="mudelatitle mudelasubtitle mudelacomposer \
163 mudelaarranger mudelainstrument"
171 # All files in one LaTeX run
175 # Find command line options and switches
177 # "x:" x takes argument
179 switches="Do:hkl:p:s\?"
182 # ugh, "\-" is a hack to support long options
183 # while getopts \-:$options$switches O
184 # must be in double quotes for bash-2.0
185 while getopts "\-:$options$switches" O
187 $debug_echo "O: \`$O'"
188 $debug_echo "arg: \`$OPTARG'"
191 if [ $debug_echo = echo ]
222 $debug_echo "long option: \`$OPTARG'"
225 if [ $debug_echo = echo ]
239 LNG=`echo $OPTARG | sed -e s/"^.*="//`
242 PSZ=`echo $OPTARG | sed -e s/"^.*="//`
251 echo $0": illegal option -- "$OPTARG;
258 shift `expr $OPTIND - 1`
265 $debug_echo $IDENTIFICATION": No input file name given"
273 if [ "$KEEP" != "Y" ]
281 $debug_echo $IDENTIFICATION": temporary directory "$TMP" not found, set to /tmp"
285 BN=`basename $File .tex`
289 BN=`basename $File .tex`
295 # paper size (PSZ, overridden by command line option -p)
296 # language (LNG, overridden by command line option -l)
300 -e 's/\\\\def\\\\mudelapapersize{\([^}]*\).*$/fPSZ=\1;/p' \\
301 -e 's/\\\\def\\\\mudelalanguage{\([^}]*\).*$/fLNG=\1;/p' \\
302 -e 's/\\\\def\\\\mudelapaperlinewidth{\([^}]*\).*$/TWN=\1;/p' \\
319 LLNG="\usepackage["$LNG"]{babel}"
341 $debug_echo "Text width = "$TW
343 TWp=`echo $TW | sed -e 's/\..*$//'`
344 PWp=600; # Width of A4 paper!
345 MARG=`expr $PWp - $TWp`
346 MARG=`expr $MARG / 2`"pt"
348 # Geometry: /var/lib/texmf/latex/geometry/geometry.dvi
354 % Creator: $IDENTIFICATION
355 % Automatically generated from $IF, $NOW
357 \documentclass$PAPER{article}
360 \usepackage{geometry}
361 \usepackage[T1]{fontenc}
362 %\addtolength{\oddsidemargin}{-1cm}
363 %\addtolength{\topmargin}{-1cm}
364 \setlength{\textwidth}{$TW}
365 \geometry{width=$TW, left=$MARG, top=1cm}
371 # Include \def\mudela-definitions
375 LL=`egrep '^\\\\def.'$L'{' $OF`
378 LLL=`echo $LL | sed -e 's/}.*$//' -e 's/.*{//'`
381 echo '\'$L'{'$LLL'}%' >> $LF
395 \vfill\hfill{\small\LilyIdString}
408 if [ x$OUTPUTDIR != x ]; then
409 RESULT="$OUTPUTDIR/$RESULT"
416 if [ "$KEEP" != "Y" ]
425 $IDENTIFICATION: dvi file name is $RESULT
430 # ugh. GF is side-effect.
432 # should check for LILYINCLUDE
433 for lypath in "." `echo $LILYINCLUDE| sed 's/:/ /g'`
435 if [ -f "$lypath/$1" ]
441 if [ -f "$lypath/$1.ly" ]
447 $debug_echo $IDENTIFICATION": Input file "$GF" not found"
448 echo $NAME": Input file "$GF" not found" 1>&2
452 # Loop through all files
459 # Check whether the file is input to or output from lilypond
462 OP=`echo $L1 | grep "^% Creator: GNU LilyPond"`
466 # OK - it's the output from lilypond.
468 # Get lilypond source file name
471 IFL=`grep mudelafilename $OF`
474 IF=`echo $IFL | sed -e 's/.*{//' -e 's/}*.$//'`
476 # Check if source file exists
480 $debug_echo $IDENTIFICATION": Mudela file not found."
484 $debug_echo $IDENTIFICATION": Mudela file name not found."
489 # I have to assume this is the lilypond input file
490 # Find output file name, if defined
495 # Grab output file names
497 $debug_echo "lilypond "$IF
499 lilypond $IF 2>&1 | tee /tmp/lilylog.$$
500 OF=`egrep '^TeX output to ' /tmp/lilylog.$$ | \\
501 sed -e 's/TeX output to//' -e 's/\.\.\.//'`
503 $debug_echo "==> "$OF
506 # "Spin through" all the files
510 $debug_echo "--- "$File
512 # Check if output file is generated
516 $debug_echo $IDENTIFICATION": hmm, I could not find the output file "$File
520 # Is this the first file?