Monday, January 31, 2011

Re: Dynamically adding custom colour to rgb.txt

On Jan 26, 1:53 pm, lessthanideal <geoffrey.w...@thomsonreuters.com>
wrote:
> I have a custom defined colour "0 96 255 Bob_Blue" that I
> want to use in more than one place in Windows gvim 7.3. I
> can do this by adding it to $VIMRUNTIME\rgb.txtbut that
> will be lost when I upgrade. Defining colours in other
> places has been discussed comprehensively before e.g. in
> 2005 [1], the upshot as I understand it is that you have to
> define the name inrgb.txt, the file cannot be in another
> location, but changes to it are instantly available without
> restarting vim. As far as I can tell from the help and other
> searches this is still how it works.
>
> I'm therefore thinking of having a few lines at the start of
> _vimrc that will searchrgb.txtfor my custom colour and add
> it if not present. I'm wondering if there's an obvious
> problem with doing that, or maybe I'm overlooking a better
> way to define a new colour - I can't find one?
>
> Some other ways I've considered and rejected:
> . Always use #0060FF instead of the name
> . Redefine one of my Windows system colours and use that
> . Define it as a highlight group instead of a colour per se
>
> regards,
> Geoff
>
> [1]http://vim.1045645.n5.nabble.com/How-to-solve-E254-Cannot-allocate-co...

For posterity, here is the code I have in my _vimrc for this now.
I define my own colours in $VIM\vimfiles\rgb.txt, same format as
the default rgb.txt but missing the first !XConsortium line.

" read in my colours and default colours from the files
let s:my_colours = readfile($VIM . "\\vimfiles\\rgb.txt")
let s:rgb_file = readfile($VIMRUNTIME . "\\rgb.txt")

let s:added_colours = 0
" for each of my colours
for s:my_colour in s:my_colours
let s:found=0
" for each of the default colours...
for s:line in s:rgb_file
"...found my colour?
if s:line =~ s:my_colour
let s:found=1
break
endif
endfor
"if didn't find my colour...
if s:found==0
" ... add it to the default colours
let s:rgb_file += [s:my_colour]
let s:added_colours += 1
endif
endfor
" if we changed the default colours, update the file
if s:added_colours > 0
call writefile(s:rgb_file, $VIMRUNTIME . "\\rgb.txt")
endif

regards,
Geoff

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: