2 " Language: X Pixmap v2
3 " Maintainer: Steve Wall (hitched97@velnet.com)
4 " Last Change: 2012 Jun 01
5 " (Dominique Pelle added @Spell)
8 " Made from xpm.vim by Ronald Schild <rs@scutum.de>
10 " For version 5.x: Clear all syntax items
11 " For version 6.x: Quit when a syntax file was already loaded
14 elseif exists("b:current_syntax")
21 syn region xpm2PixelString start="^" end="$" contains=@xpm2Colors
22 syn keyword xpm2Todo TODO FIXME XXX contained
23 syn match xpm2Comment "\!.*$" contains=@Spell,xpm2Todo
27 command -nargs=+ HiLink hi link <args>
28 command -nargs=+ Hi hi <args>
30 command -nargs=+ HiLink hi def link <args>
31 command -nargs=+ Hi hi def <args>
43 while i <= line("$") " scanning all lines
46 if match(s,"\!.*$") != -1
47 let s = matchstr(s, "^[^\!]*")
49 if s != "" " does line contain a string?
51 if n == 0 " first string is the Values string
53 " get the 3rd value: colors = number of colors
54 let colors = substitute(s, '\s*\d\+\s\+\d\+\s\+\(\d\+\).*', '\1', '')
55 " get the 4th value: cpp = number of character per pixel
56 let cpp = substitute(s, '\s*\d\+\s\+\d\+\s\+\d\+\s\+\(\d\+\).*', '\1', '')
58 break " if cpp is not made of digits there must be something wrong
61 " Highlight the Values string as normal string (no pixel string).
62 " Only when there is no slash, it would terminate the pattern.
64 exe 'syn match xpm2Values /' . s . '/'
66 HiLink xpm2Values Statement
68 let n = 1 " n = color index
70 elseif n <= colors " string is a color specification
72 " get chars = <cpp> length string representing the pixels
73 " (first incl. the following whitespace)
74 let chars = substitute(s, '\(.\{'.cpp.'}\s\+\).*', '\1', '')
76 " now get color, first try 'c' key if any (color visual)
77 let color = substitute(s, '.*\sc\s\+\(.\{-}\)\s*\(\(g4\=\|[ms]\)\s.*\)*\s*', '\1', '')
79 " no 'c' key, try 'g' key (grayscale with more than 4 levels)
80 let color = substitute(s, '.*\sg\s\+\(.\{-}\)\s*\(\(g4\|[ms]\)\s.*\)*\s*', '\1', '')
82 " next try: 'g4' key (4-level grayscale)
83 let color = substitute(s, '.*\sg4\s\+\(.\{-}\)\s*\([ms]\s.*\)*\s*', '\1', '')
85 " finally try 'm' key (mono visual)
86 let color = substitute(s, '.*\sm\s\+\(.\{-}\)\s*\(s\s.*\)*\s*', '\1', '')
94 " Vim cannot handle RGB codes with more than 6 hex digits
95 if color =~ '#\x\{10,}$'
96 let color = substitute(color, '\(\x\x\)\x\x', '\1', 'g')
97 elseif color =~ '#\x\{7,}$'
98 let color = substitute(color, '\(\x\x\)\x', '\1', 'g')
100 elseif color =~ '#\x\{3}$'
101 let color = substitute(color, '\(\x\)\(\x\)\(\x\)', '0\10\20\3', '')
104 " escape meta characters in patterns
105 let s = escape(s, '/\*^$.~[]')
106 let chars = escape(chars, '/\*^$.~[]')
108 " change whitespace to "\s\+"
109 let s = substitute(s, "[ \t][ \t]*", "\\\\s\\\\+", "g")
110 let chars = substitute(chars, "[ \t][ \t]*", "\\\\s\\\\+", "g")
112 " now create syntax items
113 " highlight the color string as normal string (no pixel string)
114 exe 'syn match xpm2Col'.n.'Def /'.s.'/ contains=xpm2Col'.n.'inDef'
115 exe 'HiLink xpm2Col'.n.'Def Constant'
117 " but highlight the first whitespace after chars in its color
118 exe 'syn match xpm2Col'.n.'inDef /^'.chars.'/hs=s+'.(cpp).' contained'
119 exe 'HiLink xpm2Col'.n.'inDef xpm2Color'.n
121 " remove the following whitespace from chars
122 let chars = substitute(chars, '\\s\\+$', '', '')
124 " and create the syntax item contained in the pixel strings
125 exe 'syn match xpm2Color'.n.' /'.chars.'/ contained'
126 exe 'syn cluster xpm2Colors add=xpm2Color'.n
128 " if no color or color = "None" show background
129 if color == "" || substitute(color, '.*', '\L&', '') == 'none'
130 exe 'Hi xpm2Color'.n.' guifg=bg guibg=NONE'
132 exe 'Hi xpm2Color'.n." guifg='".color."' guibg='".color."'"
136 break " no more color string
142 unlet color chars colors cpp n i s
144 endif " has("gui_running")
146 " Define the default highlighting.
147 " For version 5.7 and earlier: only when not done already
148 " For version 5.8 and later: only when an item doesn't have highlighting yet
149 if version >= 508 || !exists("did_xpm2_syntax_inits")
151 let did_xpm2_syntax_inits = 1
154 " The default highlighting.
156 HiLink xpm2StorageClass StorageClass
158 HiLink xpm2Comment Comment
159 HiLink xpm2PixelString String
164 let b:current_syntax = "xpm2"
166 let &cpo = s:cpo_save
168 " vim: ts=8:sw=2:noet: