aboutsummaryrefslogtreecommitdiff
path: root/runtime/syntax/xpm.vim
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2023-11-29 22:39:54 +0000
committerJosh Rahm <joshuarahm@gmail.com>2023-11-29 22:39:54 +0000
commit21cb7d04c387e4198ca8098a884c78b56ffcf4c2 (patch)
tree84fe5690df1551f0bb2bdfe1a13aacd29ebc1de7 /runtime/syntax/xpm.vim
parentd9c904f85a23a496df4eb6be42aa43f007b22d50 (diff)
parent4a8bf24ac690004aedf5540fa440e788459e5e34 (diff)
downloadrneovim-colorcolchar.tar.gz
rneovim-colorcolchar.tar.bz2
rneovim-colorcolchar.zip
Merge remote-tracking branch 'upstream/master' into colorcolcharcolorcolchar
Diffstat (limited to 'runtime/syntax/xpm.vim')
-rw-r--r--runtime/syntax/xpm.vim181
1 files changed, 97 insertions, 84 deletions
diff --git a/runtime/syntax/xpm.vim b/runtime/syntax/xpm.vim
index be9f38723e..b094092b73 100644
--- a/runtime/syntax/xpm.vim
+++ b/runtime/syntax/xpm.vim
@@ -1,10 +1,12 @@
" Vim syntax file
" Language: X Pixmap
" Maintainer: Ronald Schild <rs@scutum.de>
-" Last Change: 2021 Oct 04
-" Version: 5.4n.1
+" Last Change: 2023 May 24
+" Version: 5.4n.2
" Jemma Nelson added termguicolors support
" Dominique Pellé fixed spelling support
+" Christian J. Robinson fixed use of global variables, moved
+" loop into a function
" quit when a syntax file was already loaded
if exists("b:current_syntax")
@@ -21,108 +23,119 @@ syn region xpmPixelString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@xpmCo
if has("gui_running") || has("termguicolors") && &termguicolors
-let color = ""
-let chars = ""
-let colors = 0
-let cpp = 0
-let n = 0
-let i = 1
+function s:CreateSyntax() abort
+ let color = ""
+ let chars = ""
+ let colors = 0
+ let cpp = 0
+ let n = 0
+ let lines = getline(1, '$')
-while i <= line("$") " scanning all lines
+ for line in lines " scanning all lines
- let s = matchstr(getline(i), '".\{-1,}"')
- if s != "" " does line contain a string?
+ let s = matchstr(line, '".\{-1,}"')
- if n == 0 " first string is the Values string
+ if s != "" " does line contain a string?
- " get the 3rd value: colors = number of colors
- let colors = substitute(s, '"\s*\d\+\s\+\d\+\s\+\(\d\+\).*"', '\1', '')
- " get the 4th value: cpp = number of character per pixel
- let cpp = substitute(s, '"\s*\d\+\s\+\d\+\s\+\d\+\s\+\(\d\+\).*"', '\1', '')
- if cpp =~ '[^0-9]'
- break " if cpp is not made of digits there must be something wrong
- endif
+ if n == 0 " first string is the Values string
- " Highlight the Values string as normal string (no pixel string).
- " Only when there is no slash, it would terminate the pattern.
- if s !~ '/'
- exe 'syn match xpmValues /' . s . '/'
- endif
- hi link xpmValues String
+ let values = split(s[1 : -2])
+
+ " Values string invalid, bail out
+ if len(values) != 4 && len(values) != 6 && len(values) != 7
+ return
+ endif
- let n = 1 " n = color index
+ " get the 3rd value: colors = number of colors
+ let colors = str2nr(values[2])
+ " get the 4th value: cpp = number of character per pixel
+ let cpp = str2nr(values[3])
- elseif n <= colors " string is a color specification
+ " these values must be positive, nonzero
+ if colors < 1 || cpp < 1
+ return
+ endif
+
+ " Highlight the Values string as normal string (no pixel string).
+ " Only when there is no slash, it would terminate the pattern.
+ if s !~ '/'
+ exe 'syn match xpmValues /' .. s .. '/'
+ endif
+ hi link xpmValues String
- " get chars = <cpp> length string representing the pixels
- " (first incl. the following whitespace)
- let chars = substitute(s, '"\(.\{'.cpp.'}\s\).*"', '\1', '')
+ let n = 1 " n = color index
- " now get color, first try 'c' key if any (color visual)
- let color = substitute(s, '".*\sc\s\+\(.\{-}\)\s*\(\(g4\=\|[ms]\)\s.*\)*\s*"', '\1', '')
- if color == s
- " no 'c' key, try 'g' key (grayscale with more than 4 levels)
- let color = substitute(s, '".*\sg\s\+\(.\{-}\)\s*\(\(g4\|[ms]\)\s.*\)*\s*"', '\1', '')
+ elseif n <= colors " string is a color specification
+
+ " get chars = <cpp> length string representing the pixels
+ " (first incl. the following whitespace)
+ let chars = substitute(s, '"\(.\{' .. cpp .. '}\s\).*"', '\1', '')
+
+ " now get color, first try 'c' key if any (color visual)
+ let color = substitute(s, '".*\sc\s\+\(.\{-}\)\s*\(\(g4\=\|[ms]\)\s.*\)*\s*"', '\1', '')
if color == s
- " next try: 'g4' key (4-level grayscale)
- let color = substitute(s, '".*\sg4\s\+\(.\{-}\)\s*\([ms]\s.*\)*\s*"', '\1', '')
+ " no 'c' key, try 'g' key (grayscale with more than 4 levels)
+ let color = substitute(s, '".*\sg\s\+\(.\{-}\)\s*\(\(g4\|[ms]\)\s.*\)*\s*"', '\1', '')
if color == s
- " finally try 'm' key (mono visual)
- let color = substitute(s, '".*\sm\s\+\(.\{-}\)\s*\(s\s.*\)*\s*"', '\1', '')
+ " next try: 'g4' key (4-level grayscale)
+ let color = substitute(s, '".*\sg4\s\+\(.\{-}\)\s*\([ms]\s.*\)*\s*"', '\1', '')
if color == s
- let color = ""
+ " finally try 'm' key (mono visual)
+ let color = substitute(s, '".*\sm\s\+\(.\{-}\)\s*\(s\s.*\)*\s*"', '\1', '')
+ if color == s
+ let color = ""
+ endif
endif
endif
endif
- endif
- " Vim cannot handle RGB codes with more than 6 hex digits
- if color =~ '#\x\{10,}$'
- let color = substitute(color, '\(\x\x\)\x\x', '\1', 'g')
- elseif color =~ '#\x\{7,}$'
- let color = substitute(color, '\(\x\x\)\x', '\1', 'g')
- " nor with 3 digits
- elseif color =~ '#\x\{3}$'
- let color = substitute(color, '\(\x\)\(\x\)\(\x\)', '0\10\20\3', '')
- endif
+ " Vim cannot handle RGB codes with more than 6 hex digits
+ if color =~ '#\x\{10,}$'
+ let color = substitute(color, '\(\x\x\)\x\x', '\1', 'g')
+ elseif color =~ '#\x\{7,}$'
+ let color = substitute(color, '\(\x\x\)\x', '\1', 'g')
+ " nor with 3 digits
+ elseif color =~ '#\x\{3}$'
+ let color = substitute(color, '\(\x\)\(\x\)\(\x\)', '0\10\20\3', '')
+ endif
- " escape meta characters in patterns
- let s = escape(s, '/\*^$.~[]')
- let chars = escape(chars, '/\*^$.~[]')
-
- " now create syntax items
- " highlight the color string as normal string (no pixel string)
- exe 'syn match xpmCol'.n.'Def /'.s.'/ contains=xpmCol'.n.'inDef'
- exe 'hi link xpmCol'.n.'Def String'
-
- " but highlight the first whitespace after chars in its color
- exe 'syn match xpmCol'.n.'inDef /"'.chars.'/hs=s+'.(cpp+1).' contained'
- exe 'hi link xpmCol'.n.'inDef xpmColor'.n
-
- " remove the following whitespace from chars
- let chars = substitute(chars, '.$', '', '')
-
- " and create the syntax item contained in the pixel strings
- exe 'syn match xpmColor'.n.' /'.chars.'/ contained'
- exe 'syn cluster xpmColors add=xpmColor'.n
-
- " if no color or color = "None" show background
- if color == "" || substitute(color, '.*', '\L&', '') == 'none'
- exe 'hi xpmColor'.n.' guifg=bg'
- exe 'hi xpmColor'.n.' guibg=NONE'
- elseif color !~ "'"
- exe 'hi xpmColor'.n." guifg='".color."'"
- exe 'hi xpmColor'.n." guibg='".color."'"
+ " escape meta characters in patterns
+ let s = escape(s, '/\*^$.~[]')
+ let chars = escape(chars, '/\*^$.~[]')
+
+ " now create syntax items
+ " highlight the color string as normal string (no pixel string)
+ exe 'syn match xpmCol' .. n .. 'Def /' .. s .. '/ contains=xpmCol' .. n .. 'inDef'
+ exe 'hi link xpmCol' .. n .. 'Def String'
+
+ " but highlight the first whitespace after chars in its color
+ exe 'syn match xpmCol' .. n .. 'inDef /"' .. chars .. '/hs=s+' .. (cpp + 1) .. ' contained'
+ exe 'hi link xpmCol' .. n .. 'inDef xpmColor' .. n
+
+ " remove the following whitespace from chars
+ let chars = substitute(chars, '.$', '', '')
+
+ " and create the syntax item contained in the pixel strings
+ exe 'syn match xpmColor' .. n .. ' /' .. chars .. '/ contained'
+ exe 'syn cluster xpmColors add=xpmColor' .. n
+
+ " if no color or color = "None" show background
+ if color == "" || substitute(color, '.*', '\L&', '') == 'none'
+ exe 'hi xpmColor' .. n .. ' guifg=bg'
+ exe 'hi xpmColor' .. n .. ' guibg=NONE'
+ elseif color !~ "'"
+ exe 'hi xpmColor' .. n .. " guifg='" .. color .. "'"
+ exe 'hi xpmColor' .. n .. " guibg='" .. color .. "'"
+ endif
+ let n += 1
+ else
+ break " no more color string
endif
- let n = n + 1
- else
- break " no more color string
endif
- endif
- let i = i + 1
-endwhile
+ endfor
+endfunction
-unlet color chars colors cpp n i s
+call s:CreateSyntax()
endif " has("gui_running") || has("termguicolors") && &termguicolors