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
  • 75,699
  • 9
  • 109
  • 225
Tom
  • 1,190
  • 7
  • 16
  • For this kind of stuff there exists the package [calc-embedded](https://www.gnu.org/software/emacs/manual/html_node/calc/Basic-Embedded-Mode.html). – Tobias Dec 09 '19 at 16:15

2 Answers2

1

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

wasamasa
  • 21,803
  • 1
  • 65
  • 97
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,190
  • 7
  • 16