Where parallels cross

Interesting bits of life

Moldable Emacs: evaluate arithmetic at point

This is a short post: moldable-emacs has a mold to evaluate arithmetic expression at point!

Say you are reading some text and you see a formula. You get curious but would like to finish the text first. Now how can you run that maths again? Emacs comes with the fantastic calc library. So a good option in vanilla Emacs is to use calc-grab-region. You need to highlight something and this will show up in a calc buffer.

Well, maybe that is too much though. You just would like to pop the result of that operation on the side, while you keep reading.

How can we do that?

Simple, run your me/mold command. You will see a mold just for that. Let me show you how that looks.

/assets/blog/2021/08/06/moldable-emacs-evaluate-arithmetic-at-point/screen-2021-07-03-13-15-34.jpg

When your pointer is on an expression, it will run that. The outcome is green so you can find the result immediately.

By the way you may want to access your computation in a Playground mold. In the self variable (of the buffer) you can find the following value: (:given "(1+2) / 2 ^ 4" :then "0.1875"). This gives you a chance to program the computation.

Anyway you may want to focus on a sub-expression of the formula. For that I also added the possibility to use the region to select the formula you care about.

/assets/blog/2021/08/06/moldable-emacs-evaluate-arithmetic-at-point/screen-2021-07-03-13-16-14.jpg

This is it really. The functions I made for this may break easily because they only match my basic use cases. Feedback and PRs are welcome, if this does not work for you. I am happy I don't have to decide between keeping the formula or the result in my notes anymore!

You can try this by using moldable-emacs. And no external dependencies, all thanks to the power of calc!

Comments