-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprolog-style.sty
152 lines (135 loc) · 4.38 KB
/
prolog-style.sty
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
% --- ugly internals for language definition ---
%
\makeatletter
% initialisation of user macros
\newcommand\PrologPredicateStyle{}
\newcommand\PrologVarStyle{}
\newcommand\PrologAnonymVarStyle{}
\newcommand\PrologAtomStyle{}
\newcommand\PrologOtherStyle{}
\newcommand\PrologCommentStyle{}
% useful switches (to keep track of context)
\newif\ifpredicate@prolog@
\newif\ifwithinparens@prolog@
% save definition of underscore for test
\lst@SaveOutputDef{`_}\underscore@prolog
% local variables
\newcount\currentchar@prolog
\newcommand\@testChar@prolog%
{%
% if we're in processing mode...
\ifnum\lst@mode=\lst@Pmode%
\detectTypeAndHighlight@prolog%
\else
% ... or within parentheses
\ifwithinparens@prolog@%
\detectTypeAndHighlight@prolog%
\fi
\fi
% Some housekeeping...
\global\predicate@prolog@false%
}
% helper macros
\newcommand\detectTypeAndHighlight@prolog
{%
% First, assume that we have an atom.
\def\lst@thestyle{\PrologAtomStyle}%
% Test whether we have a predicate and modify the style accordingly.
\ifpredicate@prolog@%
\def\lst@thestyle{\PrologPredicateStyle}%
\else
% Test whether we have a predicate and modify the style accordingly.
\expandafter\splitfirstchar@prolog\expandafter{\the\lst@token}%
% Check whether the identifier starts by an underscore.
\expandafter\ifx\@testChar@prolog\underscore@prolog%
% Check whether the identifier is '_' (anonymous variable)
\ifnum\lst@length=1%
\let\lst@thestyle\PrologAnonymVarStyle%
\else
\let\lst@thestyle\PrologVarStyle%
\fi
\else
% Check whether the identifier starts by a capital letter.
\currentchar@prolog=65
\loop
\expandafter\ifnum\expandafter`\@testChar@prolog=\currentchar@prolog%
\let\lst@thestyle\PrologVarStyle%
\let\iterate\relax
\fi
\advance \currentchar@prolog by 1
\unless\ifnum\currentchar@prolog>90
\repeat
\fi
\fi
}
\newcommand\splitfirstchar@prolog{}
\def\splitfirstchar@prolog#1{\@splitfirstchar@prolog#1\relax}
\newcommand\@splitfirstchar@prolog{}
\def\@splitfirstchar@prolog#1#2\relax{\def\@testChar@prolog{#1}}
% helper macro for () delimiters
\def\beginlstdelim#1#2%
{%
\def\endlstdelim{\PrologOtherStyle #2\egroup}%
{\PrologOtherStyle #1}%
\global\predicate@prolog@false%
\withinparens@prolog@true%
\bgroup\aftergroup\endlstdelim%
}
% language name
\newcommand\lang@prolog{Prolog-pretty}
% ``normalised'' language name
\expandafter\lst@NormedDef\expandafter\normlang@prolog%
\expandafter{\lang@prolog}
% language definition
\expandafter\expandafter\expandafter\lstdefinelanguage\expandafter%
{\lang@prolog}
{%
language = Prolog,
keywords = {}, % reset all preset keywords
showstringspaces = false,
% alsoletter = (,
alsoother = @$,
% moredelim = **[is][\beginlstdelim{(}{)}]{(}{)},
MoreSelectCharTable =
\lst@DefSaveDef{`(}\opparen@prolog{\global\predicate@prolog@true\opparen@prolog},
}
% Hooking into listings to test each ``identifier''
\newcommand\@ddedToOutput@prolog\relax
\lst@AddToHook{Output}{\@ddedToOutput@prolog}
\lst@AddToHook{PreInit}
{%
\ifx\lst@language\normlang@prolog%
\let\@ddedToOutput@prolog\@testChar@prolog%
\fi
}
\lst@AddToHook{DeInit}{\renewcommand\@ddedToOutput@prolog{}}
\makeatother
%
% --- end of ugly internals ---
% --- definition of a custom style similar to that of Pygments ---
% custom colors
\definecolor{PrologPredicate}{RGB}{000,031,255}
\definecolor{PrologVar} {RGB}{024,021,125}
\definecolor{PrologAnonymVar}{RGB}{000,127,000}
\definecolor{PrologAtom} {RGB}{186,032,032}
\definecolor{PrologComment} {RGB}{063,128,127}
\definecolor{PrologOther} {RGB}{000,000,000}
% redefinition of user macros for Prolog style
\renewcommand\PrologPredicateStyle{\color{PrologPredicate}}
\renewcommand\PrologVarStyle{\color{PrologVar}}
\renewcommand\PrologAnonymVarStyle{\color{PrologAnonymVar}}
\renewcommand\PrologAtomStyle{\color{PrologAtom}}
\renewcommand\PrologCommentStyle{\itshape\color{PrologComment}}
\renewcommand\PrologOtherStyle{\color{PrologOther}}
% custom style definition
\lstdefinestyle{Prolog-cool}
{
language = Prolog-pretty,
upquote = true,
stringstyle = \PrologAtomStyle,
commentstyle = \PrologCommentStyle,
literate =
{:-}{{\PrologOtherStyle :-}}2
{,}{{\PrologOtherStyle ,}}1
{.}{{\PrologOtherStyle .}}1
}