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