Skip to content

Instantly share code, notes, and snippets.

@cdlm
Last active February 12, 2016 09:43
Smalltalk language for listings.sty with specific literate chevron for the method pattern. Not that useful in practice, unless all listings are single methods…
\ProvidesFile{lstsmalltalk.sty}
[2016/02/12 0.3 listings Smalltalk definitions file]
%
\RequirePackage{listings}
\RequirePackage{relsize}
\RequirePackage{calc}
\newcommand\language@smalltalk{smalltalk}
% all this needed to incept the above macro as the name of the language
% definition
\expandafter\expandafter\expandafter\lstdefinelanguage
\expandafter{\language@smalltalk}
{
keywords={self,super,true,false,nil,thisContext},
otherkeywords={^,:=,->,==,~=},
alsoletter={\#:},
string=[d]',
comment=[s]{"}{"},
literate={\ >>\ }{{\smalltalk@chevron}}3,
}[keywords,comments,strings]
\newif\ifsmalltalk@methodpattern
\smalltalk@methodpatterntrue
\lst@AddToHook{EndGroup}{% first line is class >> selector
\global\smalltalk@methodpatterntrue}
\lst@AddToHook{EOL}{% switch >> back to operator appearance
\global\smalltalk@methodpatternfalse}
\newcommand\smalltalk@methodpattern@chevron{\makebox[\widthof{>>>}][c]{\textsmaller{>>}}}
\newcommand\smalltalk@operator@chevron{>>}
\newcommand\smalltalk@chevron{%
\ifsmalltalk@methodpattern
\smalltalk@methodpattern@chevron
\else
\smalltalk@operator@chevron
\fi}
\endinput
%%%
%%% And now for something completely different... Some stupid example,
%%% to put in a file to test this language definition...
%%%
% \lstset{
% inputencoding=utf8,
% columns=fullflexible,
% basicstyle=\Large
% }
%
% \begin{lstlisting}[language=Smalltalk]
% SomeClass >> testNumberOfRelationships
% "this is a test"
% | bazEntity |
% self assert: fooEntity numberOfOutgoingRelationships = 1.
% bazEntity := MyEntity named: #baz.
% MyRelationship from: fooEntity to: bazEntity.
% self assert: fooEntity numberOfOutgoingRelationships = 2.
% string := 'Hello, world!'
% aCollection do: [ each |
% | local |
% each someMessage ].
% ^ string
% \end{lstlisting}
%%%%%%%%
% Local Variables:
% coding: utf-8
% End:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment