Skip to content

Commit

Permalink
avoid conditional on critical path w/o self-modification
Browse files Browse the repository at this point in the history
When displaying the source code of an existing function, the :function
command will respect settings such as 'listchars' which makes the
recompilation code fragile. (I could research all the settings that
affect the output in such a way it can no longer be :execute’d and add
code to save, reset and restore them, but even if the list of settings
is comprehensive now, more such settings may get added to Vim in the
future. Therefore it is preferrable to circumstep the issue entirely.)
  • Loading branch information
ap committed Apr 19, 2012
1 parent 3a2faac commit d812b6c
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions after/syntax/css.vim
Expand Up @@ -10,8 +10,8 @@ for i in range(0, 255)
let s:hex[ printf( '%02x', i ) ] = i let s:hex[ printf( '%02x', i ) ] = i
endfor endfor


let s:black = '000000' let s:black = '#000000'
let s:white = 'ffffff' let s:white = '#ffffff'


function! s:FGForBG(color) function! s:FGForBG(color)
" pick suitable text color given a background color " pick suitable text color given a background color
Expand All @@ -22,6 +22,9 @@ function! s:FGForBG(color)
return r*30 + g*59 + b*11 > 12000 ? s:black : s:white return r*30 + g*59 + b*11 > 12000 ? s:black : s:white
endfunction endfunction


let s:color_prefix = 'gui'
let s:fg_color_calc = 'let color = "#" . a:color'

function! s:MatchColorValue(color, pattern) function! s:MatchColorValue(color, pattern)
if ! len(a:color) | return | endif if ! len(a:color) | return | endif
let group = 'cssColor' . tolower(a:color) let group = 'cssColor' . tolower(a:color)
Expand All @@ -33,7 +36,8 @@ function! s:MatchColorValue(color, pattern)
if stridx( currentmatch, 'match /'.pattern.'/' ) >= 0 | return '' | endif if stridx( currentmatch, 'match /'.pattern.'/' ) >= 0 | return '' | endif
exe 'syn match' group '/'.pattern.'/ contained' exe 'syn match' group '/'.pattern.'/ contained'
exe 'syn cluster cssColors add='.group exe 'syn cluster cssColors add='.group
exe 'hi' group 'ctermbg='.s:XTermColorForRGB(a:color) 'ctermfg='.s:FGForBG(a:color) exe s:fg_color_calc
exe 'hi' group s:color_prefix.'bg='.color s:color_prefix.'fg='.s:FGForBG(a:color)
return '' return ''
endfunction endfunction


Expand Down Expand Up @@ -94,6 +98,12 @@ if has("gui_running") || &t_Co==256


if ! has('gui_running') if ! has('gui_running')


let s:black = 0
let s:white = 15

let s:color_prefix = 'cterm'
let s:fg_color_calc = 'let color = s:XTermColorForRGB(a:color)'

" preset 16 vt100 colors " preset 16 vt100 colors
let s:xtermcolor = [ let s:xtermcolor = [
\ [ 0x00, 0x00, 0x00, 0 ], \ [ 0x00, 0x00, 0x00, 0 ],
Expand Down Expand Up @@ -140,9 +150,6 @@ if has("gui_running") || &t_Co==256
\ [ 0xE4, 0xE4, 0xE4, 254 ], \ [ 0xE4, 0xE4, 0xE4, 254 ],
\ [ 0xEE, 0xEE, 0xEE, 255 ]] \ [ 0xEE, 0xEE, 0xEE, 255 ]]


let s:black = 0
let s:white = 15

" the 6 values used in the xterm color cube " the 6 values used in the xterm color cube
" 0 95 135 175 215 255 " 0 95 135 175 215 255
let s:cubergb = [ 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF ] let s:cubergb = [ 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF ]
Expand Down Expand Up @@ -187,18 +194,6 @@ if has("gui_running") || &t_Co==256
endfor endfor
return best_match return best_match
endfunction endfunction

else
" recompile main functions after patching console code for GUI
" this avoids having any conditionals on a critical execution path
redir => source
silent! function s:MatchColorValue
redir END
let source = substitute( source, '\n[0-9]\+', "\n", 'g' )
let source = substitute( source, ' \zsfunction\ze ', 'function!', 'g' )
let source = substitute( source, 'cterm\([bf]g\)=', 'gui\1=#', 'g' )
let source = substitute( source, 's:XTermColorForRGB(', '(', 'g' )
exe source
endif endif


hi cssColor000000 guibg=#000000 guifg=#FFFFFF ctermbg=16 ctermfg=231 | syn cluster cssColors add=cssColor000000 hi cssColor000000 guibg=#000000 guifg=#FFFFFF ctermbg=16 ctermfg=231 | syn cluster cssColors add=cssColor000000
Expand Down

0 comments on commit d812b6c

Please sign in to comment.