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.6 1998/09/15 17:22:43 fred 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 LILYINCLUDE
="$SOURCEDIR/init:$SOURCEDIR/mf/out:$LILYINCLUDE"
618 TEXINPUTS
="$SOURCEDIR/tex:$TEXINPUTS:"
619 MFINPUTS
="$SOURCEDIR/mf:$MFINPUTS:"
620 LILYPOND_EXECUTABLE
="$SOURCEDIR/lily/out/lilypond"
621 export MFINPUTS LILYPOND_EXECUTABLE TEXINPUTS SOURCEDIR
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
641 # The aim here is to pick up the definition for the
642 # current file, then any other file.
644 mudelatmp
=$TMP/mudelaDefs$$
645 # Use `cat' to prevent filenames being prepended
647 cat "$File" $OF | fgrep
"$MU_DEF" > $mudelatmp
650 # This converts \def\mudelatitle{fred}
651 # to \mudelatitle{fred} or to
652 # \def\mudelatitle{fred}
653 # and stops after the first one found.
654 sed -n '/\\def\\'"$L"'{\([^}]*\)}.*$/{
655 s//'"`regexp_quote \"$1\"`"'\\'"$L"'{\1}%/p
657 }' $mudelatmp >> $LatF
667 BN
=`basename "$File" .tex`
669 if [ "$KEEP_LY2DVI_OUTPUT" != "Y" ]
677 # paper size (PAPERSIZE, overridden by command line option -p)
678 # paper orientation (ORIENTATION, overridden by option -o)
679 # language (LANGUAGE, overridden by option -l)
683 -e 's/\\\\def\\\\mudelalanguage{\([^}]*\).*$/fLNG=\1;/p' \\
684 -e 's/\\\\def\\\\mudelalatexheaders{\([^}]*\).*$/fLHF=\1;/p' \\
685 -e 's/\\\\def\\\\mudelaorientation{\([^}]*\).*$/fORI=\1;/p' \\
686 -e 's/\\\\def\\\\mudelapaperlinewidth{\([^}]*\).*$/TWN=\1;/p' \\
687 -e 's/\\\\def\\\\mudelapapertextheight{\([^}]*\).*$/THN=\1;/p' \\
688 -e 's/\\\\def\\\\mudelapapersize{\([^}]*\).*$/fPSZ=\1;/p' \\
698 [ -f "$LATEXHF" ] && LLHF
="\input{$LATEXHF}"
701 if [ -z "$PAPERSIZE" ]
705 if [ -n "$PAPERSIZE" ]
708 PAPEROPT
="$PAPERSIZE"
711 if [ -z "$ORIENTATION" ]
715 if [ -n "$ORIENTATION" ]
717 if [ -z "$PAPEROPT" ]
719 PAPEROPT
="$ORIENTATION"
721 PAPEROPT
="$PAPEROPT,$ORIENTATION"
725 if [ -n "$PAPEROPT" ]
730 if [ -z "$LANGUAGE" ]
734 if [ -n "$LANGUAGE" ]
736 LLNG
="\usepackage[$LANGUAGE]{babel}"
758 $debug_echo "Text width = $TW"
760 TWp
=`Echo $TW | sed -e 's/\..*$//'`
779 $debug_echo "Text height = $TH"
781 THp
="`echo $TH | sed -e 's/\..*$//'`"
783 if [ "$ORIENTATION" = "landscape" ]
788 HMARG
="`expr '(' $PWp - $TWp ')' / 2`"pt
789 $debug_echo "Text left = $HMARG"
790 VMARG
="`expr '(' $PHp - $THp ')' / 2`"pt
791 $debug_echo "Text top = $VMARG"
793 # Geometry: /var/lib/texmf/latex/geometry/geometry.dvi
799 % Creator: $IDENTIFICATION
800 % Automatically generated from $IF, $NOW
802 \documentclass$PAPER{article}
805 \usepackage{geometry}
806 \usepackage[latin1]{inputenc}
807 %\usepackage[T1]{fontenc}
809 %\addtolength{\oddsidemargin}{-1cm}
810 %\addtolength{\topmargin}{-1cm}
811 %\setlength{\textwidth}{$TW}
812 %\setlength{\textheight}{$TH}
813 \geometry{width=$TW, left=$HMARG, height=$TH, top=$VMARG}
834 \def\theopus{\mudelaopus}% ugh
835 \def\thepiece{\mudelapiece}%
844 \vfill\hfill{\LilyIdString}
850 latex
'\nonstopmode \input '$LatF ||
exit 5
857 [ -n "$OUTPUTDIR" ] && RESULT
="$OUTPUTDIR/$RESULT"
859 cp "$FN.dvi" "$RESULT" ||
exit 5
866 $IDENTIFICATION: dvi file name is $RESULT
871 # ugh. GF is side-effect.
873 # should check for LILYINCLUDE
876 x
=`echo $LILYINCLUDE | sed "s!:! !g"`
880 if [ -f "$lypath/$1" ]
886 if [ -f "$lypath/$1.ly" ]
891 if [ -f "$lypath/$1.fly" ]
897 $debug_echo "$IDENTIFICATION: Input file \"$GF\" not found"
898 echo "$NAME: Input file \"$GF\" not found" 1>&2
902 # Loop through all files
912 # Check whether the file is input to or output from lilypond
915 OP
=`echo $L1 | grep "^% Creator: GNU LilyPond"`
919 # OK - it's the output from lilypond.
921 # Get lilypond source file name
924 IFL
=`grep mudelafilename $OF`
927 IF
=`echo $IFL | sed -e 's/.*{//' -e 's/}*.$//'`
929 # Check if source file exists
933 $debug_echo $IDENTIFICATION": Mudela file not found."
938 $debug_echo $IDENTIFICATION": Mudela file name not found."
944 # I have to assume this is the lilypond input file
945 # Find output file name, if defined
950 # Grab output file names
952 $debug_echo "lilypond "$LILYOPTS $IF
954 $LILYPOND_EXECUTABLE $LILYOPTS $IF 2>&1 |
tee $LOGFILE
955 OF
="`sed -n -e 's/TeX output to \([^\.]*\.tex\)\.\.\.$/\1/p' $LOGFILE`"
956 $debug_echo "==> $OF"
957 LILY_OUTPUT_FILES
="$LILY_OUTPUT_FILES $OF"
958 STATUS
=`egrep -i "error|segmentation|abort" $LOGFILE`
960 if [ ! -z "$STATUS" ]
966 # "Spin through" all the files
970 $debug_echo "--- "$File
972 # Check if output file is generated
976 $debug_echo "$IDENTIFICATION: hmm, I could not find the output file $File"
980 # Is this the first file?