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 # $Id: ly2dvi.sh,v 1.16 1998/05/08 21:18:34 janaf Exp $
10 # Original LaTeX file made by Mats Bengtsson, 17/8 1997
15 IDENTIFICATION="$NAME $VERSION"
17 echo "$IDENTIFICATION" 1>&2
20 # prevent orphaned "Lily is here" strings
25 # - -S/--sourcedir switch
30 # - mudelaDefs fixes, (thanks PC)
32 # - pass -I, --include to Lily
33 # - bf: split $LILYINCLUDE and try all dirs
34 # - bf: geometry left/top
37 # - fix backslash gobbling, reduce number of separate processes used
38 # - require readable rc files, allow rc file in /usr/local/share/lilypond/lilyrc
39 # - use latex path, not tex path
40 # - Don't override if kpsepath returns non-NULL
41 # - Don't clobber x.tex if it wasn't created by ly2dvi
45 # - find .fly files too
49 # - robustification: give \nonstopmode as LaTeX arg; no hanging if no TeX file.
50 # - robustification: notice failed cp.
53 # - more papersizes (thanks Han-Wen) - don't expect LaTeX to accept all...
54 # - -W,--Width= : set paper width (points)
55 # - -H,--Height= : set paper height (points)
56 # - -H and -W is used only when an unknown papersize is to be specified,
57 # and has to be combined with a papersize option known to LaTeX
58 #NB! - -F,--headers= : name of additional LaTeX headers input file.
59 #NB! Changed from -H to -F
60 # - -d,--dependencies : passed to lilypond
65 # - vertical margins (for a4), same on both sides
66 # - textheight from lilypond output file (mudelapapertextheight)
68 # - mudelapiece, mudelaopus
71 # - -K,--keeplilypond : Keep lilypond output files (default delete)
72 # - -k,--keeply2dvi : Keep ly2dvi output files (default delete)
73 # - -L,--landscape : Set landscape orientation
74 # - -N,--nonumber : Turn off page numbering (\pagestyle{empty})
75 # - Could not reinsert "\usepackage[T1]{fontenc}" because
76 # "los-toros" won't work with it
77 # - Ekstra LaTeX headers from input file
79 # - option to remove output of lily
81 # - Trap Lilypond abort
82 # - Replaced "\usepackage[T1]{fontenc}" with
83 # \usepackage[latin1]{inputenc} (takk, Mats)
84 # - Removed "()" around "\LilyIdString" (Janne didn't want it)
85 # 0.8 - Trap Lilypond segmentation fault
86 # - Function for cleanup
88 # - More human-readable variables
89 # - Some logics concerning rc-files
91 # - Improved Lilypond error checking
92 # - Output orientation (landscape...). Overrides mudela file
93 # variable orientation="landscape";
94 # - Paper width and height put into variables (only A4!)
95 # - Adjusted top margin to default.....
98 # - Include more papersizes.
99 # - Manual page.......
101 # - LILYINCLUDE update
104 # - handle LILYINCLUDE
108 # - Source rc-files, if present. Files are:
109 # /usr/local/share/lilyrc /etc/lilyrc $HOME/.lilyrc ./.lilyrc
110 # - tee output from Lilypond
111 # - Handles margins for A4 paper (equal on both sides)
112 # - new option -s (--separate) for one LaTeX run per file,
113 # else all files are run together
116 # - More useful ("two-level") debug.
117 # - The Q&D hack to find file names and not handling \include
118 # is replaced by grabbing output file names from Lilypond.
119 # = Detects multiple output files - adds them when running
121 # - Works with multiple input files - no matter if they are
122 # (a mix of) input to or output from Lilypond.
125 # - Still no margins handling.
126 # - We have to discuss how to handle multiple output files
127 # from Lilypond - 'ly2dvi standchen' gives a rather odd
131 # - Always exit after printing help info
133 # - Changes to ensure for more strict grep'ing of parameters
134 # Thanks to from G.B.Stott@bolton.ac.uk
135 # - More efficient use of sed -e 's///' -e 's///'
136 # Thanks to Johan Vromans <jvromans@squirrel.nl> and GBS
137 # - Ask tex for location of titledefs.tex (Thanks to JV)
138 # - Accept only exact match of "\def\mudelacomposer{"
139 # (or whatever mudela* defined in titledefs.tex)
140 # - Even more efficient use of sed (Thanks to JV)
141 # - Default file name for single output file implemented.
142 # - Moved help into function - finally included from 0.1.jcn1
145 # - Still doesn't handle \include
146 # - The Q&D for finding output file name from the sequence of
147 # \paper \output \midi \output really needs to be looked at.
148 # I have improved it a lot, but it's only capable of finding
149 # one (the last) file name.
150 # Well, I have to rewrite this entirely to handle \include,
152 # - Still no margins handling.
155 # - Some lines of output from lilypond do NOT start
156 # at first character position, therefore I have removed "^"
157 # in sed'ing and grep'ing.
160 # - add "Creator: " line to output
163 # - multiple input files to make score from several files
164 # (extra files assumed to be Lilypond output)
165 # - cp dvi-file instead of mv, to allow for xdvi reload
166 # - check for illegal long options
167 # - put in pt in text width, if not given
168 # - put in \nonstopmode in LaTeX file
169 # - restored LaTeX exit value check
172 # - temporarily omit LaTeX exit value check
173 # - remove ALL temporary files
176 # - fix for solaris - included from 0.1.jcn1
177 # - long option hack - included from 0.1.jcn1 - modified
178 # - moved help into function - NOT included from 0.1.jcn1 yet
179 # - various improvements - included from 0.1.jcn1
180 # - find mudela definitions from titledefs.tex
181 # - find papersize from lilypond output file (mudelapapersize),
182 # overridden by option '-p size' or '--papersize=size'
183 # - option -l lang or --language=lang overrides
184 # lilypond output file definition (mudelalanguage)
185 # - textwidth from lilypond output file (mudelapaperlinewidth)
190 # - moved help into function
196 if [ "$KEEP_LY2DVI_OUTPUT" != "Y" ]
198 [ -n "$LatF" -a -f "$LatF" ] && rm -f $LatF
199 [ -n "$LOGFILE" -a -f "$LOGFILE" ] && rm -f $LOGFILE
200 [ -n "$FN" ] && rm -f $FN.*
201 for F in *$$* $TMP/*$$*
206 if [ "$KEEP_LILY_OUTPUT" != "Y" ]
208 for F in $LILY_OUTPUT_FILES
210 [ -f $F ] && rm -f $F
220 Generate dvi file from mudela or lilypond output
221 Usage: $0 [OPTION]... [FILE]...
224 -D,--debug increase verbosity
225 -F,--headers= name of additional LaTeX headers file
226 -H,--Height= set paper height (points) (see manual page)
227 -I,--include=DIR add DIR to search path of LilyPond
228 -K,--keeplilypond keep lilypond output files
229 -L,--landscape set landscape orientation
230 -N,--nonumber switch off page numbering
231 -O,--orientation= set orientation (obsolete - use -L instead)
232 -W,--Width= set paper width (points) (see manual page)
233 -d,--dependencies tell lilypond make a dependencies file
234 -h,--help this help text
235 -k,--keeply2dvi keep ly2dvi output files
236 -l,--language= give LaTeX language (babel)
237 -o,--output= set output directory
238 -p,--papersize= give LaTeX papersize (eg. a4)
239 -s,--separate run all files separately through LaTeX
240 -S,--sourcedir set sourcedir
242 files may be (a mix of) input to or output from lilypond(1)
248 # SVr4 echo swallows backslashes, and there's no way to turn it off.
249 # Therefore use Echo whenever backslashes may be important.
250 # printf is in the posix.2 standard -- which means it's in
391 echo $0": unknown papersize -- "$PAPERSIZE
402 for D in /usr/local/share/ /usr/local/share/lilypond/ /etc/ $HOME/. ./.
405 [ -r $RCfile ] && . $RCfile
411 unset ORIENTATION LANGUAGE PAPERSIZE LATEXHF
417 # Trap function (cleanup)
429 [ -z "$TMP" ] && TMP=/tmp
432 $debug_echo $IDENTIFICATION": temporary directory "$TMP" not found, set to /tmp"
435 LOGFILE=$TMP/lilylog.$$ # Logfile for lilypond
436 PAPERSIZE=a4 # Default papersize name
437 PWIDTH=597 # Default paperwidth
438 PHEIGHT=845 # Default paperheight
439 PNUM="%" # Page numbering on
440 LILYOPTS="" # Options to lilypond
442 LILYPOND_EXECUTABLE=lilypond
447 # Keywords defined in titledefs.tex
449 TF="`kpsewhich -n latex tex titledefs.tex`"
451 if [ -r "${TF:=/usr/local/share/texmf/tex/lilypond/titledefs.tex}" ]
453 MU_DEF=`sed -n -e 's/.*newcommand\*{.\(mudela[^}]*\)}.*$/\1/p' "$TF"`
456 : "${MU_DEF:=mudelatitle mudelasubtitle mudelacomposer \
457 mudelameter mudelaopus mudelaarranger mudelapiece \
458 mudelapoet mudelainstrument \
466 # All files in one LaTeX run
470 # Find command line options and switches
472 # "x:" x takes argument
474 switches="DF:H:I:KLNO:W:dhkl:o:p:S:s\?"
477 # ugh, "\-" is a hack to support long options
478 # while getopts \-:$options$switches O
479 # must be in double quotes for bash-2.0
480 while getopts "\-:$options$switches" O
482 $debug_echo "O: \`$O'"
483 $debug_echo "arg: \`$OPTARG'"
486 [ $debug_echo = echo ] && set -x
496 LILYOPTS="$LILYOPTS -I $OPTARG"
497 LILYINCLUDE="$LILYINCLUDE:$OPTARG"
503 ORIENTATION=landscape
509 PNUM="\pagestyle{empty}"
515 LILYOPTS=$LILYOPTS" -d"
545 $debug_echo "long option: \`$OPTARG'"
548 PHEIGHT"`expr \"$OPTARG\" ':' '[^=]*=\(.*\)'`"=
551 PWIDTH="`expr \"$OPTARG\" ':' '[^=]*=\(.*\)'`"
554 LILYOPTS="$LILYOPTS -d"
557 [ $debug_echo = echo ] && set -x
561 LATEXHF="`expr \"$OPTARG\" ':' '[^=]*=\(.*\)'`"
568 dir="`expr \"$OPTARG\" ':' '[^=]*=\(.*\)'`"
569 LILYOPTS="$LILYOPTS --include=$dir"
570 LILYINCLUDE="$LILYINCLUDE:$dir"
579 ORIENTATION=landscape
582 LANGUAGE="`expr \"$OPTARG\" ':' '[^=]*=\(.*\)'`"
585 PNUM="\pagestyle{empty}"
588 ORIENTATION="`expr \"$OPTARG\" ':' '[^=]*=\(.*\)'`"
591 OUTPUTDIR="`expr \"$OPTARG\" ':' '[^=]*=\(.*\)'`"
594 PAPERSIZE="`expr \"$OPTARG\" ':' '[^=]*=\(.*\)'`"
599 sourcedir*|-sourcedir*)
603 echo "$0: illegal option -- \"$OPTARG\""
610 shift `expr $OPTIND - 1`
612 if [ "$SOURCEDIR" != "" ]; then
613 # apparently MakeTeXTFM can't handle relative dirs
614 SOURCEDIR=`cd $SOURCEDIR; pwd`
615 export LILYINCLUDE="$SOURCEDIR/init:$SOURCEDIR/mf/out:$LILYINCLUDE"
616 export TEXINPUTS="$SOURCEDIR/tex:$TEXINPUTS:"
617 export MFINPUTS="$SOURCEDIR/mf:$MFINPUTS:"
618 export LILYPOND_EXECUTABLE="$SOURCEDIR/lily/out/lilypond"
630 $debug_echo "$IDENTIFICATION: No input file name given"
633 # regexp_quote -- quote a string as a regular expression for egrep or sed
635 Echo "$@" | sed 's/\([]\[\.*?+^$]\)/\\\1/g'
640 # Include \def\mudela-definitions
644 for F in "$File" "$OF"
646 LL=`sed -n 's/\\\\def\\\\'"$L"'{\([^}]*\)}.*$/\1/p' "$F"`
648 Echo "$1\\"$L'{'"$LL"'}%' >> $LatF
659 BN=`basename "$File" .tex`
661 if [ "$KEEP_LY2DVI_OUTPUT" != "Y" ]
669 # paper size (PAPERSIZE, overridden by command line option -p)
670 # paper orientation (ORIENTATION, overridden by option -o)
671 # language (LANGUAGE, overridden by option -l)
675 -e 's/\\\\def\\\\mudelalanguage{\([^}]*\).*$/fLNG=\1;/p' \\
676 -e 's/\\\\def\\\\mudelalatexheaders{\([^}]*\).*$/fLHF=\1;/p' \\
677 -e 's/\\\\def\\\\mudelaorientation{\([^}]*\).*$/fORI=\1;/p' \\
678 -e 's/\\\\def\\\\mudelapaperlinewidth{\([^}]*\).*$/TWN=\1;/p' \\
679 -e 's/\\\\def\\\\mudelapapertextheight{\([^}]*\).*$/THN=\1;/p' \\
680 -e 's/\\\\def\\\\mudelapapersize{\([^}]*\).*$/fPSZ=\1;/p' \\
690 [ -f "$LATEXHF" ] && LLHF="\input{$LATEXHF}"
693 if [ -z "$PAPERSIZE" ]
697 if [ -n "$PAPERSIZE" ]
700 PAPEROPT="$PAPERSIZE"
703 if [ -z "$ORIENTATION" ]
707 if [ -n "$ORIENTATION" ]
709 if [ -z "$PAPEROPT" ]
711 PAPEROPT="$ORIENTATION"
713 PAPEROPT="$PAPEROPT,$ORIENTATION"
717 if [ -n "$PAPEROPT" ]
722 if [ -z "$LANGUAGE" ]
726 if [ -n "$LANGUAGE" ]
728 LLNG="\usepackage[$LANGUAGE]{babel}"
750 $debug_echo "Text width = $TW"
752 TWp=`Echo $TW | sed -e 's/\..*$//'`
771 $debug_echo "Text height = $TH"
773 THp="`echo $TH | sed -e 's/\..*$//'`"
775 if [ "$ORIENTATION" = "landscape" ]
780 HMARG="`expr '(' $PWp - $TWp ')' / 2`"pt
781 $debug_echo "Text left = $HMARG"
782 VMARG="`expr '(' $PHp - $THp ')' / 2`"pt
783 $debug_echo "Text top = $VMARG"
785 # Geometry: /var/lib/texmf/latex/geometry/geometry.dvi
791 % Creator: $IDENTIFICATION
792 % Automatically generated from $IF, $NOW
794 \documentclass$PAPER{article}
797 \usepackage{geometry}
798 \usepackage[latin1]{inputenc}
799 %\usepackage[T1]{fontenc}
801 %\addtolength{\oddsidemargin}{-1cm}
802 %\addtolength{\topmargin}{-1cm}
803 %\setlength{\textwidth}{$TW}
804 %\setlength{\textheight}{$TH}
805 \geometry{width=$TW, left=$HMARG, height=$TH, top=$VMARG}
826 \def\theopus{\mudelaopus}% ugh
827 \def\thepiece{\mudelapiece}%
836 \vfill\hfill{\LilyIdString}
842 latex '\nonstopmode \input '$LatF || exit 5
849 [ -n "$OUTPUTDIR" ] && RESULT="$OUTPUTDIR/$RESULT"
851 cp "$FN.dvi" "$RESULT" || exit 5
858 $IDENTIFICATION: dvi file name is $RESULT
863 # ugh. GF is side-effect.
865 # should check for LILYINCLUDE
868 x=`echo $LILYINCLUDE | sed "s!:! !g"`
872 if [ -f "$lypath/$1" ]
878 if [ -f "$lypath/$1.ly" ]
883 if [ -f "$lypath/$1.fly" ]
889 $debug_echo "$IDENTIFICATION: Input file \"$GF\" not found"
890 echo "$NAME: Input file \"$GF\" not found" 1>&2
894 # Loop through all files
904 # Check whether the file is input to or output from lilypond
907 OP=`echo $L1 | grep "^% Creator: GNU LilyPond"`
911 # OK - it's the output from lilypond.
913 # Get lilypond source file name
916 IFL=`grep mudelafilename $OF`
919 IF=`echo $IFL | sed -e 's/.*{//' -e 's/}*.$//'`
921 # Check if source file exists
925 $debug_echo $IDENTIFICATION": Mudela file not found."
930 $debug_echo $IDENTIFICATION": Mudela file name not found."
936 # I have to assume this is the lilypond input file
937 # Find output file name, if defined
942 # Grab output file names
944 $debug_echo "lilypond "$LILYOPTS $IF
946 $LILYPOND_EXECUTABLE $LILYOPTS $IF 2>&1 | tee $LOGFILE
947 OF="`sed -n -e 's/TeX output to \([^\.]*\.tex\)\.\.\.$/\1/p' $LOGFILE`"
948 $debug_echo "==> $OF"
949 LILY_OUTPUT_FILES="$LILY_OUTPUT_FILES $OF"
950 STATUS=`egrep -i "error|segmentation|abort" $LOGFILE`
952 if [ ! -z "$STATUS" ]
958 # "Spin through" all the files
962 $debug_echo "--- "$File
964 # Check if output file is generated
968 $debug_echo "$IDENTIFICATION: hmm, I could not find the output file $File"
972 # Is this the first file?