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 # - mudelaDefs really fixed (sorry, PC)
28 # - -S/--sourcedir switch
32 # - mudelaDefs fixes, (thanks PC)
34 # - pass -I, --include to Lily
35 # - bf: split $LILYINCLUDE and try all dirs
36 # - bf: geometry left/top
39 # - fix backslash gobbling, reduce number of separate processes used
40 # - require readable rc files, allow rc file in /usr/local/share/lilypond/lilyrc
41 # - use latex path, not tex path
42 # - Don't override if kpsepath returns non-NULL
43 # - Don't clobber x.tex if it wasn't created by ly2dvi
47 # - find .fly files too
51 # - robustification: give \nonstopmode as LaTeX arg; no hanging if no TeX file.
52 # - robustification: notice failed cp.
55 # - more papersizes (thanks Han-Wen) - don't expect LaTeX to accept all...
56 # - -W,--Width= : set paper width (points)
57 # - -H,--Height= : set paper height (points)
58 # - -H and -W is used only when an unknown papersize is to be specified,
59 # and has to be combined with a papersize option known to LaTeX
60 #NB! - -F,--headers= : name of additional LaTeX headers input file.
61 #NB! Changed from -H to -F
62 # - -d,--dependencies : passed to lilypond
67 # - vertical margins (for a4), same on both sides
68 # - textheight from lilypond output file (mudelapapertextheight)
70 # - mudelapiece, mudelaopus
73 # - -K,--keeplilypond : Keep lilypond output files (default delete)
74 # - -k,--keeply2dvi : Keep ly2dvi output files (default delete)
75 # - -L,--landscape : Set landscape orientation
76 # - -N,--nonumber : Turn off page numbering (\pagestyle{empty})
77 # - Could not reinsert "\usepackage[T1]{fontenc}" because
78 # "los-toros" won't work with it
79 # - Ekstra LaTeX headers from input file
81 # - option to remove output of lily
83 # - Trap Lilypond abort
84 # - Replaced "\usepackage[T1]{fontenc}" with
85 # \usepackage[latin1]{inputenc} (takk, Mats)
86 # - Removed "()" around "\LilyIdString" (Janne didn't want it)
87 # 0.8 - Trap Lilypond segmentation fault
88 # - Function for cleanup
90 # - More human-readable variables
91 # - Some logics concerning rc-files
93 # - Improved Lilypond error checking
94 # - Output orientation (landscape...). Overrides mudela file
95 # variable orientation="landscape";
96 # - Paper width and height put into variables (only A4!)
97 # - Adjusted top margin to default.....
100 # - Include more papersizes.
101 # - Manual page.......
103 # - LILYINCLUDE update
106 # - handle LILYINCLUDE
110 # - Source rc-files, if present. Files are:
111 # /usr/local/share/lilyrc /etc/lilyrc $HOME/.lilyrc ./.lilyrc
112 # - tee output from Lilypond
113 # - Handles margins for A4 paper (equal on both sides)
114 # - new option -s (--separate) for one LaTeX run per file,
115 # else all files are run together
118 # - More useful ("two-level") debug.
119 # - The Q&D hack to find file names and not handling \include
120 # is replaced by grabbing output file names from Lilypond.
121 # = Detects multiple output files - adds them when running
123 # - Works with multiple input files - no matter if they are
124 # (a mix of) input to or output from Lilypond.
127 # - Still no margins handling.
128 # - We have to discuss how to handle multiple output files
129 # from Lilypond - 'ly2dvi standchen' gives a rather odd
133 # - Always exit after printing help info
135 # - Changes to ensure for more strict grep'ing of parameters
136 # Thanks to from G.B.Stott@bolton.ac.uk
137 # - More efficient use of sed -e 's///' -e 's///'
138 # Thanks to Johan Vromans <jvromans@squirrel.nl> and GBS
139 # - Ask tex for location of titledefs.tex (Thanks to JV)
140 # - Accept only exact match of "\def\mudelacomposer{"
141 # (or whatever mudela* defined in titledefs.tex)
142 # - Even more efficient use of sed (Thanks to JV)
143 # - Default file name for single output file implemented.
144 # - Moved help into function - finally included from 0.1.jcn1
147 # - Still doesn't handle \include
148 # - The Q&D for finding output file name from the sequence of
149 # \paper \output \midi \output really needs to be looked at.
150 # I have improved it a lot, but it's only capable of finding
151 # one (the last) file name.
152 # Well, I have to rewrite this entirely to handle \include,
154 # - Still no margins handling.
157 # - Some lines of output from lilypond do NOT start
158 # at first character position, therefore I have removed "^"
159 # in sed'ing and grep'ing.
162 # - add "Creator: " line to output
165 # - multiple input files to make score from several files
166 # (extra files assumed to be Lilypond output)
167 # - cp dvi-file instead of mv, to allow for xdvi reload
168 # - check for illegal long options
169 # - put in pt in text width, if not given
170 # - put in \nonstopmode in LaTeX file
171 # - restored LaTeX exit value check
174 # - temporarily omit LaTeX exit value check
175 # - remove ALL temporary files
178 # - fix for solaris - included from 0.1.jcn1
179 # - long option hack - included from 0.1.jcn1 - modified
180 # - moved help into function - NOT included from 0.1.jcn1 yet
181 # - various improvements - included from 0.1.jcn1
182 # - find mudela definitions from titledefs.tex
183 # - find papersize from lilypond output file (mudelapapersize),
184 # overridden by option '-p size' or '--papersize=size'
185 # - option -l lang or --language=lang overrides
186 # lilypond output file definition (mudelalanguage)
187 # - textwidth from lilypond output file (mudelapaperlinewidth)
192 # - moved help into function
198 if [ "$KEEP_LY2DVI_OUTPUT" != "Y" ]
200 [ -n "$LatF" -a -f "$LatF" ] && rm -f $LatF
201 [ -n "$LOGFILE" -a -f "$LOGFILE" ] && rm -f $LOGFILE
202 [ -n "$FN" ] && rm -f $FN.*
203 for F in *$$* $TMP/*$$*
208 if [ "$KEEP_LILY_OUTPUT" != "Y" ]
210 for F in $LILY_OUTPUT_FILES
212 [ -f $F ] && rm -f $F
222 Generate dvi file from mudela or lilypond output
223 Usage: $0 [OPTION]... [FILE]...
226 -D,--debug increase verbosity
227 -F,--headers= name of additional LaTeX headers file
228 -H,--Height= set paper height (points) (see manual page)
229 -I,--include=DIR add DIR to search path of LilyPond
230 -K,--keeplilypond keep lilypond output files
231 -L,--landscape set landscape orientation
232 -N,--nonumber switch off page numbering
233 -O,--orientation= set orientation (obsolete - use -L instead)
234 -W,--Width= set paper width (points) (see manual page)
235 -d,--dependencies tell lilypond make a dependencies file
236 -h,--help this help text
237 -k,--keeply2dvi keep ly2dvi output files
238 -l,--language= give LaTeX language (babel)
239 -o,--output= set output directory
240 -p,--papersize= give LaTeX papersize (eg. a4)
241 -s,--separate run all files separately through LaTeX
242 -S,--sourcedir set sourcedir
244 files may be (a mix of) input to or output from lilypond(1)
250 # SVr4 echo swallows backslashes, and there's no way to turn it off.
251 # Therefore use Echo whenever backslashes may be important.
252 # printf is in the posix.2 standard -- which means it's in
393 echo $0": unknown papersize -- "$PAPERSIZE
404 for D in /usr/local/share/ /usr/local/share/lilypond/ /etc/ $HOME/. ./.
407 [ -r $RCfile ] && . $RCfile
413 unset ORIENTATION LANGUAGE PAPERSIZE LATEXHF
419 # Trap function (cleanup)
431 [ -z "$TMP" ] && TMP=/tmp
434 $debug_echo $IDENTIFICATION": temporary directory "$TMP" not found, set to /tmp"
437 LOGFILE=$TMP/lilylog.$$ # Logfile for lilypond
438 PAPERSIZE=a4 # Default papersize name
439 PWIDTH=597 # Default paperwidth
440 PHEIGHT=845 # Default paperheight
441 PNUM="%" # Page numbering on
442 LILYOPTS="" # Options to lilypond
444 LILYPOND_EXECUTABLE=lilypond
449 # Keywords defined in titledefs.tex
451 TF="`kpsewhich -n latex tex titledefs.tex`"
453 if [ -r "${TF:=/usr/local/share/texmf/tex/lilypond/titledefs.tex}" ]
455 MU_DEF=`sed -n -e 's/.*newcommand\*{.\(mudela[^}]*\)}.*$/\1/p' "$TF"`
458 : "${MU_DEF:=mudelatitle mudelasubtitle mudelacomposer \
459 mudelameter mudelaopus mudelaarranger mudelapiece \
460 mudelapoet mudelainstrument \
468 # All files in one LaTeX run
472 # Find command line options and switches
474 # "x:" x takes argument
476 switches="DF:H:I:KLNO:W:dhkl:o:p:S:s\?"
479 # ugh, "\-" is a hack to support long options
480 # while getopts \-:$options$switches O
481 # must be in double quotes for bash-2.0
482 while getopts "\-:$options$switches" O
484 $debug_echo "O: \`$O'"
485 $debug_echo "arg: \`$OPTARG'"
488 [ $debug_echo = echo ] && set -x
498 LILYOPTS="$LILYOPTS -I $OPTARG"
499 LILYINCLUDE="$LILYINCLUDE:$OPTARG"
505 ORIENTATION=landscape
511 PNUM="\pagestyle{empty}"
517 LILYOPTS=$LILYOPTS" -d"
547 $debug_echo "long option: \`$OPTARG'"
550 PHEIGHT"`expr \"$OPTARG\" ':' '[^=]*=\(.*\)'`"=
553 PWIDTH="`expr \"$OPTARG\" ':' '[^=]*=\(.*\)'`"
556 LILYOPTS="$LILYOPTS -d"
559 [ $debug_echo = echo ] && set -x
563 LATEXHF="`expr \"$OPTARG\" ':' '[^=]*=\(.*\)'`"
570 dir="`expr \"$OPTARG\" ':' '[^=]*=\(.*\)'`"
571 LILYOPTS="$LILYOPTS --include=$dir"
572 LILYINCLUDE="$LILYINCLUDE:$dir"
581 ORIENTATION=landscape
584 LANGUAGE="`expr \"$OPTARG\" ':' '[^=]*=\(.*\)'`"
587 PNUM="\pagestyle{empty}"
590 ORIENTATION="`expr \"$OPTARG\" ':' '[^=]*=\(.*\)'`"
593 OUTPUTDIR="`expr \"$OPTARG\" ':' '[^=]*=\(.*\)'`"
596 PAPERSIZE="`expr \"$OPTARG\" ':' '[^=]*=\(.*\)'`"
601 sourcedir*|-sourcedir*)
605 echo "$0: illegal option -- \"$OPTARG\""
612 shift `expr $OPTIND - 1`
614 if [ "$SOURCEDIR" != "" ]; then
615 # apparently MakeTeXTFM can't handle relative dirs
616 SOURCEDIR=`cd $SOURCEDIR; pwd`
617 export LILYINCLUDE="$SOURCEDIR/init:$SOURCEDIR/mf/out:$LILYINCLUDE"
618 export TEXINPUTS="$SOURCEDIR/tex:$TEXINPUTS:"
619 export MFINPUTS="$SOURCEDIR/mf:$MFINPUTS:"
620 export LILYPOND_EXECUTABLE="$SOURCEDIR/lily/out/lilypond"
632 $debug_echo "$IDENTIFICATION: No input file name given"
635 # regexp_quote -- quote a string as a regular expression for egrep or sed
637 Echo "$@" | sed 's/\([]\[\.*?+^$]\)/\\\1/g'
642 # Include \def\mudela-definitions
643 # The aim here is to pick up the definition for the
644 # current file, then any other file.
646 mudelatmp=$TMP/mudelaDefs$$
647 # Use `cat' to prevent filenames being prepended
649 cat "$File" $OF | fgrep "$MU_DEF" > $mudelatmp
652 # This converts \def\mudelatitle{fred}
653 # to \mudelatitle{fred} or to
654 # \def\mudelatitle{fred}
655 # and stops after the first one found.
656 sed -n '/\\def\\'"$L"'{\([^}]*\)}.*$/{
657 s//'"`regexp_quote \"$1\"`"'\\'"$L"'{\1}%/p
659 }' $mudelatmp >> $LatF
669 BN=`basename "$File" .tex`
671 if [ "$KEEP_LY2DVI_OUTPUT" != "Y" ]
679 # paper size (PAPERSIZE, overridden by command line option -p)
680 # paper orientation (ORIENTATION, overridden by option -o)
681 # language (LANGUAGE, overridden by option -l)
685 -e 's/\\\\def\\\\mudelalanguage{\([^}]*\).*$/fLNG=\1;/p' \\
686 -e 's/\\\\def\\\\mudelalatexheaders{\([^}]*\).*$/fLHF=\1;/p' \\
687 -e 's/\\\\def\\\\mudelaorientation{\([^}]*\).*$/fORI=\1;/p' \\
688 -e 's/\\\\def\\\\mudelapaperlinewidth{\([^}]*\).*$/TWN=\1;/p' \\
689 -e 's/\\\\def\\\\mudelapapertextheight{\([^}]*\).*$/THN=\1;/p' \\
690 -e 's/\\\\def\\\\mudelapapersize{\([^}]*\).*$/fPSZ=\1;/p' \\
700 [ -f "$LATEXHF" ] && LLHF="\input{$LATEXHF}"
703 if [ -z "$PAPERSIZE" ]
707 if [ -n "$PAPERSIZE" ]
710 PAPEROPT="$PAPERSIZE"
713 if [ -z "$ORIENTATION" ]
717 if [ -n "$ORIENTATION" ]
719 if [ -z "$PAPEROPT" ]
721 PAPEROPT="$ORIENTATION"
723 PAPEROPT="$PAPEROPT,$ORIENTATION"
727 if [ -n "$PAPEROPT" ]
732 if [ -z "$LANGUAGE" ]
736 if [ -n "$LANGUAGE" ]
738 LLNG="\usepackage[$LANGUAGE]{babel}"
760 $debug_echo "Text width = $TW"
762 TWp=`Echo $TW | sed -e 's/\..*$//'`
781 $debug_echo "Text height = $TH"
783 THp="`echo $TH | sed -e 's/\..*$//'`"
785 if [ "$ORIENTATION" = "landscape" ]
790 HMARG="`expr '(' $PWp - $TWp ')' / 2`"pt
791 $debug_echo "Text left = $HMARG"
792 VMARG="`expr '(' $PHp - $THp ')' / 2`"pt
793 $debug_echo "Text top = $VMARG"
795 # Geometry: /var/lib/texmf/latex/geometry/geometry.dvi
801 % Creator: $IDENTIFICATION
802 % Automatically generated from $IF, $NOW
804 \documentclass$PAPER{article}
807 \usepackage{geometry}
808 \usepackage[latin1]{inputenc}
809 %\usepackage[T1]{fontenc}
811 %\addtolength{\oddsidemargin}{-1cm}
812 %\addtolength{\topmargin}{-1cm}
813 %\setlength{\textwidth}{$TW}
814 %\setlength{\textheight}{$TH}
815 \geometry{width=$TW, left=$HMARG, height=$TH, top=$VMARG}
836 \def\theopus{\mudelaopus}% ugh
837 \def\thepiece{\mudelapiece}%
846 \vfill\hfill{\LilyIdString}
852 latex '\nonstopmode \input '$LatF || exit 5
859 [ -n "$OUTPUTDIR" ] && RESULT="$OUTPUTDIR/$RESULT"
861 cp "$FN.dvi" "$RESULT" || exit 5
868 $IDENTIFICATION: dvi file name is $RESULT
873 # ugh. GF is side-effect.
875 # should check for LILYINCLUDE
878 x=`echo $LILYINCLUDE | sed "s!:! !g"`
882 if [ -f "$lypath/$1" ]
888 if [ -f "$lypath/$1.ly" ]
893 if [ -f "$lypath/$1.fly" ]
899 $debug_echo "$IDENTIFICATION: Input file \"$GF\" not found"
900 echo "$NAME: Input file \"$GF\" not found" 1>&2
904 # Loop through all files
914 # Check whether the file is input to or output from lilypond
917 OP=`echo $L1 | grep "^% Creator: GNU LilyPond"`
921 # OK - it's the output from lilypond.
923 # Get lilypond source file name
926 IFL=`grep mudelafilename $OF`
929 IF=`echo $IFL | sed -e 's/.*{//' -e 's/}*.$//'`
931 # Check if source file exists
935 $debug_echo $IDENTIFICATION": Mudela file not found."
940 $debug_echo $IDENTIFICATION": Mudela file name not found."
946 # I have to assume this is the lilypond input file
947 # Find output file name, if defined
952 # Grab output file names
954 $debug_echo "lilypond "$LILYOPTS $IF
956 $LILYPOND_EXECUTABLE $LILYOPTS $IF 2>&1 | tee $LOGFILE
957 OF="`sed -n -e 's/TeX output to \([^\.]*\.tex\)\.\.\.$/\1/p' $LOGFILE`"
958 $debug_echo "==> $OF"
959 LILY_OUTPUT_FILES="$LILY_OUTPUT_FILES $OF"
960 STATUS=`egrep -i "error|segmentation|abort" $LOGFILE`
962 if [ ! -z "$STATUS" ]
968 # "Spin through" all the files
972 $debug_echo "--- "$File
974 # Check if output file is generated
978 $debug_echo "$IDENTIFICATION: hmm, I could not find the output file $File"
982 # Is this the first file?