|
| 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