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