Wednesday, June 23, 2010

Re: Making expr-mappings using getchar() work inside a macro

On Jun 22, 12:01 pm, Ben Fritz <fritzophre...@gmail.com> wrote:
>
> The <expr> mappings were used initially, because for whatever
> reason, :normal! f... and friends acts as an exclusive motion, when my
> mappings need to be inclusive just like normal-mode f, F, t, and T.
>

I'm not sure what I used to be doing in visual mode that didn't work.
Here's what I settled on, for posterity (though it doesn't answer my
original question):

" Highlight characters found by f, F, t, and T.
" Unhighlights on a cursorhold.
"
" All versions with matchadd also have <expr> mappings so no need to
check for
" both (matchadd is needed to highlight the char, expr to get the ,
and ;
" mappings to play nicely with visual and operator pending mode).
"
" Also map <leader>f to show the highlight.
if exists('*matchadd')
nnoremap f :<C-U>call FindChar('f', v:count1)<CR>
nnoremap F :<C-U>call FindChar('F', v:count1)<CR>
nnoremap t :<C-U>call FindChar('t', v:count1)<CR>
nnoremap T :<C-U>call FindChar('T', v:count1)<CR>
onoremap f :<C-U>call FindChar('f', v:count1, 'v')<CR>
onoremap F :<C-U>call FindChar('F', v:count1, 'v')<CR>
onoremap t :<C-U>call FindChar('t', v:count1, 'v')<CR>
onoremap T :<C-U>call FindChar('T', v:count1, 'v')<CR>
xnoremap f :<C-U>call FindChar('f', v:count1, 'gv')<CR>
xnoremap F :<C-U>call FindChar('F', v:count1, 'gv')<CR>
xnoremap t :<C-U>call FindChar('t', v:count1, 'gv')<CR>
xnoremap T :<C-U>call FindChar('T', v:count1, 'gv')<CR>

nnoremap <expr> , FindLastChar(',')
nnoremap <expr> ; FindLastChar(';')
onoremap <expr> , FindLastChar(',')
onoremap <expr> ; FindLastChar(';')
xnoremap <expr> , FindLastChar(',')
xnoremap <expr> ; FindLastChar(';')

" <C-U> in normal mode to remove any count, in visual mode to remove
range
" Do not provide this command in op-pending mode, it doesn't do
anything
nnoremap <Leader>f :<C-U>call HighlightFoundChar()<CR>
xnoremap <Leader>f :<C-U>call HighlightFoundChar()<CR>gv

" highlight the last found character
function! HighlightFoundChar()
if &hlsearch
if exists('w:fFtT_command_highlight')
call matchdelete(w:fFtT_command_highlight)
endif
let w:fFtT_command_highlight = matchadd(
\'Search',
\'\V\%' . line('.') . "l".escape(g:last_found_char,'/\'),
\11)
endif
endfunction

" Set the "last found character" and highlight it.
function! FindChar(op, count, ...)
echo "Enter character to find"
let g:last_found_char = nr2char(getchar())
call HighlightFoundChar()
" clear the echo
echo ''
let cmdprefix=''
if a:0
let cmdprefix=a:1
endif
exec 'normal! ' . cmdprefix . a:count . a:op . g:last_found_char
endfunction

" Highlight the "last found character" if it exists and pass on the
input
" operation which should be either , or ;
function! FindLastChar(op)
if exists('g:last_found_char') && g:last_found_char != ''
call HighlightFoundChar()
" clear the echo
echo ''
endif
return a:op
endfunction
endif

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment