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