Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Version 4.00

Fix CSApprox to not fail in vim 7.3 if not +gui, now that vim behaves properly even without +gui

Provide the :CSApprox command to re-run CSApprox's approximation algorithm even if the colorscheme hasn't changed - useful for when the user has tweaked some colors manually.

Better handling for the inverse (aka reverse) attribute for terminals that actually support it - and add the g:CSApprox_fake_reverse config variable to allow switching back to the old behavior for terminals that don't support real reverse video.

Fix an issue where CSApprox would unconditionally leave 'background' set to "light" - now it will leave 'background' unchanged when it runs.

Change the handling for Konsole to use the xterm palette by for KDE versions >= 2.2.0 - Konsole itself was changed to drop its old, slightly incompatible palette in KDE 2.2.0

Fix a minor issue where running vim in recovery mode with -r would result in a complaint from CSApprox that the terminal didn't have enough colors even when it did.

Fix an issue where, even if CSApprox had been disabled by setting g:CSApprox_loaded, a CSApprox error message could still be displayed.
  • Loading branch information...
commit a2958096696f9132ef0ece44b3fab93dac6df8d0 1 parent ce2b1fd
Matt Wozniski authored committed
View
29 after/plugin/CSApprox.vim
@@ -0,0 +1,29 @@
+" Copyright (c) 2012, Matthew J. Wozniski
+" All rights reserved.
+"
+" Redistribution and use in source and binary forms, with or without
+" modification, are permitted provided that the following conditions are met:
+" * Redistributions of source code must retain the above copyright
+" notice, this list of conditions and the following disclaimer.
+" * Redistributions in binary form must reproduce the above copyright
+" notice, this list of conditions and the following disclaimer in the
+" documentation and/or other materials provided with the distribution.
+" * The names of the contributors may not be used to endorse or promote
+" products derived from this software without specific prior written
+" permission.
+"
+" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY
+" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+" DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
+" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+" SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+" The last thing to do when sourced is to run and actually fix up the colors.
+if !has('gui_running') && exists(':CSApprox')
+ CSApprox
+endif
View
25 autoload/csapprox.vim
@@ -1,3 +1,28 @@
+" Copyright (c) 2012, Matthew J. Wozniski
+" All rights reserved.
+"
+" Redistribution and use in source and binary forms, with or without
+" modification, are permitted provided that the following conditions are met:
+" * Redistributions of source code must retain the above copyright
+" notice, this list of conditions and the following disclaimer.
+" * Redistributions in binary form must reproduce the above copyright
+" notice, this list of conditions and the following disclaimer in the
+" documentation and/or other materials provided with the distribution.
+" * The names of the contributors may not be used to endorse or promote
+" products derived from this software without specific prior written
+" permission.
+"
+" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY
+" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+" DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
+" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+" SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
let s:rgb = {}
let s:rgb["alice blue"] = "#f0f8ff"
View
78 autoload/csapprox/common.vim
@@ -0,0 +1,78 @@
+" Copyright (c) 2012, Matthew J. Wozniski
+" All rights reserved.
+"
+" Redistribution and use in source and binary forms, with or without
+" modification, are permitted provided that the following conditions are met:
+" * Redistributions of source code must retain the above copyright
+" notice, this list of conditions and the following disclaimer.
+" * Redistributions in binary form must reproduce the above copyright
+" notice, this list of conditions and the following disclaimer in the
+" documentation and/or other materials provided with the distribution.
+" * The names of the contributors may not be used to endorse or promote
+" products derived from this software without specific prior written
+" permission.
+"
+" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY
+" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+" DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
+" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+" SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+let s:xterm_colors = [ 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF ]
+let s:eterm_colors = [ 0x00, 0x2A, 0x55, 0x7F, 0xAA, 0xD4 ]
+let s:konsole_colors = [ 0x00, 0x33, 0x66, 0x99, 0xCC, 0xFF ]
+let s:xterm_greys = [ 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A,
+ \ 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
+ \ 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2,
+ \ 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE ]
+
+let s:urxvt_colors = [ 0x00, 0x8B, 0xCD, 0xFF ]
+let s:urxvt_greys = [ 0x2E, 0x5C, 0x73, 0x8B,
+ \ 0xA2, 0xB9, 0xD0, 0xE7 ]
+
+" Uses &term to determine which cube should be use. If &term is set to
+" "xterm" or begins with "screen", the variables g:CSApprox_eterm and
+" g:CSApprox_konsole can be used to select a different palette.
+function! csapprox#common#PaletteType()
+ if &t_Co == 88
+ let type = 'urxvt'
+ elseif &term ==# 'xterm' || &term =~# '^screen' || &term==# 'builtin_gui'
+ if exists('g:CSApprox_konsole') && g:CSApprox_konsole
+ let type = 'konsole'
+ elseif exists('g:CSApprox_eterm') && g:CSApprox_eterm
+ let type = 'eterm'
+ else
+ let type = 'xterm'
+ endif
+ elseif &term =~? '^konsole'
+ " Konsole only used its own palette up til KDE 4.2.0
+ if executable('kde4-config') && system('kde4-config --kde-version') =~ '^4\.[10]\.'
+ let type = 'konsole'
+ elseif executable('kde-config') && system('kde-config --version') =~# 'KDE: 3\.'
+ let type = 'konsole'
+ else
+ let type = 'xterm'
+ endif
+ elseif &term =~? '^eterm'
+ let type = 'eterm'
+ else
+ let type = 'xterm'
+ endif
+
+ return type
+endfunction
+
+" Retrieve the list of greyscale ramp colors for the current palette
+function! csapprox#common#Greys()
+ return (&t_Co == 88 ? s:urxvt_greys : s:xterm_greys)
+endfunction
+
+" Retrieve the list of non-greyscale ramp colors for the current palette
+function! csapprox#common#Colors()
+ return s:{csapprox#common#PaletteType()}_colors
+endfunction
View
91 autoload/csapprox/per_component.vim
@@ -0,0 +1,91 @@
+" Copyright (c) 2012, Matthew J. Wozniski
+" All rights reserved.
+"
+" Redistribution and use in source and binary forms, with or without
+" modification, are permitted provided that the following conditions are met:
+" * Redistributions of source code must retain the above copyright
+" notice, this list of conditions and the following disclaimer.
+" * Redistributions in binary form must reproduce the above copyright
+" notice, this list of conditions and the following disclaimer in the
+" documentation and/or other materials provided with the distribution.
+" * The names of the contributors may not be used to endorse or promote
+" products derived from this software without specific prior written
+" permission.
+"
+" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY
+" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+" DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
+" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+" SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+" Integer comparator used to sort the complete list of possible colors
+function! s:IntCompare(i1, i2)
+ return a:i1 == a:i2 ? 0 : a:i1 > a:i2 ? 1 : -1
+endfunc
+
+" Color comparator to find the nearest element to a given one in a given list
+function! s:NearestElemInList(elem, list)
+ let len = len(a:list)
+ for i in range(len-1)
+ if (a:elem <= (a:list[i] + a:list[i+1]) / 2)
+ return a:list[i]
+ endif
+ endfor
+ return a:list[len-1]
+endfunction
+
+" Takes 3 decimal values for r, g, and b, and returns the closest cube number.
+"
+" This approximator considers closeness based upon the individiual components.
+" For each of r, g, and b, it finds the closest cube component available on
+" the cube. If the three closest matches can combine to form a valid color,
+" this color is used, otherwise we repeat the search with the greys removed,
+" meaning that the three new matches must make a valid color when combined.
+function! csapprox#per_component#Approximate(r,g,b)
+ let hex = printf("%02x%02x%02x", a:r, a:g, a:b)
+
+ let colors = csapprox#common#Colors()
+ let greys = csapprox#common#Greys()
+ let type = csapprox#common#PaletteType()
+
+ if !exists('s:approximator_cache_'.type)
+ let s:approximator_cache_{type} = {}
+ endif
+
+ let rv = get(s:approximator_cache_{type}, hex, -1)
+ if rv != -1
+ return rv
+ endif
+
+ " Only obtain sorted list once
+ if !exists("s:".type."_greys_colors")
+ let s:{type}_greys_colors = sort(greys + colors, "s:IntCompare")
+ endif
+
+ let greys_colors = s:{type}_greys_colors
+
+ let r = s:NearestElemInList(a:r, greys_colors)
+ let g = s:NearestElemInList(a:g, greys_colors)
+ let b = s:NearestElemInList(a:b, greys_colors)
+
+ let len = len(colors)
+ if (r == g && g == b && index(greys, r) != -1)
+ let rv = 16 + len * len * len + index(greys, r)
+ else
+ let r = s:NearestElemInList(a:r, colors)
+ let g = s:NearestElemInList(a:g, colors)
+ let b = s:NearestElemInList(a:b, colors)
+ let rv = index(colors, r) * len * len
+ \ + index(colors, g) * len
+ \ + index(colors, b)
+ \ + 16
+ endif
+
+ let s:approximator_cache_{type}[hex] = rv
+ return rv
+endfunction
View
153 doc/CSApprox.txt
@@ -8,10 +8,10 @@
\___//___//_/ |_|/ .__// .__//_/ \___//_\_\ ~
/_/ /_/ ~
For Vim version 7.0 or newer
- Last changed 01 Apr 2009
+ Last changed 14 Sep 2012
By Matt Wozniski
- mjw@drexel.edu
+ godlygeek@gmail.com
Reference Manual~
@@ -74,18 +74,33 @@ NOTE: The generated colorscheme will only work in 88- and 256-color terminals,
standard for what those 16 colors look like other than 'orange-ish',
'red-ish', etc.
-NOTE: Although :CSApproxSnapshot can be used in both GVim and terminal Vim,
+NOTE: Although |:CSApproxSnapshot| can be used in both GVim and terminal Vim,
the resulting colors might be slightly off when run from terminal Vim.
I can find no way around this; Vim internally sets different colors when
running in a terminal than running in the GUI, and there's no way for
terminal Vim to figure out what color would have been used in GVim.
+ *:CSApprox*
+A command is also provided to run the approximation manually. This might be
+useful if some colors were set outside of a colorscheme file, for instance.
+By default, it will not change any colors unless no highlight group is set to
+a color above 15, which is CSApprox's normal behavior. This probably isn't
+useful in most use cases, though. On the other hand, if a ! is provided,
+CSApprox will skip that test and update the cterm value for every highlight
+group from the corresponding gui color. Syntax:
+>
+ :CSApprox[!]
+<
+
==============================================================================
2. Requirements *csapprox-requirements*
For CSApprox to work, there are 2 major requirements that must be met.
-a) GUI support *csapprox-gui-support* *csapprox-+gui*
+a) GUI support (or vim >= 7.3) *csapprox-gui-support* *csapprox-+gui*
+
+NOTE This section only applies to vim versions before 7.3.000 - a modern vim
+does not need GUI support in order for CSApprox to function.
If CSApprox is being used to adjust a scheme's colors transparently, then the
terminal "vim" binary that is being run must be built with GUI support (see
@@ -93,6 +108,7 @@ terminal "vim" binary that is being run must be built with GUI support (see
used to create a terminal colorscheme for high color terminals, then the
"vim" binary being used to create the scheme must be built with +gui, but the
scheme can be used in terminal "vim" binaries that weren't built with +gui.
+
NOTE that creating snapshots with GVim will work better than making them with
Vim, and (obviously) all "gvim" binaries are built with +gui.
@@ -134,20 +150,41 @@ set to something that implies high color support. Possible choices include
"xterm-256color" for 256 color support and "rxvt-unicode" for 88 color
support.
*csapprox-palettes*
-Also, there are three different 256 color cube palettes available and CSApprox
-has no way to tell which you're using unless $TERM is set to something that is
-specific to the terminal, like "konsole-256color" or "Eterm". Because of this, the
-most sane behavior is assuming the user is using the most popular palette,
-which is used by all but Konsole and Eterm, whenever $TERM is set to something
-generic like "xterm" or "screen". You can override this default, however -
-see |csapprox-configure|.
+Also, there are at least three different 256-color palettes in use. Nearly
+all terminals use an xterm-compatible palette, so most users need not concern
+themselves with this, with only two exceptions: Eterm uses a slightly
+different palette, and older Konsole (pre KDE 2.2.0) used a third palette.
+CSApprox has no reliable way to tell which palette your terminal uses, so it
+makes some educated guesses:
+
+ i) If vim thinks that there are 88 colors available, CSApprox will use the
+ xterm/urxvt-compatible 88 color palette (I don't know of any other
+ 88 color palette in use anywhere).
+
+ ii) If $TERM starts with "Eterm", CSApprox will approximate based on the
+ Eterm palette.
+
+ iii) If $TERM starts with "konsole", CSApprox will use the legacy Konsole
+ palette if either "kde4-config --kde-version" or "kde-config --version"
+ reports that the KDE version on the system is less than 4.2.0.
+ Otherwise, it will use the xterm palette.
+
+ iv) If $TERM starts with "xterm" or "screen", then CSApprox looks for the
+ vim variables "g:CSApprox_eterm" and "g:CSApprox_konsole".
+ If g:CSApprox_eterm is true, CSApprox uses the Eterm palette.
+ If g:CSApprox_konsole is true, CSApprox uses the old konsole palette.
+ Otherwise, CSApprox uses the xterm palette.
+
+ v) For all other $TERM values, CSApprox uses the xterm palette.
+
*csapprox-terminal-example*
-To turn on high color support without fixing $TERM, you can change t_Co in
-your .vimrc, and set either CSApprox_konsole or CSApprox_eterm if appropriate.
-One way would be to put something like this into your |vimrc|:
+To turn on high color support despite an incorrect $TERM, you can override
+t_Co (the vim name for the terminfo setting defining how many colors are
+available) in your .vimrc, and set either CSApprox_konsole or CSApprox_eterm
+if appropriate. You could put something like this into your |vimrc|:
>
if (&term == 'xterm' || &term =~? '^screen') && hostname() == 'my-machine'
- " On my machine, I use Konsole with 256 color support
+ " On my machine, I use an old Konsole with 256 color support
set t_Co=256
let g:CSApprox_konsole = 1
endif
@@ -180,15 +217,29 @@ g:CSApprox_verbose_level *g:CSApprox_verbose_level*
shown at verbosity level 1; some less important ones will be shown at
higher verbosity levels. Has no effect on snapshotted schemes.
+g:CSApprox_fake_reverse *g:CSApprox_fake_reverse*
+ In gvim, setting a highlight group like "Visual" (the color of your visual
+ mode selection) to do reverse video results in it reversing the colors of
+ each character cell under it. Some terminals don't support this and will
+ instead always use the default background color on the default foreground
+ color when asked for reverse video. If this variable is set to a non-zero
+ number, CSApprox will change any request for reverse video to the "Normal"
+ group's bg color on the "Normal" group's fg color, instead of asking the
+ terminal to do reverse video. This provides a middle ground for terminals
+ that don't properly support reverse video - it's worse than having the
+ terminal properly reverse the colors of each character cell, but it's
+ better than the broken behavior of some terminal emulators. This was the
+ default behavior before CSApprox 4.0.
+
g:CSApprox_eterm *g:CSApprox_eterm*
If set to a non-zero number, CSApprox will use the Eterm palette when
'term' is set to "xterm" or begins with "screen". Otherwise, the xterm
palette would be used. This also affects snapshotted schemes.
g:CSApprox_konsole *g:CSApprox_konsole*
- If set to a non-zero number, CSApprox will use the Konsole palette when
- 'term' is set to "xterm" or begins with "screen". Otherwise, the xterm
- palette would be used. This also affects snapshotted schemes.
+ If set to a non-zero number, CSApprox will use the old Konsole palette
+ when 'term' is set to "xterm" or begins with "screen". Otherwise, the
+ xterm palette would be used. This also affects snapshotted schemes.
g:CSApprox_attr_map *g:CSApprox_attr_map*
Since some attributes (like 'guisp') can't be used in a terminal, and
@@ -350,7 +401,7 @@ gives a very close approximation to the GVim colors without requiring any
changes to the colorscheme, or any user interaction. It only requires that
the plugin be installed on the machine where Vim is being run, and that the
user's environment meets the needs specified at |csapprox-requirements|. In
-the event that this doesn't work, a second option - using :CSApproxSnapshot
+the event that this doesn't work, a second option - using |:CSApproxSnapshot|
to create a new, 88-/256-color capable colorscheme - is available.
Ideally, the aim is for CSApprox to be completely transparent to the user.
@@ -378,15 +429,16 @@ improvements or complaints.
==============================================================================
5. Known Bugs and Limitations *csapprox-limitations*
-GUI support is required for transparently adapting schemes.
+GUI support or vim >= 7.3 is required for transparently adapting schemes.
There is nothing I can do about this given my chosen design. CSApprox works
by being notified every time a colorscheme sets some GUI colors, then
approximating those colors to similar terminal colors. Unfortunately, when
- Vim is not built with GUI support, it doesn't bother to store the GUI
+ Vim < 7.3 is not built with GUI support, it doesn't bother to store the GUI
colors, so querying for them fails. This leaves me completely unable to
tell what the colorscheme was trying to do. See |csapprox-+gui| for some
- potential workarounds if your distribution doesn't provide a Vim with +gui.
+ potential workarounds if your distribution doesn't provide a Vim with +gui
+ and you can't upgrade to a modern vim.
User intervention is sometimes required for information about the terminal.
@@ -428,6 +480,14 @@ Transparently adapting schemes is slow.
NOTE: As of CSApprox 3.50, the overhead is down to about 0.10 seconds on my
test machine.
+It isn't possible to approximate only a particular set of groups.
+
+ Unfortunately, the :CSApprox command will always update all groups, even if
+ only a small set of groups has changed. A future improvement would be to
+ provide a function called, say, CSApprox(), that takes an optional list of
+ highlight groups (default: all) and only does approximation for those
+ groups.
+
==============================================================================
6. Appendix - Terminals and Palettes *csapprox-terminal-list*
@@ -481,10 +541,9 @@ iTerm (iTerm.app):
*csapprox-konsole*
Konsole (konsole):
256 color palette
- Colors composed of: [ 0x00, 0x33, 0x66, 0x99, 0xCC, 0xFF ]
- Same greyscales as xterm
- You should set the g:CSApprox_konsole variable unless $TERM begins with
- 'konsole', case insensitive
+ Colors used to be composed of: [ 0x00, 0x33, 0x66, 0x99, 0xCC, 0xFF ]
+ As of KDE 2.2.0, colors match the xterm palette
+ Always used the same greyscales as xterm
*csapprox-eterm*
eterm (Eterm):
@@ -529,11 +588,42 @@ kterm (as of version 6.2.0)
==============================================================================
7. Changelog *csapprox-changelog*
+ 4.00 14 Sep 2012 Fix CSApprox to not fail in vim 7.3 if not +gui, now
+ that vim behaves properly even without +gui
+
+ Provide the |:CSApprox| command to re-run CSApprox's
+ approximation algorithm even if the colorscheme hasn't
+ changed - useful for when the user has tweaked some
+ colors manually.
+
+ Better handling for the |inverse| (aka reverse) attribute
+ for terminals that actually support it - and add the
+ g:CSApprox_fake_reverse config variable to allow
+ switching back to the old behavior for terminals that
+ don't support real reverse video.
+
+ Fix an issue where CSApprox would unconditionally leave
+ 'background' set to "light" - now it will leave
+ 'background' unchanged when it runs.
+
+ Change the handling for Konsole to use the xterm palette
+ by for KDE versions >= 2.2.0 - Konsole itself was
+ changed to drop its old, slightly incompatible palette
+ in KDE 2.2.0
+
+ Fix a minor issue where running vim in recovery mode
+ with |-r| would result in a complaint from CSApprox that
+ the terminal didn't have enough colors even when it did.
+
+ Fix an issue where, even if CSApprox had been disabled
+ by setting |g:CSApprox_loaded|, a CSApprox error message
+ could still be displayed.
+
3.50 01 Apr 2009 Fix a major regression that prevented the Eterm and
Konsole colors from being correctly snapshotted
Fix a related bug causing incorrect terminal colors
- after calling :CSApproxSnapshot
+ after calling |:CSApproxSnapshot|
Fix a bug causing black to be used instead of dark grey
@@ -553,12 +643,12 @@ kterm (as of version 6.2.0)
light of the above change
3.05 31 Jan 2009 Fix a harmless "Undefined variable" error in
- :CSApproxSnapshot
+ |:CSApproxSnapshot|
Fix a behavioral bug when dumping out colors defined
external to the scheme.
- 3.00 21 Jan 2009 Update the docs for better info on :CSApproxSnapshot
+ 3.00 21 Jan 2009 Update the docs for better info on |:CSApproxSnapshot|
Allow snapshotted schemes to work on Vim 6, and work
properly in Konsole and Eterm (thanks David Majnemer!)
@@ -570,7 +660,7 @@ kterm (as of version 6.2.0)
to run, either before or after the approximation
algorithm is run, for all schemes or one specific one.
- Also rewrite :CSApproxSnapshot to be more maintainable
+ Also rewrite |:CSApproxSnapshot| to be more maintainable
and less of a hack, and fix several bugs that it
contained.
@@ -593,7 +683,8 @@ always get the short end of the stick. He'd like to be notified of any
problems you find - after all, he took the time to write all this lovely
documentation, and this plugin, which took more time than you could possibly
imagine to get working transparently for every colorscheme he could get his
-hands on. You can contact him with any problems or praises at mjw@drexel.edu
+hands on. You can contact him with any problems or praises at
+godlygeek@gmail.com
==============================================================================
vim:tw=78:fo=tcq2:isk=!-~,^*,^\|,^\":ts=8:ft=help:norl:
View
246 plugin/CSApprox.vim
@@ -1,7 +1,7 @@
" CSApprox: Make gvim-only colorschemes Just Work terminal vim
-" Maintainer: Matthew Wozniski (mjw@drexel.edu)
-" Date: Wed, 01 Apr 2009 22:10:19 -0400
-" Version: 3.50
+" Maintainer: Matthew Wozniski (godlygeek@gmail.com)
+" Date: Fri, 14 Sep 2012 01:12:13 -0400
+" Version: 4.00
" History: :help csapprox-changelog
"
" Long Description:
@@ -23,13 +23,13 @@
" high color already.
"
" License:
-" Copyright (c) 2009, Matthew J. Wozniski
+" Copyright (c) 2012, Matthew J. Wozniski
" All rights reserved.
"
" Redistribution and use in source and binary forms, with or without
" modification, are permitted provided that the following conditions are met:
-" * Redistributions of source code must retain the above copyright notice,
-" this list of conditions and the following disclaimer.
+" * Redistributions of source code must retain the above copyright
+" notice, this list of conditions and the following disclaimer.
" * Redistributions in binary form must reproduce the above copyright
" notice, this list of conditions and the following disclaimer in the
" documentation and/or other materials provided with the distribution.
@@ -37,16 +37,16 @@
" products derived from this software without specific prior written
" permission.
"
-" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS
-" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
-" NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT,
-" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
-" OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
-" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
-" NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
-" EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY
+" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+" DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
+" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+" SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
" {>1} Basic plugin setup
@@ -54,20 +54,16 @@
" Quit if the user doesn't want or need us or is missing the gui feature. We
" need +gui to be able to check the gui color settings; vim doesn't bother to
" store them if it is not built with +gui.
-if !has('gui') || exists('g:CSApprox_loaded')
- " XXX This depends upon knowing the default for g:CSApprox_verbose_level
- let s:verbose = 1
- if exists("g:CSApprox_verbose_level")
- let s:verbose = g:CSApprox_verbose_level
- endif
-
- if ! has('gui') && s:verbose > 0
+if exists('g:CSApprox_loaded')
+ finish
+elseif !has('gui') && v:version < 703
+ " Vim versions less than < 7.3.0 need +gui.
+ " Warn unless the user set g:CSApprox_verbose_level to zero.
+ if get(g:, 'CSApprox_verbose_level', 1)
echomsg "CSApprox needs gui support - not loading."
echomsg " See :help |csapprox-+gui| for possible workarounds."
endif
- unlet s:verbose
-
finish
endif
@@ -77,110 +73,6 @@ let g:CSApprox_loaded = 1
let s:savecpo = &cpo
set cpo&vim
-" {>1} Built-in approximation algorithm
-
-" {>2} Cube definitions
-let s:xterm_colors = [ 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF ]
-let s:eterm_colors = [ 0x00, 0x2A, 0x55, 0x7F, 0xAA, 0xD4 ]
-let s:konsole_colors = [ 0x00, 0x33, 0x66, 0x99, 0xCC, 0xFF ]
-let s:xterm_greys = [ 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A,
- \ 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
- \ 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2,
- \ 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE ]
-
-let s:urxvt_colors = [ 0x00, 0x8B, 0xCD, 0xFF ]
-let s:urxvt_greys = [ 0x2E, 0x5C, 0x73, 0x8B,
- \ 0xA2, 0xB9, 0xD0, 0xE7 ]
-
-" {>2} Integer comparator
-" Used to sort the complete list of possible colors
-function! s:IntCompare(i1, i2)
- return a:i1 == a:i2 ? 0 : a:i1 > a:i2 ? 1 : -1
-endfunc
-
-" {>2} Approximator
-" Takes 3 decimal values for r, g, and b, and returns the closest cube number.
-" Uses &term to determine which cube should be used, though if &term is set to
-" "xterm" or begins with "screen", the variables g:CSApprox_eterm and
-" g:CSApprox_konsole can be used to select a different palette.
-"
-" This approximator considers closeness based upon the individiual components.
-" For each of r, g, and b, it finds the closest cube component available on
-" the cube. If the three closest matches can combine to form a valid color,
-" this color is used, otherwise we repeat the search with the greys removed,
-" meaning that the three new matches must make a valid color when combined.
-function! s:ApproximatePerComponent(r,g,b)
- let hex = printf("%02x%02x%02x", a:r, a:g, a:b)
-
- let greys = (&t_Co == 88 ? s:urxvt_greys : s:xterm_greys)
-
- if &t_Co == 88
- let colors = s:urxvt_colors
- let type = 'urxvt'
- elseif ((&term ==# 'xterm' || &term =~# '^screen' || &term==# 'builtin_gui')
- \ && exists('g:CSApprox_konsole') && g:CSApprox_konsole)
- \ || &term =~? '^konsole'
- let colors = s:konsole_colors
- let type = 'konsole'
- elseif ((&term ==# 'xterm' || &term =~# '^screen' || &term==# 'builtin_gui')
- \ && exists('g:CSApprox_eterm') && g:CSApprox_eterm)
- \ || &term =~? '^eterm'
- let colors = s:eterm_colors
- let type = 'eterm'
- else
- let colors = s:xterm_colors
- let type = 'xterm'
- endif
-
- if !exists('s:approximator_cache_'.type)
- let s:approximator_cache_{type} = {}
- endif
-
- let rv = get(s:approximator_cache_{type}, hex, -1)
- if rv != -1
- return rv
- endif
-
- " Only obtain sorted list once
- if !exists("s:".type."_greys_colors")
- let s:{type}_greys_colors = sort(greys + colors, "s:IntCompare")
- endif
-
- let greys_colors = s:{type}_greys_colors
-
- let r = s:NearestElemInList(a:r, greys_colors)
- let g = s:NearestElemInList(a:g, greys_colors)
- let b = s:NearestElemInList(a:b, greys_colors)
-
- let len = len(colors)
- if (r == g && g == b && index(greys, r) != -1)
- let rv = 16 + len * len * len + index(greys, r)
- else
- let r = s:NearestElemInList(a:r, colors)
- let g = s:NearestElemInList(a:g, colors)
- let b = s:NearestElemInList(a:b, colors)
- let rv = index(colors, r) * len * len
- \ + index(colors, g) * len
- \ + index(colors, b)
- \ + 16
- endif
-
- let s:approximator_cache_{type}[hex] = rv
- return rv
-endfunction
-
-" {>2} Color comparator
-" Finds the nearest element to the given element in the given list
-function! s:NearestElemInList(elem, list)
- let len = len(a:list)
- for i in range(len-1)
- if (a:elem <= (a:list[i] + a:list[i+1]) / 2)
- return a:list[i]
- endif
- endfor
- return a:list[len-1]
-endfunction
-
" {>1} Collect info for the set highlights
" {>2} Determine if synIDattr is usable
@@ -225,7 +117,7 @@ function! s:Highlights(modes)
for where in a:modes
let rv[i][where] = {}
- for attr in [ "bold", "italic", "reverse", "underline", "undercurl" ]
+ for attr in s:PossibleAttributes()
let rv[i][where][attr] = synIDattr(i, attr, where)
endfor
@@ -393,6 +285,11 @@ endfunction
" {>1} Derive and set cterm attributes
+" {>2} List of all possible attributes
+function! s:PossibleAttributes()
+ return [ "bold", "italic", "reverse", "underline", "undercurl" ]
+endfunction
+
" {>2} Attribute overrides
" Allow the user to override a specified attribute with another attribute.
" For example, the default is to map 'italic' to 'underline' (since many
@@ -414,8 +311,7 @@ function! s:NormalizeAttrMap(map)
let old = copy(a:map)
let new = filter(a:map, '0')
- let valid_attrs = [ 'bg', 'fg', 'sp', 'bold', 'italic',
- \ 'reverse', 'underline', 'undercurl' ]
+ let valid_attrs = [ 'bg', 'fg', 'sp' ] + s:PossibleAttributes()
let colorattrs = [ 'fg', 'bg', 'sp' ]
@@ -483,7 +379,7 @@ function! s:FixupCtermInfo(highlights)
endif
" Find attributes to be set in the terminal
- for attr in [ "bold", "italic", "reverse", "underline", "undercurl" ]
+ for attr in s:PossibleAttributes()
let hl.cterm[attr] = ''
if hl.gui[attr] == 1
if s:attr_map(attr) != ''
@@ -505,20 +401,27 @@ function! s:FixupCtermInfo(highlights)
let hl.cterm[s:attr_map('sp')] = hl.gui['sp']
endif
- if hl.cterm['reverse'] && hl.cterm.bg == ''
- let hl.cterm.bg = 'fg'
- endif
+ if exists("g:CSApprox_fake_reverse") && g:CSApprox_fake_reverse
+ if hl.cterm['reverse'] && hl.cterm.bg == ''
+ let hl.cterm.bg = 'fg'
+ endif
- if hl.cterm['reverse'] && hl.cterm.fg == ''
- let hl.cterm.fg = 'bg'
- endif
+ if hl.cterm['reverse'] && hl.cterm.fg == ''
+ let hl.cterm.fg = 'bg'
+ endif
- if hl.cterm['reverse']
- let hl.cterm.reverse = ''
+ if hl.cterm['reverse']
+ let hl.cterm.reverse = ''
+ endif
endif
endfor
endfunction
+" {>2} Kludge around inability to reference autoload functions
+function! s:DefaultApproximator(...)
+ return call('csapprox#per_component#Approximate', a:000)
+endfunction
+
" {>2} Set cterm colors for a highlight group
" Given the information for a single highlight group (ie, the value of
" one of the items in s:Highlights() already normalized with s:FixupCtermInfo
@@ -531,7 +434,7 @@ function! s:SetCtermFromGui(hl)
" Set up the default approximator function, if needed
if !exists("g:CSApprox_approximator_function")
- let g:CSApprox_approximator_function=function("s:ApproximatePerComponent")
+ let g:CSApprox_approximator_function = function("s:DefaultApproximator")
endif
" Clear existing highlights
@@ -584,7 +487,7 @@ function! s:SetCtermFromGui(hl)
endfor
" Finally, set the attributes
- let attrs = [ 'bold', 'italic', 'underline', 'undercurl' ]
+ let attrs = s:PossibleAttributes()
call filter(attrs, 'hl.cterm[v:val] == 1')
if !empty(attrs)
@@ -693,8 +596,15 @@ endfunction
" main function. This allows us to default to a message whenever any error,
" even a recoverable one, occurs, meaning the user quickly finds out when
" something's wrong, but makes it very easy for the user to make us silent.
-function! s:CSApprox()
+function! s:CSApprox(...)
try
+ if a:0 == 1 && a:1
+ if !exists('s:inhibit_hicolor_test')
+ let s:inhibit_hicolor_test = 0
+ endif
+ let s:inhibit_hicolor_test += 1
+ endif
+
let savelz = &lz
set lz
@@ -729,9 +639,13 @@ function! s:CSApprox()
call s:HandleHooks("pre", (exists("colors_name") ? colors_name : ""))
+ let old_bg = &bg
+
" Set 'verbose' set to the maximum of &verbose and CSApprox_verbose_level
exe max([&vbs, g:CSApprox_verbose_level]) 'verbose call s:CSApproxImpl()'
+ let &bg = old_bg
+
call s:HandleHooks("post", (exists("colors_name") ? colors_name : ""))
finally
if exists("colors_name")
@@ -744,6 +658,13 @@ function! s:CSApprox()
endif
let &lz = savelz
+
+ if a:0 == 1 && a:1
+ let s:inhibit_hicolor_test -= 1
+ if s:inhibit_hicolor_test == 0
+ unlet s:inhibit_hicolor_test
+ endif
+ endif
endtry
endfunction
@@ -755,7 +676,7 @@ endfunction
function! s:CSApproxImpl()
" Return if not running in an 88/256 color terminal
if &t_Co != 256 && &t_Co != 88
- if &verbose && !has('gui_running')
+ if &verbose && &t_Co != ''
echomsg "CSApprox skipped; terminal only has" &t_Co "colors, not 88/256"
echomsg "Try checking :help csapprox-terminal for workarounds"
endif
@@ -873,6 +794,18 @@ function! s:CSApproxSnapshot(file, overwrite)
let lines += [ ' command! -nargs=+ CSAHi exe "hi" <q-args>' ]
let lines += [ 'endif' ]
let lines += [ '' ]
+ let lines += [ 'function! s:old_kde()' ]
+ let lines += [ ' " Konsole only used its own palette up til KDE 4.2.0' ]
+ let lines += [ " if executable('kde4-config') && system('kde4-config --kde-version') =~ '^4\.[10]\.'" ]
+ let lines += [ ' return 1' ]
+ let lines += [ " elseif executable('kde-config') && system('kde-config --version') =~# 'KDE: 3\.'" ]
+ let lines += [ ' return 1' ]
+ let lines += [ ' else' ]
+ let lines += [ ' return 0' ]
+ let lines += [ ' endif' ]
+ let lines += [ 'endfunction' ]
+ let lines += [ '' ]
+
let lines += [ 'if 0' ]
for round in [ 'konsole', 'eterm', 'xterm', 'urxvt' ]
@@ -897,11 +830,17 @@ function! s:CSApproxSnapshot(file, overwrite)
call s:FixupGuiInfo(highlights)
if round == 'konsole' || round == 'eterm'
+ if round == 'konsole'
+ let term_matches_round = '(&term =~? "^konsole" && s:old_kde())'
+ else
+ let term_matches_round = '&term =~? "^' . round . '"'
+ endif
+
let lines += [ 'elseif has("gui_running") || (&t_Co == ' . &t_Co
\ . ' && (&term ==# "xterm" || &term =~# "^screen")'
\ . ' && exists("g:CSApprox_' . round . '")'
\ . ' && g:CSApprox_' . round . ')'
- \ . ' || &term =~? "^' . round . '"' ]
+ \ . ' || ' . term_matches_round ]
else
let lines += [ 'elseif has("gui_running") || &t_Co == ' . &t_Co ]
endif
@@ -914,7 +853,7 @@ function! s:CSApproxSnapshot(file, overwrite)
let hl = highlights[hlnum]
let line = ' CSAHi ' . hl.name
for type in [ 'term', 'cterm', 'gui' ]
- let attrs = [ 'reverse', 'bold', 'italic', 'underline', 'undercurl' ]
+ let attrs = s:PossibleAttributes()
call filter(attrs, 'hl[type][v:val] == 1')
let line .= ' ' . type . '=' . (empty(attrs) ? 'NONE' : join(attrs, ','))
if type != 'term'
@@ -963,9 +902,10 @@ endfunction
command! -bang -nargs=1 -complete=file -bar CSApproxSnapshot
\ call s:CSApproxSnapshot(<f-args>, strlen("<bang>"))
-" {>1} Hooks
+" {>2} Manual updates
+command -bang -bar CSApprox call s:CSApprox(strlen("<bang>"))
-" {>2} Autocmds
+" {>1} Autocmds
" Set up an autogroup to hook us on the completion of any :colorscheme command
augroup CSApprox
au!
@@ -973,12 +913,6 @@ augroup CSApprox
"au User CSApproxPost highlight Normal ctermbg=none | highlight NonText ctermbg=None
augroup END
-" {>2} Execute
-" The last thing to do when sourced is to run and actually fix up the colors.
-if !has('gui_running')
- call s:CSApprox()
-endif
-
" {>1} Restore compatibility options
let &cpo = s:savecpo
unlet s:savecpo
Please sign in to comment.
Something went wrong with that request. Please try again.