Refactor: normalize_link_v, creating u#get_selection (Fix #382)
* Less hacky selection handling: nomal command hack -> vimscript code * Some prettifying, antipattern removal
This commit is contained in:
parent
edf562fab0
commit
ac38bb2f09
@ -2380,50 +2380,55 @@ endfunction
|
|||||||
" TODO mutualize most code with syntax_n
|
" TODO mutualize most code with syntax_n
|
||||||
" Normalize link in visual mode Enter keypress
|
" Normalize link in visual mode Enter keypress
|
||||||
function! s:normalize_link_syntax_v() abort
|
function! s:normalize_link_syntax_v() abort
|
||||||
let sel_save = &selection
|
" Get selection content
|
||||||
let &selection = 'old'
|
let visual_selection = vimwiki#u#get_selection()
|
||||||
let default_register_save = @"
|
|
||||||
let registertype_save = getregtype('"')
|
|
||||||
|
|
||||||
try
|
" Embed link in template
|
||||||
" Save selected text to register "
|
" In case of a diary link, wiki or markdown link
|
||||||
normal! gv""y
|
if vimwiki#base#is_diary_file(expand('%:p'))
|
||||||
|
let link = vimwiki#base#normalize_link_in_diary(visual_selection)
|
||||||
" Set substitution
|
else
|
||||||
" Replace Url
|
" Warning nested syntax discrimination
|
||||||
if vimwiki#base#is_diary_file(expand('%:p'))
|
if vimwiki#vars#get_wikilocal('syntax') ==# 'markdown'
|
||||||
let sub = vimwiki#base#normalize_link_in_diary(@")
|
let template = vimwiki#vars#get_syntaxlocal('Weblink1Template')
|
||||||
else
|
else
|
||||||
let sub = s:safesubstitute(vimwiki#vars#get_global('WikiLinkTemplate1'),
|
let template = vimwiki#vars#get_global('WikiLinkTemplate1')
|
||||||
\ '__LinkUrl__', @", '')
|
|
||||||
endif
|
endif
|
||||||
" Replace file extension
|
let link = s:safesubstitute(template, '__LinkUrl__', visual_selection, '')
|
||||||
let file_extension = vimwiki#vars#get_wikilocal('ext', vimwiki#vars#get_bufferlocal('wiki_nr'))
|
endif
|
||||||
let sub = s:safesubstitute(sub, '__FileExtension__', file_extension , '')
|
|
||||||
|
|
||||||
" Put substitution in register " and change text
|
" Transform link:
|
||||||
let sc = vimwiki#vars#get_wikilocal('links_space_char')
|
" Replace description (used for markdown)
|
||||||
call setreg('"', substitute(substitute(sub, '\n', '', ''), '\s', sc, 'g'), visualmode())
|
let link = s:safesubstitute(link, '__LinkDescription__', visual_selection, '')
|
||||||
normal! `>""pgvd
|
" Replace file extension
|
||||||
finally
|
let file_extension = vimwiki#vars#get_wikilocal('ext', vimwiki#vars#get_bufferlocal('wiki_nr'))
|
||||||
call setreg('"', default_register_save, registertype_save)
|
let link = s:safesubstitute(link, '__FileExtension__', file_extension , '')
|
||||||
let &selection = sel_save
|
" Replace space characters
|
||||||
endtry
|
let sc = vimwiki#vars#get_wikilocal('links_space_char')
|
||||||
|
let link = substitute(link, '\s', sc, 'g')
|
||||||
|
" Remove newlines
|
||||||
|
let link = substitute(link, '\n', '', '')
|
||||||
|
|
||||||
|
" Paste result
|
||||||
|
call vimwiki#u#get_selection(link)
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
" Normalize link
|
" Normalize link
|
||||||
function! vimwiki#base#normalize_link(is_visual_mode) abort
|
function! vimwiki#base#normalize_link(is_visual_mode) abort
|
||||||
if exists('*vimwiki#'.vimwiki#vars#get_wikilocal('syntax').'_base#normalize_link')
|
" Switch implementation
|
||||||
" Syntax-specific links
|
" If visual mode
|
||||||
call vimwiki#{vimwiki#vars#get_wikilocal('syntax')}_base#normalize_link(a:is_visual_mode)
|
" TODO elseif line("'<") == line("'>")
|
||||||
|
if a:is_visual_mode
|
||||||
|
return s:normalize_link_syntax_v()
|
||||||
|
|
||||||
|
" If Syntax-specific normalizer exists: call it
|
||||||
|
elseif exists('*vimwiki#'.vimwiki#vars#get_wikilocal('syntax').'_base#normalize_link')
|
||||||
|
return vimwiki#{vimwiki#vars#get_wikilocal('syntax')}_base#normalize_link()
|
||||||
|
|
||||||
|
" Normal mode default
|
||||||
else
|
else
|
||||||
if !a:is_visual_mode
|
return s:normalize_link_syntax_n()
|
||||||
call s:normalize_link_syntax_n()
|
|
||||||
elseif line("'<") == line("'>")
|
|
||||||
" action undefined for multi-line visual mode selections
|
|
||||||
call s:normalize_link_syntax_v()
|
|
||||||
endif
|
|
||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
@ -111,55 +111,7 @@ function! s:normalize_link_syntax_n() abort
|
|||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
function! s:normalize_link_syntax_v() abort
|
function! vimwiki#markdown_base#normalize_link() abort
|
||||||
let lnum = line('.')
|
" TODO mutualize with base
|
||||||
let sel_save = &selection
|
call s:normalize_link_syntax_n()
|
||||||
let &selection = 'old'
|
|
||||||
let rv = @"
|
|
||||||
let rt = getregtype('"')
|
|
||||||
let done = 0
|
|
||||||
|
|
||||||
try
|
|
||||||
norm! gvy
|
|
||||||
let visual_selection = @"
|
|
||||||
|
|
||||||
if vimwiki#base#is_diary_file(expand('%:p'))
|
|
||||||
let link = vimwiki#base#normalize_link_in_diary(visual_selection)
|
|
||||||
else
|
|
||||||
let link = s:safesubstitute(vimwiki#vars#get_syntaxlocal('Weblink1Template'),
|
|
||||||
\ '__LinkUrl__', visual_selection, '')
|
|
||||||
endif
|
|
||||||
|
|
||||||
" Replace spaces with new character if option is set
|
|
||||||
let link = substitute(link, '\s', vimwiki#vars#get_wikilocal('links_space_char'), 'g')
|
|
||||||
|
|
||||||
" Replace description
|
|
||||||
let link = s:safesubstitute(link, '__LinkDescription__', visual_selection, '')
|
|
||||||
|
|
||||||
" Replace file extension
|
|
||||||
let file_extension = vimwiki#vars#get_wikilocal('ext', vimwiki#vars#get_bufferlocal('wiki_nr'))
|
|
||||||
let link = s:safesubstitute(link, '__FileExtension__', file_extension , '')
|
|
||||||
|
|
||||||
call setreg('"', substitute(link, '\n', '', ''), visualmode())
|
|
||||||
|
|
||||||
" paste result
|
|
||||||
norm! `>""pgvd
|
|
||||||
|
|
||||||
finally
|
|
||||||
call setreg('"', rv, rt)
|
|
||||||
let &selection = sel_save
|
|
||||||
endtry
|
|
||||||
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
function! vimwiki#markdown_base#normalize_link(is_visual_mode) abort
|
|
||||||
|
|
||||||
if !a:is_visual_mode
|
|
||||||
call s:normalize_link_syntax_n()
|
|
||||||
elseif line("'<") == line("'>")
|
|
||||||
" action undefined for multi-line visual mode selections
|
|
||||||
call s:normalize_link_syntax_v()
|
|
||||||
endif
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
|
@ -4,6 +4,55 @@
|
|||||||
" Home: https://github.com/vimwiki/vimwiki/
|
" Home: https://github.com/vimwiki/vimwiki/
|
||||||
|
|
||||||
|
|
||||||
|
" Get visual selection text content, optionaly replace its content
|
||||||
|
" :param: Text to replace selection
|
||||||
|
function! vimwiki#u#get_selection(...) abort
|
||||||
|
" Copyed from DarkWiiPlayer at stackoverflow
|
||||||
|
" https://stackoverflow.com/a/47051271/2544873
|
||||||
|
" Get selection extremity position,
|
||||||
|
" Discriminate selection mode
|
||||||
|
if mode() ==? 'v'
|
||||||
|
let [line_start, column_start] = getpos('v')[1:2]
|
||||||
|
let [line_end, column_end] = getpos('.')[1:2]
|
||||||
|
else
|
||||||
|
let [line_start, column_start] = getpos("'<")[1:2]
|
||||||
|
let [line_end, column_end] = getpos("'>")[1:2]
|
||||||
|
end
|
||||||
|
|
||||||
|
" Guard
|
||||||
|
if (line2byte(line_start)+column_start) > (line2byte(line_end)+column_end)
|
||||||
|
let [line_start, column_start, line_end, column_end] =
|
||||||
|
\ [line_end, column_end, line_start, column_start]
|
||||||
|
end
|
||||||
|
let lines = getline(line_start, line_end)
|
||||||
|
if len(lines) == 0
|
||||||
|
return ''
|
||||||
|
endif
|
||||||
|
|
||||||
|
" If want to modify selection
|
||||||
|
if a:0 > 0
|
||||||
|
" Grab new content
|
||||||
|
let line_link = a:1
|
||||||
|
|
||||||
|
" Grab the content of line around the link: pre and post
|
||||||
|
let start_link = max([column_start - 2, 0])
|
||||||
|
let line_pre = ''
|
||||||
|
if start_link > 0
|
||||||
|
let line_pre .= lines[0][ : start_link]
|
||||||
|
endif
|
||||||
|
let line_post = lines[0][column_end - (&selection ==# 'inclusive' ? 0 : 1) : ]
|
||||||
|
|
||||||
|
" Set the only single selected line
|
||||||
|
call setline(line_start, line_pre . line_link . line_post)
|
||||||
|
endif
|
||||||
|
|
||||||
|
" Get selection extremity position, take into account selection option
|
||||||
|
let lines[-1] = lines[-1][: column_end - (&selection ==# 'inclusive' ? 1 : 2)]
|
||||||
|
let lines[0] = lines[0][column_start - 1:]
|
||||||
|
return join(lines, "\n")
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
" Execute: string v:count times
|
" Execute: string v:count times
|
||||||
function! vimwiki#u#count_exe(cmd) abort
|
function! vimwiki#u#count_exe(cmd) abort
|
||||||
for i in range( max([1, v:count]) )
|
for i in range( max([1, v:count]) )
|
||||||
@ -12,6 +61,7 @@ function! vimwiki#u#count_exe(cmd) abort
|
|||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
" Trim spaces: leading and trailing
|
||||||
function! vimwiki#u#trim(string, ...) abort
|
function! vimwiki#u#trim(string, ...) abort
|
||||||
let chars = ''
|
let chars = ''
|
||||||
if a:0 > 0
|
if a:0 > 0
|
||||||
@ -42,11 +92,13 @@ function! vimwiki#u#os_name() abort
|
|||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
" Check if OS is windows
|
||||||
function! vimwiki#u#is_windows() abort
|
function! vimwiki#u#is_windows() abort
|
||||||
return has('win32') || has('win64') || has('win95') || has('win16')
|
return has('win32') || has('win64') || has('win95') || has('win16')
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
" Check if OS is mac
|
||||||
function! vimwiki#u#is_macos() abort
|
function! vimwiki#u#is_macos() abort
|
||||||
if has('mac') || has('macunix') || has('gui_mac')
|
if has('mac') || has('macunix') || has('gui_mac')
|
||||||
return 1
|
return 1
|
||||||
@ -81,15 +133,13 @@ endfunction
|
|||||||
|
|
||||||
|
|
||||||
" Backward compatible version of the built-in function shiftwidth()
|
" Backward compatible version of the built-in function shiftwidth()
|
||||||
if exists('*shiftwidth')
|
function! vimwiki#u#sw() abort
|
||||||
function! vimwiki#u#sw() abort
|
if exists('*shiftwidth')
|
||||||
return shiftwidth()
|
return shiftwidth()
|
||||||
endfunc
|
else
|
||||||
else
|
|
||||||
function! vimwiki#u#sw() abort
|
|
||||||
return &shiftwidth
|
return &shiftwidth
|
||||||
endfunc
|
endif
|
||||||
endif
|
endfunc
|
||||||
|
|
||||||
" a:mode single character indicating the mode as defined by :h maparg
|
" a:mode single character indicating the mode as defined by :h maparg
|
||||||
" a:key the key sequence to map
|
" a:key the key sequence to map
|
||||||
@ -116,7 +166,7 @@ function! vimwiki#u#map_key(mode, key, plug, ...) abort
|
|||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
" returns 1 if line is a code block or math block
|
" Returns: 1 if line is a code block or math block
|
||||||
"
|
"
|
||||||
" The last two conditions are needed for this to correctly
|
" The last two conditions are needed for this to correctly
|
||||||
" detect nested syntaxes within code blocks
|
" detect nested syntaxes within code blocks
|
||||||
|
@ -1,5 +1,89 @@
|
|||||||
Include: vader_includes/vader_setup.vader
|
Include: vader_includes/vader_setup.vader
|
||||||
|
|
||||||
|
# Visual Creatin {{{1
|
||||||
|
# Issues: #382
|
||||||
|
####################
|
||||||
|
|
||||||
|
Execute (Log):
|
||||||
|
Log 'Visual Creation'
|
||||||
|
|
||||||
|
# For markdown {{{2
|
||||||
|
# ------------------
|
||||||
|
|
||||||
|
Given vimwiki (abc def ghi jkl):
|
||||||
|
abc def ghi jkl
|
||||||
|
|
||||||
|
Execute (Set filename wiki_test.md):
|
||||||
|
Log '>> Visual creation, markdown syntax'
|
||||||
|
file wiki_test.md
|
||||||
|
call SetSyntax('markdown')
|
||||||
|
AssertEqual vimwiki#vars#get_wikilocal('syntax'), 'markdown'
|
||||||
|
|
||||||
|
Do (v3e):
|
||||||
|
v3e\<Cr>
|
||||||
|
|
||||||
|
Expect (3 Words []()):
|
||||||
|
[abc def ghi](abc def ghi) jkl
|
||||||
|
|
||||||
|
Do (v3e):
|
||||||
|
wv2e\<Cr>
|
||||||
|
|
||||||
|
Expect (2 Words []()):
|
||||||
|
abc [def ghi](def ghi) jkl
|
||||||
|
|
||||||
|
Do (selection=exclusive v3e):
|
||||||
|
:set selection=exclusive\<Cr>
|
||||||
|
wv2e\<Cr>
|
||||||
|
|
||||||
|
Expect (2 Words []()):
|
||||||
|
abc [def ghi](def ghi) jkl
|
||||||
|
|
||||||
|
Do (selection=exclusive wv$):
|
||||||
|
:set selection=exclusive\<Cr>
|
||||||
|
wv$\<Cr>
|
||||||
|
|
||||||
|
Expect (3 Words []()):
|
||||||
|
abc [def ghi jkl](def ghi jkl)
|
||||||
|
|
||||||
|
|
||||||
|
# For Wiki {{{2
|
||||||
|
# ------------------
|
||||||
|
|
||||||
|
Given vimwiki (abc def ghi jkl):
|
||||||
|
abc def ghi jkl
|
||||||
|
|
||||||
|
Execute (Set filename wiki_test.md):
|
||||||
|
Log '>> Visual creation, wiki syntax'
|
||||||
|
file wiki_test.wiki
|
||||||
|
call SetSyntax('default')
|
||||||
|
|
||||||
|
Do (v3e):
|
||||||
|
v3e\<Cr>
|
||||||
|
|
||||||
|
Expect (3 Words []()):
|
||||||
|
[[abc def ghi]] jkl
|
||||||
|
|
||||||
|
Do (v3e):
|
||||||
|
wv2e\<Cr>
|
||||||
|
|
||||||
|
Expect (2 Words []()):
|
||||||
|
abc [[def ghi]] jkl
|
||||||
|
|
||||||
|
Do (selection=exclusive v3e):
|
||||||
|
:set selection=exclusive\<Cr>
|
||||||
|
wv2e\<Cr>
|
||||||
|
|
||||||
|
Expect (2 Words []()):
|
||||||
|
abc [[def ghi]] jkl
|
||||||
|
|
||||||
|
Do (selection=exclusive wv$):
|
||||||
|
:set selection=exclusive\<Cr>
|
||||||
|
wv$\<Cr>
|
||||||
|
|
||||||
|
Expect (3 Words []()):
|
||||||
|
abc [[def ghi jkl]]
|
||||||
|
|
||||||
|
|
||||||
# Absolute links {{{1
|
# Absolute links {{{1
|
||||||
####################
|
####################
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user