lilypond-1.0.8
[lilypond.git] / bin / ly2dvi.in
blobac70e37209488378b8ab0b85008d61fa334d3073
1 #!/bin/sh
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
11 VERSION="0.8"
12 NAME=ly2dvi.sh
13 IDENTIFICATION="$NAME $VERSION"
14 NOW=`date`
15 echo "$IDENTIFICATION" 1>&2
17 # NEWS
19 # 0.8 - Trap Lilypond segmentation fault
20 # - Function for cleanup
21 # - Trap line
22 # - More human-readable variables
23 # - Some logics concerning rc-files
25 # 0.7
26 # - Improved Lilypond error checking
27 # - Output orientation (landscape...). Overrides mudela file
28 # variable orientation="landscape";
29 # - Paper width and heigth put into variables (only A4!)
30 # - Adjusted top margin to default.....
32 #TODO
33 # - Include more papersizes.
34 # - Manual page.......
35 # 0.6.jaf2
36 # - LILYINCLUDE update
38 # 0.6.hwn1
39 # - handle LILYINCLUDE
40 # - --output
42 # 0.6
43 # - Source rc-files, if present. Files are:
44 # /usr/local/share/lilyrc /etc/lilyrc $HOME/.lilyrc ./.lilyrc
45 # - tee output from Lilypond
46 # - Handles margins for A4 paper (equal on both sides)
47 # - new option -s (--separate) for one LaTeX run per file,
48 # else all files are run together
50 # 0.5
51 # - More useful ("two-level") debug.
52 # - The Q&D hack to find file names and not handling \include
53 # is replaced by grabbing output file names from Lilypond.
54 # = Detects multiple output files - adds them when running
55 # LaTeX.
56 # - Works with multiple input files - no matter if they are
57 # (a mix of) input to or output from Lilypond.
59 #TODO
60 # - Still no margins handling.
61 # - We have to discuss how to handle multiple output files
62 # from Lilypond - 'ly2dvi standchen' gives a rather odd
63 # result....
65 # 0.4.1
66 # - Always exit after printing help info
67 # 0.4
68 # - Changes to ensure for more strict grep'ing of parameters
69 # Thanks to from G.B.Stott@bolton.ac.uk
70 # - More efficient use of sed -e 's///' -e 's///'
71 # Thanks to Johan Vromans <jvromans@squirrel.nl> and GBS
72 # - Ask tex for location of titledefs.tex (Thanks to JV)
73 # - Accept only exact match of "\def\mudelacomposer{"
74 # (or whatever mudela* defined in titledefs.tex)
75 # - Even more efficient use of sed (Thanks to JV)
76 # - Default file name for single output file implemented.
77 # - Moved help into function - finally included from 0.1.jcn1
79 #TODO
80 # - Still doesn't handle \include
81 # - The Q&D for finding output file name from the sequence of
82 # \paper \output \midi \output really needs to be looked at.
83 # I have improved it a lot, but it's only capable of finding
84 # one (the last) file name.
85 # Well, I have to rewrite this entirely to handle \include,
86 # then I can fix it.
87 # - Still no margins handling.
89 #WARNING
90 # - Some lines of output from lilypond do NOT start
91 # at first character position, therefore I have removed "^"
92 # in sed'ing and grep'ing.
94 # 0.3.hwn1
95 # - add "Creator: " line to output
97 # 0.3
98 # - multiple input files to make score from several files
99 # (extra files assumed to be Lilypond output)
100 # - cp dvi-file instead of mv, to allow for xdvi reload
101 # - check for illegal long options
102 # - put in pt in text width, if not given
103 # - put in \nonstopmode in LaTeX file
104 # - restored LaTeX exit value check
106 # 0.2.1
107 # - temporarily omit LaTeX exit value check
108 # - remove ALL temporary files
110 # 0.2
111 # - fix for solaris - included from 0.1.jcn1
112 # - long option hack - included from 0.1.jcn1 - modified
113 # - moved help into function - NOT included from 0.1.jcn1 yet
114 # - various improvements - included from 0.1.jcn1
115 # - find mudela definitions from titledefs.tex
116 # - find papersize from lilypond output file (mudelapapersize),
117 # overridden by option '-p size' or '--papersize=size'
118 # - option -l lang or --language=lang overrides
119 # lilypond output file definition (mudelalanguage)
120 # - textwidth from lilypond output file (mudelapaperlinewidth)
122 # 0.1.jcn1
123 # - fix for solaris
124 # - long option hack
125 # - moved help into function
128 # Clean up
130 cleanup() {
131 $debug_echo "("$LF")("$FN")("$LOGFILE")"
132 if [ "$KEEP" != "Y" ]
133 then
134 [ -n "$LF" -a -f "$LF" ] && rm -f $LF
135 [ -n "$LOGFILE" -a -f "$LOGFILE" ] && rm -f $LOGFILE
136 [ -n "$FN" ] && rm -f $FN.*
140 # print usage
142 help() {
143 cat << EOF
144 Generate dvi file from mudela or lilypond output
145 Usage: $0 [options] file[s]
147 Options:
148 -D, --debug set debug mode
149 -O, --orientation= set orientation (landscape or portrait (default))
150 -o, --output= set output directory
151 -h, --help this help text
152 -k, --keep keep LaTeX file
153 -l, --language= give LaTeX language (babel)
154 -p, --papersize= give LaTeX papersize (eg. a4paper)
155 -s, --separate run all files separately through LaTeX
157 files may be (a mix of) input to or output from lilypond(1)
161 # Trap function (cleanup)
163 trap cleanup 0 9 15
165 # Various defaults
167 [ -z "$TMP" ] && TMP=/tmp
168 if [ ! -d $TMP ]
169 then
170 $debug_echo $IDENTIFICATION": temporary directory "$TMP" not found, set to /tmp"
171 TMP=/tmp
173 LOGFILE=$TMP/lilylog.$$ # Logfile for lilypond
174 PWIDTH=600; # Width of A4 paper!
175 PHEIGTH=830; # Heigth of A4 paper!
177 # RC-files ?
179 for D in /usr/local/share/ /etc/ $HOME/. ./.
181 RCfile=$D"lilyrc"
182 [ -f $RCfile ] && . $RCfile
183 done
184 fORI=$ORIENTATION
185 fLNG=$LANGUAGE
186 fPSZ=$PAPERSIZE
187 unset ORIENTATION LANGUAGE PAPERSIZE
189 # Keywords defined in titledefs.tex
191 TF=`kpsewhich -n tex tex titledefs.tex`
192 if [ -n $TF ]
193 then
194 TF=/usr/lib/texmf/tex/lilypond/titledefs.tex
196 MU_DEF=""
197 if [ -f $TF ]
198 then
199 MU_DEF=`egrep "^.newcommand...mudela" $TF | \\
200 sed -e 's/^.newcommand...//' -e 's/\\}.*$//'`
203 if [ -z "$MU_DEF" ]
204 then
205 MU_DEF="mudelatitle mudelasubtitle mudelacomposer \
206 mudelaarranger mudelainstrument"
210 # debugging
212 debug_echo=true
214 # All files in one LaTeX run
216 SEPFILE=N
218 # Find command line options and switches
220 # "x:" x takes argument
222 switches="DO:hkl:o:p:s\?"
223 options=""
225 # ugh, "\-" is a hack to support long options
226 # while getopts \-:$options$switches O
227 # must be in double quotes for bash-2.0
228 while getopts "\-:$options$switches" O
230 $debug_echo "O: \`$O'"
231 $debug_echo "arg: \`$OPTARG'"
232 case $O in
234 [ $debug_echo = echo ] && set -x
235 debug_echo=echo
238 ORIENTATION=$OPTARG
241 help;
242 exit 0
245 KEEP=Y
248 LANGUAGE=$OPTARG
251 OUTPUTDIR=$OPTARG
254 PAPERSIZE=$OPTARG
257 SEPFILE=Y
259 \? )
260 help;
261 exit -1
263 # a long option!
265 $debug_echo "long option: \`$OPTARG'"
266 case "$OPTARG" in
267 D*|-D*)
268 [ $debug_echo = echo ] && set -x
269 debug_echo=echo
271 h*|-h*)
272 help;
273 exit 0
275 k*|-k*)
276 KEEP=Y
278 l*|-l*)
279 LANGUAGE=`echo $OPTARG | sed -e s/"^.*="//`
281 p*|-p*)
282 PAPERSIZE=`echo $OPTARG | sed -e s/"^.*="//`
284 or*|-or*)
285 ORIENTATION=`echo $OPTARG | sed -e s/"^.*="//`
287 ou*|-ou*)
288 OUTPUTDIR=`echo $OPTARG | sed -e s/"^.*="//`
290 s*|-s*)
291 SEPFILE=Y
293 *|-*)
294 echo $0": illegal option -- "$OPTARG;
295 help;
296 exit -1
298 esac
299 esac
300 done
301 shift `expr $OPTIND - 1`
303 # Input file name
305 if [ "$1" = "" ]
306 then
307 help
308 $debug_echo $IDENTIFICATION": No input file name given"
309 exit 1
312 startFile(){
314 # LaTeX file name
316 BN=`basename $File .tex`
317 FN=$BN.$$
318 if [ "$KEEP" != "Y" ]
319 then
320 LF=$TMP/$FN.tex
321 else
322 LF=$FN.tex
325 # Find:
326 # paper size (PAPERSIZE, overridden by command line option -p)
327 # paper orientation (ORIENTATION, overridden by option -o)
328 # language (LANGUAGE, overridden by option -l)
329 # textwidth
331 eval `sed -n \\
332 -e 's/\\\\def\\\\mudelapapersize{\([^}]*\).*$/fPSZ=\1;/p' \\
333 -e 's/\\\\def\\\\mudelaorientation{\([^}]*\).*$/fORI=\1;/p' \\
334 -e 's/\\\\def\\\\mudelalanguage{\([^}]*\).*$/fLNG=\1;/p' \\
335 -e 's/\\\\def\\\\mudelapaperlinewidth{\([^}]*\).*$/TWN=\1;/p' \\
336 $File`
337 if [ -z "$PAPERSIZE" ]
338 then
339 PAPERSIZE=$fPSZ
341 if [ -n "$PAPERSIZE" ]
342 then
343 PAPEROPT=$PAPERSIZE
346 if [ -z "$ORIENTATION" ]
347 then
348 ORIENTATION=$fORI
350 if [ -n "$ORIENTATION" ]
351 then
352 if [ -z "$PAPEROPT" ]
353 then
354 PAPEROPT=$ORIENTATION
355 else
356 PAPEROPT=$PAPEROPT,$ORIENTATION
360 if [ -n "$PAPEROPT" ]
361 then
362 PAPER="["$PAPEROPT"]"
365 if [ -z "$LANGUAGE" ]
366 then
367 LANGUAGE=$fLNG
369 if [ -n "$LANGUAGE" ]
370 then
371 LLNG="\usepackage["$LANGUAGE"]{babel}"
372 else
373 LLNG="%"
377 # Find textwidth
379 if [ -n "$TWN" ]
380 then
381 TW=$TWN
382 case $TW in
383 *mm)
385 *cm)
387 *pt)
390 TW=$TW"pt"
392 esac
393 $debug_echo "Text width = "$TW
395 TWp=`echo $TW | sed -e 's/\..*$//'`
396 PWp=$PWIDTH
397 if [ "$ORIENTATION" = "landscape" ]
398 then
399 PWp=$PHEIGTH
401 MARG=`expr $PWp - $TWp`
402 MARG=`expr $MARG / 2`"pt"
404 # Geometry: /var/lib/texmf/latex/geometry/geometry.dvi
407 # Write LaTeX file
409 cat << EOF > $LF
410 % Creator: $IDENTIFICATION
411 % Automatically generated from $IF, $NOW
413 \documentclass$PAPER{article}
414 \nonstopmode
415 $LLNG
416 \usepackage{geometry}
417 \usepackage[T1]{fontenc}
418 %\addtolength{\oddsidemargin}{-1cm}
419 %\addtolength{\topmargin}{-1cm}
420 \setlength{\textwidth}{$TW}
421 %\geometry{width=$TW, left=$MARG, top=1cm}
422 \geometry{width=$TW, left=$MARG}
423 \input lilyponddefs
424 \input titledefs
425 \begin{document}
428 # Include \def\mudela-definitions
430 for L in $MU_DEF
432 LL=`egrep '^\\\\def.'$L'{' $OF`
433 if [ "$LL" != "" ]
434 then
435 LLL=`echo $LL | sed -e 's/}.*$//' -e 's/.*{//'`
436 if [ "$LLL" != "" ]
437 then
438 echo '\'$L'{'$LLL'}%' >> $LF
441 done
443 cat << EOF >> $LF
444 \makelilytitle
448 # Conclusion
450 endFile(){
451 cat << EOF >> $LF
452 \vfill\hfill{(\LilyIdString)}
453 \end{document}
456 # Run LaTeX
458 latex $LF || exit 5
460 # Rename dvi file
462 if [ -f $FN.dvi ]
463 then
464 RESULT=$BN.dvi
465 [ -n "$OUTPUTDIR" ] && RESULT="$OUTPUTDIR/$RESULT"
466 cp $FN.dvi $RESULT
469 # Clean up
471 cleanup
473 # Output some info
475 cat << EOF
477 $IDENTIFICATION: dvi file name is $RESULT
482 # ugh. GF is side-effect.
483 findInput() {
484 # should check for LILYINCLUDE
485 for lypath in "." `echo $LILYINCLUDE| sed 's/:/ /g'`
487 if [ -f "$lypath/$1" ]
488 then
489 GF="$lypath/$1"
490 return
493 if [ -f "$lypath/$1.ly" ]
494 then
495 GF="$lypath/$1.ly"
496 return
498 done
499 $debug_echo $IDENTIFICATION": Input file "$GF" not found"
500 echo $NAME": Input file "$GF" not found" 1>&2
501 exit 2
504 # Loop through all files
506 for GF in $*
508 findInput $GF
511 # Check whether the file is input to or output from lilypond
513 L1=`head -1 $GF`
514 OP=`echo $L1 | grep "^% Creator: GNU LilyPond"`
515 if [ -n "$OP" ]
516 then
518 # OK - it's the output from lilypond.
520 # Get lilypond source file name
522 OF=$GF
523 IFL=`grep mudelafilename $OF`
524 if [ "$IFL" != "" ]
525 then
526 IF=`echo $IFL | sed -e 's/.*{//' -e 's/}*.$//'`
528 # Check if source file exists
530 if [ ! -f $IF ]
531 then
532 $debug_echo $IDENTIFICATION": Mudela file not found."
533 TW=15.5cm
535 else
536 $debug_echo $IDENTIFICATION": Mudela file name not found."
537 TW=15.5cm
539 else
541 # I have to assume this is the lilypond input file
542 # Find output file name, if defined
544 IF=$GF
546 # Run lilypond
547 # Grab output file names
549 $debug_echo "lilypond "$IF
551 lilypond $IF 2>&1 | tee $LOGFILE
552 OF=`egrep '^TeX output to ' $LOGFILE | \\
553 sed -e 's/TeX output to//' -e 's/\.\.\.//'`
554 $debug_echo "==> "$OF
555 STATUS=`egrep -i "error|segmentation" $LOGFILE`
556 echo $STATUS
557 if [ ! -z "$STATUS" ]
558 then
559 exit 10
563 # "Spin through" all the files
565 for File in $OF
567 $debug_echo "--- "$File
569 # Check if output file is generated
571 if [ ! -f $File ]
572 then
573 $debug_echo $IDENTIFICATION": hmm, I could not find the output file "$File
574 exit 4
577 # Is this the first file?
579 if [ -z "$FFile" ]
580 then
581 FFile=$File
582 startFile
584 cat << EOF >> $LF
585 \input{$File}
587 if [ $SEPFILE = Y ]
588 then
589 FFile=""
590 endFile
592 done
593 done
594 if [ $SEPFILE = N ]
595 then
596 endFile
599 # OK - finished