commit 0a9488f3f1ec2a559283f02148b76e1c69ecad4c
parent e91711eb79f1fd2b19c8bec80cb6ce73895bd414
Author: Tinmarino <tinmarino@gmail.com>
Date: Sun, 10 Jan 2021 16:17:15 -0300
Feature VimwikiColorize maps in visual and normal mode #990
Colorize working for visual selection
Add map (,wc), test and doc
Diffstat:
5 files changed, 174 insertions(+), 20 deletions(-)
diff --git a/autoload/vimwiki/base.vim b/autoload/vimwiki/base.vim
@@ -2918,23 +2918,59 @@ function! vimwiki#base#complete_colorize(ArgLead, CmdLine, CursorPos) abort
return join(colorlist, "\n")
endfunction
-function! vimwiki#base#colorize(...) abort
+function! vimwiki#base#get_user_color(...) abort
+ " Returns a color key <- user input, '' if fails
+ let res = ''
+ let display_list = []
+ let color_dic = vimwiki#vars#get_wikilocal('color_dic')
+ let key_list = sort(keys(color_dic))
+ let i = 1
+ for key in key_list
+ call add(display_list, string(i) . '. ' . key)
+ let i += 1
+ endfor
+ call insert(display_list, 'Select color:')
+ " Ask user, fails if 0
+ let i_selected = inputlist(display_list)
+ if i_selected != 0
+ let res = key_list[i_selected - 1]
+ endif
+ return res
+endfunction
+
+function! vimwiki#base#colorize(...) range abort
" TODO Must be coherent with color_tag_template
+ " Arg1: Key, list them with VimwikiColorize completion
+ " Arg2: visualmode()
" -- Just removeing spaces, \/ -> /, replacing COLORFG will do it
let key = a:0 ? a:1 : 'default'
+ let mode = a:0 > 1 ? a:2 : ''
let color_dic = vimwiki#vars#get_wikilocal('color_dic')
+ " Guard: if key = '', silently leave (user left inputlist)
+ if key ==# ''
+ return
+ endif
+
" Guard: color key nust exist
if !has_key(color_dic, key)
call vimwiki#u#error('color_dic variable has no key ''' . key . '''')
return
endif
- " Get content
- let content = getline('.')
- " TODO save position for visual selection: see u#get_selection
+ " Get content if called with a map with range
+ if mode !=# ''
+ " Visual mode
+ let firstline = getpos("'<")[1]
+ let lastline = getpos("'>")[1]
+ else
+ " Range command
+ let firstline = a:firstline
+ let lastline = a:lastline
+ endif
+ let lines = getline(firstline, lastline)
- " Surround
+ " Prepare
" -- pre
let [fg, bg] = color_dic[key]
let pre = '<span style="'
@@ -2947,11 +2983,24 @@ function! vimwiki#base#colorize(...) abort
let pre .= '">'
" -- post
let post = '</span>'
- " -- concat
- let content = pre . content . post
+
+ " Concat
+ if mode !=# ''
+ " Visual mode (vim indexing ...)
+ let pos = getpos("'>")[2] - 1
+ let lines[len(lines)-1] = strpart(lines[len(lines)-1], 0, pos+1) . post . strpart(lines[len(lines)-1], pos+1)
+ let pos = getpos("'<")[2]
+ let lines[0] = strpart(lines[0],0, pos-1) . pre . strpart(lines[0], pos-1)
+ else
+ " Normal or Command
+ let lines[len(lines)-1] = lines[len(lines)-1] . post
+ let lines[0] = pre . lines[0]
+ endif
" Set buffer content
- call setline('.', content)
+ for line in range(firstline, lastline)
+ call setline(line, lines[line - firstline])
+ endfor
endfunction
" -------------------------------------------------------------------------
diff --git a/doc/vimwiki.txt b/doc/vimwiki.txt
@@ -381,6 +381,11 @@ MAP MODE
Maps to |:VimwikiPrevLink|.
Remap command: `<Plug>VimwikiPrevLink`
+ *vimwiki_<Leader>wc*
+<Leader>wc n v Colorize line or selection if from visual mode
+ Maps to |:VimwikiColorize| after asking user for color
+ Remap command: `<Plug>VimwikiColorize`
+
*vimwiki_<Leader>wn*
<Leader>wn n Goto or create new wiki page.
Maps to |:VimwikiGoto|.
@@ -965,13 +970,9 @@ Vimwiki file.
sure the tags have been built (see |vimwiki-build-tags|).
*:VimwikiColorize* red
- Colorize current line with the color given in argument. The possible
+ Colorize |range| of lines with the color given in argument. The possible
colors are configured by |vimwiki-option-color_dic| and the format of the
surrounding color tags by |vimwiki-option-color_tag_template|
- TODO
- Currently only the current line is colorized, a support for the selection
- will come. Maybe some mapping and color completion in popup (See
- changelog
==============================================================================
@@ -2867,7 +2868,7 @@ Default: 0
Dictionary containing the possible html colors for |:VimwikiColorize| the
keys are the color names used as argument, the values are a list [foreground,
-backgroud] of the color. For example:
+background] of the color. For example:
{'red': ['#cc241d', ''], 'bred': ['', '#cc241d']}
@@ -3902,6 +3903,7 @@ http://code.google.com/p/vimwiki/issues/list. They may be accessible from
https://github.com/vimwiki-backup/vimwiki/issues.
New:~
+ * Feature: VimwikiColorize maps in visual and normal mode `<leader>wc`
* Feature: PR #686: add a flag lists_return to disable mappings
to <CR> and <S-CR> in insert mode
* Feature: enable re-mapping insert mode table mappings
diff --git a/ftplugin/vimwiki.vim b/ftplugin/vimwiki.vim
@@ -370,9 +370,8 @@ command! -buffer -nargs=* -complete=custom,vimwiki#tags#complete_tags
command! -buffer VimwikiPasteUrl call vimwiki#html#PasteUrl(expand('%:p'))
command! -buffer VimwikiCatUrl call vimwiki#html#CatUrl(expand('%:p'))
-
-command! -buffer -nargs=* -complete=custom,vimwiki#base#complete_colorize
- \ VimwikiColorize call vimwiki#base#colorize(<f-args>)
+command! -buffer -nargs=* -range -complete=custom,vimwiki#base#complete_colorize
+ \ VimwikiColorize <line1>,<line2>call vimwiki#base#colorize(<f-args>)
" ------------------------------------------------
" Keybindings
@@ -430,6 +429,10 @@ nnoremap <silent><script><buffer> <Plug>VimwikiDiaryNextDay
\ :VimwikiDiaryNextDay<CR>
nnoremap <silent><script><buffer> <Plug>VimwikiDiaryPrevDay
\ :VimwikiDiaryPrevDay<CR>
+noremap <script><buffer> <Plug>VimwikiColorizeNormal
+ \ :call vimwiki#base#colorize(vimwiki#base#get_user_color(), '')<CR>
+vnoremap <script><buffer> <Plug>VimwikiColorize
+ \ :call vimwiki#base#colorize(vimwiki#base#get_user_color(), visualmode())<CR>
" Declare Map: default links key mappings
if str2nr(vimwiki#vars#get_global('key_mappings').links)
@@ -447,6 +450,8 @@ if str2nr(vimwiki#vars#get_global('key_mappings').links)
call vimwiki#u#map_key('n', vimwiki#vars#get_global('map_prefix').'n', '<Plug>VimwikiGoto')
call vimwiki#u#map_key('n', vimwiki#vars#get_global('map_prefix').'d', '<Plug>VimwikiDeleteFile')
call vimwiki#u#map_key('n', vimwiki#vars#get_global('map_prefix').'r', '<Plug>VimwikiRenameFile')
+ call vimwiki#u#map_key('n', vimwiki#vars#get_global('map_prefix').'c', '<Plug>VimwikiColorizeNormal')
+ call vimwiki#u#map_key('v', vimwiki#vars#get_global('map_prefix').'c', '<Plug>VimwikiColorize')
call vimwiki#u#map_key('n', '<C-Down>', '<Plug>VimwikiDiaryNextDay')
call vimwiki#u#map_key('n', '<C-Up>', '<Plug>VimwikiDiaryPrevDay')
endif
diff --git a/test/map.vader b/test/map.vader
@@ -86,9 +86,90 @@ Do (,w,m -> open tomorrow [Assert]):
# 2 Local {{{1
##############
-Execute (===========================================================):
- Log "Checking local map"
-
+#Execute (===========================================================):
+# Log "Checking local map"
+#
+#
+## 2.3 Font color {{{2
+Given (Some paragraphs):
+ Some paragraph with some words 1
+ Some paragraph with some words 2
+ Some paragraph with some words 3
+ Some paragraph with some words 4
+
+Execute(Colorize1: Current line):
+ set ft=vimwiki
+ call SetSyntax('markdown')
+ AssertEqual 3, vimwiki#vars#get_bufferlocal('wiki_nr')
+ AssertEqual 'vimwiki', &ft
+ "AssertEqual ',', mapleader
+ " Returns: Undefeind mapleader
+ VimwikiColorize red
+
+Expect (Some paragraphs):
+ <span style="color:#cc241d;">Some paragraph with some words 1</span>
+ Some paragraph with some words 2
+ Some paragraph with some words 3
+ Some paragraph with some words 4
+
+Execute(Colorize2: 2 lines):
+ 2,3VimwikiColorize red
+
+Expect (Some paragraphs):
+ Some paragraph with some words 1
+ <span style="color:#cc241d;">Some paragraph with some words 2
+ Some paragraph with some words 3</span>
+ Some paragraph with some words 4
+
+Do(,wc):
+ \wc1\<Cr>\<Cr>
+
+Expect (Some paragraphs):
+ <span style="background:#458588;">Some paragraph with some words 1</span>
+ Some paragraph with some words 2
+ Some paragraph with some words 3
+ Some paragraph with some words 4
+
+Do(User leave menu):
+ ,wc\<Esc>
+
+Expect (Some paragraphs, nothing changed):
+ Some paragraph with some words 1
+ Some paragraph with some words 2
+ Some paragraph with some words 3
+ Some paragraph with some words 4
+
+Do(v,wc):
+ jwll
+ v
+ jjllll
+ \wc14\<Cr>
+
+Expect (Some paragraphs):
+ Some paragraph with some words 1
+ Some pa<span style="color:#cc241d;">ragraph with some words 2
+ Some paragraph with some words 3
+ Some paragra</span>ph with some words 4
+
+Do(With emoji):
+ Go
+ 🤥 abcdefghi 🤥 🤥\<Cr>
+ 🤥 abcdefghi 🤥 🤥\<Cr>
+ 🤥 abcdefghi 🤥 🤥\<Esc>
+ /abc\<Cr>
+ ll
+ \<esc>\<C-v>
+ jjllll
+ \wc1\<Cr>
+
+Expect (Some paragraphs):
+ Some paragraph with some words 1
+ Some paragraph with some words 2
+ Some paragraph with some words 3
+ Some paragraph with some words 4
+ 🤥 ab<span style="background:#458588;">cdefghi 🤥 🤥
+ 🤥 abcdefghi 🤥 🤥
+ 🤥 abcdefg</span>hi 🤥 🤥
# 2.1 Heading {{{2
##############
diff --git a/test/vimrc b/test/vimrc
@@ -54,6 +54,23 @@
let g:vimwiki_list = [vimwiki_default, vimwiki_markdown, vimwiki_mediawiki]
let g:vimwiki_list_vimrc = [vimwiki_default, vimwiki_markdown, vimwiki_mediawiki]
+ " Test VimwikiColorize and ,wc
+ let g:vimwiki_color_dic = {
+ \ 'default': ['', '#d79921'],
+ \ 'red': ['#cc241d', ''],
+ \ 'bred': ['', '#cc241d'],
+ \ 'green': ['#98971a', ''],
+ \ 'bgreen': ['', '#98971a'],
+ \ 'yellow': ['#d79921', ''],
+ \ 'byellow': ['', '#d79921'],
+ \ 'blue': ['#458588', ''],
+ \ 'bblue': ['', '#458588'],
+ \ 'purple': ['#b16286', ''],
+ \ 'bpurple': ['', '#b16286'],
+ \ 'orange': ['#d65d0e', ''],
+ \ 'borange': ['', '#d65d0e'],
+ \ 'gray': ['#a89984', ''],
+ \ 'bgray': ['', '#a89984']}
" Set basic settings
" Avoid more prompt