On Tue, Jan 10, 2012 at 12:10 PM, lith <[email protected]> wrote:
> Am Dienstag, 10. Januar 2012 06:03:02 UTC+1 schrieb Karthick:
>>
>> > You can do calculations. Neither the wiki nor the code of "table.zip"
>> > contained the word "sum". Didn't read the docs though.
>>
>> But it is possible to script it in vim. I would do it little
>>
>>
>> differently than what was shown in video...
>
> While it's possible in vim to add up visually selected numbers, I think
> you'd have a hard time implementing field functions as shown in the video. I
> bet they used overlays
> (http://www.gnu.org/savannah-checkouts/gnu/emacs/manual/html_node/elisp/Overlays.html)
> to implement these field functions. Unfortunately, vim doesn't have such a
> feature (yet?).

Thanks for the link, will read up.

FWIW, a crude proto that can add/multiply a selected range:

" Insert the result of the operation 'op' at current cursor position
" The data being operated on is the previous visual select area
function! InsertOpResult(op)
   " Save current cursor position, later operations will move it
   let saved_cursor = getpos(".")
   " Save contents of reg a, later ops will trash it
   let saved_a = @a

   " Yank last visual selection to '@a', split to numbers and add them up
   silent! normal gv"ay
   let nums = split(@a, '\n')
   let result = nums[0]
   for num in nums[1:-1]
      exe "let result = result " . a:op . " num"
   endfor

   " Restore reg a
   let @a = saved_a
   " Restore cursor and insert result
   call setpos('.', saved_cursor)
   exe "normal i" . result
endfunction

nmap <Leader>+ :call InsertOpResult("+")<CR>
nmap <Leader>* :call InsertOpResult("*")<CR>

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

Reply via email to