1
0
Fork 0
mirror of https://github.com/vim/vim synced 2025-03-17 23:37:08 +01:00
vim/runtime/syntax/xpm.vim

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

155 lines
4.8 KiB
VimL
Raw Normal View History

2004-06-13 20:20:40 +00:00
" Vim syntax file
" Language: X Pixmap
" Maintainer: Ronald Schild <rs@scutum.de>
2023-06-10 21:40:39 +01:00
" Last Change: 2023 May 24
2023-05-14 18:50:25 +01:00
" Version: 5.4n.2
2017-02-17 22:47:16 +01:00
" Jemma Nelson added termguicolors support
2021-10-04 21:32:54 +01:00
" Dominique Pellé fixed spelling support
2023-05-14 18:50:25 +01:00
" Christian J. Robinson fixed use of global variables, moved
" loop into a compiled function
2004-06-13 20:20:40 +00:00
" quit when a syntax file was already loaded
if exists("b:current_syntax")
2004-06-13 20:20:40 +00:00
finish
endif
2021-10-04 21:32:54 +01:00
syn spell notoplevel
2004-06-13 20:20:40 +00:00
syn keyword xpmType char
syn keyword xpmStorageClass static
syn keyword xpmTodo TODO FIXME XXX contained
2021-10-04 21:32:54 +01:00
syn region xpmComment start="/\*" end="\*/" contains=xpmTodo,@Spell
2004-06-13 20:20:40 +00:00
syn region xpmPixelString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@xpmColors
2017-02-17 22:47:16 +01:00
if has("gui_running") || has("termguicolors") && &termguicolors
2004-06-13 20:20:40 +00:00
2023-05-14 18:50:25 +01:00
def s:CreateSyntax(): void
var color = ""
var chars = ""
var colors = 0
var cpp = 0
var n = 0
var lines = getline(1, '$')
2004-06-13 20:20:40 +00:00
2023-05-14 18:50:25 +01:00
for line in lines # scanning all lines
2004-06-13 20:20:40 +00:00
2023-05-14 18:50:25 +01:00
var s = matchstr(line, '".\{-1,}"')
2004-06-13 20:20:40 +00:00
2023-05-14 18:50:25 +01:00
if s != "" # does line contain a string?
2004-06-13 20:20:40 +00:00
2023-05-14 18:50:25 +01:00
if n == 0 # first string is the Values string
2004-06-13 20:20:40 +00:00
2023-05-14 18:50:25 +01:00
var values = split(s[1 : -2])
# Values string invalid, bail out
2023-06-10 21:40:39 +01:00
if len(values) != 4 && len(values) != 6 && len(values) != 7
2023-05-14 18:50:25 +01:00
return
endif
2004-06-13 20:20:40 +00:00
2023-05-14 18:50:25 +01:00
# get the 3rd value: colors = number of colors
colors = str2nr(values[2])
# get the 4th value: cpp = number of character per pixel
cpp = str2nr(values[3])
2004-06-13 20:20:40 +00:00
2023-05-14 18:50:25 +01:00
# 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
2004-06-13 20:20:40 +00:00
2023-05-14 18:50:25 +01:00
n = 1 # n = color index
2004-06-13 20:20:40 +00:00
2023-05-14 18:50:25 +01:00
elseif n <= colors # string is a color specification
# get chars = <cpp> length string representing the pixels
# (first incl. the following whitespace)
chars = substitute(s, '"\(.\{' .. cpp .. '}\s\).*"', '\1', '')
# now get color, first try 'c' key if any (color visual)
color = substitute(s, '".*\sc\s\+\(.\{-}\)\s*\(\(g4\=\|[ms]\)\s.*\)*\s*"', '\1', '')
2004-06-13 20:20:40 +00:00
if color == s
2023-05-14 18:50:25 +01:00
# no 'c' key, try 'g' key (grayscale with more than 4 levels)
color = substitute(s, '".*\sg\s\+\(.\{-}\)\s*\(\(g4\|[ms]\)\s.*\)*\s*"', '\1', '')
2004-06-13 20:20:40 +00:00
if color == s
2023-05-14 18:50:25 +01:00
# next try: 'g4' key (4-level grayscale)
color = substitute(s, '".*\sg4\s\+\(.\{-}\)\s*\([ms]\s.*\)*\s*"', '\1', '')
2004-06-13 20:20:40 +00:00
if color == s
2023-05-14 18:50:25 +01:00
# finally try 'm' key (mono visual)
color = substitute(s, '".*\sm\s\+\(.\{-}\)\s*\(s\s.*\)*\s*"', '\1', '')
if color == s
color = ""
endif
2004-06-13 20:20:40 +00:00
endif
endif
endif
2023-05-14 18:50:25 +01:00
# Vim cannot handle RGB codes with more than 6 hex digits
if color =~ '#\x\{10,}$'
color = substitute(color, '\(\x\x\)\x\x', '\1', 'g')
elseif color =~ '#\x\{7,}$'
color = substitute(color, '\(\x\x\)\x', '\1', 'g')
# nor with 3 digits
elseif color =~ '#\x\{3}$'
color = substitute(color, '\(\x\)\(\x\)\(\x\)', '0\10\20\3', '')
endif
2004-06-13 20:20:40 +00:00
2023-05-14 18:50:25 +01:00
# escape meta characters in patterns
2023-06-10 21:40:39 +01:00
s = escape(s, '/\*^$.~[]')
chars = escape(chars, '/\*^$.~[]')
2023-05-14 18:50:25 +01:00
# 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
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
n += 1
else
break # no more color string
2004-06-13 20:20:40 +00:00
endif
endif
2023-05-14 18:50:25 +01:00
endfor
enddef
2004-06-13 20:20:40 +00:00
2023-05-14 18:50:25 +01:00
call s:CreateSyntax()
2004-06-13 20:20:40 +00:00
2017-02-17 22:47:16 +01:00
endif " has("gui_running") || has("termguicolors") && &termguicolors
2004-06-13 20:20:40 +00:00
" Define the default highlighting.
" Only when an item doesn't have highlighting yet
hi def link xpmType Type
hi def link xpmStorageClass StorageClass
hi def link xpmTodo Todo
hi def link xpmComment Comment
hi def link xpmPixelString String
2004-06-13 20:20:40 +00:00
let b:current_syntax = "xpm"
" vim: ts=8:sw=3:noet: