├── LICENSE ├── autoload └── cpatch.vim ├── .gitignore ├── plugin └── cpatch.vim └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Linwei 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /autoload/cpatch.vim: -------------------------------------------------------------------------------- 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 | 67 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/#use-with-ide 110 | .pdm.toml 111 | 112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 113 | __pypackages__/ 114 | 115 | # Celery stuff 116 | celerybeat-schedule 117 | celerybeat.pid 118 | 119 | # SageMath parsed files 120 | *.sage.py 121 | 122 | # Environments 123 | .env 124 | .venv 125 | env/ 126 | venv/ 127 | ENV/ 128 | env.bak/ 129 | venv.bak/ 130 | 131 | # Spyder project settings 132 | .spyderproject 133 | .spyproject 134 | 135 | # Rope project settings 136 | .ropeproject 137 | 138 | # mkdocs documentation 139 | /site 140 | 141 | # mypy 142 | .mypy_cache/ 143 | .dmypy.json 144 | dmypy.json 145 | 146 | # Pyre type checker 147 | .pyre/ 148 | 149 | # pytype static type analyzer 150 | .pytype/ 151 | 152 | # Cython debug symbols 153 | cython_debug/ 154 | 155 | # PyCharm 156 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 157 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 158 | # and can be added to the global gitignore or merged into this file. For a more nuclear 159 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 160 | #.idea/ 161 | 162 | /update.cmd 163 | /.vscode/* 164 | -------------------------------------------------------------------------------- /plugin/cpatch.vim: -------------------------------------------------------------------------------- 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/16 19:38 8 | " 9 | " Homepage: https://github.com/skywind3000/vim-color-patch 10 | " 11 | " USAGE: 12 | " 13 | " This script will load colorscheme patch when current color changed 14 | " 15 | " let g:cpatch_path = '~/.vim/colors/patch' 16 | " 17 | " After setting "g:cpatch_path", if you change the current color: 18 | " 19 | " :color {NAME} 20 | " 21 | " This script will try to load the following scripts in order: 22 | " 23 | " 1) "~/.vim/colors/patch/__init__.vim" 24 | " 2) "~/.vim/colors/patch/__init__.lua" 25 | " 3) "~/.vim/colors/patch/{NAME}.vim" 26 | " 4) "~/.vim/colors/patch/{NAME}.lua" 27 | " 28 | " The first script "__init__.vim" in the "g:cpatch_path" folder will 29 | " be loaded for every colorscheme 30 | " 31 | "====================================================================== 32 | 33 | 34 | "---------------------------------------------------------------------- 35 | " configuration 36 | "---------------------------------------------------------------------- 37 | 38 | " color patch path: script will be searched here 39 | let g:cpatch_path = get(g:, 'cpatch_path', '~/.vim/colors/patch') 40 | 41 | " color patch subdirectory in every runtime path 42 | let g:cpatch_name = get(g:, 'cpatch_name', '') 43 | 44 | " runtime bang 45 | let g:cpatch_bang = get(g:, 'cpatch_bang', 0) 46 | 47 | " don't load .lua files 48 | let g:cpatch_disable_lua = get(g:, 'cpatch_disable_lua', 0) 49 | 50 | 51 | "---------------------------------------------------------------------- 52 | " display error 53 | "---------------------------------------------------------------------- 54 | function! s:traceback() abort 55 | let msg = v:throwpoint 56 | let p1 = stridx(msg, '_load_patch[') 57 | if p1 > 0 58 | let p2 = stridx(msg, ']..', p1) 59 | if p2 > 0 60 | let msg = strpart(msg, p2 + 3) 61 | endif 62 | endif 63 | redraw 64 | echohl ErrorMsg 65 | echom 'Error detected in ' . msg 66 | echom v:exception 67 | echohl None 68 | endfunc 69 | 70 | 71 | "---------------------------------------------------------------------- 72 | " load script 73 | "---------------------------------------------------------------------- 74 | function! s:load_patch(name, force) 75 | let names = ['__init__', a:name] 76 | let paths = [] 77 | let s:previous_color = get(s:, 'previous_color', '') 78 | if a:force == 0 79 | if a:name == s:previous_color 80 | return 1 81 | endif 82 | endif 83 | let s:previous_color = a:name 84 | if type(g:cpatch_path) == type('') 85 | let paths = split(g:cpatch_path, ',') 86 | elseif type(g:cpatch_path) == type([]) 87 | let paths = g:cpatch_path 88 | endif 89 | for name in names 90 | let rtpname = g:cpatch_name . '/' . name . '.vim' 91 | if g:cpatch_name != '' 92 | let bang = (g:cpatch_bang == 0)? '' : '!' 93 | try 94 | exec printf('runtime%s %s', bang, fnameescape(rtpname)) 95 | catch 96 | call s:traceback() 97 | endtry 98 | endif 99 | for p in paths 100 | if p == '' 101 | continue 102 | endif 103 | let p = expand(p) 104 | if isdirectory(p) 105 | if p !~ '\v[\/\\]$' 106 | let p = p . '/' 107 | endif 108 | let p = tr(p, '\', '/') 109 | for extname in ['.vim', '.lua'] 110 | let t = p . name . extname 111 | if extname == '.vim' 112 | let cmd = 'source ' . fnameescape(t) 113 | else 114 | let cmd = 'luafile ' . fnameescape(t) 115 | if g:cpatch_disable_lua 116 | continue 117 | endif 118 | endif 119 | if filereadable(t) 120 | try 121 | exec cmd 122 | catch 123 | call s:traceback() 124 | endtry 125 | endif 126 | endfor 127 | endif 128 | endfor 129 | endfor 130 | if has('autocmd') 131 | if exists('#User#CPatchPost') 132 | exec 'doautocmd User CPatchPost' 133 | endif 134 | endif 135 | return 0 136 | endfunc 137 | 138 | 139 | "---------------------------------------------------------------------- 140 | " load script 141 | "---------------------------------------------------------------------- 142 | let g:colors_name = get(g:, 'colors_name', '') 143 | call s:load_patch(g:colors_name, 0) 144 | 145 | 146 | "---------------------------------------------------------------------- 147 | " autocmd 148 | "---------------------------------------------------------------------- 149 | augroup CPatchEventGroup 150 | au! 151 | au VimEnter * call s:load_patch(g:colors_name, 0) 152 | au ColorScheme * call s:load_patch(expand(''), 1) 153 | augroup END 154 | 155 | 156 | 157 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Preface 2 | 3 | I usually like to make some modifications for the colorschemes I love to match my personal preference. In the past, I had three options: 4 | 5 | - Creating a PR for that colorscheme: It's my own preference, and I don't think it's likely to be accepted. 6 | - Maintaining my own fork: I need to track the upstream updates myself. 7 | - Writing ad hoc code for different colors in my `.vimrc`: Ugly. 8 | 9 | Therefore, I created this plugin to centralize all colorscheme patches in one place, typically in my dotfiles repository, and have them load automatically when I type `:color xxx` . 10 | 11 | ## Demo 12 | 13 | Random colorscheme `white-sand` before patch: 14 | 15 | ![](https://skywind3000.github.io/images/p/cpatch/c1-before.jpg) 16 | 17 | Apply patch `white-sand.vim`: 18 | 19 | ```VimL 20 | hi! SpecialKey term=bold ctermfg=238 guifg=#cac3bc 21 | ``` 22 | 23 | result: 24 | 25 | ![](https://skywind3000.github.io/images/p/cpatch/c1-after.jpg) 26 | 27 | I am interested in trying out this Emacs colorscheme that has been backported to Vim. However, it has some flaws. This plugin helps me fix it permanently without modifying the original colors. 28 | 29 | 30 | ## Get started 31 | 32 | Install this plugin with your plugin manager (without lazy loading): 33 | 34 | ```VimL 35 | Plug 'skywind3000/vim-color-patch' 36 | ``` 37 | 38 | Setup the patch search path: 39 | 40 | ```VimL 41 | let g:cpatch_path = '~/.vim/colors/patch' 42 | ``` 43 | 44 | And script with the same name will be loaded in this locations after `:color xxx` command. 45 | 46 | 47 | ## Examples 48 | 49 | #### 1) Change the line number style in "desert": 50 | 51 | create a new file named `desert.vim` in the `~/.vim/colors/patch` folder: 52 | 53 | ```viml 54 | highlight! LineNr term=bold cterm=NONE ctermfg=DarkGrey ctermbg=NONE 55 | \ gui=NONE guifg=#585858 guibg=NONE 56 | ``` 57 | 58 | and this script will be loaded after executing: 59 | 60 | ```VimL 61 | :color desert 62 | ``` 63 | 64 | And `LineNr` in `desert` will be overrided. 65 | 66 | #### 2) Remove all italic for Windows: 67 | 68 | edit `~/.vim/colors/patch/__init__.vim` : 69 | 70 | ```VimL 71 | if has('win32') || has('win64') 72 | call cpatch#disable_italic() 73 | endif 74 | ``` 75 | 76 | The `__init__.vim` is a public script and it will be sourced for every colorscheme. 77 | 78 | `vim-color-patch` provides some help functions like `disable_italic()` for style tuning. 79 | 80 | #### 3) Remove background colors for listchars in "monokai": 81 | 82 | edit `~/.vim/colors/patch/monokai.vim`: 83 | 84 | ```VimL 85 | call cpatch#remove_background('SpecialKey') 86 | ``` 87 | 88 | #### 4) Change VertSplit style for "gruvbox": 89 | 90 | edit `~/.vim/colors/patch/gruvbox.vim`: 91 | 92 | ```VimL 93 | hi! VertSplit term=reverse ctermfg=239 ctermbg=233 guifg=#64645e guibg=#211F1C 94 | ``` 95 | 96 | And `VertSplit` style in `gruvbox` will be overrided. 97 | 98 | 99 | ## Configuration 100 | 101 | #### g:cpatch_path 102 | 103 | This is where you keep your color patches, when you type: 104 | 105 | ```VimL 106 | :color {NAME} 107 | ``` 108 | 109 | This plugin will try to find scripts located in the directory specified by `g:cpatch_path` and source them in the following order: 110 | 111 | 1) `__init__.vim` 112 | 2) `__init__.lua` 113 | 3) `{NAME}.vim` 114 | 4) `{NAME}.lua` 115 | 116 | Default value: `"~/.vim/colors/patch"`. 117 | 118 | Can accept a list or a comma separated string. 119 | 120 | 121 | #### g:cpatch_disable_lua 122 | 123 | Disable loading lua files in the patch directory. 124 | 125 | Default value: `0` . 126 | 127 | 128 | ## Help functions 129 | 130 | This plugin provides some help functions for highlight manipulation: 131 | 132 | #### Remove style in all highlight groups: 133 | 134 | ```VimL 135 | function cpatch#remove_style(what) 136 | ``` 137 | 138 | argument `what` can be one of: 139 | 140 | ['underline', 'undercurl', 'reverse', 'inverse', 'italic', 'bold', 'standout'] 141 | 142 | #### Disable italics: 143 | 144 | ```VimL 145 | function cpatch#disable_italic() 146 | ``` 147 | 148 | same as `call cpatch#remove_style("italic")` . 149 | 150 | #### Disable bolds: 151 | 152 | ```VimL 153 | function cpatch#disable_bold() 154 | ``` 155 | 156 | same as `call cpatch#remove_style("bold")` . 157 | 158 | #### Remove background for a highlighting group: 159 | 160 | ```VimL 161 | function cpatch#remove_background(group) 162 | ``` 163 | 164 | eg. remove background in the SignColumn: 165 | 166 | ```VimL 167 | call cpatch#remove_background('SignColumn') 168 | ``` 169 | 170 | ## Credit 171 | 172 | Related project: 173 | 174 | - [vim-color-export](https://github.com/skywind3000/vim-color-export): Backport NeoVim colorschemes to Vim !! 175 | 176 | 177 | --------------------------------------------------------------------------------