view runtime/colors/tools/check_colors.vim @ 34336:d2ad8733db75 v9.1.0101

patch 9.1.0101: upper-case of German sharp s should be U+1E9E Commit: https://github.com/vim/vim/commit/bd1232a1faf56b614a1e74c4ce51bc6e0650ae00 Author: glepnir <glephunter@gmail.com> Date: Mon Feb 12 22:14:53 2024 +0100 patch 9.1.0101: upper-case of German sharp s should be U+1E9E Problem: upper-case of ? should be U+1E9E (CAPITAL LETTER SHARP S) (fenuks) Solution: Make gU, ~ and g~ convert the U+00DF LATIN SMALL LETTER SHARP S (?) to U+1E9E LATIN CAPITAL LETTER SHARP S (?), update tests (glepnir) This is part of Unicode 5.1.0 from April 2008, so should be fairly safe to use now and since 2017 is part of the German standard orthography, according to Wikipedia: https://en.wikipedia.org/wiki/Capital_%E1%BA%9E#cite_note-auto-12 There is however one exception: UnicodeData.txt for U+00DF LATIN SMALL LETTER SHARP S does NOT define U+1E9E LATIN CAPITAL LETTER SHARP S as its upper case version. Therefore, toupper() won't be able to convert from lower sharp s to upper case sharp s (the other way around however works, since U+00DF is considered the lower case character of U+1E9E and therefore tolower() works correctly for the upper case version). fixes: #5573 closes: #14018 Signed-off-by: glepnir <glephunter@gmail.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
author Christian Brabandt <cb@256bit.org>
date Mon, 12 Feb 2024 22:45:02 +0100
parents 90a966f5c77a
children
line wrap: on
line source

vim9script
# This script tests a color scheme for some errors and lists potential errors.
# Load the scheme and source this script, like this:
#    :edit colors/desert.vim | :ru colors/tools/check_colors.vim

def Test_check_colors()
    const savedview = winsaveview()
    cursor(1, 1)

    # err is
    # {
    #    colors_name: "message",
    #    init: "message",
    #    background: "message",
    #    ....etc
    #    highlight: {
    #       'Normal': "Missing ...",
    #       'Conceal': "Missing ..."
    #       ....etc
    #    }
    # }
    var err: dict<any> = {}

    # 1) Check g:colors_name is existing
    if search('\<\%(g:\)\?colors_name\>', 'cnW') == 0
        err['colors_name'] = 'g:colors_name not set'
    else
        err['colors_name'] = 'OK'
    endif

    # 2) Check for some well-defined highlighting groups
    const hi_groups = [
        'ColorColumn',
        'Comment',
        'Conceal',
        'Constant',
	'CurSearch',
        'Cursor',
        'CursorColumn',
        'CursorLine',
        'CursorLineNr',
        'CursorLineFold',
        'CursorLineSign',
        'DiffAdd',
        'DiffChange',
        'DiffDelete',
        'DiffText',
        'Directory',
        'EndOfBuffer',
        'Error',
        'ErrorMsg',
        'FoldColumn',
        'Folded',
        'Identifier',
        'Ignore',
        'IncSearch',
        'LineNr',
        'LineNrAbove',
        'LineNrBelow',
        'MatchParen',
        'ModeMsg',
        'MoreMsg',
        'NonText',
        'Normal',
        'Pmenu',
        'PmenuSbar',
        'PmenuSel',
        'PmenuThumb',
        'PreProc',
        'Question',
        'QuickFixLine',
        'Search',
        'SignColumn',
        'Special',
        'SpecialKey',
        'SpellBad',
        'SpellCap',
        'SpellLocal',
        'SpellRare',
        'Statement',
        'StatusLine',
        'StatusLineNC',
        'StatusLineTerm',
        'StatusLineTermNC',
        'TabLine',
        'TabLineFill',
        'TabLineSel',
        'Title',
        'Todo',
        'ToolbarButton',
        'ToolbarLine',
        'Type',
        'Underlined',
        'VertSplit',
        'Visual',
        'VisualNOS',
        'WarningMsg',
        'WildMenu',
        'debugPC',
        'debugBreakpoint',
    ]
    var groups = {}
    for group in hi_groups
        if search('\c@suppress\s\+\<' .. group .. '\>', 'cnW') != 0
            # skip check, if the script contains a line like
            # @suppress Visual:
            continue
        endif
        if search('hi\%[ghlight]!\= \+link \+' .. group, 'cnW') != 0 # Linked group
            continue
        endif
        if search('hi\%[ghlight] \+\<' .. group .. '\>', 'cnW') == 0
            groups[group] = 'No highlight definition for ' .. group
            continue
        endif
        if search('hi\%[ghlight] \+\<' .. group .. '\>.*[bf]g=', 'cnW') == 0
            groups[group] = 'Missing foreground or background color for ' .. group
            continue
        endif
        if search('hi\%[ghlight] \+\<' .. group .. '\>.*guibg=', 'cnW') != 0
            && search('hi\%[ghlight] \+\<' .. group .. '\>.*ctermbg=', 'cnW') == 0
            && group != 'Cursor'
            groups[group] = 'Missing bg terminal color for ' .. group
            continue
        endif
        if search('hi\%[ghlight] \+\<' .. group .. '\>.*guifg=', 'cnW') == 0
            && group !~ '^Diff'
            groups[group] = 'Missing guifg definition for ' .. group
            continue
        endif
        if search('hi\%[ghlight] \+\<' .. group .. '\>.*ctermfg=', 'cnW') == 0
            && group !~ '^Diff'
            && group != 'Cursor'
            groups[group] = 'Missing ctermfg definition for ' .. group
            continue
        endif
        # do not check for background colors, they could be intentionally left out
        cursor(1, 1)
    endfor
    err['highlight'] = groups

    # 3) Check, that it does not set background highlighting
    # Doesn't ':hi Normal ctermfg=253 ctermfg=233' also set the background sometimes?
    const bg_set = '\(set\?\|setl\(ocal\)\?\) .*\(background\|bg\)=\(dark\|light\)'
    const bg_let = 'let \%([&]\%([lg]:\)\?\)\%(background\|bg\)\s*=\s*\([''"]\?\)\w\+\1'
    const bg_pat = '\%(' .. bg_set .. '\|' .. bg_let .. '\)'
    const line = search(bg_pat, 'cnW')
    if search(bg_pat, 'cnW') != 0
        exe ":" .. line
        if search('hi \U\w\+\s\+\S', 'cbnW') != 0
            err['background'] = 'Should not set background option after :hi statement'
        endif
    else
        err['background'] = 'OK'
    endif
    cursor(1, 1)

    # 4) Check, that t_Co is checked
    var pat = '[&]t_Co\s*[<>=]=\?\s*\d\+'
    if search(pat, 'ncW') == 0
        err['t_Co'] = 'Does not check terminal for capable colors'
    endif

    # 5) Initializes correctly, e.g. should have at least:
    # hi clear
    pat = '^\s*hi\%[ghlight]\s*clear\s*$'
    if search(pat, 'cnW') == 0
        err['init'] = 'No initialization'
    endif

    # 6) Does not use :syn on
    if search('syn\%[tax]\s\+on', 'cnW') != 0
        err['background'] = 'Should not issue :syn on'
    endif

    # 7) Normal should be defined first, not use reverse, fg or bg
    cursor(1, 1)
    pat = 'hi\%[light] \+\%(link\|clear\)\@!\w\+\>'
    search(pat, 'cW') # Look for the first hi def, skipping `hi link` and `hi clear`
    if getline('.') !~# '\m\<Normal\>'
        err['highlight']['Normal'] = 'Should be defined first'
    elseif getline('.') =~# '\m\%(=\%(fg\|bg\)\)'
        err['highlight']['Normal'] = "Should not use 'fg' or 'bg'"
    elseif getline('.') =~# '\m=\%(inv\|rev\)erse'
        err['highlight']['Normal'] = 'Should not use reverse mode'
    endif

    # 8) TODO: XXX: Check if g:terminal_ansi_colors are defined

    winrestview(savedview)
    g:err = err

    Result(err)
enddef


def Result(err: dict<any>)
    var do_groups: bool = v:false
    echohl Title | echomsg "---------------" | echohl Normal
    for key in sort(keys(err))
        if key == 'highlight'
            do_groups = !empty(err[key])
            continue
        else
            if err[key] !~ 'OK'
                echohl Title
            endif
            echomsg printf("%15s: %s", key, err[key])
            echohl Normal
        endif
    endfor
    echohl Title | echomsg "---------------" | echohl Normal
    if do_groups
        echohl Title | echomsg "Groups" | echohl Normal
        for v1 in sort(keys(err['highlight']))
            echomsg printf("%25s: %s", v1, err['highlight'][v1])
        endfor
    endif
enddef

Test_check_colors()