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