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