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