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