Monday, July 16, 2012

Allow normal command then motion (using g@) and [count]command in the same mapping

Hello. I've been trying to figure out this for some time now, and couldn't find a solution.

I have this mapping:

nmap <silent> -c :<C-U>set opfunc=Add_comment_operator<CR>g@

Below you'll find the Add_comment_operator() code, but I think it's not really relevant. This mapping allows me to do things like -ciw to comment the word under the cursor, or -ca{ to comment the block the cursor is in.

Now, I would like it also to be able to type 4-c and comment four lines of code (the current one and three below). I don't know how to do this. I tried checking if there is a count querying the v:count variable, but then if I find out there isn't, the :normal g@ command doesn't do what I expect (it doesn't executes &opfunc after the motion). I suppose this is due to this:

"... {commands} should be a complete command. If {commands} does not finish a command, the last one will be aborted as if <Esc> or <C-C> was typed."

from :h :normal, but I'm not sure.

Any ideas on how could I achieve what I want?

Thanks a lot in advance,
FaQ


function! Add_comment_operator(type)
if a:type == 'line'
let start_col = 0
let end_col = len(getline("']"))
elseif a:type == 'char'
let start_col = col("'[") - 1
let end_col = col("']")
endif
if line("'[") == line("']")
let end_col = end_col + 2
endif
let line = getline("'[")
call setline("'[", strpart(line, 0, start_col) . '/*' . strpart(line, start_col))
let line = getline("']")
call setline("']", strpart(line, 0, end_col) . '*/' . strpart(line, end_col))
endfunction

--
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