2 " Language: X Pixmap v2
3 " Maintainer: Steve Wall (steve_wall@redcom.com)
4 " Last Change: 2001 Apr 25
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 " highlight the Values string as normal string (no pixel string)
55 exe 'syn match xpm2Values /'.s.'/'
56 HiLink xpm2Values Statement
58 let n = 1 " n = color index
60 elseif n <= colors " string is a color specification
62 " get chars = <cpp> length string representing the pixels
63 " (first incl. the following whitespace)
64 let chars = substitute(s, '\(.\{'.cpp.'}\s\+\).*', '\1', '')
66 " now get color, first try 'c' key if any (color visual)
67 let color = substitute(s, '.*\sc\s\+\(.\{-}\)\s*\(\(g4\=\|[ms]\)\s.*\)*\s*', '\1', '')
69 " no 'c' key, try 'g' key (grayscale with more than 4 levels)
70 let color = substitute(s, '.*\sg\s\+\(.\{-}\)\s*\(\(g4\|[ms]\)\s.*\)*\s*', '\1', '')
72 " next try: 'g4' key (4-level grayscale)
73 let color = substitute(s, '.*\sg4\s\+\(.\{-}\)\s*\([ms]\s.*\)*\s*', '\1', '')
75 " finally try 'm' key (mono visual)
76 let color = substitute(s, '.*\sm\s\+\(.\{-}\)\s*\(s\s.*\)*\s*', '\1', '')
84 " Vim cannot handle RGB codes with more than 6 hex digits
85 if color =~ '#\x\{10,}$'
86 let color = substitute(color, '\(\x\x\)\x\x', '\1', 'g')
87 elseif color =~ '#\x\{7,}$'
88 let color = substitute(color, '\(\x\x\)\x', '\1', 'g')
90 elseif color =~ '#\x\{3}$'
91 let color = substitute(color, '\(\x\)\(\x\)\(\x\)', '0\10\20\3', '')
94 " escape meta characters in patterns
95 let s = escape(s, '/\*^$.~[]')
96 let chars = escape(chars, '/\*^$.~[]')
98 " change whitespace to "\s\+"
99 let s = substitute(s, "[ \t][ \t]*", "\\\\s\\\\+", "g")
100 let chars = substitute(chars, "[ \t][ \t]*", "\\\\s\\\\+", "g")
102 " now create syntax items
103 " highlight the color string as normal string (no pixel string)
104 exe 'syn match xpm2Col'.n.'Def /'.s.'/ contains=xpm2Col'.n.'inDef'
105 exe 'HiLink xpm2Col'.n.'Def Constant'
107 " but highlight the first whitespace after chars in its color
108 exe 'syn match xpm2Col'.n.'inDef /^'.chars.'/hs=s+'.(cpp).' contained'
109 exe 'HiLink xpm2Col'.n.'inDef xpm2Color'.n
111 " remove the following whitespace from chars
112 let chars = substitute(chars, '\\s\\+$', '', '')
114 " and create the syntax item contained in the pixel strings
115 exe 'syn match xpm2Color'.n.' /'.chars.'/ contained'
116 exe 'syn cluster xpm2Colors add=xpm2Color'.n
118 " if no color or color = "None" show background
119 if color == "" || substitute(color, '.*', '\L&', '') == 'none'
120 exe 'Hi xpm2Color'.n.' guifg=bg guibg=NONE'
122 exe 'Hi xpm2Color'.n." guifg='".color."' guibg='".color."'"
126 break " no more color string
132 unlet color chars colors cpp n i s
134 endif " has("gui_running")
136 " Define the default highlighting.
137 " For version 5.7 and earlier: only when not done already
138 " For version 5.8 and later: only when an item doesn't have highlighting yet
139 if version >= 508 || !exists("did_xpm2_syntax_inits")
141 let did_xpm2_syntax_inits = 1
144 " The default highlighting.
146 HiLink xpm2StorageClass StorageClass
148 HiLink xpm2Comment Comment
149 HiLink xpm2PixelString String
154 let b:current_syntax = "xpm2"
156 " vim: ts=8:sw=2:noet: