From faf9b29a25da337431e1ddaaf95d06c8783262cf Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Fri, 16 Sep 2022 06:30:48 +0000 Subject: wip --- plugin/insert.vim | 119 ++++++++++++++++++++++++++++++++++++++++++-------- plugin/remappings.vim | 2 + plugin/supert.vim | 108 ++++++++++++++++++++++----------------------- 3 files changed, 157 insertions(+), 72 deletions(-) (limited to 'plugin') diff --git a/plugin/insert.vim b/plugin/insert.vim index dea819a..722c0b5 100644 --- a/plugin/insert.vim +++ b/plugin/insert.vim @@ -57,6 +57,14 @@ if g:field_marshal_insert_include_bindings " noremap Zi (insert-before-motion) noremap Za (append-after-motion) + noremap Zo (open-after-motion) + noremap ZO (OPEN-after-motion) + + noremap Z[ (to-object-start) + noremap Z] (to-object-end) + + " Zgi -- start another insert using the same parameters from the last Z[ia] + noremap Zgi c(insert-about-obj) vnoremap Zi "\'<" . (visualmode() == "V" ? "0" : "") . "i" vnoremap Za "\'>" . (visualmode() == "V" ? "$" : "") . "a" @@ -113,12 +121,62 @@ function! s:insert_comment_obj(g, ...) abort endif endfunction -noremap (insert-before-motion) call save_state('i') - \call start_recording() - \set operatorfunc=recordopg@ -noremap (append-after-motion) call save_state('a') - \call start_recording() - \set operatorfunc=recordopg@ +noremap (insert-before-motion) + \ call prepare_operation('i', "c\(insert-about-obj)") + \call start_recording() + \set operatorfunc=recordopg@ + +noremap (append-after-motion) + \ call prepare_operation('a', "c\(insert-about-obj)") + \call start_recording() + \set operatorfunc=recordopg@ + +noremap (open-after-motion) + \ call prepare_operation('o', "c\(insert-about-obj)") + \call start_recording() + \set operatorfunc=recordopg@ + +noremap (OPEN-after-motion) + \ call prepare_operation('O', "c\(insert-about-obj)") + \call start_recording() + \set operatorfunc=recordopg@ + +nnoremap (to-object-start) + \ call prepare_operation('[', "\(to-object-pos-post)") + \call start_recording() + \set operatorfunc=recordopg@ +nnoremap (to-object-end) + \ call prepare_operation(']', "\(to-object-pos-post)") + \call start_recording() + \set operatorfunc=recordopg@ + +" The Z[ and Z] commands as text motions. These are done in a similar hacky way as other plugin +" mappings in this file +" +" What they do, is: +" +" 1. prepare the operation, and prepare to send \(to-object-pos-post) +" 2. escape operator-pending mode. This mapping is not the actual mapping we want to use for the +" operation, we just use this as a dummy to set up all the variables needed for the actual +" operation. +" 3. start the recording macro. +" 4. enter operating-pending mode again. This will record the macro. Once the user has entered the +" operator, it will then call the \(to-object-pos-post). This is the command +" that we really want to be able repeat with the dot (.) operator. +onoremap (to-object-start) + \ call prepare_operation('[', printf("%s\(to-object-pos-post)", v:operator)) + \ + \call start_recording() + \set operatorfunc=recordopg@ +onoremap (to-object-end) + \ call prepare_operation(']', printf("%s\(to-object-pos-post)", v:operator)) + \ + \call start_recording() + \set operatorfunc=recordopg@ + +onoremap (to-object-pos-post) call to_object_start() +nnoremap (to-object-pos-post) call to_object_start() + onoremap (insert-about-obj) call insert_before_recorded() nnoremap (insert-about-obj) call insert_before_recorded() @@ -140,7 +198,9 @@ function! s:start_recording() exec "normal! q" . s:reg_to_clobber endfunction -function! s:save_state(instype) +let s:operate_keys='' +function! s:prepare_operation(instype, operate_keys) + echom "operate_keys: " . a:operate_keys " Unfortunately macros kinda break this feature. While I think I might be able " to patch together a fix to make them work, I figure that I'll see if there's " any real need to implement it. @@ -151,6 +211,7 @@ function! s:save_state(instype) let s:savereg = [getreg(s:reg_to_clobber, 1, 1), getregtype(s:reg_to_clobber)] let s:savepos = getpos('.') let s:instype = a:instype + let s:operate_keys = a:operate_keys endfunction noremap (ñóþ) @@ -164,13 +225,18 @@ function s:save_object(t) abort \ } endfunction -function! s:insert_before_recorded() abort - " A limitation of normal! is that it can't start with a space, so use a NOP - " instead. - if s:recorded =~ '^\s' - let s:recorded = "\(ñóþ)" . s:recorded - endif +function! s:to_object_start() abort + set operatorfunc=s:save_object + exec "normal g@" . s:recorded + let pos = s:instype == '[' ? s:object.start : s:object.end + + echom printf("Jump To [%s] (%s) (%s)", string(pos), s:recorded, v:operator) + call setpos('.', pos) +endfunction + +function! s:insert_before_recorded() abort + let l:instype = s:instype " Something of a hack. If the motion starts with i or a, it is probably a " text object. " @@ -184,25 +250,37 @@ function! s:insert_before_recorded() abort set operatorfunc=s:save_object exec "normal g@" . s:recorded - if s:instype == 'a' + if l:instype == 'a' call setpos(".", s:object.end) if s:object.type == 'line' normal! $ endif - else + elseif l:instype == 'i' call setpos(".", s:object.start) if s:object.type == 'line' normal! 0 endif + if s:object.start[2] > col(".") + " Trying to insert at the $. Needs to be append. + let l:instype = 'a' + endif + elseif l:instype == 'o' + call setpos(".", s:object.end) + elseif l:instype == 'O' + call setpos(".", s:object.start) endif else exec "normal " . s:recorded endif - if s:instype == 'a' + if l:instype == 'a' exec "normal! \a \v" - else + elseif l:instype == 'i' exec "normal! \i \v" + elseif l:instype == 'o' + exec "normal! \o \v" + elseif l:instype == 'O' + exec "normal! \O \v" endif endfunction @@ -212,12 +290,17 @@ function! s:recordop(...) abort normal! q " Save the recorded amount let s:recorded=getreg(s:reg_to_clobber) + " A limitation of normal! is that it can't start with a space, so use a NOP + " instead. + if s:recorded =~ '^\s' + let s:recorded = "\(ñóþ)" . s:recorded + endif " Restore the register call setreg(s:reg_to_clobber, s:savereg[0], s:savereg[1]) " Restore the position call setpos('.', s:savepos) " Called (insert-about-obj). This is the part that can be repeated. - call feedkeys("c\(insert-about-obj)") + call feedkeys(s:operate_keys) endfunction diff --git a/plugin/remappings.vim b/plugin/remappings.vim index 8b3133a..90dd3c9 100644 --- a/plugin/remappings.vim +++ b/plugin/remappings.vim @@ -49,3 +49,5 @@ endfunction " Fix silly yank behavior. nnoremap Y y$ + +onoremap ^ "^" . repeat("w", v:count) diff --git a/plugin/supert.vim b/plugin/supert.vim index f4a8e85..e3db464 100644 --- a/plugin/supert.vim +++ b/plugin/supert.vim @@ -6,13 +6,9 @@ endif let s:last = [] -let s:last_char = '' +let s:insert_char = '' function! s:getchar() - echom "Is not repeating? " . string(! exists('v:repeating') || ! v:repeating) - if ! exists('v:repeating') || ! v:repeating - let s:last_char = nr2char(getchar()) - endif - return s:last_char + let s:insert_char = nr2char(getchar()) endfunction function! s:do_search(type, vis) abort @@ -21,16 +17,18 @@ function! s:do_search(type, vis) abort elseif a:type == ',' call s:do_last(1, a:vis) else - call s:do_search_ch(a:type, a:vis, s:getchar()) + call s:do_search_ch(a:type, a:vis, s:insert_char) endif endfunction function! s:do_last(inv, vis) abort if len(s:last) < 2 + + return return endif - let type = s:last[0] + le] type = s:last[0] let ch = s:last[1] if a:inv @@ -56,10 +54,22 @@ function! s:do_search_ch(type, vis, ch) let pattern = a:ch - if a:type == 't' - let pattern = '\zs\_.\ze' . pattern - elseif a:type == 'T' - let pattern = pattern . '\zs\_.\ze' + " if a:vis != '' + if a:type == 'f' + let pattern = pattern + elseif a:type == 'F' + let pattern = pattern + endif + " else + " if a:type == 't' + " let pattern = '\zs\_.\ze' . pattern + " elseif a:type == 'T' + " let pattern = pattern . '\zs\_.\ze' + " endif + " endif + + if a:vis != '' + exec "normal! v" endif let i = 0 @@ -69,53 +79,43 @@ function! s:do_search_ch(type, vis, ch) endwhile endfunction -nnoremap (supert-replace-t) call do_search('t', '') -nnoremap (supert-replace-T) call do_search('T', '') -nnoremap (supert-replace-f) call do_search('f', '') -nnoremap (supert-replace-F) call do_search('F', '') - -vnoremap (supert-replace-t) call do_search('t', 'v') -vnoremap (supert-replace-T) call do_search('T', 'v') -vnoremap (supert-replace-f) call do_search('f', 'v') -vnoremap (supert-replace-F) call do_search('F', 'v') - -onoremap (supert-replace-t) call do_search('t', 'o') -onoremap (supert-replace-T) call do_search('T', 'o') -onoremap (supert-replace-f) call do_search('f', 'o') -onoremap (supert-replace-F) call do_search('F', 'o') +nnoremap + \ call getchar() + \ call do_search('t', '') -onoremap (supert-replace-,) call do_search(',', 'o') -onoremap (supert-replace-;) call do_search(';', 'o') - -vnoremap (supert-replace-,) call do_search(',', 'v') -vnoremap (supert-replace-;) call do_search(';', 'v') - -nnoremap (supert-replace-,) call do_search(',', '') -nnoremap (supert-replace-;) call do_search(';', '') +function! s:prepare_operation(t, op) + let s:operator = a:op + let s:type = a:t + let s:insert_char = nr2char(getchar()) + call feedkeys("\") + call feedkeys(printf("%s\(srtf-post)", s:operator)) +endfunction -if g:supert_provide_bindings - nnoremap (supert-replace-t) - nnoremap (supert-replace-T) - nnoremap (supert-replace-f) - nnoremap (supert-replace-F) +onoremap t (srtf-to) +onoremap f (srtf-fo) +onoremap T (srtf-To) +onoremap F (srtf-Fo) - vnoremap (supert-replace-t) - vnoremap (supert-replace-T) - vnoremap (supert-replace-f) - vnoremap (supert-replace-F) +nnoremap t (srtf-t) +nnoremap f (srtf-f) +nnoremap T (srtf-T) +nnoremap F (srtf-F) - onoremap (supert-replace-t) - onoremap (supert-replace-T) - onoremap (supert-replace-f) - onoremap (supert-replace-F) +nnoremap (srtf-t) call getchar()call do_search('t', '') +nnoremap (srtf-f) call getchar()call do_search('f', '') +nnoremap (srtf-T) call getchar()call do_search('T', '') +nnoremap (srtf-F) call getchar()call do_search('F', '') - vnoremap (supert-replace-;) - vnoremap (supert-replace-,) +onoremap (srtf-to) call prepare_operation('t', v:operator) +onoremap (srtf-fo) call prepare_operation('f', v:operator) +onoremap (srtf-To) call prepare_operation('T', v:operator) +onoremap (srtf-Fo) call prepare_operation('F', v:operator) - nnoremap (supert-replace-;) - nnoremap (supert-replace-,) +" is this a question? Or is that? - onoremap (supert-replace-;) - onoremap (supert-replace-,) -endif +onoremap (srtf-post) call do_search_postchar('v') +function! s:do_search_postchar(vis) abort + echom printf("do_search(%s, %s)", s:type, a:vis) + call s:do_search(s:type, a:vis) +endfunction -- cgit