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