Skip to content

Commit 252dbd7

Browse files
author
skywind3000
committed
initial commit
1 parent 8c87cec commit 252dbd7

File tree

3 files changed

+207
-0
lines changed

3 files changed

+207
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,6 @@ cython_debug/
158158
# and can be added to the global gitignore or merged into this file. For a more nuclear
159159
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160160
#.idea/
161+
162+
/update.cmd
163+

autoload/cpatch.vim

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"======================================================================
2+
"
3+
" cpatch.vim - help functions
4+
"
5+
" Created by skywind on 2024/01/05
6+
" Last Modified: 2024/01/05 16:29:52
7+
"
8+
"======================================================================
9+
10+
11+
"----------------------------------------------------------------------
12+
" remove style
13+
"----------------------------------------------------------------------
14+
function! cpatch#remove_style(what) abort
15+
let need = ['underline', 'undercurl', 'reverse', 'inverse', 'italic']
16+
call extend(need, ['standout', 'bold'])
17+
call filter(need, 'v:val != a:what')
18+
let hid = 1
19+
while 1
20+
let hln = synIDattr(hid, 'name')
21+
if !hlexists(hln) | break | endif
22+
if hid == synIDtrans(hid)
23+
let change = ''
24+
for mode in ['gui', 'term', 'cterm']
25+
if synIDattr(hid, a:what, mode)
26+
let atr = deepcopy(need)
27+
call filter(atr, 'synIDattr(hid, v:val, mode)')
28+
let result = empty(atr) ? 'NONE' : join(atr, ',')
29+
let change .= printf(' %s=%s', mode, result)
30+
endif
31+
endfor
32+
if change != ''
33+
exec 'highlight ' . hln . ' ' . change
34+
endif
35+
endif
36+
let hid += 1
37+
endwhile
38+
endfunc
39+
40+
41+
42+
"----------------------------------------------------------------------
43+
"
44+
"----------------------------------------------------------------------
45+
function! cpatch#disable_italic() abort
46+
call cpatch#remove_style('italic')
47+
endfunc
48+
49+
50+
"----------------------------------------------------------------------
51+
"
52+
"----------------------------------------------------------------------
53+
function! cpatch#disable_bold() abort
54+
call cpatch#remove_style('bold')
55+
endfunc
56+
57+
58+
"----------------------------------------------------------------------
59+
"
60+
"----------------------------------------------------------------------
61+
function! cpatch#remove_background(group) abort
62+
exe printf('hi %s ctermbg=NONE guibg=NONE', a:group)
63+
endfunc
64+
65+
66+

plugin/cpatch.vim

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
" vim: set ts=4 sw=4 tw=78 noet :"
2+
"======================================================================
3+
"
4+
" cpatch.vim - load colorscheme patch automatically
5+
"
6+
" Created by skywind on 2024/01/05
7+
" Last Modified: 2024/01/05 14:48:57
8+
"
9+
" USAGE:
10+
"
11+
" This script will load colorscheme patch when current color changed
12+
"
13+
" let g:cpatch_path = '~/.vim/cpatch'
14+
"
15+
" After setting "g:cpatch_path", if you change the current color:
16+
"
17+
" :color {NAME}
18+
"
19+
" This script will try to load the following scripts in order:
20+
"
21+
" 1) "~/.vim/cpatch/__init__.vim"
22+
" 2) "~/.vim/cpatch/{NAME}.vim"
23+
"
24+
" The first script "__init__.vim" in the "g:cpatch_path" folder will
25+
" be loaded for every colorscheme
26+
"
27+
"======================================================================
28+
29+
30+
"----------------------------------------------------------------------
31+
" configuration
32+
"----------------------------------------------------------------------
33+
34+
" color patch path: script will be searched here
35+
let g:cpatch_path = get(g:, 'cpatch_path', '')
36+
37+
" color patch subdirectory in every runtime path
38+
let g:cpatch_name = get(g:, 'cpatch_name', '')
39+
40+
" runtime bang
41+
let g:cpatch_bang = get(g:, 'cpatch_bang', 0)
42+
43+
44+
"----------------------------------------------------------------------
45+
" load script
46+
"----------------------------------------------------------------------
47+
function! s:load_patch(name, force)
48+
let names = ['__init__.vim', a:name .. '.vim']
49+
let paths = []
50+
let s:previous_color = get(s:, 'previous_color', '')
51+
if a:force == 0
52+
if a:name == s:previous_color
53+
return 1
54+
endif
55+
endif
56+
let s:previous_color = a:name
57+
if type(g:cpatch_path) == type('')
58+
let paths = split(g:cpatch_path, ',')
59+
elseif type(g:cpatch_path) == type([])
60+
let paths = g:cpatch_path
61+
endif
62+
for name in names
63+
let rtpname = g:cpatch_name .. '/' .. name
64+
if g:cpatch_name != ''
65+
let bang = (g:cpatch_bang == 0)? '' : '!'
66+
try
67+
exec printf('runtime%s %s', bang, fnameescape(rtpname))
68+
catch
69+
let msg = v:throwpoint
70+
let p1 = stridx(msg, '_load_patch[')
71+
if p1 > 0
72+
let p2 = stridx(msg, ']..', p1)
73+
if p2 > 0
74+
let msg = strpart(msg, p2 + 3)
75+
endif
76+
endif
77+
redraw
78+
echohl ErrorMsg
79+
echom 'Error detected in ' . msg
80+
echom v:exception
81+
echohl None
82+
endtry
83+
endif
84+
for p in paths
85+
if p == ''
86+
continue
87+
endif
88+
let p = expand(p)
89+
if isdirectory(p)
90+
if p !~ '\v[\/\\]$'
91+
let p = p .. '/'
92+
endif
93+
let p = tr(p, '\', '/')
94+
let t = p .. name
95+
if filereadable(t)
96+
try
97+
exec 'source ' .. fnameescape(t)
98+
catch
99+
let msg = v:throwpoint
100+
let p1 = stridx(msg, '_load_patch[')
101+
if p1 > 0
102+
let p2 = stridx(msg, ']..', p1)
103+
if p2 > 0
104+
let msg = strpart(msg, p2 + 3)
105+
endif
106+
endif
107+
redraw
108+
echohl ErrorMsg
109+
echom 'Error detected in ' . msg
110+
echom v:exception
111+
echohl None
112+
endtry
113+
endif
114+
endif
115+
endfor
116+
endfor
117+
return 0
118+
endfunc
119+
120+
121+
"----------------------------------------------------------------------
122+
" load script
123+
"----------------------------------------------------------------------
124+
let g:colors_name = get(g:, 'colors_name', '')
125+
call s:load_patch(g:colors_name, 0)
126+
127+
128+
"----------------------------------------------------------------------
129+
" autocmd
130+
"----------------------------------------------------------------------
131+
augroup CPatchEventGroup
132+
au!
133+
au VimEnter * call s:load_patch(g:colors_name, 0)
134+
au ColorScheme * call s:load_patch(expand('<amatch>'), 1)
135+
augroup END
136+
137+
138+

0 commit comments

Comments
 (0)