summaryrefslogtreecommitdiff
path: root/xxx/vim-old/vim-colortune/colortune.vim
blob: 97f2afd7ee04899fac625e29944d7e5be5252901 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" Miguel's ColorTune plugin
"
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" {{{
"
" TODO LIST:
" {{{
" * restrict movement and values, but how???
" * use R/G/B r/g/b keys to increase/decrease rgb values
" * map some nice short-keys for this buffer only
" * show rgb.txt! to alllow selection of named gui-colors
" * show some dropdowns or something for other stuff
" * let user override some of the values from users vimrc!!
" * enable reset to colortheme
" * clear all groups / clear group completely or just for active mode!
" * document and publish
" * treat NONE and NOT SET AT ALL differently!!
" * add some error detection and treatment! ie. setline etc..
" * just redraw single lines where possible
" * REORDER and CLEANUP
" * create bundle and move most of functioanlity to autoload,
"	except Colortune_start or somehting
" * check if we are not reinventing the wheel at some places:
"	ie: getline()?
" * check if there are already such plugins, and find out why this one is
"	better ;)
" * BUG: regex limited number so start/stop omittted in complete list
"	so it is also not exported via config_dump right now
" * BUG: i do not understand set background. it is always set to dark by now
" * BUG: column names are not nicely aligned
" * BUG: spaces are not supported within key-values
"
" }}}

" Set some basic defaults and initialize
" {{{

" modes are: 0-term, 1-cterm, 2-gui, 3-complete
let s:default_mode=2

let s:colortune_buf_name='__COLORTUNE_PLUGIN__'

let s:temp_reg='a'

" Define the popular highlight groups
let s:colortable=[
    \	'Normal', 'Comment', 'Constant', 'Identifier',
    \	'Statement', 'PreProc', 'Type', 'Special', 'Underlined',
    \	'Ignore', 'Error', 'Todo', 'ColorColumn', 'Conceal', 'Cursor',
    \	'CursorIM', 'CursorColumn', 'CursorLine', 'Directory',
    \	'DiffAdd', 'DiffChange', 'DiffText', 'ErrorMsg', 'VertSplit',
    \	'Folded', 'FoldColumn', 'SignColumn', 'IncSearch', 'LineNr',
    \	'CursorLineNr', 'MatchParen', 'ModeMsg', 'MoreMsg', 'NonText',
    \	'PmenuSel', 'PmenuSbar', 'PmenuThumb', 'Question', 'Search',
    \	'SpecialKey', 'SpellBad', 'SpellCap', 'SpellLocal',
    \	'SpellRare', 'StatusLine', 'StatusLineNC', 'Title', 'Visual',
    \	'VisualNOS', 'WarningMsg', 'WildMenu', 'TabLine',
    \	'TabLineFill', 'TabLineSel' ]


" Define modes and their relevant keys in the highlight groups
let s:modes_labels=['Black & White Terminals','Color Terminals','GUI','Complete']
let s:modes=[	['term','start','stop'],
	    \	['cterm','ctermfg','ctermbg'],
	    \	['gui','guifg','guibg','guisp'],
	    \   ['term','cterm','ctermfg','ctermbg','gui','guifg','guibg','guisp']]

" init
"
let s:current_mode=s:default_mode
" }}}

" Colortune_getline()
" {{{
" Retruns current line of current buffer.
"
function! Colortune_getline()
    return getline(line('.'))
endfunction
" }}}

" Colortune_getcmdoutput(cmd)
" {{{
" Redirect the output of cmd to a temporary register and return result.
"
function! Colortune_getcmdoutput(cmd)

	let reg_val=getreg(s:temp_reg)
	redir @a
	    silent execute a:cmd
	redir END
	let val=getreg(s:temp_reg)
	call setreg(s:temp_reg,reg_val)
	return val

endfunction
" }}}

" Colortune_getattr(group,key)
" {{{
" Get the value of one 'key' of a highlighting group
" returns 'NONE' if the requested key is not specified
"
function! Colortune_getattr(group,key)

	let hil=Colortune_getcmdoutput('hi '.a:group)

	" extract the value for given key (if exists) and return
	if hil !~# a:key.'=.\+'
	    let val='NONE'
	else
	    let val=substitute(hil.' ','^.* '.a:key.'=\(.\{-}\)\s.*$','\1','')
	endif
	return val

endfunction
"}}}

" Colortune_getmax(key)
" {{{
" Returns maximal length of values for requested key.
" key='GROUP_NAME' returns maximal length of highlight group names.
"
function! Colortune_getmax(key)
    let max=0
    for group in s:colortable
	if(a:key==#'GROUP_NAME')
	    let length=len(group)
	else
	    let length=len(Colortune_getattr(group,a:key))
	endif
	let max = max<length ? length : max
    endfor
    return max
endfunction
" }}}

" Colortune_extend(val,fill,len)
" {{{
" Extend string 'val' with characters 'fill' to match length of 'len'
"
function! Colortune_extend(val,fill,len)

    let len=a:len-len(a:val)
    let val=a:val

    while len>0
	let len-=1
	let val.=a:fill
    endwhile
    return val

endfunction
" }}}

" Colortune_togglemode()
" {{{
" Cycles through available modes
"
function! Colortune_togglemode()

    let max=len(s:modes)-1
    let s:current_mode+=1
    if s:current_mode > max
	let s:current_mode=0
    endif
    call Colortune_fill()
endfunction
" }}}

" Colortune_getregexpattern()
" {{{
" Helper function that builds our regex-pattern for setting and clearing
" the highlight values, based on the current buffer-line
"
function! Colortune_getregexpattern()

    let str='^\(.\{-}\)\s.*'

    for column in s:modes[s:current_mode]
	let str.='.*\[\(.*\)\]'
    endfor

    return str

endfunction
" }}}

" Colortune_getregexstring()
" {{{
" Helper function that builds our substitution string for setting
" the highlight values, based on the current buffer-line
"
function! Colortune_getregexstring()
    let str=''
    let idx=2
    for column in s:modes[s:current_mode]
	let str.=' '.column.'=\'.idx
	let idx+=1
    endfor
    return str
endfunction
" }}}

" Colortune_clearbyline()
" {{{
" Clear highlight group in current line
" Set highlight group values by extracting them from the current line
"
function! Colortune_clearbyline()
    let myline=Colortune_getline()
    exec substitute(myline,Colortune_getregexpattern(),'hi clear \1','')
    call Colortune_fill()
endfunction
" }}}

" Colortune_setbyline()
" {{{
" Set highlight group values by extracting them from the current line
"
function! Colortune_setbyline()
    let myline=Colortune_getline()
    exec substitute(myline,Colortune_getregexpattern(),'hi \1 '.Colortune_getregexstring(),'')
    call Colortune_fill()
endfunction
" }}}

" Colortune_fill()
" {{{
" Create or Redraw the ColorTune Window/Buffer
"
function! Colortune_fill()

    " open a new split or switch to it (if already open)
    let potbufnr=bufwinnr(s:colortune_buf_name)
    if potbufnr==#-1
        execute 'vsplit '.s:colortune_buf_name
    else
	execute potbufnr.'wincmd w'
    endif

    " set some options
    setlocal filetype=colortune
    setlocal buftype=nofile
    setlocal nowrap

    " set mappings
    nnoremap <buffer> <cr> :call Colortune_setbyline()<cr>
    inoremap <buffer> <cr> <esc>:call Colortune_setbyline()<cr>

    nnoremap <buffer> X :call Colortune_clearbyline()<cr>
    nnoremap <buffer> M :call Colortune_togglemode()<cr>
    nnoremap <buffer> C :call Colortune_dumpconfig()<cr>

    nnoremap <buffer> Ar :call Colortune_manipulate_rgb(16,0,0)<cr>
    nnoremap <buffer> Sr :call Colortune_manipulate_rgb(-16,0,0)<cr>
    nnoremap <buffer> Ag :call Colortune_manipulate_rgb(0,16,0)<cr>
    nnoremap <buffer> Sg :call Colortune_manipulate_rgb(0,-16,0)<cr>
    nnoremap <buffer> Ab :call Colortune_manipulate_rgb(0,0,16)<cr>
    nnoremap <buffer> Sb :call Colortune_manipulate_rgb(0,0,-16)<cr>

    nnoremap <buffer> ar :call Colortune_manipulate_rgb(1,0,0)<cr>
    nnoremap <buffer> sr :call Colortune_manipulate_rgb(-1,0,0)<cr>
    nnoremap <buffer> ag :call Colortune_manipulate_rgb(0,1,0)<cr>
    nnoremap <buffer> sg :call Colortune_manipulate_rgb(0,-1,0)<cr>
    nnoremap <buffer> ab :call Colortune_manipulate_rgb(0,0,1)<cr>
    nnoremap <buffer> sb :call Colortune_manipulate_rgb(0,0,-1)<cr>
    

    nnoremap <buffer> VG :call Colortune_showguirgb()<cr>
    nnoremap <buffer> VC :call Colortune_showctermrgb()<cr>


    " set syntax highlighting for our group keywords
    for mycolor in s:colortable
	execute 'syn keyword '.mycolor.' '.mycolor
    endfor

    " remember current position and clean the buffer
    let top_line=line('w0')
    let cur_line=line('.')
    let save_cursor = getpos(".")
    normal! ggdG

    " display header with some infos
    call append(line('$')-1,'------------------------------------------')
    call append(line('$')-1,'Colortable Mode: ['.s:modes_labels[s:current_mode].']')
    call append(line('$')-1,'------------------------------------------')
    call append(line('$')-1,'ENTER  - Set from current line -----------')
    call append(line('$')-1,'X      - Unset current line --------------')
    call append(line('$')-1,'M      - Toggle Mode ---------------------')
    call append(line('$')-1,'C      - Dump Config to new buffer -------')
    call append(line('$')-1,'VC     - View palette  (cterm) -----------')
    call append(line('$')-1,'VG     - View palette  (gui) -------------')
    call append(line('$')-1,'a[rgb] - increase [rgb]] by 1 ------------')
    call append(line('$')-1,'A[rgb] - increase [rgb]] by 16 -----------')
    call append(line('$')-1,'s[rgb] - decrease [rgb]] by 1 ------------')
    call append(line('$')-1,'S[rgb] - decrease [rgb]] by 16 -----------')
    call append(line('$')-1,'------------------------------------------')
    call append(line('$')-1,'-')

    " find out the maximal length for each column
    let max_len=[Colortune_getmax('GROUP_NAME')]
    for column in s:modes[s:current_mode]
	let max_len+=[Colortune_getmax(column)]
    endfor
	
    " display column names
    let labels=Colortune_extend('GROUP_NAME'.' ',' ',5+max_len[0])
    let idx=1
    for column in s:modes[s:current_mode]
	let labels.='  '.Colortune_extend(column,' ',max_len[idx]).'  '
	let idx+=1
    endfor
    call append(line('$')-1,labels)
    call append(line('$')-1,'------------------------------------------')

    " paint the color table
    for group in s:colortable

	let colorline=Colortune_extend(group.' ','-',5+max_len[0])
	
	let idx=1
	for column in s:modes[s:current_mode]
	    let value=Colortune_getattr(group,column)
	    let colorline.=' ['.Colortune_extend(value,' ',max_len[idx]).'] '
	    let idx+=1
	endfor

	call append(line('$')-1,colorline)

    endfor

    " delete last (empty) line and restore initial position.
    normal ddgg
    exec 'normal G'.top_line.'G'.cur_line.'G'
    call setpos('.', save_cursor)

endfunction
" }}}

" Colortune_dumpconfig()
" {{{
"
function! Colortune_dumpconfig()

    new
    set ft=vim
    set nowrap

    " add header stuff for our colortheme
    call append(line('$')-1,[
	\   'set background=dark',
	\   'hi clear',
	\   'if exists("syntax_on")', 'syntax reset', 'endif',
	\   'let g:colors_name = "mycolorscheme"'])


    for group in s:colortable
	let config=''

	for column in s:modes[s:current_mode]
	    let value=Colortune_getattr(group,column)
	    let config.=' '.column.'='.value
	endfor
	call append(line('$')-1,'hi '.group.config)
    endfor

    normal gg

endfunction
" }}}

" Colortune_manipulate_rgb()
" {{{
" TODO: restore register!
" TODO: check overflow/underflow
" TODO: check format
"
function! Colortune_manipulate_rgb(r,g,b)

    normal T["adt]
    let val=getreg('a')
    let r='0x'.substitute(val,'^#\(\x\x\)\x\x\x\x','\1','')
    let g='0x'.substitute(val,'^#\x\x\(\x\x\)\x\x','\1','')
    let b='0x'.substitute(val,'^#\x\x\x\x\(\x\x\)','\1','')
    call setreg('a',printf("#%02x%02x%02x",r+a:r,g+a:g,b+a:b))
    normal "aP
    call Colortune_setbyline()

endfunction


" }}}

" Colortune_showguirgb()
"{{{
function! Colortune_showguirgb()

    vnew
    set nowrap
    nnoremap <buffer> <cr> :call Colortune_fillincolor()<cr>

    r $VIMRUNTIME/rgb.txt
    let linenr=1
    let result=[]
    while linenr<line('$')

	let myline=getline(linenr)
	if myline =~# '^\s*\d\+\s\+\d\+\s\+\d\+\s\+\w\+\s*$'
	    exec substitute(myline,'^\s*\(\d\+\)\s\+\(\d\+\)\s\+\(\d\+\)\s\+\(\w\+\)\s*$','let extr=["\4",\1,\2,\3]','')
	    exec 'hi color_'.extr[0].' guibg='.printf("#%02x%02x%02x",extr[1],extr[2],extr[3])
	    exec 'syntax match color_'.extr[0].' "color_'.extr[0].'"'
	    let result+=[extr[0].' ~ color_'.extr[0]]
	else
	endif

	let linenr+=1

    endwhile

    normal ggdG
    call append(0,result)


endfunction
"}}}

" Colortune_showctermrgb()
"{{{
function! Colortune_showctermrgb()

    vnew
    set nowrap
    nnoremap <buffer> <cr> :call Colortune_fillincolor()<cr>

    let colornr=0
    let result=[]
    while colornr<256

	exec 'hi color_'.colornr.' ctermbg='.colornr
	exec 'syntax match color_'.colornr.' "color_'.colornr.'"'
	let result+=[colornr.' ~ color_'.colornr]
	let colornr+=1

    endwhile

    call append(0,result)


endfunction
"}}}

" Colortune_fillincolor()
" {{{
function! Colortune_fillincolor()
    normal 0"ayaw
    let potbufnr=bufwinnr(s:colortune_buf_name)
    execute potbufnr.'wincmd w'
    normal di]"aP
    call Colortune_setbyline()
endfunction
" }}}

" }}}