Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. documentclass{article}
  2.  
  3. usepackage{xcolor}
  4. usepackage{listings}
  5.  
  6. makeatletter
  7.  
  8. % ``state variables''
  9. newififincommentincommentfalse
  10. newififinstringinstringfalse
  11.  
  12. % language and style definition
  13. lstdefinestyle{mybash}
  14. {%
  15. language = bash,
  16. basicstyle = ttfamily,
  17. keywordstyle = color{blue},
  18. }[keywords,strings,comments]
  19.  
  20. % --- patch to automatically highlight identifier starting by @
  21. % (only outside strings and comments, though) ---
  22. lst@AddToHook{Output}{@ddedToOutput}
  23. lst@AddToHook{Endgroup}{incommentfalseinstringfalse}
  24.  
  25. % local variables
  26. newifif@identifierStartsByAt@
  27. newcountcurrentchar
  28.  
  29. defsplitfirstchar#1{@splitfirstchar#1@nil}
  30. def@splitfirstchar#1#2@nil{gdef@testChar{#1}}
  31.  
  32. def@testChar%
  33. {%
  34. % copy the first token in thelst@token to @testChar
  35. expandaftersplitfirstcharexpandafter{thelst@token}%
  36. %
  37. % reset switch
  38. @identifierStartsByAt@false
  39. %
  40. % equality test
  41. expandafterifnumexpandafter`@testChar=`_%
  42. @identifierStartsByAt@true % if equality, set switch to true
  43. fi
  44. %
  45. % apply class style if not within string or comment
  46. if@identifierStartsByAt@
  47. ifincomment
  48. else
  49. ifinstring
  50. else
  51. deflst@thestyle{lst@keywordstyle}%
  52. fi
  53. fi
  54. fi
  55. }
  56. let@ddedToOutput@testChar
  57. makeatother
  58.  
  59. begin{document}
  60. begin{lstlisting}[style=mybash]
  61. # This is an example
  62. @Blueword(foo) _Blueword bar
  63. end{lstlisting}
  64. end{document}
  65.  
  66. ! Improper alphabetic constant.
  67. <to be read again>
  68. lst@um_
  69. l.62 @Blueword(foo) _Blueword
  70. bar
  71. ?
  72. ! Missing = inserted for ifnum.
  73. <to be read again>
  74. char
  75. l.62 @Blueword(foo) _Blueword
  76. bar
  77. ?
  78. ! Missing number, treated as zero.
  79. <to be read again>
  80. char
  81. l.62 @Blueword(foo) _Blueword
  82. bar
  83. ?
  84. [1{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}] (./t.aux) )
  85. (end occurred when ifnum on line 62 was incomplete)
  86. (end occurred when iftrue on line 62 was incomplete)
  87. (end occurred when ifnum on line 62 was incomplete)
  88. (end occurred when iffalse on line 62 was incomplete)
  89. (end occurred when ifnum on line 62 was incomplete)
  90. (end occurred when iftrue on line 62 was incomplete)
  91. (end occurred when ifnum on line 62 was incomplete)
  92. (end occurred when iftrue on line 62 was incomplete)
  93. (end occurred when ifnum on line 61 was incomplete)
  94. (end occurred when iftrue on line 61 was incomplete)
  95. (end occurred when ifnum on line 61 was incomplete)
  96. (end occurred when iffalse on line 61 was incomplete)
  97. (end occurred when ifnum on line 61 was incomplete)
  98. (end occurred when iffalse on line 61 was incomplete)
  99. (end occurred when ifnum on line 61 was incomplete)
  100. (end occurred when iffalse on line 61 was incomplete)
  101.  
  102. documentclass{article}
  103. usepackage{xcolor}
  104. usepackage{listings}
  105.  
  106. makeatletter
  107.  
  108. lstdefinestyle{mybash}
  109. {%
  110. language = bash,
  111. basicstyle = ttfamily,
  112. keywordstyle = color{green},
  113. identifierstyle = @identifierstyle,
  114. }[keywords,strings,comments]
  115.  
  116. def@headtoken#1#2end{noexpand#1}
  117.  
  118. def@identifierstyle{%
  119. edef@tempa{expandafter@headtokenthelst@tokenend}%
  120. edef@tempb{expandafternoexpandcsname lst@um_endcsname}%
  121. ifx@tempa@tempb
  122. % apply special style for identifies staring with '_'
  123. color{red}%
  124. else
  125. % normal identifier style
  126. color{blue}%
  127. fi
  128. }
  129.  
  130. makeatother
  131.  
  132. begin{document}
  133. begin{lstlisting}[style=mybash]
  134. # This is an example for _Blueword
  135. @Blueword(foo) _Blueword bar
  136. '_Blueword in string'
  137. if _cond then __this__ else __that__
  138. end{lstlisting}
  139. end{document}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement