xpm.vim 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. " Vim syntax file
  2. " Language: X Pixmap
  3. " Maintainer: Ronald Schild <rs@scutum.de>
  4. " Last Change: 2017 Feb 01
  5. " Version: 5.4n.1
  6. " Jemma Nelson added termguicolors support
  7. " quit when a syntax file was already loaded
  8. if exists("b:current_syntax")
  9. finish
  10. endif
  11. syn keyword xpmType char
  12. syn keyword xpmStorageClass static
  13. syn keyword xpmTodo TODO FIXME XXX contained
  14. syn region xpmComment start="/\*" end="\*/" contains=xpmTodo
  15. syn region xpmPixelString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@xpmColors
  16. if has("gui_running") || has("termguicolors") && &termguicolors
  17. let color = ""
  18. let chars = ""
  19. let colors = 0
  20. let cpp = 0
  21. let n = 0
  22. let i = 1
  23. while i <= line("$") " scanning all lines
  24. let s = matchstr(getline(i), '".\{-1,}"')
  25. if s != "" " does line contain a string?
  26. if n == 0 " first string is the Values string
  27. " get the 3rd value: colors = number of colors
  28. let colors = substitute(s, '"\s*\d\+\s\+\d\+\s\+\(\d\+\).*"', '\1', '')
  29. " get the 4th value: cpp = number of character per pixel
  30. let cpp = substitute(s, '"\s*\d\+\s\+\d\+\s\+\d\+\s\+\(\d\+\).*"', '\1', '')
  31. if cpp =~ '[^0-9]'
  32. break " if cpp is not made of digits there must be something wrong
  33. endif
  34. " Highlight the Values string as normal string (no pixel string).
  35. " Only when there is no slash, it would terminate the pattern.
  36. if s !~ '/'
  37. exe 'syn match xpmValues /' . s . '/'
  38. endif
  39. hi link xpmValues String
  40. let n = 1 " n = color index
  41. elseif n <= colors " string is a color specification
  42. " get chars = <cpp> length string representing the pixels
  43. " (first incl. the following whitespace)
  44. let chars = substitute(s, '"\(.\{'.cpp.'}\s\).*"', '\1', '')
  45. " now get color, first try 'c' key if any (color visual)
  46. let color = substitute(s, '".*\sc\s\+\(.\{-}\)\s*\(\(g4\=\|[ms]\)\s.*\)*\s*"', '\1', '')
  47. if color == s
  48. " no 'c' key, try 'g' key (grayscale with more than 4 levels)
  49. let color = substitute(s, '".*\sg\s\+\(.\{-}\)\s*\(\(g4\|[ms]\)\s.*\)*\s*"', '\1', '')
  50. if color == s
  51. " next try: 'g4' key (4-level grayscale)
  52. let color = substitute(s, '".*\sg4\s\+\(.\{-}\)\s*\([ms]\s.*\)*\s*"', '\1', '')
  53. if color == s
  54. " finally try 'm' key (mono visual)
  55. let color = substitute(s, '".*\sm\s\+\(.\{-}\)\s*\(s\s.*\)*\s*"', '\1', '')
  56. if color == s
  57. let color = ""
  58. endif
  59. endif
  60. endif
  61. endif
  62. " Vim cannot handle RGB codes with more than 6 hex digits
  63. if color =~ '#\x\{10,}$'
  64. let color = substitute(color, '\(\x\x\)\x\x', '\1', 'g')
  65. elseif color =~ '#\x\{7,}$'
  66. let color = substitute(color, '\(\x\x\)\x', '\1', 'g')
  67. " nor with 3 digits
  68. elseif color =~ '#\x\{3}$'
  69. let color = substitute(color, '\(\x\)\(\x\)\(\x\)', '0\10\20\3', '')
  70. endif
  71. " escape meta characters in patterns
  72. let s = escape(s, '/\*^$.~[]')
  73. let chars = escape(chars, '/\*^$.~[]')
  74. " now create syntax items
  75. " highlight the color string as normal string (no pixel string)
  76. exe 'syn match xpmCol'.n.'Def /'.s.'/ contains=xpmCol'.n.'inDef'
  77. exe 'hi link xpmCol'.n.'Def String'
  78. " but highlight the first whitespace after chars in its color
  79. exe 'syn match xpmCol'.n.'inDef /"'.chars.'/hs=s+'.(cpp+1).' contained'
  80. exe 'hi link xpmCol'.n.'inDef xpmColor'.n
  81. " remove the following whitespace from chars
  82. let chars = substitute(chars, '.$', '', '')
  83. " and create the syntax item contained in the pixel strings
  84. exe 'syn match xpmColor'.n.' /'.chars.'/ contained'
  85. exe 'syn cluster xpmColors add=xpmColor'.n
  86. " if no color or color = "None" show background
  87. if color == "" || substitute(color, '.*', '\L&', '') == 'none'
  88. exe 'hi xpmColor'.n.' guifg=bg'
  89. exe 'hi xpmColor'.n.' guibg=NONE'
  90. elseif color !~ "'"
  91. exe 'hi xpmColor'.n." guifg='".color."'"
  92. exe 'hi xpmColor'.n." guibg='".color."'"
  93. endif
  94. let n = n + 1
  95. else
  96. break " no more color string
  97. endif
  98. endif
  99. let i = i + 1
  100. endwhile
  101. unlet color chars colors cpp n i s
  102. endif " has("gui_running") || has("termguicolors") && &termguicolors
  103. " Define the default highlighting.
  104. " Only when an item doesn't have highlighting yet
  105. hi def link xpmType Type
  106. hi def link xpmStorageClass StorageClass
  107. hi def link xpmTodo Todo
  108. hi def link xpmComment Comment
  109. hi def link xpmPixelString String
  110. let b:current_syntax = "xpm"
  111. " vim: ts=8:sw=3:noet: