3 " Maintainer: Ronald Schild <rs@scutum.de>
4 " Last Change: 2001 May 09
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 " highlight the Values string as normal string (no pixel string)
43 exe 'syn match xpmValues /'.s.'/'
44 hi link xpmValues String
46 let n = 1 " n = color index
48 elseif n <= colors " string is a color specification
50 " get chars = <cpp> length string representing the pixels
51 " (first incl. the following whitespace)
52 let chars = substitute(s, '"\(.\{'.cpp.'}\s\).*"', '\1', '')
54 " now get color, first try 'c' key if any (color visual)
55 let color = substitute(s, '".*\sc\s\+\(.\{-}\)\s*\(\(g4\=\|[ms]\)\s.*\)*\s*"', '\1', '')
57 " no 'c' key, try 'g' key (grayscale with more than 4 levels)
58 let color = substitute(s, '".*\sg\s\+\(.\{-}\)\s*\(\(g4\|[ms]\)\s.*\)*\s*"', '\1', '')
60 " next try: 'g4' key (4-level grayscale)
61 let color = substitute(s, '".*\sg4\s\+\(.\{-}\)\s*\([ms]\s.*\)*\s*"', '\1', '')
63 " finally try 'm' key (mono visual)
64 let color = substitute(s, '".*\sm\s\+\(.\{-}\)\s*\(s\s.*\)*\s*"', '\1', '')
72 " Vim cannot handle RGB codes with more than 6 hex digits
73 if color =~ '#\x\{10,}$'
74 let color = substitute(color, '\(\x\x\)\x\x', '\1', 'g')
75 elseif color =~ '#\x\{7,}$'
76 let color = substitute(color, '\(\x\x\)\x', '\1', 'g')
78 elseif color =~ '#\x\{3}$'
79 let color = substitute(color, '\(\x\)\(\x\)\(\x\)', '0\10\20\3', '')
82 " escape meta characters in patterns
83 let s = escape(s, '/\*^$.~[]')
84 let chars = escape(chars, '/\*^$.~[]')
86 " now create syntax items
87 " highlight the color string as normal string (no pixel string)
88 exe 'syn match xpmCol'.n.'Def /'.s.'/ contains=xpmCol'.n.'inDef'
89 exe 'hi link xpmCol'.n.'Def String'
91 " but highlight the first whitespace after chars in its color
92 exe 'syn match xpmCol'.n.'inDef /"'.chars.'/hs=s+'.(cpp+1).' contained'
93 exe 'hi link xpmCol'.n.'inDef xpmColor'.n
95 " remove the following whitespace from chars
96 let chars = substitute(chars, '.$', '', '')
98 " and create the syntax item contained in the pixel strings
99 exe 'syn match xpmColor'.n.' /'.chars.'/ contained'
100 exe 'syn cluster xpmColors add=xpmColor'.n
102 " if no color or color = "None" show background
103 if color == "" || substitute(color, '.*', '\L&', '') == 'none'
104 exe 'hi xpmColor'.n.' guifg=bg'
105 exe 'hi xpmColor'.n.' guibg=NONE'
107 exe 'hi xpmColor'.n." guifg='".color."'"
108 exe 'hi xpmColor'.n." guibg='".color."'"
112 break " no more color string
118 unlet color chars colors cpp n i s
120 endif " has("gui_running")
122 " Define the default highlighting.
123 " For version 5.7 and earlier: only when not done already
124 " For version 5.8 and later: only when an item doesn't have highlighting yet
125 if version >= 508 || !exists("did_xpm_syntax_inits")
127 let did_xpm_syntax_inits = 1
128 command -nargs=+ HiLink hi link <args>
130 command -nargs=+ HiLink hi def link <args>
134 HiLink xpmStorageClass StorageClass
136 HiLink xpmComment Comment
137 HiLink xpmPixelString String
142 let b:current_syntax = "xpm"
144 " vim: ts=8:sw=3:noet: