> From: oversky <mailcwc@gmail.com>
> Subject: replace x^y by pow(x, y)
>
> I found a very useful in-line calculator in
> http://vimrc-dissection.blogspot.com/2011/01/vim-inline-calculator-revisited.html
>
> Is it possible to improve it such that it can handle power syntax,
> like 2.2^3.4
> I know power can be written as pow(x,y) in vim.
> But, using ^ to donate power can make the same equation work in
> matlab.
> And this way is more nature to me.
I recently made a python calculator for vim. Perhaps not as
sophisticated as the one you mentioned, but I think it could be easily
adapted. It inherits all the goodness of python, including the more
reasonable `**` syntax for exponentiation, and it also has advantage of
working with arbitrary precision integers. Here it is, in case it is
useful: >
" python calculator: saves result to default register {{{
function s:pyCalculator(whichMode)
if !has("python")
echo "Vim must be compiled with +python to use this function."
return
endif
if a:whichMode == 0 || a:whichMode == 1 "normal or insert mode
let sexpr = input("Python expr: ")
if sexpr == ""
return
endif
elseif a:whichMode == 2 "visual mode
let tempreg = @a
normal! gv"ay
let sexpr = @a
let @a = tempreg
endif
python import vim
exe "python vim.command('let @@ = \"' + str(" . sexpr . ") + '\"')"
" Note: might want to save the old value of @" somewhere.
if a:whichMode == 1 "insert mode
normal! p
else
exe 'normal! :echo "' . @@ . "\"\<CR>"
endif
" Note: to expand the contents of @", we invoke the echo
" command from exe, rather than by itself.
endfunction
" }}}
and here are some mappings:
nnoremap <F3> :call <SID>pyCalculator(0)<CR>
inoremap <F3> <C-O>:call <SID>pyCalculator(1)<CR>
vnoremap <F3> :call <SID>pyCalculator(2)<CR>
Note that all but the insert mode mapping will overwrite the " register.
Hope that helps.
-WES
--
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