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