blob: dfa6945a3195d482ed43f9ea0754ea4958493400 [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 Moolenaar3df01732017-02-17 22:47:16 +01004" Last Change: 2017 Feb 01
Bram Moolenaarc8734422012-06-01 22:38:45 +02005" (Dominique Pelle added @Spell)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006" Version: 5.8
Bram Moolenaar3df01732017-02-17 22:47:16 +01007" Jemma Nelson added termguicolors support
Bram Moolenaar071d4272004-06-13 20:20:40 +00008"
9" Made from xpm.vim by Ronald Schild <rs@scutum.de>
10
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020011" quit when a syntax file was already loaded
12if exists("b:current_syntax")
Bram Moolenaar071d4272004-06-13 20:20:40 +000013 finish
14endif
15
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +010016let s:cpo_save = &cpo
17set cpo&vim
18
Bram Moolenaar071d4272004-06-13 20:20:40 +000019syn region xpm2PixelString start="^" end="$" contains=@xpm2Colors
20syn keyword xpm2Todo TODO FIXME XXX contained
Bram Moolenaarc8734422012-06-01 22:38:45 +020021syn match xpm2Comment "\!.*$" contains=@Spell,xpm2Todo
Bram Moolenaar071d4272004-06-13 20:20:40 +000022
23
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020024command -nargs=+ Hi hi def <args>
Bram Moolenaar071d4272004-06-13 20:20:40 +000025
Bram Moolenaar3df01732017-02-17 22:47:16 +010026if has("gui_running") || has("termguicolors") && &termguicolors
Bram Moolenaar071d4272004-06-13 20:20:40 +000027
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', '')
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000049 if cpp =~ '[^0-9]'
50 break " if cpp is not made of digits there must be something wrong
51 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000053 " 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
Bram Moolenaarf37506f2016-08-31 22:22:10 +020058 hi def link xpm2Values Statement
Bram Moolenaar071d4272004-06-13 20:20:40 +000059
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'
Bram Moolenaarf37506f2016-08-31 22:22:10 +0200107 exe 'hi def link xpm2Col'.n.'Def Constant'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108
109 " but highlight the first whitespace after chars in its color
110 exe 'syn match xpm2Col'.n.'inDef /^'.chars.'/hs=s+'.(cpp).' contained'
Bram Moolenaarf37506f2016-08-31 22:22:10 +0200111 exe 'hi def link xpm2Col'.n.'inDef xpm2Color'.n
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112
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'
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000123 elseif color !~ "'"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 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
Bram Moolenaar3df01732017-02-17 22:47:16 +0100136endif " has("gui_running") || has("termguicolors") && &termguicolors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137
138" Define the default highlighting.
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200139" Only when an item doesn't have highlighting yet
140" The default highlighting.
Bram Moolenaarf37506f2016-08-31 22:22:10 +0200141hi def link xpm2Type Type
142hi def link xpm2StorageClass StorageClass
143hi def link xpm2Todo Todo
144hi def link xpm2Comment Comment
145hi def link xpm2PixelString String
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147delcommand Hi
148
149let b:current_syntax = "xpm2"
150
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +0100151let &cpo = s:cpo_save
152unlet s:cpo_save
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153" vim: ts=8:sw=2:noet: