blob: 8cc92a1189b0b2900d4e63e37d694d7f01d5153f [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim syntax file
2" Language: X Pixmap
3" Maintainer: Ronald Schild <rs@scutum.de>
Bram Moolenaar446cb832008-06-24 21:56:24 +00004" Last Change: 2008 May 28
Bram Moolenaar071d4272004-06-13 20:20:40 +00005" Version: 5.4n.1
6
Bram Moolenaar89bcfda2016-08-30 23:26:57 +02007" quit when a syntax file was already loaded
8if exists("b:current_syntax")
Bram Moolenaar071d4272004-06-13 20:20:40 +00009 finish
10endif
11
12syn keyword xpmType char
13syn keyword xpmStorageClass static
14syn keyword xpmTodo TODO FIXME XXX contained
15syn region xpmComment start="/\*" end="\*/" contains=xpmTodo
16syn region xpmPixelString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@xpmColors
17
18if has("gui_running")
19
20let color = ""
21let chars = ""
22let colors = 0
23let cpp = 0
24let n = 0
25let i = 1
26
27while i <= line("$") " scanning all lines
28
29 let s = matchstr(getline(i), '".\{-1,}"')
30 if s != "" " does line contain a string?
31
32 if n == 0 " first string is the Values string
33
34 " get the 3rd value: colors = number of colors
35 let colors = substitute(s, '"\s*\d\+\s\+\d\+\s\+\(\d\+\).*"', '\1', '')
36 " get the 4th value: cpp = number of character per pixel
37 let cpp = substitute(s, '"\s*\d\+\s\+\d\+\s\+\d\+\s\+\(\d\+\).*"', '\1', '')
Bram Moolenaar446cb832008-06-24 21:56:24 +000038 if cpp =~ '[^0-9]'
39 break " if cpp is not made of digits there must be something wrong
40 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
Bram Moolenaar446cb832008-06-24 21:56:24 +000042 " Highlight the Values string as normal string (no pixel string).
43 " Only when there is no slash, it would terminate the pattern.
44 if s !~ '/'
45 exe 'syn match xpmValues /' . s . '/'
46 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000047 hi link xpmValues String
48
49 let n = 1 " n = color index
50
51 elseif n <= colors " string is a color specification
52
53 " get chars = <cpp> length string representing the pixels
54 " (first incl. the following whitespace)
55 let chars = substitute(s, '"\(.\{'.cpp.'}\s\).*"', '\1', '')
56
57 " now get color, first try 'c' key if any (color visual)
58 let color = substitute(s, '".*\sc\s\+\(.\{-}\)\s*\(\(g4\=\|[ms]\)\s.*\)*\s*"', '\1', '')
59 if color == s
60 " no 'c' key, try 'g' key (grayscale with more than 4 levels)
61 let color = substitute(s, '".*\sg\s\+\(.\{-}\)\s*\(\(g4\|[ms]\)\s.*\)*\s*"', '\1', '')
62 if color == s
63 " next try: 'g4' key (4-level grayscale)
64 let color = substitute(s, '".*\sg4\s\+\(.\{-}\)\s*\([ms]\s.*\)*\s*"', '\1', '')
65 if color == s
66 " finally try 'm' key (mono visual)
67 let color = substitute(s, '".*\sm\s\+\(.\{-}\)\s*\(s\s.*\)*\s*"', '\1', '')
68 if color == s
69 let color = ""
70 endif
71 endif
72 endif
73 endif
74
75 " Vim cannot handle RGB codes with more than 6 hex digits
76 if color =~ '#\x\{10,}$'
77 let color = substitute(color, '\(\x\x\)\x\x', '\1', 'g')
78 elseif color =~ '#\x\{7,}$'
79 let color = substitute(color, '\(\x\x\)\x', '\1', 'g')
80 " nor with 3 digits
81 elseif color =~ '#\x\{3}$'
82 let color = substitute(color, '\(\x\)\(\x\)\(\x\)', '0\10\20\3', '')
83 endif
84
85 " escape meta characters in patterns
86 let s = escape(s, '/\*^$.~[]')
87 let chars = escape(chars, '/\*^$.~[]')
88
89 " now create syntax items
90 " highlight the color string as normal string (no pixel string)
91 exe 'syn match xpmCol'.n.'Def /'.s.'/ contains=xpmCol'.n.'inDef'
92 exe 'hi link xpmCol'.n.'Def String'
93
94 " but highlight the first whitespace after chars in its color
95 exe 'syn match xpmCol'.n.'inDef /"'.chars.'/hs=s+'.(cpp+1).' contained'
96 exe 'hi link xpmCol'.n.'inDef xpmColor'.n
97
98 " remove the following whitespace from chars
99 let chars = substitute(chars, '.$', '', '')
100
101 " and create the syntax item contained in the pixel strings
102 exe 'syn match xpmColor'.n.' /'.chars.'/ contained'
103 exe 'syn cluster xpmColors add=xpmColor'.n
104
105 " if no color or color = "None" show background
106 if color == "" || substitute(color, '.*', '\L&', '') == 'none'
107 exe 'hi xpmColor'.n.' guifg=bg'
108 exe 'hi xpmColor'.n.' guibg=NONE'
Bram Moolenaar446cb832008-06-24 21:56:24 +0000109 elseif color !~ "'"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110 exe 'hi xpmColor'.n." guifg='".color."'"
111 exe 'hi xpmColor'.n." guibg='".color."'"
112 endif
113 let n = n + 1
114 else
115 break " no more color string
116 endif
117 endif
118 let i = i + 1
119endwhile
120
121unlet color chars colors cpp n i s
122
123endif " has("gui_running")
124
125" Define the default highlighting.
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200126" Only when an item doesn't have highlighting yet
127command -nargs=+ HiLink hi def link <args>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200129HiLink xpmType Type
130HiLink xpmStorageClass StorageClass
131HiLink xpmTodo Todo
132HiLink xpmComment Comment
133HiLink xpmPixelString String
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200135delcommand HiLink
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
137let b:current_syntax = "xpm"
138
139" vim: ts=8:sw=3:noet: