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