blob: efacdb9dc70d867bc15cb22d3d882aa4e9dfbaad [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 Moolenaarb7398fe2023-05-14 18:50:25 +01004" Last Change: 2023 May 11
5" Version: 5.4n.2
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 Moolenaarb7398fe2023-05-14 18:50:25 +01008" Christian J. Robinson fixed use of global variables, moved
9" loop into a compiled function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010
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 Moolenaar6e649222021-10-04 21:32:54 +010016syn spell notoplevel
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018syn keyword xpmType char
19syn keyword xpmStorageClass static
20syn keyword xpmTodo TODO FIXME XXX contained
Bram Moolenaar6e649222021-10-04 21:32:54 +010021syn region xpmComment start="/\*" end="\*/" contains=xpmTodo,@Spell
Bram Moolenaar071d4272004-06-13 20:20:40 +000022syn region xpmPixelString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@xpmColors
23
Bram Moolenaar3df01732017-02-17 22:47:16 +010024if has("gui_running") || has("termguicolors") && &termguicolors
Bram Moolenaar071d4272004-06-13 20:20:40 +000025
Bram Moolenaarb7398fe2023-05-14 18:50:25 +010026def s:CreateSyntax(): void
27 var color = ""
28 var chars = ""
29 var colors = 0
30 var cpp = 0
31 var n = 0
32 var lines = getline(1, '$')
Bram Moolenaar071d4272004-06-13 20:20:40 +000033
Bram Moolenaarb7398fe2023-05-14 18:50:25 +010034 for line in lines # scanning all lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaarb7398fe2023-05-14 18:50:25 +010036 var s = matchstr(line, '".\{-1,}"')
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaarb7398fe2023-05-14 18:50:25 +010038 if s != "" # does line contain a string?
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
Bram Moolenaarb7398fe2023-05-14 18:50:25 +010040 if n == 0 # first string is the Values string
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
Bram Moolenaarb7398fe2023-05-14 18:50:25 +010042 var values = split(s[1 : -2])
Bram Moolenaar071d4272004-06-13 20:20:40 +000043
Bram Moolenaarb7398fe2023-05-14 18:50:25 +010044 # Values string invalid, bail out
45 if len(values) != 4
46 return
47 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000048
Bram Moolenaarb7398fe2023-05-14 18:50:25 +010049 # get the 3rd value: colors = number of colors
50 colors = str2nr(values[2])
51 # get the 4th value: cpp = number of character per pixel
52 cpp = str2nr(values[3])
Bram Moolenaar071d4272004-06-13 20:20:40 +000053
Bram Moolenaarb7398fe2023-05-14 18:50:25 +010054 # these values must be positive, nonzero
55 if colors < 1 || cpp < 1
56 return
57 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000058
Bram Moolenaarb7398fe2023-05-14 18:50:25 +010059 # Highlight the Values string as normal string (no pixel string).
60 # Only when there is no slash, it would terminate the pattern.
61 if s !~ '/'
62 exe 'syn match xpmValues /' .. s .. '/'
63 endif
64 hi link xpmValues String
65
66 n = 1 # n = color index
67
68 elseif n <= colors # string is a color specification
69
70 # get chars = <cpp> length string representing the pixels
71 # (first incl. the following whitespace)
72 chars = substitute(s, '"\(.\{' .. cpp .. '}\s\).*"', '\1', '')
73
74 # now get color, first try 'c' key if any (color visual)
75 color = substitute(s, '".*\sc\s\+\(.\{-}\)\s*\(\(g4\=\|[ms]\)\s.*\)*\s*"', '\1', '')
Bram Moolenaar071d4272004-06-13 20:20:40 +000076 if color == s
Bram Moolenaarb7398fe2023-05-14 18:50:25 +010077 # no 'c' key, try 'g' key (grayscale with more than 4 levels)
78 color = substitute(s, '".*\sg\s\+\(.\{-}\)\s*\(\(g4\|[ms]\)\s.*\)*\s*"', '\1', '')
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 if color == s
Bram Moolenaarb7398fe2023-05-14 18:50:25 +010080 # next try: 'g4' key (4-level grayscale)
81 color = substitute(s, '".*\sg4\s\+\(.\{-}\)\s*\([ms]\s.*\)*\s*"', '\1', '')
Bram Moolenaar071d4272004-06-13 20:20:40 +000082 if color == s
Bram Moolenaarb7398fe2023-05-14 18:50:25 +010083 # finally try 'm' key (mono visual)
84 color = substitute(s, '".*\sm\s\+\(.\{-}\)\s*\(s\s.*\)*\s*"', '\1', '')
85 if color == s
86 color = ""
87 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 endif
89 endif
90 endif
Bram Moolenaarb7398fe2023-05-14 18:50:25 +010091
92 # Vim cannot handle RGB codes with more than 6 hex digits
93 if color =~ '#\x\{10,}$'
94 color = substitute(color, '\(\x\x\)\x\x', '\1', 'g')
95 elseif color =~ '#\x\{7,}$'
96 color = substitute(color, '\(\x\x\)\x', '\1', 'g')
97 # nor with 3 digits
98 elseif color =~ '#\x\{3}$'
99 color = substitute(color, '\(\x\)\(\x\)\(\x\)', '0\10\20\3', '')
100 endif
101
102 # escape meta characters in patterns
103 s = escape(s, '/\*^$.~[] ')
104 chars = escape(chars, '/\*^$.~[] ')
105
106 # now create syntax items
107 # highlight the color string as normal string (no pixel string)
108 exe 'syn match xpmCol' .. n .. 'Def /' .. s .. '/ contains=xpmCol' .. n .. 'inDef'
109 exe 'hi link xpmCol' .. n .. 'Def String'
110
111 # but highlight the first whitespace after chars in its color
112 exe 'syn match xpmCol' .. n .. 'inDef /"' .. chars .. '/hs=s+' .. (cpp + 1) .. ' contained'
113 exe 'hi link xpmCol' .. n .. 'inDef xpmColor' .. n
114
115 # remove the following whitespace from chars
116 chars = substitute(chars, '.$', '', '')
117
118 # and create the syntax item contained in the pixel strings
119 exe 'syn match xpmColor' .. n .. ' /' .. chars .. '/ contained'
120 exe 'syn cluster xpmColors add=xpmColor' .. n
121
122 # if no color or color = "None" show background
123 if color == "" || substitute(color, '.*', '\L&', '') == 'none'
124 exe 'hi xpmColor' .. n .. ' guifg=bg'
125 exe 'hi xpmColor' .. n .. ' guibg=NONE'
126 elseif color !~ "'"
127 exe 'hi xpmColor' .. n .. " guifg='" .. color .. "'"
128 exe 'hi xpmColor' .. n .. " guibg='" .. color .. "'"
129 endif
130 n += 1
131 else
132 break # no more color string
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 endif
Bram Moolenaarb7398fe2023-05-14 18:50:25 +0100135 endfor
136enddef
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137
Bram Moolenaarb7398fe2023-05-14 18:50:25 +0100138call s:CreateSyntax()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139
Bram Moolenaar3df01732017-02-17 22:47:16 +0100140endif " has("gui_running") || has("termguicolors") && &termguicolors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141
142" Define the default highlighting.
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200143" Only when an item doesn't have highlighting yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
Bram Moolenaarf37506f2016-08-31 22:22:10 +0200145hi def link xpmType Type
146hi def link xpmStorageClass StorageClass
147hi def link xpmTodo Todo
148hi def link xpmComment Comment
149hi def link xpmPixelString String
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
152let b:current_syntax = "xpm"
153
154" vim: ts=8:sw=3:noet: