neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

xpm2.vim (4697B)


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