3 " Maintainer: Ronald Schild <rs@scutum.de>
4 " Last Change: 2008 May 28
7 " For version 5.x: Clear all syntax items
8 " For version 6.x: Quit when a syntax file was already loaded
11 elseif exists("b:current_syntax")
15 syn keyword xpmType char
16 syn keyword xpmStorageClass static
17 syn keyword xpmTodo TODO FIXME XXX contained
18 syn region xpmComment start="/\*" end="\*/" contains=xpmTodo
19 syn region xpmPixelString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@xpmColors
30 while i <= line("$") " scanning all lines
32 let s = matchstr(getline(i), '".\{-1,}"')
33 if s != "" " does line contain a string?
35 if n == 0 " first string is the Values string
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', '')
42 break " if cpp is not made of digits there must be something wrong
45 " Highlight the Values string as normal string (no pixel string).
46 " Only when there is no slash, it would terminate the pattern.
48 exe 'syn match xpmValues /' . s . '/'
50 hi link xpmValues String
52 let n = 1 " n = color index
54 elseif n <= colors " string is a color specification
56 " get chars = <cpp> length string representing the pixels
57 " (first incl. the following whitespace)
58 let chars = substitute(s, '"\(.\{'.cpp.'}\s\).*"', '\1', '')
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', '')
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', '')
66 " next try: 'g4' key (4-level grayscale)
67 let color = substitute(s, '".*\sg4\s\+\(.\{-}\)\s*\([ms]\s.*\)*\s*"', '\1', '')
69 " finally try 'm' key (mono visual)
70 let color = substitute(s, '".*\sm\s\+\(.\{-}\)\s*\(s\s.*\)*\s*"', '\1', '')
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')
84 elseif color =~ '#\x\{3}$'
85 let color = substitute(color, '\(\x\)\(\x\)\(\x\)', '0\10\20\3', '')
88 " escape meta characters in patterns
89 let s = escape(s, '/\*^$.~[]')
90 let chars = escape(chars, '/\*^$.~[]')
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'
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
101 " remove the following whitespace from chars
102 let chars = substitute(chars, '.$', '', '')
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
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'
113 exe 'hi xpmColor'.n." guifg='".color."'"
114 exe 'hi xpmColor'.n." guibg='".color."'"
118 break " no more color string
124 unlet color chars colors cpp n i s
126 endif " has("gui_running")
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
131 if version >= 508 || !exists("did_xpm_syntax_inits")
133 let did_xpm_syntax_inits = 1
134 command -nargs=+ HiLink hi link <args>
136 command -nargs=+ HiLink hi def link <args>
140 HiLink xpmStorageClass StorageClass
142 HiLink xpmComment Comment
143 HiLink xpmPixelString String
148 let b:current_syntax = "xpm"
150 " vim: ts=8:sw=3:noet: