%% -*-TeX-*-
 % ###################################################################
 % 
 %  FILE: "abcintex.sty"
 %                                    created: 2005-01-24 17:47:39 
 %                                last update: 2006-05-23 14:56:54 
 %  Author: Frédéric Boulanger
 %  E-mail: Frederic.Boulanger@supelec.fr
 %    mail: Supélec - Service Informatique
 %          3 rue Joliot-Curie, 91192 Gif-sur-Yvette, France
 %     www: http://wwwsi.supelec.fr/fb/
 %  
 %  Description: 
 %  
 %  Support inline ABC notation in LaTeX.
 %  
 %  Options:
 %  --------
 %    keepfiles
 %      Don't delete ABC scores (useful for debugging)
 %      
 %    reusefiles
 %      Implies "keepfiles". Reuse scores that were generated during
 %      a previous run.
 %      
 %  Commands:
 %  ---------
 %    \abckeepnext
 %      Keep next score, even if keepfiles is false
 %      
 %    \abcreusenext
 %      Reuse files for next score, even if reusefiles is false
 %    
 %    \abcrebuildnext
 %      Don't reuse files for next score, even if reusefiles is true.
 %      
 %    \abcsettings{<key=value list>}
 %      Set ABC score processing options:
 %        raise=<dimen>
 %          raise ABC score boxes by <dimen>. Defaults to 0pt.
 %          
 %        height=<dimen>
 %          force height of ABC score boxes to <dimen>. Default is to 
 %          use the natural height of the score
 %          
 %        depth=<dimen>
 %          force depth of ABC score boxes to <dimen>. Defaults to 
 %          0pt.
 %        
 %        width=<dimen>
 %          force width of ABC score boxes to <dimen>. Default is to 
 %          use the natural width of the score.
 %          
 %        hpos=<l,c,r>
 %          set horizontal position of ABC scores in their box. 
 %          Defaults to 'c'.
 %          
 %        clear
 %          reset ABC score processing options to their default value.
 %          
 %        prefix=<prefix name>
 %          Use <prefix name> as the prefix for ABC code. Defaults to 
 %          the empty prefix.
 %          The prefix is inserted at the very beginning of the 
 %          generated ABC file, before the X:1 tag that starts a song.
 %          
 %        prelude=<prefix name>
 %          Use <prefix name> as the prelude for ABC code. Defaults 
 %          to the empty prelude.
 %          The prelude is inserted at the beginning of the song, 
 %          just after the X:1 tag.
 %          
 %        midi=<midi file name>
 %          Generate <midi file name> from ABC code. No midi file is 
 %          generated by default.
 %          
 %        abc=<ABC file name>
 %          Copy ABC code to file <ABC file name>.
 %          
 %        name=<file name>
 %          Same as midi=<file name>.mid and abc=<file name>.abc
 %        
 %        num=<integer>
 %          Set ABC song number to <integer>
 %          
 %        Other keys are interpreted as ABC tags. Therefore, if you 
 %        put "K=C" in the options of \abcsettings, the tag "K:C" 
 %        will be put in your ABC song. There is no check for valid 
 %        ABC tags.
 %        
 %    \abcpssettings{<key=value list>}
 %      Set ABC to PostScript options:
 %        pagewidth=<dimen>
 %          Set PostScript page width to <dimen>
 %          
 %        pageheight=<dimen>
 %          Set PostScript page height to <dimen>
 %          
 %        topmargin=<dimen>
 %          Set PostScript top margin to <dimen>
 %          
 %        leftmargin=<dimen>
 %          Set PostScript left margin to <dimen>
 %      
 %        botmargin=<dimen>
 %          Set PostScript bottom margin to <dimen>
 %      
 %        rightmargin=<dimen>
 %          Set PostScript right margin to <dimen>
 %      
 %        staffwidth=<dimen>
 %          Set the width of a staff to <dimen>
 %      
 %        staffsep=<dimen>
 %          Set vertical interstaff space to <dimen>
 %      
 %        scale=<[0,1]>
 %          Set score scale. Lower values put more music on the score.
 %          Defaults to 0.75
 %          
 %        maxshrink=<[0,1]>
 %          Set max horizontal compression. Higher values put more 
 %          music on one line. Defaults to 0.65
 %          
 %        maxstaffsep=<dimen>
 %          Set max vertical interstaff space.
 %          
 %        maxsysstaffsep=<dimen>
 %          Set max vertical interstaff space between systems.
 %          
 %        musicspace=<dimen>
 %          Set vertical space before first staff.
 %          
 %        partspace=<dimen>
 %          Set vertical space before new part.
 %          
 %        printparts=<0,1>
 %          Print part indications (1) or not (0). Defaults to 1
 %          
 %        partsbox=<0,1>
 %          Put part names in boxes or not. Defaults to 1
 %          
 %        measurebox=<0,1>
 %          Put bar numbers in boxes or not. Defaults to 1
 %          
 %        measurenb=<integer>
 %          Number bars.
 %          If < 0, bars are not numbered.
 %          If 0, bars are numbered at each start of a 
 %          new line, else, bars are numbered every <integer> bars.
 %          Defaults to -1.
 %          
 %        contbarnb=<0,1>
 %          Number bars sequentially in repeats. Defaults to 1
 %          
 %        setbarnb=<integer>
 %          Set next bar number.
 %          
 %        printtempo=<0,1>
 %          Print tempo indications ('Q:' ABC field) or not.
 %          
 %        stretchstaff=<0,1>
 %          Stretch underfull staves across the page or not
 %          
 %        stretchlast=<0,1>
 %          Stretch last staff when underfull.
 %          
 %        continueall=<0,1>
 %          Ignore line breaks in the ABC source.
 %        
 %        clear
 %          Reset ABC to PostScript options to the empty set.
 %          
 %        
 %      
 %  Environments:
 %  -------------
 %    abcprefix{<prefix name>}
 %      Set the <prefix name> prefix to the contents of the 
 %      environment.
 %      
 %    abcmoreprefix{<prefix name>}
 %      Append the contents of the environment to prefix <prefix 
 %      name>.
 %        
 %    abc
 %      Build a score (and a MIDI file if requested) from the 
 %      contents of the environment, and insert it in the document.
 % 
 %    abcbox{<LaTeX box name>}
 %      Build a score (and a MIDI file if requested) from the
 %      contents of the environment, and put it in box <LaTeX box 
 %      name>.
 % 
 %    abcannotate[<options>]{<LaTeX box name>}
 %      Insert <LaTeX box name> and annotate it with the contents of 
 %      the environment, which consists in LaTeX picture commands. 
 %      The \unitlength is set to 1pt. The command 
 %      \rput(<x>,<y>){<picture stuff>} allows relative positionning 
 %      of elements over the box contents, with x varying from 0 to 1 
 %      from the left to the right of the box, and <y> varying from 0 
 %      to 1 from the bottom to the top of the box.
 %      
 %      options:
 %        top=<dimen>
 %          top margin over the box
 %        left=<dimen>
 %          left margin before the box
 %        bottom=<dimen>
 %          bottom margin under the box
 %        right=<dimen>
 %          right margin after the box
 %        
 %        under=<text>
 %          insert <text> at the begining of the picture, so that it 
 %          can be used as a background to the score.
 %        
 %        grid=<params>
 %          draw a grid ovor the box to help positionning annotations.
 %          <params> may be:
 %            xdiv=<integer>
 %              set the horizontal grid step to width/<integer>
 %            ydiv=<integer>
 %              set the horizontal grid step to height/<integer>
 %              
 %            xtick=<integer>
 %              set the horizontal step (like xdiv) for ticks (bolder 
 %              lines)
 %            ytick=<integer>
 %              set the vertical step (like ydiv) for ticks (bolder 
 %              lines)
 %   
 %  Private commands that may be redefined according to the platform:
 %  -----------------------------------------------------------------
 %  \abc@copy@file{<src>}{<dst>}
 %    copy file <src> to file <dst>. Defaults to {cp #1 #2}
 %    
 %  \abc@del@file{<file name>}
 %    delete file <file name>}. Defaults to {rm #1}
 %    
 %  \abc@abc@ps{<abc file>}{<PS file>}
 %    convert <abc file> to PostSript. Defaults to {abcm2ps -O= #1}, 
 %    the name of the PS file being the same as the source file, but 
 %    with extension '.ps'
 %    
 %  \abc@abc@midi{<abc file>}{<MIDI file>}
 %    convert <abc file> to MIDI. Defaults to {abc2midi #1 -o #2}
 %    
 %  \abc@ps@bb{<ps file>}{<bb file>}
 %    compute bouding box of <ps file> and write it to <bb file>. 
 %    Defaults to {gs -sDEVICE=bbox -q -dNOPAUSE -- #1 >& #2}
 %  
 %  \abc@ps@pdf{<ps file>}{<pdf file>}
 %    convert <ps file> to PDF. Defaults to {ps2pdf #1}
 %    Used when \pdfoutput is defined and > 0
 %    
 %  \abc@ps@eps{<ps file>}{<eps file>}
 %    convert <ps file> to EPS. Defaults to {ps2eps #1}
 %    Used when \pfdoutput is not defined or is lower or equal to 0
 %    
 %  History
 % 
 %  modified   by  rev reason
 %  ---------- --- --- -----------
 %  2005-01-24 FBO 1.0 original
 %  2005-04-12 FBO 1.1 added 'under' key to abcannotate
 %  2005-04-22 FBO 1.2 X field no longer in prefix, num key 
 %                     to set song number
 %  2006-05-17 FBO 1.3 Added checks for failure to generate files
 % ###################################################################
 %%
\NeedsTeXFormat{LaTeX2e}
\def\abcintex@package{abcintex}
\def\abcintex@version{1.3}
\def\abcintex@release{2006/05/17}
\ProvidesPackage{\abcintex@package}[%
  \abcintex@release v\abcintex@version
   ABC music notation in LaTeX%
]

\RequirePackage{graphicx}
\RequirePackage{color}
\RequirePackage{verbatim}
\RequirePackage{keyvalx}
\RequirePackage{ifthen}
\def\gsetboolean#1#2{%
  \lowercase{\def\@tempa{#2}}%
  \@ifundefined{@tempswa\@tempa}{%
    \PackageError{ifthen}%
       {You can only set a boolean to `true' or `false'}\@ehc
  }{%
    \@ifundefined{#1\@tempa}{%
      \PackageError{ifthen}{Boolean #1 undefined}\@ehc
    }{%
      \global\csname#1\@tempa\endcsname
    }%
  }%
}

\newboolean{abc@keepfiles}
\newboolean{abc@reusefiles}
\DeclareOption{keepfiles}{\setboolean{abc@keepfiles}{true}}
\DeclareOption{reusefiles}{%
  \setboolean{abc@reusefiles}{true}\setboolean{abc@keepfiles}{true}%
}

\ProcessOptions\relax

% Command to copy a file #1 to #2
\newcommand{\abc@copy@file}[2]{%
  cp #1 #2
}
% Command to delete a file #1
\newcommand{\abc@del@file}[1]{%
  rm #1
}
% Command to convert an ABC file (#1) into a PS file (#2)
% #2 is the same as #1, but with extension .ps instead of .abc
\newcommand{\abc@abc@ps}[2]{%
  abcm2ps -O= #1
}
% Command to convert an ABC file (#1) into a MIDI file (#2)
% #1 and #2 may not be related at all
\newcommand{\abc@abc@midi}[2]{%
  abc2midi #1 -o #2
}
% Command to get the bounding box (#2) of a PS file (#1)
% #2 is the same as #1, but with extension .bb instead of .abc
\newcommand{\abc@ps@bb}[2]{%
  gs -sDEVICE=bbox -q -dNOPAUSE -- #1 >& #2
}
% \newcommand{\abc@ps@bb}[2]{%
%   gs -sDEVICE=bbox #1 </dev/null 2> #2 >/dev/null
% }

% Command to convert a PS file (#1) into a PDF file (#2)
% #2 is the same as #1, but with extension .pdf instead of .ps
\newcommand{\abc@ps@pdf}[2]{%
  ps2pdf #1
}
% Command to convert a PS file (#1) into an EPS file (#2)
% #2 is the same as #1, but with extension .eps instead of .ps
\newcommand{\abc@ps@eps}[2]{%
  ps2eps #1
}

\newboolean{abc@pdf}
\@ifundefined{pdfoutput}{%
  \setboolean{abc@pdf}{false}%
}{%
  \ifthenelse{\pdfoutput>0}{%
    \setboolean{abc@pdf}{true}%
  }{%
    \setboolean{abc@pdf}{false}%
  }%
}%

\newcommand*{\abc@box@raise}{}
\newcommand*{\abc@box@height}{}
\newcommand*{\abc@box@depth}{}
\newcommand*{\abc@box@width}{}
\newcommand*{\abc@box@hpos}{}
\newcommand*{\abc@songnum}{}
\newcommand*{\abc@prefix}{}

\newcount\abc@songnum
\newtoks\abc@abctoks
\newcommand*{\abc@box@defaults}{%
  \let\abc@box@raise\relax
  \let\abc@box@height\relax
  \let\abc@box@depth\relax
  \let\abc@box@width\relax
  \let\abc@box@hpos\relax
  \let\abc@prefix\relax
  \let\abc@prelude\relax
  \let\abc@midi\relax
  \let\abc@abc\relax
  \abc@abctoks{}\relax
  \abc@songnum=1%
}
\abc@box@defaults

\define@key{abc}{raise}{\def\abc@box@raise{#1}}
\define@key{abc}{height}{\def\abc@box@height{#1}}
\define@key{abc}{depth}{\def\abc@box@depth{#1}}
\define@key{abc}{width}{\def\abc@box@width{#1}}
\define@key{abc}{hpos}{\def\abc@box@hpos{#1}}
\define@key{abc}{clear}{\abc@box@defaults}
\define@key{abc}{prefix}{\let\abc@prefix=#1}
\define@key{abc}{prelude}{\let\abc@prelude=#1}
\define@key{abc}{midi}{\def\abc@midi{#1}}
\define@key{abc}{abc}{\def\abc@abc{#1}}
\define@key{abc}{name}{\def\abc@abc{#1.abc}\def\abc@midi{#1.mid}}
\define@key{abc}{num}{\abc@songnum=#1}

\newcommand{\abc@defkey}[2]{%
  \if|\the\abc@abctoks|
    \abc@abctoks{#1:#2}%
  \else
    \expandafter\abc@abctoks\expandafter{\the\abc@abctoks^^J#1:#2}%
  \fi
}
\default@key{abc}{\abc@defkey}

\newcommand*{\abckeepnext}{\setboolean{abc@keep@next}{true}}
\newcommand*{\abcreusenext}{\setboolean{abc@reuse@next}{true}}
\newcommand*{\abcrebuildnext}{\setboolean{abc@rebuild@next}{true}}

\newcommand{\abcsettings}[1]{%
  \if|#1|
    \abc@box@defaults
  \else
    \abc@abctoks{}\relax
    \let\abc@midi\relax
    \let\abc@abc\relax
    \setkeys{abc}{#1}%
  \fi
}

\def\abc@addtoksline#1#2{%
  \if|\the#1|
    #1{#2}%
  \else
    \expandafter#1\expandafter{\the#1^^J#2}%
  \fi
}
\def\abc@addtoprefix#1\end{%
  \abc@addtoksline\abc@setting@prefix{#1}%
}
\newcommand{\abc@toks@strip@spaces}[1]{%
  \let\abc@strip@finish\abc@addtoprefix
  \expandafter\abc@strip@spaces\expandafter\ #1\end
}
\newenvironment{abcprefix}[1]{%
  \@bsphack
  \ifx#1\@undefined
    \newtoks#1%
  \fi
  \let\abc@setting@prefix#1
  #1{}%
  \let\do\@makeother\dospecials
  \catcode`\^^M\active
  \def\verbatim@processline{%
    \abc@toks@strip@spaces{\the\verbatim@line}%
  }%
  \verbatim@start%
}{%
  \global\expandafter\abc@setting@prefix\expandafter{\the\abc@setting@prefix}%
  \@esphack
}
\newenvironment{abcmoreprefix}[1]{%
  \@bsphack
  \ifx#1\@undefined
    \PackageError{abcintex}{No prefix named #1.}%
  \fi
  \let\abc@setting@prefix#1
  \let\do\@makeother\dospecials
  \catcode`\^^M\active
  \def\verbatim@processline{%
    \abc@toks@strip@spaces{\the\verbatim@line}%
  }%
  \verbatim@start%
}{%
  \global\expandafter\abc@setting@prefix\expandafter{\the\abc@setting@prefix}%
  \@esphack
}
%
%
\newtoks\abc@pstoks
{\catcode`\|=14\catcode`\%=12
 \gdef\abc@addcmttoks#1#2{|
   \if1\the#11
     #1{%%#2}|
   \else
     \expandafter#1\expandafter{\the#1^^J%%#2}|
   \fi
 }|
}
\def\abc@add@pstoks#1{%
  \expandafter\abc@pstoks\expandafter{\the\abc@pstoks#1}%
}

\newcommand{\abc@trivial@pskey}[2][]{%
  \if|#1|
    \define@key{abc2ps}{#2}{\abc@addcmttoks\abc@pstoks{#2 ##1}}%
  \else
    \define@key{abc2ps}{#2}[#1]{\abc@addcmttoks\abc@pstoks{#2 ##1}}%
  \fi
}
\newcommand{\abc@trivial@pslenkey}[2][]{%
  \if|#1|
    \define@key{abc2ps}{#2}{%
      \abc@addcmttoks\abc@pstoks{#2 }%
      \setlength{\abc@x}{##1}%
      \expandafter\abc@add@pstoks\expandafter{\the\abc@x}%
     }%
  \else
    \define@key{abc2ps}{#2}[#1]{%
      \abc@addcmttoks\abc@pstoks{#2 }%
      \setlength{\abc@x}{##1}%
      \expandafter\abc@add@pstoks\expandafter{\the\abc@x}%
    }%
  \fi
}

\abc@trivial@pslenkey{pagewidth}
\abc@trivial@pslenkey{pageheight}
\abc@trivial@pslenkey{topmargin}
\abc@trivial@pslenkey{leftmargin}
\abc@trivial@pslenkey{botmargin}
\abc@trivial@pslenkey{rightmargin}
\abc@trivial@pslenkey{staffwidth}     % width of a staff
\abc@trivial@pslenkey{staffsep}       % vertical interstaff space
\abc@trivial@pskey{scale}             % [0,1] global scale. (0.75)
\abc@trivial@pskey{maxshrink}         % [0,1] max. horiz. compression. (0.65)
\abc@trivial@pslenkey{maxstaffsep}    % max. vert. interstaff space
\abc@trivial@pslenkey{maxsysstaffsep} % max. vert. system interstaff space
\abc@trivial@pslenkey{musicspace}     % vert. space before first staff
\abc@trivial@pslenkey{partsspace}     % vert. space before new part
\abc@trivial@pskey[1]{printparts}     % print part indications (P:)
\abc@trivial@pskey[1]{partsbox}       % put part names in boxes
\abc@trivial@pskey[1]{measurebox}     % put bar numbers in boxes
\abc@trivial@pskey[0]{measurenb}   % put bar numbers (no < 0 = each line < each x bars
\abc@trivial@pskey[1]{contbarnb}   % number bars sequentially in repeat
\abc@trivial@pskey{setbarnb}       % set next bar number
\abc@trivial@pskey[1]{printtempo}  % print tempo indications (Q:)
\abc@trivial@pskey[1]{stretchstaff}% stretch underfull staves across the page
\abc@trivial@pskey[1]{stretchlast} % stretch last staff when underfull
\abc@trivial@pskey[1]{continueall} % ignore line breaks

\define@key{abc2ps}{clear}[]{\abc@pstoks{}}

\newcommand{\abc@set@pskeys}[1]{%
  \setkeys{abc2ps}{#1}%
}
\newcommand{\abcpssettings}[1]{%
  \if|#1|
    \abc@pstoks{}%
  \else
    \edef\abc@tmp@cmd{#1}%
    \expandafter\abc@set@pskeys\expandafter{\abc@tmp@cmd}%
  \fi
}

%
%
\newcommand{\abc@write@strip@spaces}[1]{%
  \let\abc@strip@finish\abc@write@abcfile
  \expandafter\abc@strip@spaces\expandafter\ #1\end
}
\newcommand{\abc@write@abcfile}{}
\def\abc@write@abcfile#1\end{%
  \immediate\write\abc@out{#1}%
}
\newcommand{\abc@strip@spaces}[2]{%
  \ifx\end#2\relax
    \let\next\relax
  \else
    \if#2 \relax
      \let\next\abc@strip@spaces
    \else
      \let\next\abc@strip@finish
    \fi
  \fi\next#2%
}

{\catcode`\ =12\gdef\parse@abc@bbox#1: #2 #3 #4 #5*{%
\def\abc@llx{#2}\def\abc@lly{#3}\def\abc@urx{#4}\def\abc@ury{#5}%
}}%

% \newcommand{\abc@build@eps@suffix}[1]{%
%   \ifnum#1<10
%     \edef\abc@eps@suffix{00#1}%
%   \else
%     \ifnum#1<100
%       \edef\abc@eps@suffix{0#1}%
%     \else
%       \edef\abc@eps@suffix{#1}%
%     \fi
%   \fi
% }
\newboolean{abc@rebuild@next}
\newread\abc@bb
\newcommand{\abc@process@file}{%
  \ifx\abc@abc\relax\else
    \immediate\write18{\abc@copy@file{\abc@src@name.abc}{\abc@abc}}%
    \global\let\abc@abc\relax
  \fi
  \ifthenelse{\(\boolean{abc@reusefiles}\or\boolean{abc@reuse@next}\)%
              \and\not\boolean{abc@rebuild@next}}{%
    \IfFileExists{\abc@res@name.bb}{%
      \ifthenelse{\boolean{abc@pdf}}{%
        \IfFileExists{\abc@res@name.pdf}{%
          \message{^^J*** Reusing \abc@res@name.pdf \on@line.^^J}%
          \let\next\relax
        }{%
          % no .pdf file
          \let\next\abc@build@files
        }%
      }{% not using PDF
        \IfFileExists{\abc@res@name.eps}{%
          \message{^^J*** Reusing \abc@res@name.eps \on@line.^^J}%
          \let\next\relax
        }{%
          % no .eps file
          \let\next\abc@build@files
        }%
      }% PDF or EPS ?
    }{% no .bb file
      \let\next\abc@build@files
    }%
  }{% not reusing files
    \let\next\abc@build@files
  }%
  \gsetboolean{abc@rebuild@next}{false}%
  \gsetboolean{abc@reuse@next}{false}%
  \ifx\next\relax
    \global\let\abc@midi\relax
  \fi
  \next
  \abc@read@bbox
}
\newcommand{\abc@build@files}{%
  \message{^^J*** Processing ABC data \abc@src@name.abc \on@line.^^J}%
  \immediate\write18{\abc@abc@ps{\abc@src@name.abc}{\abc@res@name.ps}}%
  \IfFileExists{\abc@res@name.ps}{}{%
    \PackageError{abcintex}{%
      Could not generate file \abc@res@name.ps.\MessageBreak
      Check that you use -shell-escape option.\MessageBreak
      Command to convert ABC to PS that failed is:\MessageBreak
      \space\space\abc@abc@ps{\abc@src@name.abc}{\abc@res@name.ps}
    }{%
      I could not read the PostScript file for a score.^^J
      This may be because your TeX engine does not allow to
      execute system commands with \write18.^^J
      It may also be because your ABC code contains errors.
    }%
  }%
  \message{^^J*** Finding bounding box of \abc@res@name.ps 
  \on@line.^^J}%
  \immediate\write18{\abc@ps@bb{\abc@res@name.ps}{\abc@res@name.bb}}%
  \IfFileExists{\abc@res@name.bb}{}{%
    \PackageError{abcintex}{%
      Could not generate file \abc@res@name.bb.\MessageBreak
      Check that you use -shell-escape option.\MessageBreak
      Command to compute bounding box that failed is:\MessageBreak
      \space\space\abc@ps@bb{\abc@res@name.ps}{\abc@res@name.bb}
    }{%
      I could not read the bounding box of the PostScript file for a 
      score.^^J
      This may be because your TeX engine does not allow to
      execute system commands with \write18.
    }%
  }%
  \ifthenelse{\boolean{abc@pdf}}{%
    \message{^^J*** Converting \abc@res@name.ps to PDF \on@line.^^J}%
    \immediate\write18{\abc@ps@pdf{\abc@res@name.ps}{\abc@res@name.pdf}}%
    \IfFileExists{\abc@res@name.pdf}{}{%
      \PackageError{abcintex}{%
        Could not generate file \abc@res@name.pdf.\MessageBreak
        Check that you use -shell-escape option.\MessageBreak
        Command to convert PostScript to PDF that failed 
        is:\MessageBreak
        \space\space\abc@ps@pdf{\abc@res@name.ps}{\abc@res@name.pdf}
      }{%
        I could not read the PDF file for a score.^^J
        This may be because your TeX engine does not allow to
        execute system commands with \write18.
      }%
    }%
  }{% not using PDF
    \message{^^J*** Converting \abc@res@name.ps to EPS \on@line.^^J}%
    \immediate\write18{\abc@ps@eps{\abc@res@name.ps}{\abc@res@name.eps}}%
    \IfFileExists{\abc@res@name.eps}{}{%
      \PackageError{abcintex}{%
        Could not generate file \abc@res@name.eps.\MessageBreak
        Check that you use -shell-escape option.\MessageBreak
        Command to convert PostScript to EPS that failed 
        is:\MessageBreak
        \space\space\abc@ps@eps{\abc@res@name.ps}{\abc@res@name.eps}
      }{%
        I could not read the EPS file for a score.^^J
        This may be because your TeX engine does not allow to
        execute system commands with \write18.
      }%
    }%
  }%
  \ifx\abc@midi\relax\else
    \message{^^J*** Converting \abc@src@name.abc to \abc@midi 
    \on@line.^^J}%
    \immediate\write18{\abc@abc@midi{\abc@src@name.abc}{\abc@midi}}%
    \IfFileExists{\abc@midi}{}{%
      \PackageWarning{abcintex}{%
        Could not generate file \abc@midi.\MessageBreak
        Check that you use -shell-escape option.\MessageBreak
        Command to convert ABC to MIDI that failed is:\MessageBreak
        \space\space\abc@abc@midi{\abc@src@name.abc}{\abc@midi}
      }%
    }%
    \global\let\abc@midi\relax
  \fi
  \message{^^J*** ABC processing of \abc@src@name.abc done 
  \on@line.*** ^^J}%
}
\newcommand{\abc@read@bbox}{%
  \immediate\openin\abc@bb \abc@res@name.bb
  {\endlinechar=-1
  \immediate\global\read\abc@bb to \abc@bbox}%
  \immediate\closein\abc@bb
  \expandafter\parse@abc@bbox\abc@bbox*\relax
}

\newcount\abc@file@cnt
\newwrite\abc@out
\newtoks\abc@cleanup
\newcommand{\abc@add@cleanup@cmd}[1]{%
  \global\expandafter\abc@cleanup\expandafter{\the\abc@cleanup\abc@cleanup@file{#1}}%
}
\newcommand{\abc@add@cleanup@file}[1]{%
  \global\expandafter\abc@cleanup\expandafter{\the\abc@cleanup{#1}}%
}
\newboolean{abc@keep@next}
\newboolean{abc@reuse@next}
\newcommand{\abc@read@data}{%
  \abc@build@abc@name
  \ifthenelse{\boolean{abc@keep@next}\or\boolean{abc@reuse@next}}{%
    % don't trash this one
  }{%
    \expandafter\abc@add@cleanup@cmd\expandafter{\abc@src@name}%
    \expandafter\abc@add@cleanup@file\expandafter{\abc@res@name}%
  }%
  \gsetboolean{abc@keep@next}{false}%
  \immediate\openout\abc@out\abc@src@name.abc
  \ifx\abc@prefix\relax\else
    \if|\the\abc@prefix|\else
      \immediate\write\abc@out{\the\abc@prefix}%
    \fi
  \fi
  \if|\the\abc@pstoks|\else
    \immediate\write\abc@out{\the\abc@pstoks}%
  \fi
  \ifnum\abc@songnum>0
    \immediate\write\abc@out{X:\the\abc@songnum}%
  \fi
  \if|\the\abc@abctoks|\else
    \immediate\write\abc@out{\the\abc@abctoks}%
  \fi
  \ifx\abc@prelude\relax\else
    \if|\the\abc@prelude|\else
      \immediate\write\abc@out{\the\abc@prelude}%
    \fi
  \fi
  \let\do\@makeother\dospecials
  \catcode`\^^M\active
  \def\verbatim@processline{%
    \abc@write@strip@spaces{\the\verbatim@line}%
  }%
  \verbatim@start%
}

\newcommand{\abc@cleanup@file}[2]{%
  \immediate\write18{\abc@del@file{#1.abc}}%
  \immediate\write18{\abc@del@file{#2.ps}}%
  \immediate\write18{\abc@del@file{#2.bb}}%
  \ifthenelse{\boolean{abc@pdf}}{%
    \immediate\write18{\abc@del@file{#2.pdf}}%
  }{%
    \immediate\write18{\abc@del@file{#2.eps}}%
  }%
}

\newcommand*{\abc@build@abc@name}{%
  \global\advance\abc@file@cnt1
  \edef\abc@src@name{abcintex-tmp\the\abc@file@cnt}%
  \edef\abc@res@name{abcintex-tmp\the\abc@file@cnt}%
}
  
\newtoks\abc@boxtoks
\newenvironment{abc}{%
  \abc@read@data
}{%
  \immediate\closeout\abc@out
  \abc@process@file
  \abc@boxtoks{}%
  \ifx\abc@box@width\relax\else
    \abc@boxtoks{\makebox[\abc@box@width]}%
    \ifx\abc@box@hpos\relax\else
      \expandafter\abc@boxtoks\expandafter{\the\abc@boxtoks[\abc@box@hpos]}%
    \fi
  \fi
  \the\abc@boxtoks{%
  \abc@boxtoks{}%
  \ifx\abc@box@raise\relax\else
    \abc@boxtoks{\raisebox{\abc@box@raise}}%
    \ifx\abc@box@height\relax\else
      \expandafter\abc@boxtoks\expandafter{\the\abc@boxtoks[\abc@box@height]}%
      \ifx\abc@box@depth\relax\else
        \expandafter\abc@boxtoks\expandafter{\the\abc@boxtoks[\abc@box@depth]}%
      \fi
    \fi
  \fi
  \the\abc@boxtoks{%
    \includegraphics[viewport={\abc@llx} {\abc@lly} {\abc@urx} 
    {\abc@ury}]{%
      \abc@res@name
    }%
  }%
  }%
}
\newenvironment{abcbox}[1]{%
  \let\abc@box=#1%
  \abc@read@data
}{%
  \immediate\closeout\abc@out
  \abc@process@file
  \global\sbox{\abc@box}{%
    \includegraphics[viewport={\abc@llx} {\abc@lly} {\abc@urx} 
    {\abc@ury}]{%
      \abc@res@name
    }%
  }%
}

\newboolean{abc@drawgrid}
\newlength{\abc@x@unit}
\newlength{\abc@y@unit}
\newlength{\abc@x}
\newlength{\abc@y}
\newlength{\abc@top@pic}
\newlength{\abc@left@pic}
\newlength{\abc@bot@pic}
\newlength{\abc@right@pic}
\newtoks{\abc@under@music}
\define@key{abcpic}{top}{\setlength{\abc@top@pic}{#1}}
\define@key{abcpic}{left}{\setlength{\abc@left@pic}{#1}}
\define@key{abcpic}{bottom}{\setlength{\abc@bot@pic}{#1}}
\define@key{abcpic}{right}{\setlength{\abc@right@pic}{#1}}
\define@key{abcpic}{under}{\abc@under@music={#1}}
\define@key{abcpic}{grid}[]{%
  \def\abc@picgrid@parms{#1}\setboolean{abc@drawgrid}{true}%
}

\newenvironment{abcannotate}[2][]{%
  \setboolean{abc@drawgrid}{false}%
  \setlength{\abc@x@unit}{\wd#2}%
  \setlength{\abc@y@unit}{\ht#2}%
  \setlength{\unitlength}{1pt}%
  \def\height{\abc@y@unit}%
  \def\width{\abc@x@unit}%
  \def\depth{\dp#2}%
  \let\totalheight\@ovri
  \totalheight\height\advance\totalheight\depth
  \setlength{\abc@top@pic}{0pt}%
  \setlength{\abc@left@pic}{0pt}%
  \setlength{\abc@bot@pic}{0pt}%
  \setlength{\abc@right@pic}{0pt}%
  \setkeys{abcpic}{#1}%
  \def\rput(##1,##2){%
    \setlength{\abc@x}{##1\abc@x@unit}%
    \setlength{\abc@y}{##2\abc@y@unit}%
    \put(\strip@pt\abc@x,\strip@pt\abc@y)%
  }%
  \vspace*{\abc@top@pic}\leavevmode\hspace*{\abc@left@pic}%
  \begin{picture}(\strip@pt\abc@x@unit,\strip@pt\abc@y@unit)(0,0)
    \the\abc@under@music
    \put(0,0){\usebox{#2}}%
    \ifthenelse{\boolean{abc@drawgrid}}{\abc@gridpaper}{}%
}{%
  \end{picture}\hspace*{\abc@right@pic}\vspace*{\abc@bot@pic}%
  \abc@under@music={}%
}

\define@key{abcgrid}{xdiv}{\def\abc@x@div{#1}}
\define@key{abcgrid}{ydiv}{\def\abc@y@div{#1}}
\define@key{abcgrid}{xtick}{\def\abc@x@tick{#1}}
\define@key{abcgrid}{ytick}{\def\abc@y@tick{#1}}

\newcount\abc@tmp@cnt
\newcommand{\abc@gridpaper}{%
  \def\abc@x@div{50}\def\abc@y@div{1}%
  \def\abc@x@tick{10}\def\abc@y@tick{10}%
  \def\abc@set@gridkeys##1{\setkeys{abcgrid}{##1}}%
  \expandafter\abc@set@gridkeys\expandafter{\abc@picgrid@parms}%
  %
  \setlength{\abc@x}{\width}%
  \edef\abc@w{\strip@pt\abc@x}%
  \setlength{\abc@y}{\height}%
  \edef\abc@h{\strip@pt\abc@y}%
  %
  \put(0,0){\line(1,0){\abc@w}}%
  \put(0,0){\line(0,1){\abc@h}}%
  %
  \setlength{\abc@x}{\abc@x@unit}%
  \divide\abc@x\abc@x@div
  \edef\step@x{\strip@pt\abc@x}%
  \setlength{\abc@y}{\abc@y@unit}%
  \divide\abc@y\abc@y@div
  \edef\step@y{\strip@pt\abc@y}%
  %
  \abc@tmp@cnt\abc@x@div\advance\abc@tmp@cnt1
  \edef\abc@x@rep{\the\abc@tmp@cnt}%
  \abc@tmp@cnt\abc@y@div\advance\abc@tmp@cnt1
  \edef\abc@y@rep{\the\abc@tmp@cnt}%
  {\thinlines\color[rgb]{0.4,0.0,0.0}%
  \multiput(0,0)(\step@x,0){\abc@x@rep}{\line(0,1){\abc@h}}%
  \multiput(0,0)(0,\step@y){\abc@y@rep}{\line(1,0){\abc@w}}%
  }%
  \setlength{\abc@x}{\abc@x@unit}%
  \divide\abc@x\abc@x@tick
  \edef\step@x{\strip@pt\abc@x}%
  \setlength{\abc@y}{\abc@y@unit}%
  \divide\abc@y\abc@y@tick
  \edef\step@y{\strip@pt\abc@y}%
  %
  \abc@tmp@cnt\abc@x@tick\advance\abc@tmp@cnt1
  \edef\abc@x@rep{\the\abc@tmp@cnt}%
  \abc@tmp@cnt\abc@y@tick\advance\abc@tmp@cnt1
  \edef\abc@y@rep{\the\abc@tmp@cnt}%
  \setlength{\abc@x}{\abc@h\unitlength}%
  \addtolength{\abc@x}{4pt}%
  \edef\abc@h{\strip@pt\abc@x}%
  \setlength{\abc@y}{\abc@w\unitlength}%
  \addtolength{\abc@y}{4pt}%
  \edef\abc@w{\strip@pt\abc@y}%
  {\thinlines\color[rgb]{0.8,0.0,0.0}%
  \abc@tmp@cnt0
  \multiput(0,-2)(\step@x,0){\abc@x@rep}{%
    \begin{picture}(0,0)(0,0)
      \put(0,-3){\makebox(0,0)[t]{\the\abc@tmp@cnt}\global\advance\abc@tmp@cnt1}
      \put(0,0){\line(0,1){\abc@h}}%
    \end{picture}
  }%
  \abc@tmp@cnt0
  \multiput(-2,0)(0,\step@y){\abc@y@rep}{%
    \begin{picture}(0,0)(0,0)
      \put(-3,0){\makebox(0,0)[r]{\the\abc@tmp@cnt}\global\advance\abc@tmp@cnt1}
      \put(0,0){\line(1,0){\abc@w}}%
    \end{picture}
  }%
  }%
}

\AtEndDocument{%
  \ifthenelse{\boolean{abc@keepfiles}}{%
    % keep files
  }{%
    % trash files
    \the\abc@cleanup
  }
}

\endinput
