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