Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- documentclass{article}
- usepackage{xcolor}
- usepackage{listings}
- makeatletter
- % ``state variables''
- newififincommentincommentfalse
- newififinstringinstringfalse
- % language and style definition
- lstdefinestyle{mybash}
- {%
- language = bash,
- basicstyle = ttfamily,
- keywordstyle = color{blue},
- }[keywords,strings,comments]
- % --- patch to automatically highlight identifier starting by @
- % (only outside strings and comments, though) ---
- lst@AddToHook{Output}{@ddedToOutput}
- lst@AddToHook{Endgroup}{incommentfalseinstringfalse}
- % local variables
- newifif@identifierStartsByAt@
- newcountcurrentchar
- defsplitfirstchar#1{@splitfirstchar#1@nil}
- def@splitfirstchar#1#2@nil{gdef@testChar{#1}}
- def@testChar%
- {%
- % copy the first token in thelst@token to @testChar
- expandaftersplitfirstcharexpandafter{thelst@token}%
- %
- % reset switch
- @identifierStartsByAt@false
- %
- % equality test
- expandafterifnumexpandafter`@testChar=`_%
- @identifierStartsByAt@true % if equality, set switch to true
- fi
- %
- % apply class style if not within string or comment
- if@identifierStartsByAt@
- ifincomment
- else
- ifinstring
- else
- deflst@thestyle{lst@keywordstyle}%
- fi
- fi
- fi
- }
- let@ddedToOutput@testChar
- makeatother
- begin{document}
- begin{lstlisting}[style=mybash]
- # This is an example
- @Blueword(foo) _Blueword bar
- end{lstlisting}
- end{document}
- ! Improper alphabetic constant.
- <to be read again>
- lst@um_
- l.62 @Blueword(foo) _Blueword
- bar
- ?
- ! Missing = inserted for ifnum.
- <to be read again>
- char
- l.62 @Blueword(foo) _Blueword
- bar
- ?
- ! Missing number, treated as zero.
- <to be read again>
- char
- l.62 @Blueword(foo) _Blueword
- bar
- ?
- [1{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}] (./t.aux) )
- (end occurred when ifnum on line 62 was incomplete)
- (end occurred when iftrue on line 62 was incomplete)
- (end occurred when ifnum on line 62 was incomplete)
- (end occurred when iffalse on line 62 was incomplete)
- (end occurred when ifnum on line 62 was incomplete)
- (end occurred when iftrue on line 62 was incomplete)
- (end occurred when ifnum on line 62 was incomplete)
- (end occurred when iftrue on line 62 was incomplete)
- (end occurred when ifnum on line 61 was incomplete)
- (end occurred when iftrue on line 61 was incomplete)
- (end occurred when ifnum on line 61 was incomplete)
- (end occurred when iffalse on line 61 was incomplete)
- (end occurred when ifnum on line 61 was incomplete)
- (end occurred when iffalse on line 61 was incomplete)
- (end occurred when ifnum on line 61 was incomplete)
- (end occurred when iffalse on line 61 was incomplete)
- documentclass{article}
- usepackage{xcolor}
- usepackage{listings}
- makeatletter
- lstdefinestyle{mybash}
- {%
- language = bash,
- basicstyle = ttfamily,
- keywordstyle = color{green},
- identifierstyle = @identifierstyle,
- }[keywords,strings,comments]
- def@headtoken#1#2end{noexpand#1}
- def@identifierstyle{%
- edef@tempa{expandafter@headtokenthelst@tokenend}%
- edef@tempb{expandafternoexpandcsname lst@um_endcsname}%
- ifx@tempa@tempb
- % apply special style for identifies staring with '_'
- color{red}%
- else
- % normal identifier style
- color{blue}%
- fi
- }
- makeatother
- begin{document}
- begin{lstlisting}[style=mybash]
- # This is an example for _Blueword
- @Blueword(foo) _Blueword bar
- '_Blueword in string'
- if _cond then __this__ else __that__
- end{lstlisting}
- end{document}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement