2 " Language: X Pixmap v2
3 " Maintainer: Steve Wall (hitched97@velnet.com)
4 " Last Change: 2008 May 28
7 " Made from xpm.vim by Ronald Schild <rs@scutum.de>
9 " For version 5.x: Clear all syntax items
10 " For version 6.x: Quit when a syntax file was already loaded
13 elseif exists("b:current_syntax")
17 syn region xpm2PixelString start="^" end="$" contains=@xpm2Colors
18 syn keyword xpm2Todo TODO FIXME XXX contained
19 syn match xpm2Comment "\!.*$" contains=xpm2Todo
23 command -nargs=+ HiLink hi link <args>
24 command -nargs=+ Hi hi <args>
26 command -nargs=+ HiLink hi def link <args>
27 command -nargs=+ Hi hi def <args>
39 while i <= line("$") " scanning all lines
42 if match(s,"\!.*$") != -1
43 let s = matchstr(s, "^[^\!]*")
45 if s != "" " does line contain a string?
47 if n == 0 " first string is the Values string
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', '')
54 break " if cpp is not made of digits there must be something wrong
57 " Highlight the Values string as normal string (no pixel string).
58 " Only when there is no slash, it would terminate the pattern.
60 exe 'syn match xpm2Values /' . s . '/'
62 HiLink xpm2Values Statement
64 let n = 1 " n = color index
66 elseif n <= colors " string is a color specification
68 " get chars = <cpp> length string representing the pixels
69 " (first incl. the following whitespace)
70 let chars = substitute(s, '\(.\{'.cpp.'}\s\+\).*', '\1', '')
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', '')
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', '')
78 " next try: 'g4' key (4-level grayscale)
79 let color = substitute(s, '.*\sg4\s\+\(.\{-}\)\s*\([ms]\s.*\)*\s*', '\1', '')
81 " finally try 'm' key (mono visual)
82 let color = substitute(s, '.*\sm\s\+\(.\{-}\)\s*\(s\s.*\)*\s*', '\1', '')
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')
96 elseif color =~ '#\x\{3}$'
97 let color = substitute(color, '\(\x\)\(\x\)\(\x\)', '0\10\20\3', '')
100 " escape meta characters in patterns
101 let s = escape(s, '/\*^$.~[]')
102 let chars = escape(chars, '/\*^$.~[]')
104 " change whitespace to "\s\+"
105 let s = substitute(s, "[ \t][ \t]*", "\\\\s\\\\+", "g")
106 let chars = substitute(chars, "[ \t][ \t]*", "\\\\s\\\\+", "g")
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'
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
117 " remove the following whitespace from chars
118 let chars = substitute(chars, '\\s\\+$', '', '')
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
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'
128 exe 'Hi xpm2Color'.n." guifg='".color."' guibg='".color."'"
132 break " no more color string
138 unlet color chars colors cpp n i s
140 endif " has("gui_running")
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
145 if version >= 508 || !exists("did_xpm2_syntax_inits")
147 let did_xpm2_syntax_inits = 1
150 " The default highlighting.
152 HiLink xpm2StorageClass StorageClass
154 HiLink xpm2Comment Comment
155 HiLink xpm2PixelString String
160 let b:current_syntax = "xpm2"
162 " vim: ts=8:sw=2:noet: