% \iffalse meta-comment
%
% Copyright (C) 2014 by Julien Cretel
%                       <jubobs.tex at gmail.com>
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%
%     http://www.latex-project.org/lppl.txt
%
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% \fi
%
% \iffalse
%<package>\NeedsTeXFormat{LaTeX2e}[2011/06/27]
%<package>\ProvidesPackage{sclang-prettifier}
%<package>  [2014/06/14 v0.1 A package for prettyprinting SuperCollider
%<package>   source code]
%
%<*driver>
\documentclass[a4paper]{ltxdoc}
\EnableCrossrefs
\CodelineIndex
\RecordChanges

\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{xcolor}
\usepackage{lstdoc}
\usepackage[framed,numbered]{sclang-prettifier}
\usepackage{hyperref}
\usepackage{cleveref}

\lstset
{
  style      = SuperCollider-IDE,
  basicstyle = \scttfamily\small,
  upquote    = true,
}

\lstdefinestyle{nonbnoframe}
{
  frame   = none,
  numbers = none,
}

\newcommand*{\pkg}[1]{\textsf{#1}}
\newcommand*{\opt}[1]{\texttt{#1}}
\newcommand\SC{SuperCollider}
\newcommand\scpkg{\textsf{sclang-prettifier}}
\newcommand\lstpkg{\textsf{listings}}
\newcommand\scsty{\texttt{SuperCollider-IDE}}
\newcommand\sclng{\texttt{SuperCollider}}
\newcommand*{\sccode}[1]{%
  \lstinline[style=SuperCollider-IDE,basicstyle=\scttfamily]|#1|
}
\makeindex

\begin{document}
  \DocInput{sclang-prettifier.dtx}
\end{document}
%</driver>
% \fi
%
% \CheckSum{199}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
% \changes{v0.1}{2014/04/24}{Initial release.}
%
% \GetFileInfo{sclang-prettifier.sty}
%
%  ^^A see http://www.latex-project.org/cgi-bin/ltxbugs2html?pr=latex%2F3540
%  \begingroup
%    \def\x{\#,\$,\%,\^,\_,\~,\&,\{,\},\/}%^^A
%    \makeatletter
%    \@onelevel@sanitize\x
%  \expandafter\endgroup\expandafter\DoNotIndex\expandafter{\x}
%
% \begingroup
%    \makeatletter
%    \lccode`9=32\relax
%    \lowercase{%^^A
%      \edef\x{\noexpand\DoNotIndex{\@backslashchar9}}%^^A
%    }%^^A
%  \expandafter\endgroup\x
%
% \DoNotIndex{\@ifpackagewith,\@ne,\@tempa,\\}
% \DoNotIndex{\advance}
% \DoNotIndex{\begingroup,\bfseries}
% \DoNotIndex{\color,\CurrentOption}
% \DoNotIndex{\DeclareOption,\def}
% \DoNotIndex{\edef,\else,\endgroup,\expandafter}
% \DoNotIndex{\fi,\fontfamily,\framed@scpr@true}
% \DoNotIndex{\ifnum,\ifx,\iterate}
% \DoNotIndex{\let,\loop,\lst@AddToHook,\lst@Key,\lst@language,\lst@mode,^^A
%   \lst@NormedDef,\lst@Pmode,\lst@SaveOutputDef,\lst@thestyle,\lst@token,^^A
%   \lstdefinelanguage,\lstdefinestyle,\lsthk@DetectKeywords,\lstum@backslash}
% \DoNotIndex{\newcommand,\newcount,\newif,\newtoks,\noexpand,\normalsize,^^A
%   \numbered@scpr@true}
% \DoNotIndex{\OptionNotUsed}
% \DoNotIndex{\PackageError,\PackageWarning,\PassOptionsToPackage,^^A
%   \ProcessOptions}
% \DoNotIndex{\relax,\renewcommand,\repeat,\RequirePackage}
% \DoNotIndex{\selectfont}
% \DoNotIndex{\the,\ttfamily}
% \DoNotIndex{\unless}
%
% \title^^A
% {^^A
%   The \scpkg{} package^^A
%   \thanks^^A
%   {^^A
%     This document corresponds to \scpkg~\fileversion,
%     dated~\filedate.^^A
%   }^^A
%  }
% \author{Julien Cretel\\ \texttt{jubobs.tex at gmail.com}}
% \date{\filedate}
% \thispagestyle{empty}
% \maketitle
%
% \begin{abstract}
%   Built on top of the \lstpkg{} package, the \scpkg{} package allows you to
%   effortlessly prettyprint \SC{} source code in documents typeset with
%   \LaTeX{} \& friends. 
% \end{abstract}
%
% \tableofcontents\newpage
%
%
% \part{Introduction}
%
% \section{Why this package?}
%
% \href{http://supercollider.github.io}{\SC} is a programming
% language for real-time audio synthesis and algorithmic composition.
% In February 2014, James Harkins, a \SC{} user,
% enquired on \href{http://tex.stackexchange.com}{TeX.SX}
% (\href{http://tex.stackexchange.com/q/159245/21891}{here} and
% \href{http://tex.stackexchange.com/q/159246/21891}{here})
% about the possibility of using the \lstpkg{} package to automatically
% highlight syntactic elements of the \SC{} language such as symbols,
% environment variables, and classes, without having to list them manually as
% \lstpkg{} keywords.
% My answers to James's questions form the basis of this package.
%
%
% \section{\scpkg{} in action}
%
% The \scpkg{} package defines a \lstpkg{} style, called
% \scsty{}, that mimics the style of the \SC{} IDE.
% For an example, see \cref{SCsample}, which is adapted from this
% \href{http://tex.stackexchange.com/q/159245/21891}{TeX.SX question}.
%^^A
% \iffalse
%<*example>
% \fi
\begin{lstlisting}[
  float,
  label=SCsample,
  caption={Some dummy \SC{} code},
]
p.clear;

"Hello World!".postln;
~grains.addSpec(\tfreq, [1, 40, \exp]);
~grains.addSpec(\overlap, [0.1, 10, \exp]);
~grains.addSpec(\pos, [0, b.duration]);  // 3.43 is nice!
~grains.addSpec(\rate, [0.5, 2, 'exp']);
/*
  Dummy block comment.
*/
~grains = { |tfreq = 25, overlap = 6, pan = 0, amp = 0.2, pos = 3.43,
   rate = 1|
   var trig = Impulse.ar(tfreq);
   TGrains.ar(2, trig, b, rate, pos, overlap / tfreq, pan, amp)
};
~grains.play;
\end{lstlisting}
% \iffalse
%</example>
% \fi
% 
% The \scpkg{} package automatically
% highlights the following syntactic elements of the \SC{} language.
%
% \paragraph{Keywords}
% \sccode{var}
% 
% \paragraph{To-end-of-line and block comments}
% \sccode{// 3.43 is nice!}
% 
% \paragraph{Symbols}
% \lstinline[style=SuperCollider-IDE]|\tfreq|,
% \lstinline[style=SuperCollider-IDE]|'exp'|, etc.
%
% \paragraph{Environment variables}
% \lstinline[style=SuperCollider-IDE]|~grains|
% 
% \paragraph{Classes}
% \lstinline[style=SuperCollider-IDE]|Impulse|,
% \lstinline[style=SuperCollider-IDE]|TGrains|, etc.
% 
%
% \part{User's guide}
%
% \section{Installation}
%
% \subsection{Package dependencies}
%
% \scpkg{} requires relatively up-to-date versions of packages \pkg{textcomp},
% \pkg{xcolor}, and \lstpkg{}, all three of which ship with popular \TeX{}
% distributions. It loads those three packages without any options.
%
% \subsection{Installing \scpkg{}}
%
% Once the package gets officially released on
% \href{http://www.ctan.org}{CTAN},
% you should be able to install it directly through your package manager.
%
% However, if you need to install \scpkg{} manually, you should run
%^^A
% \begin{verbatim}
%   latex sclang-prettifier.ins\end{verbatim}
%^^A
% and copy the |sclang-prettifier.sty| file to a path
% where \LaTeX{} (or your preferred typesetting engine) can find it.
% To generate the documentation, run
%^^A
% \begin{verbatim}
%   pdflatex sclang-prettifier.dtx
%   makeindex -s gglo.ist -o sclang-prettifier.gls sclang-prettifier.glo
%   makeindex -s gind.ist -o sclang-prettifier.ind sclang-prettifier.idx
%   pdflatex sclang-prettifier.dtx
%   pdflatex sclang-prettifier.dtx\end{verbatim}
%^^A
%
%
% \section{Getting started}
%
% As stated above, the \scpkg{} package is built on top of the \lstpkg{}
% package.
% If you already are a seasoned \lstpkg{} user, you should feel right at home.
% If you're not, be aware that this user's guide makes use of some \lstpkg{}
% functionalities (such as key-value options) without describing their usage.
% For more details on those functionalities, you should consult the
% \href{http://www.ctan.org/pkg/listings}{\lstpkg{} documentation}.
%
%
% \subsection{Loading \texorpdfstring{\scpkg{}}{sclang-prettifier}}
%
% Simply write
%^^A
% \begin{verbatim}
%   \usepackage{sclang-prettifier}\end{verbatim}
%^^A
% somewhere in your preamble.
%
% You may want to load the \lstpkg{} and
% \pkg{xcolor} packages with some options; in that case, make sure those
% options are passed to those two packages \emph{before} loading the \scpkg{}
% package.
%
% The \scpkg{} package currently offers two options.
%^^A
% \begin{description}
% \item[\opt{framed}]\leavevmode
%
%       Draws (by default) a dark gray frame around each listing that uses
%       the \scsty{} style.
%
% \item[\opt{numbered}]\leavevmode
%
%       Prints (by default) line numbers in light gray to the left of each
%       listing that uses the \scsty{} style.
%
% \end{description}
%
% \subsection{Displayed listings}
%
% To typeset a \SC{} listing embedded in your |tex| file, simply enclose
% it in an |lstlisting| environment, and load the \scsty{}
% style in the environment's optional argument,
% using \lstpkg{}' \keyname{style} key.
%^^A
% \begin{verbatim}
%   \begin{lstlisting}[style=SuperCollider-IDE]
%     ...
%   \end{lstlisting}\end{verbatim}
%
%
% \subsection{Standalone listings}
%
% In practice, though, keeping your \SC{} listings in external files^^A
% ---rather than embedding them in a |tex|~file---^^A
% is preferable, for maintainability reasons.
% To typeset a \SC{} listing residing in an |sc| (or |scx|, or |sco|) file,
% simply invoke the
% |\lstinputlisting| macro, load the \scsty{} style
% in the environment's optional argument,
% and specify the path to the file in question in the mandatory argument.
%^^A
% \begin{verbatim}
%   \lstinputlisting[style=SuperCollider-IDE]{sample.sc}\end{verbatim}
%^^A
%
%
% \subsection{Inline listings}
%
% You may want to typeset fragments of \SC{} code within the main text of
% your document. For instance, you may want to typeset the
% \lstinline[style=SuperCollider-IDE]|var| keyword in a sentence,
% in order to explain its usage.
% The |\lstinline| macro can be used for typesetting such inline code.
%^^A
% \begin{verbatim}
%   \lstinline[style=SuperCollider-IDE]|var|\end{verbatim}
%^^A
% Arguably, typing all this only to typeset such a simple \SC{} keyword can
% rapidly become tedious.
% Fortunately, the \lstpkg{} allows you to define a character as a shorthand
% for inline code, via the |\lstMakeShortInline| macro.
% However, this character should ideally neither be used by the language
% itself nor occur elsewhere in your document.
% Unfortunately, because the \SC{} language already uses most (all?) ASCII
% characters, your choice is limited\ldots{}
% Proceed with caution.
% For more details about inline code,
% see subsection~4.17 in the \lstpkg{} manual.
%
%
% \section{Advanced customization}
% 
% \subsection{\scpkg{}'s key-value interface}
%
% The \lstpkg{} package provides a large number of options accessible via a
% nifty key-value interface, which is described in its excellent
% \href{http://www.ctan.org/pkg/listings}{documentation}.
% The \scpkg{} package extends \lstpkg{}' key-value interface interface by
% defining three additional keys that allow you to customize the styles
% applied to \SC{} symbols, environment variables, and classes,
% should you wish to do so.
% All three keys are prefixed by ``|sc|'',
% to help you distinguish them from native \lstpkg{} keys.
%
% For each of the three keys described below,
% the value assigned to it in the \scsty{} style is indicated
% on the right-hand side.
%^^A
% \begin{syntax}
%
%   \item[0.1,{\color[RGB]{0,113,0}}]
%     \rkeyname{scsymbolstyle}|=|\meta{style}
%
%     This key determines the style applied to \SC{} symbols.
%     The last token can be a one-parameter command,
%     such as |\textbf| or |\underbar|.
%
%   \item[0.1,{\color[RGB]{147,70,14}}]
%     \rkeyname{scenvvarstyle}|=|\meta{style}
%
%     This key determines the style applied to \SC{} environment variables.
%     The last token can be a one-parameter command,
%     such as |\textbf| or |\underbar|.
%
%   \item[0.1,{\color[RGB]{0,40,211}}]
%     \rkeyname{scclassstyle}|=|\meta{style}
%
%     This key determines the style applied to \SC{} classes.
%     The last token can be a one-parameter command,
%     such as |\textbf| or |\underbar|.
%
% \end{syntax}
%
%
% \subsection{Changing the font of your \texorpdfstring{\SC}{SuperCollider}
%   listings}
% 
% The \scpkg{} package uses the Computer Modern typewriter font by default,
% which, arguably, is far from ideal.
% I encourage you to switch to your favourite ``programmer font'' instead.
%
% For |pdflatex| users,
% \scpkg{} conveniently provides a macro for easily selecting the Courier
% font---which is used by default by the \SC{} IDE.
%^^A
% \begin{syntax}
%
%   \item[0.1] \rcmdname\scttfamily
%
%     selects the Courier font.
%
% \end{syntax}
%^^A
% To use Courier in your \SC{} listings, you must pass |\scttfamily| to
% \lstpkg{}' \keyname{basicstyle} key (\emph{after} loading the
% \scsty{} style) and also---this is important---^^A
% load the \pkg{fontenc} package with option \opt{T1}:
%^^A
% \begin{verbatim}
%   \usepackage[T1]{fontenc}\end{verbatim}
% 
%
% \part{Miscellaneous}
% 
% \section{Missing features and known issues}
%
% The \scpkg{} currently does not highlight numbers as the \SC{} IDE does.
% Highlighting numbers in \lstpkg{} in a robust manner is notoriously
% difficult;
% I might implement a solution in the future, if I ever find a good one.
%
%
% \section{Bug reports and feature suggestions}
%
% The development version of \scpkg{} is currently hosted on GitHub at
% \href{https://github.com/Jubobs/sclang-prettifier/}
%   {Jubobs/sclang-prettifier}.
% If you find an issue in \scpkg{} that this manual does not mention,
% if you would like to see a feature implemented in the package,
% or if you can think of ways in which the \scpkg{} documentation could be
% improved, please add an entry to the repository's issue tracker on GitHub;
% alternatively, you can send me an email at
% \href{mailto:jubobs.tex@gmail.com}{jubobs.tex@gmail.com}
%
% \makeatletter
% \def\index@prologue{\part{Index}\markboth{Index}{Index}}
% \makeatother
% \StopEventually{^^A
%   \clearpage^^A
%   \PrintChanges^^A
%   \setcounter{IndexColumns}{2}^^A
%   \PrintIndex^^A
% }
%
% \section{Acknowledgments}
% Thanks to the developers of the \lstpkg{} package,
% without which \scpkg{} would never have existed.
% I'm also in debt to many
% \href{http://tex.stackexchange.com}{TeX.SX} users for their help,
% encouragements, and suggestions.
% Thanks in particular to
% James Harkins, whose questions inspired me to write this package,
% and to Marco Daniel, Enrico Gregorio (egreg), and Heiko Oberdiek,
% whose contributions to TeX.SX proved particularly helpful for the
% development of this package.
%
%
% \part{Implementation}
%
% Be aware that, for ``namespacing'', the \scpkg{} package uses, not a prefix,
% but the ``|scpr|''~suffix (preceded by an |@| character) throughout.
%
%
% \section{Preliminary checks}
%
% \begin{macro}{\lstoptcheck@scpr}
%   Because the \lstpkg{} options \opt{noaspects}, \opt{0.21}, and
%   \opt{savemem} are incompatible with \pkg{sclang-prettifier}, checking
%   whether the \lstpkg{} package has been loaded with any of those options is
%   a good idea; if so, we should issue an error.
%   This macro checks whether \lstpkg{} was loaded with a given option
%   and, if so, throws an error.
%    \begin{macrocode}
\newcommand\lstoptcheck@scpr[1]
{%
  \@ifpackagewith{listings}{#1}%
  {
    \PackageError{sclang-prettifier}%
      {incompatible listings' option #1}%
      {%
        Make sure the `listings' package
        doesn't get loaded with option `#1'%
      }
  }
  {}
}
%    \end{macrocode}
% \end{macro}
% We now use this macro to make sure that none of the problematic \lstpkg{}
% options has been passed to \lstpkg{} during an earlier loading of that
% package.
%    \begin{macrocode}
\lstoptcheck@scpr{noaspects}
\lstoptcheck@scpr{0.21}
\lstoptcheck@scpr{savemem}
%    \end{macrocode}
%
%
% \section{Package options}
%
% \paragraph{Framed listings}
% \begin{macro}{\ifframed@scpr@}
% This option draws (by default) a frame around each listing that uses 
% the \scsty{} style.
%    \begin{macrocode}
\newif\ifframed@scpr@
\DeclareOption{framed}{\framed@scpr@true}
%    \end{macrocode}
% \end{macro}
%
% \paragraph{Numbered lines}
% \begin{macro}{\ifnumbered@scpr@}
% This option prints (by default) line numbers to the left of each listing
% that uses the \scsty{} style.
%    \begin{macrocode}
\newif\ifnumbered@scpr@
\DeclareOption{numbered}{\numbered@scpr@true}
%    \end{macrocode}
% \end{macro}
%
% \paragraph{Draft}
% This option is simply passed to \lstpkg{}.
%    \begin{macrocode}
\DeclareOption{draft}{\PassOptionsToPackage{\CurrentOption}{listings}}
%    \end{macrocode}
%
% \paragraph{Final}
% This option is simply passed to \lstpkg{}.
%    \begin{macrocode}
\DeclareOption{final}{\PassOptionsToPackage{\CurrentOption}{listings}}
%    \end{macrocode}
%
% \paragraph{Discard undefined options}
% We discard any other option passed to \scpkg{} by the user
% and issue a warning.
%    \begin{macrocode}
\DeclareOption*%
{%
  \OptionNotUsed
  \PackageWarning{sclang-prettifier}{Unknown `\CurrentOption' option}
}
%    \end{macrocode}
% \paragraph{Process options}
%    \begin{macrocode}
\ProcessOptions\relax
%    \end{macrocode}
%
%
% \section{Required packages}
% 
% The \scpkg{} package require three packages without any package option:
% the \pkg{textcomp} package, in order to use \lstpkg{}' \keyname{upquote}
% key;
% the \pkg{xcolor} package, in order to color our \SC{} code;
% and, of course, the \lstpkg{} package.
%    \begin{macrocode}
\RequirePackage{textcomp}[2005/09/27]
\RequirePackage{xcolor}[2007/01/21]
\RequirePackage{listings}[2013/08/26]
%    \end{macrocode}
%
%
% \section{Definition of the \texorpdfstring{\sclng}{SuperCollider} language}
%
% \paragraph{Language name}
% \begin{macro}{\language@scpr}
%   To avoid code duplication in this package file, we define a macro that
% expands to the name of our new language, \sclng{}.
%    \begin{macrocode}
\newcommand\language@scpr{SuperCollider}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\languageNormedDefd@scpr}
%   However, because \lstpkg{} ``normalizes'' language names internally,
%   we also need to define a macro that expands to the normalized name of
%   the new language.
%    \begin{macrocode}
\expandafter\lst@NormedDef\expandafter\languageNormedDefd@scpr%
  \expandafter{\language@scpr}
%    \end{macrocode}
% \end{macro}
%
% \paragraph{Language definition}
% We can now define our new \lstpkg{} language, using some |\expandafter|
% trickery on |\lstdefinelanguage|.
%    \begin{macrocode}
\expandafter\expandafter\expandafter\lstdefinelanguage\expandafter
{\language@scpr}
{%
  morekeywords  = {var},
  alsoletter    = \\~,
  alsoother     = @,
  sensitive     = true,
  morecomment   = [l]{//},
  morecomment   = [s]{/*}{*/},
  morestring    = [s]{"}{"},
  moredelim     = [s][\symbolStyle@scpr]{'}{'},
}[keywords,strings,comments]
%    \end{macrocode}
%
% \section{Symbols, classes and environment variables}
%
% \paragraph{Storing relevant characters}
% To detect whether an identifier is an environment variable or a symbol,
% we will need to test whether the identifier in question starts with a tilde
% or a backslash, respectively.
% \lstpkg{} developer's guide tells us that the only safe way to test
% against a character is to store it in a macro using \lstpkg{}' internal
% macro |\lst@SaveOutputDef|.
% \begin{macro}{\tilde@scpr}
% We save the tilde character thus.
%    \begin{macrocode}
\lst@SaveOutputDef{`~}\tilde@scpr
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\dollar@scpr}
% We save the dollar-sign character thus.
%    \begin{macrocode}
\lst@SaveOutputDef{`$}\dollar@scpr
%    \end{macrocode}
% \end{macro}
% 
% No need for such definition for the backslash:
% \lstpkg{} already stores the backslash in a macro called |\lstum@backslash|.
%
%
% \section{Using into \lstpkg{}' hooks}
%
% We apply some necessary patches in two \lstpkg{}' hooks;
% but first, we define a couple of helper macros.
%
% \paragraph{Helper macros}
% \begin{macro}{\getfirstchar@scpr}
% \begin{macro}{\getfirstchar@@scpr}
% \begin{macro}{\firstchar@scpr}
% Of these three helper macros, the first two macros extract the
% first character token in a given sequence of character tokens
% and store it in the third macro.
% This approach is adapted from this
% \href{http://tex.stackexchange.com/a/159267/21891}
%   {TeX.SX answer by Marco Daniel}.
%    \begin{macrocode}
\newcommand\getfirstchar@scpr{}
\newcommand\getfirstchar@@scpr{}
\newcommand\firstchar@scpr{}
\def\getfirstchar@scpr#1{\getfirstchar@@scpr#1\relax}
\def\getfirstchar@@scpr#1#2\relax{\def\firstchar@scpr{#1}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \paragraph{\hookname{Output}}
% (See the
% \href{http://www.ctan.org/pkg/listings}{\lstpkg{} documentation} 
% for more details on this hook.)
% \begin{macro}{\addedToOutput@scpr}
% We add this macro (initially empty) to \lstpkg{}'
% \hookname{Output} hook.
%    \begin{macrocode}
\newcommand\addedToOutput@scpr{}
\lst@AddToHook{Output}{\addedToOutput@scpr}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\currentchar@scpr}
% This count is used to test a character token against A-Z.
%    \begin{macrocode}
\newcount\currentchar@scpr
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@ddedToOutput@scpr}
% The |\addedToOutput@scpr}| macro is let to this one under certain
% conditions (more details follow).
%    \begin{macrocode}
\newcommand\@ddedToOutput@scpr
{%
%    \end{macrocode}
% If we're in \lstpkg{}' processing mode\ldots
%    \begin{macrocode}
  \ifnum\lst@mode=\lst@Pmode%
%    \end{macrocode}
% \ldots{} we save the first character token in the identifier being processed
% to a macro called |\firstchar@scpr|.
%    \begin{macrocode}
    \expandafter\getfirstchar@scpr\expandafter{\the\lst@token}%
%    \end{macrocode}
% If that token is a backslash, we apply the style associated to symbols.
%    \begin{macrocode}
    \expandafter\ifx\firstchar@scpr\lstum@backslash%
      \let\lst@thestyle\symbolStyle@scpr%
%    \end{macrocode}
% If that token is a dollar sign, we have a SuperCollider ``character''.
% we apply the style associated to symbols (as in the \SC{} IDE).
%    \begin{macrocode}
    \else
      \expandafter\ifx\firstchar@scpr\dollar@scpr%
        \let\lst@thestyle\symbolStyle@scpr%
%    \end{macrocode}
% If that that token is a tilde, we apply the style associated to environment
% variables.
%    \begin{macrocode}
      \else
        \expandafter\ifx\firstchar@scpr\tilde@scpr%
          \def\lst@thestyle{\envvarStyle@scpr}%
%    \end{macrocode}
% Otherwise, if that character is a capital letter (A-Z), we apply the style
% associated to classes.
%    \begin{macrocode}
        \else
          \currentchar@scpr=65
          \loop
            \expandafter\ifnum%
            \expandafter`\firstchar@scpr=\currentchar@scpr%
              \let\lst@thestyle\classStyle@scpr%
              \let\iterate\relax%
            \fi
            \advance\currentchar@scpr by \@ne%
            \unless\ifnum\currentchar@scpr>90%
          \repeat%
        \fi
      \fi    
    \fi
  \fi
%    \end{macrocode}
% Whatever style was applied, we still check whether the identifier is a
% keyword; if it is one, the keyword style is applied to it.
%    \begin{macrocode}
  \lsthk@DetectKeywords% 
}
%    \end{macrocode}
% \end{macro}
%
% \paragraph{\hookname{PreInit}}
% (See the
% \href{http://www.ctan.org/pkg/listings}{\lstpkg{} documentation} 
% for more details on this hook.)
% Because the |\lst@AddToHook| affects hooks globally
% (i.e.\ for all listings),
% we must apply our patches only when required, i.e.\ 
% in listings that use \sclng{}, and not in others.
% The \hookname{PreInit}, which is called at the very beginning of each
% listing, is where we do that.
% We check
% whether |\lst@language| and |\languageNormedDefd@scpr| expand (once) to the
% same replacement text and only apply our patches under that condition.
%    \begin{macrocode}
\lst@AddToHook{PreInit}
{%
  \ifx\lst@language\languageNormedDefd@scpr%
    \let\addedToOutput@scpr\@ddedToOutput@scpr%
  \fi
}
%    \end{macrocode}
%
%
% \section{Key-value interface}
%
% We extend \lstpkg{}' key-value interface by defining several additional
% keys, which we will use to define a style similar to that of the \SC{} IDE,
% and which will allow the user to customize the style of their \SC{}
% listings.
%
% \paragraph{Symbol style}
% \begin{lstkey}{scsymbolstyle}
% \begin{macro}{\classStyle@scpr}
% This key determines the style applied to \SC{} symbols.
%    \begin{macrocode}
\newcommand\symbolStyle@scpr{}
\lst@Key{scsymbolstyle}\relax%
  {\def\symbolStyle@scpr{#1}}
%    \end{macrocode}
% \end{macro}
% \end{lstkey}
%
% \paragraph{Environment-variable style}
% \begin{lstkey}{scenvvarstyle}
% \begin{macro}{\envvarStyle@scpr}
% This key determines the style applied to \SC{} environment variables.
%    \begin{macrocode}
\newcommand\envvarStyle@scpr{}
\lst@Key{scenvvarstyle}\relax%
  {\def\envvarStyle@scpr{#1}}
%    \end{macrocode}
% \end{macro}
% \end{lstkey}
% 
% \paragraph{Class style}
% \begin{lstkey}{scclassstyle}
% \begin{macro}{\classStyle@scpr}
% This key determines the style applied to \SC{} classes.
%    \begin{macrocode}
\newcommand\classStyle@scpr{}
\lst@Key{scclassstyle}\relax%
  {\def\classStyle@scpr{#1}}
%    \end{macrocode}
% \end{macro}
% \end{lstkey}
%
%
% \section{User-level font macro}
%
% \begin{macro}{\scttfamily}
% This user-level macro can be used for selecting the Courier font family,
% which is used by default in the \SC{} IDE (v3.6.6, at least), and which,
% contrary to \TeX{} default font family (Computer Modern),
% comes with a boldface version.
%    \begin{macrocode}
\newcommand\scttfamily{\fontfamily{pcr}\selectfont}
%    \end{macrocode}
% \end{macro}
%
%
% \section{\texorpdfstring{\scsty{}}{SuperCollider-IDE} style}
% 
% The \scsty{} style mimics the default style of the \SC{} IDE.
%
% \begin{macro}{\toks@scpr}
% We allocate a token list register in which we store settings that we'll use
% to define the style.
%    \begin{macrocode}
\newtoks\toks@scpr
\toks@scpr=%
{
  language               = \languageNormedDefd@scpr,
  basicstyle             = \color{black}\ttfamily\normalsize,
  breaklines             = true,
  showspaces             = false,
  showstringspaces       = false,
  upquote                = true,
  rulecolor              = \color{black!67},
  numberstyle            = \color{black!33},
  keywordstyle           = \color[RGB]{000,045,231}\bfseries,
  commentstyle           = \color[RGB]{202,018,000}         ,
  stringstyle            = \color[RGB]{095,095,095}         ,
  scsymbolstyle          = \color[RGB]{000,113,000}         ,
  scenvvarstyle          = \color[RGB]{147,070,014}         ,
  scclassstyle           = \color[RGB]{000,040,211}         ,
}
%    \end{macrocode}
% \end{macro}
%    \begin{macrocode}
\ifframed@scpr@
  \toks@scpr\expandafter{\the\toks@scpr frame=single,}
\fi
\ifnumbered@scpr@
  \toks@scpr=\expandafter{\the\toks@scpr numbers=left,}
\fi
\begingroup\edef\@tempa{\endgroup
  \noexpand\lstdefinestyle{SuperCollider-IDE}{\the\toks@scpr}
}\@tempa
%    \end{macrocode}
%
% \Finale
\endinput
