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