1

Is there some package or builtin emacs feature which evaluates an algebraic formula in place?

E.g. I type in the scratch buffer:

(3 + 4) *  5  

and when I invoke some command on this line then I get in the buffer:

(3 + 4) *  5 = 35

It's not hard to implement, I'd just like to know of there is an existing feature which can do this.

Drew
  • 77,472
  • 10
  • 114
  • 243
Tom
  • 1,260
  • 8
  • 16

2 Answers2

1

calc provides calc-eval for this kind of use, just pass it a string and receive a string result back.

wasamasa
  • 22,178
  • 1
  • 66
  • 99
1

For reference I'm posting my solution based on wasamasa's answer:

(save-excursion
  (beginning-of-line)
  (if (re-search-forward " *=.*" (line-end-position) t)
      (replace-match ""))
  (end-of-line)
  (insert " = "
          (calc-eval
           (buffer-substring-no-properties
            (line-beginning-position)
            (line-end-position)
            ))))
Tom
  • 1,260
  • 8
  • 16