1" Vim syntax file
2" Language:	X Pixmap v2
3" Maintainer:	Steve Wall (hitched97@velnet.com)
4" Last Change:	2008 May 28
5" Version:	5.8
6"
7" Made from xpm.vim by Ronald Schild <rs@scutum.de>
8
9" For version 5.x: Clear all syntax items
10" For version 6.x: Quit when a syntax file was already loaded
11if version < 600
12  syntax clear
13elseif exists("b:current_syntax")
14  finish
15endif
16
17syn region  xpm2PixelString	start="^"  end="$"  contains=@xpm2Colors
18syn keyword xpm2Todo		TODO FIXME XXX  contained
19syn match   xpm2Comment		"\!.*$"  contains=xpm2Todo
20
21
22if version < 508
23  command -nargs=+ HiLink hi link <args>
24  command -nargs=+ Hi hi <args>
25else
26  command -nargs=+ HiLink hi def link <args>
27  command -nargs=+ Hi hi def <args>
28endif
29
30if has("gui_running")
31
32  let color  = ""
33  let chars  = ""
34  let colors = 0
35  let cpp    = 0
36  let n      = 0
37  let i      = 1
38
39  while i <= line("$")		" scanning all lines
40
41    let s = getline(i)
42    if match(s,"\!.*$") != -1
43      let s = matchstr(s, "^[^\!]*")
44    endif
45    if s != ""			" does line contain a string?
46
47      if n == 0			" first string is the Values string
48
49	" get the 3rd value: colors = number of colors
50	let colors = substitute(s, '\s*\d\+\s\+\d\+\s\+\(\d\+\).*', '\1', '')
51	" get the 4th value: cpp = number of character per pixel
52	let cpp = substitute(s, '\s*\d\+\s\+\d\+\s\+\d\+\s\+\(\d\+\).*', '\1', '')
53	if cpp =~ '[^0-9]'
54	  break  " if cpp is not made of digits there must be something wrong
55	endif
56
57	" Highlight the Values string as normal string (no pixel string).
58	" Only when there is no slash, it would terminate the pattern.
59	if s !~ '/'
60	  exe 'syn match xpm2Values /' . s . '/'
61	endif
62	HiLink xpm2Values Statement
63
64	let n = 1			" n = color index
65
66      elseif n <= colors		" string is a color specification
67
68	" get chars = <cpp> length string representing the pixels
69	" (first incl. the following whitespace)
70	let chars = substitute(s, '\(.\{'.cpp.'}\s\+\).*', '\1', '')
71
72	" now get color, first try 'c' key if any (color visual)
73	let color = substitute(s, '.*\sc\s\+\(.\{-}\)\s*\(\(g4\=\|[ms]\)\s.*\)*\s*', '\1', '')
74	if color == s
75	  " no 'c' key, try 'g' key (grayscale with more than 4 levels)
76	  let color = substitute(s, '.*\sg\s\+\(.\{-}\)\s*\(\(g4\|[ms]\)\s.*\)*\s*', '\1', '')
77	  if color == s
78	    " next try: 'g4' key (4-level grayscale)
79	    let color = substitute(s, '.*\sg4\s\+\(.\{-}\)\s*\([ms]\s.*\)*\s*', '\1', '')
80	    if color == s
81	      " finally try 'm' key (mono visual)
82	      let color = substitute(s, '.*\sm\s\+\(.\{-}\)\s*\(s\s.*\)*\s*', '\1', '')
83	      if color == s
84		let color = ""
85	      endif
86	    endif
87	  endif
88	endif
89
90	" Vim cannot handle RGB codes with more than 6 hex digits
91	if color =~ '#\x\{10,}$'
92	  let color = substitute(color, '\(\x\x\)\x\x', '\1', 'g')
93	elseif color =~ '#\x\{7,}$'
94	  let color = substitute(color, '\(\x\x\)\x', '\1', 'g')
95	" nor with 3 digits
96	elseif color =~ '#\x\{3}$'
97	  let color = substitute(color, '\(\x\)\(\x\)\(\x\)', '0\10\20\3', '')
98	endif
99
100	" escape meta characters in patterns
101	let s = escape(s, '/\*^$.~[]')
102	let chars = escape(chars, '/\*^$.~[]')
103
104	" change whitespace to "\s\+"
105	let s = substitute(s, "[ \t][ \t]*", "\\\\s\\\\+", "g")
106	let chars = substitute(chars, "[ \t][ \t]*", "\\\\s\\\\+", "g")
107
108	" now create syntax items
109	" highlight the color string as normal string (no pixel string)
110	exe 'syn match xpm2Col'.n.'Def /'.s.'/ contains=xpm2Col'.n.'inDef'
111	exe 'HiLink xpm2Col'.n.'Def Constant'
112
113	" but highlight the first whitespace after chars in its color
114	exe 'syn match xpm2Col'.n.'inDef /^'.chars.'/hs=s+'.(cpp).' contained'
115	exe 'HiLink xpm2Col'.n.'inDef xpm2Color'.n
116
117	" remove the following whitespace from chars
118	let chars = substitute(chars, '\\s\\+$', '', '')
119
120	" and create the syntax item contained in the pixel strings
121	exe 'syn match xpm2Color'.n.' /'.chars.'/ contained'
122	exe 'syn cluster xpm2Colors add=xpm2Color'.n
123
124	" if no color or color = "None" show background
125	if color == ""  ||  substitute(color, '.*', '\L&', '') == 'none'
126	  exe 'Hi xpm2Color'.n.' guifg=bg guibg=NONE'
127	elseif color !~ "'"
128	  exe 'Hi xpm2Color'.n." guifg='".color."' guibg='".color."'"
129	endif
130	let n = n + 1
131      else
132	break			" no more color string
133      endif
134    endif
135    let i = i + 1
136  endwhile
137
138  unlet color chars colors cpp n i s
139
140endif		" has("gui_running")
141
142" Define the default highlighting.
143" For version 5.7 and earlier: only when not done already
144" For version 5.8 and later: only when an item doesn't have highlighting yet
145if version >= 508 || !exists("did_xpm2_syntax_inits")
146  if version < 508
147    let did_xpm2_syntax_inits = 1
148  endif
149
150  " The default highlighting.
151  HiLink xpm2Type		Type
152  HiLink xpm2StorageClass	StorageClass
153  HiLink xpm2Todo		Todo
154  HiLink xpm2Comment		Comment
155  HiLink xpm2PixelString	String
156endif
157delcommand HiLink
158delcommand Hi
159
160let b:current_syntax = "xpm2"
161
162" vim: ts=8:sw=2:noet:
163