summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/sclang-prettifier/sclang-prettifier.dtx
blob: de30d6825a8667208dbb5d01c6af471ff0a9d504 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
% \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