Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " Vim syntax file |
| 2 | " Language: X Pixmap |
| 3 | " Maintainer: Ronald Schild <rs@scutum.de> |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 4 | " Last Change: 2023 May 11 |
| 5 | " Version: 5.4n.2 |
Bram Moolenaar | 3df0173 | 2017-02-17 22:47:16 +0100 | [diff] [blame] | 6 | " Jemma Nelson added termguicolors support |
Bram Moolenaar | 6e64922 | 2021-10-04 21:32:54 +0100 | [diff] [blame] | 7 | " Dominique Pellé fixed spelling support |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 8 | " Christian J. Robinson fixed use of global variables, moved |
| 9 | " loop into a compiled function |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10 | |
Bram Moolenaar | 89bcfda | 2016-08-30 23:26:57 +0200 | [diff] [blame] | 11 | " quit when a syntax file was already loaded |
| 12 | if exists("b:current_syntax") |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 13 | finish |
| 14 | endif |
| 15 | |
Bram Moolenaar | 6e64922 | 2021-10-04 21:32:54 +0100 | [diff] [blame] | 16 | syn spell notoplevel |
| 17 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 18 | syn keyword xpmType char |
| 19 | syn keyword xpmStorageClass static |
| 20 | syn keyword xpmTodo TODO FIXME XXX contained |
Bram Moolenaar | 6e64922 | 2021-10-04 21:32:54 +0100 | [diff] [blame] | 21 | syn region xpmComment start="/\*" end="\*/" contains=xpmTodo,@Spell |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 22 | syn region xpmPixelString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@xpmColors |
| 23 | |
Bram Moolenaar | 3df0173 | 2017-02-17 22:47:16 +0100 | [diff] [blame] | 24 | if has("gui_running") || has("termguicolors") && &termguicolors |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 25 | |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 26 | def 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 33 | |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 34 | for line in lines # scanning all lines |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 35 | |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 36 | var s = matchstr(line, '".\{-1,}"') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 37 | |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 38 | if s != "" # does line contain a string? |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 39 | |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 40 | if n == 0 # first string is the Values string |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 41 | |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 42 | var values = split(s[1 : -2]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 43 | |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 44 | # Values string invalid, bail out |
| 45 | if len(values) != 4 |
| 46 | return |
| 47 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 48 | |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 49 | # 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 53 | |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 54 | # these values must be positive, nonzero |
| 55 | if colors < 1 || cpp < 1 |
| 56 | return |
| 57 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 58 | |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 59 | # 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 76 | if color == s |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 77 | # 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 79 | if color == s |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 80 | # next try: 'g4' key (4-level grayscale) |
| 81 | color = substitute(s, '".*\sg4\s\+\(.\{-}\)\s*\([ms]\s.*\)*\s*"', '\1', '') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 82 | if color == s |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 83 | # 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 88 | endif |
| 89 | endif |
| 90 | endif |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 91 | |
| 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 133 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 134 | endif |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 135 | endfor |
| 136 | enddef |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 137 | |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 138 | call s:CreateSyntax() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 139 | |
Bram Moolenaar | 3df0173 | 2017-02-17 22:47:16 +0100 | [diff] [blame] | 140 | endif " has("gui_running") || has("termguicolors") && &termguicolors |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 141 | |
| 142 | " Define the default highlighting. |
Bram Moolenaar | 89bcfda | 2016-08-30 23:26:57 +0200 | [diff] [blame] | 143 | " Only when an item doesn't have highlighting yet |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 144 | |
Bram Moolenaar | f37506f | 2016-08-31 22:22:10 +0200 | [diff] [blame] | 145 | hi def link xpmType Type |
| 146 | hi def link xpmStorageClass StorageClass |
| 147 | hi def link xpmTodo Todo |
| 148 | hi def link xpmComment Comment |
| 149 | hi def link xpmPixelString String |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 150 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 151 | |
| 152 | let b:current_syntax = "xpm" |
| 153 | |
| 154 | " vim: ts=8:sw=3:noet: |