Add operators for making new notes from selections.
This commit is contained in:
parent
66168bf5ce
commit
d742470375
2 changed files with 82 additions and 0 deletions
|
@ -114,3 +114,75 @@ export def NewNote()
|
||||||
var file = date .. "_new-note.qmd"
|
var file = date .. "_new-note.qmd"
|
||||||
execute "edit " .. g:mbnotes_dir .. "/" .. file
|
execute "edit " .. g:mbnotes_dir .. "/" .. file
|
||||||
enddef
|
enddef
|
||||||
|
|
||||||
|
export def Operator(context = {}, type: string = ''): string
|
||||||
|
if type == ''
|
||||||
|
var _context = {
|
||||||
|
"dot_command": false,
|
||||||
|
"extend_block": '',
|
||||||
|
"virtualedit": [&l:virtualedit, &g:virtualedit]
|
||||||
|
}
|
||||||
|
&operatorfunc = function(Operator, [_context])
|
||||||
|
set virtualedit=block
|
||||||
|
return 'g@'
|
||||||
|
endif
|
||||||
|
|
||||||
|
var save = {
|
||||||
|
"clipboard": &clipboard,
|
||||||
|
"selection": &selection,
|
||||||
|
"virtualedit": [&l:virtualedit, &g:virtualedit],
|
||||||
|
"register": getreginfo('m'),
|
||||||
|
"visual_marks": [getpos("'<"), getpos("'>")]
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
set clipboard= selection=inclusive virtualedit=
|
||||||
|
var commands = {
|
||||||
|
"line": "'[V']",
|
||||||
|
"char": "`[v`]",
|
||||||
|
"block": "`[\<c-V>`]",
|
||||||
|
}[type]
|
||||||
|
|
||||||
|
var [_, _, col, off] = getpos("']")
|
||||||
|
if off != 0
|
||||||
|
var vcol = getline("'[")->strpart(0, col + off)->strdisplaywidth()
|
||||||
|
|
||||||
|
if vcol >= [line("'["), '$']->virtcol() - 1
|
||||||
|
context['extend_block'] = '$'
|
||||||
|
else
|
||||||
|
context['extend_block'] = vcol .. '|'
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
|
if context['extend_block'] != ''
|
||||||
|
commands ..= 'oO' .. context['extend_block']
|
||||||
|
endif
|
||||||
|
|
||||||
|
commands ..= '"my'
|
||||||
|
|
||||||
|
execute 'silent noautocmd keepjumps normal! ' .. commands
|
||||||
|
|
||||||
|
NewNote()
|
||||||
|
|
||||||
|
# normal! "mPG"_dd
|
||||||
|
normal! "mPG
|
||||||
|
finally
|
||||||
|
setreg('m', save['register'])
|
||||||
|
setpos("'<", save['visual_marks'][0])
|
||||||
|
setpos("'>", save['visual_marks'][1])
|
||||||
|
&clipboard = save['clipboard']
|
||||||
|
&selection = save['selection']
|
||||||
|
|
||||||
|
if context['dot_command']
|
||||||
|
&l:virtualedit = save['virtualedit'][0]
|
||||||
|
&g:virtualedit = save['virtualedit'][1]
|
||||||
|
else
|
||||||
|
&l:virtualedit = context['virtualedit'][0]
|
||||||
|
&g:virtualedit = context['virtualedit'][1]
|
||||||
|
endif
|
||||||
|
|
||||||
|
context['dot_command'] = true
|
||||||
|
endtry
|
||||||
|
|
||||||
|
return ""
|
||||||
|
enddef
|
||||||
|
|
|
@ -100,4 +100,14 @@ command -nargs=0 MBNotesNewSplit {
|
||||||
mbnotes.NewNote()
|
mbnotes.NewNote()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
nnoremap <expr> <Plug>MBNotesNew <SID>mbnotes.Operator()
|
||||||
|
xnoremap <expr> <Plug>MBNotesNew <SID>mbnotes.Operator()
|
||||||
|
nnoremap <expr> <Plug>MBNotesNewLine <SID>mbnotes.Operator() .. '_'
|
||||||
|
|
||||||
|
if !hasmapto('<Plug>MBNotesNew') || maparg('gb', 'n') ==# ''
|
||||||
|
nmap gb <Plug>MBNotesNew
|
||||||
|
xmap gb <Plug>MBNotesNew
|
||||||
|
nmap gbb <Plug>MBNotesNewLine
|
||||||
|
endif
|
||||||
|
|
||||||
g:mbnotes_loaded = 1
|
g:mbnotes_loaded = 1
|
||||||
|
|
Loading…
Add table
Reference in a new issue