GitHub - sulami/literate-calc-mode.el: 🧮 Literate programming for M-x calc
source link: https://github.com/sulami/literate-calc-mode.el
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Literate Calc Mode
Literate programming for M-x calc
. There is an announcement blog
post.
Displays inline results for calculations, supports variables and updates as you type (if you want). Also works in your favourite markup mode.
Installing
Simply grab it from MELPA.
use-package
(use-package literate-calc-mode :ensure t)
straight.el
(straight-use-package 'literate-calc-mode)
package.el
Ensure you have MELPA available.
M-x package-install
, select literate-calc-mode
.
Manual
Just fetch literate-calc-mode.el, save it somewhere and load it into Emacs.
There is both a major literate-calc-mode
and a minor
literate-calc-minor-mode
. The major mode does some basic syntax
highlighting, while the minor mode only evaluates all calc statements
while typing.
The minor mode works quite well with org-/markdown mode or other markup language major modes.
There are also some functions which can be called without any mode being active:
M-x literate-calc-eval-line | Evaluates a single line |
M-x literate-calc-eval-buffer | Evaluates the whole buffer |
M-x literate-calc-insert-results | Evaluates the whole buffer and inserts results |
M-x literate-calc-clear-overlays | Removes all overlays and clears variables |
Using Units
You can simply append units to your values like so:
Flour = 500g => Flour: 500 g
Unit conversion (and other complex functions) can be used by invoking the matching Algebraic Function.
= usimplify(1m + 3mm) => 1.003 m
You can also use unknown mathematical symbols:
= x*2 + x-3 => 3 x - 3
Full Example
This is a literate calc file. Lines without "=" are ignored. All results starting with "=>" are an overlay generated by literate-calc-mode. That means they are displayed in Emacs, but not actually in the buffer/file contents. We can calculate a value like so: = 2 + 2 => 4 If there is any string on the left hand side, it becomes a bound variable. Pi = 3.14159 => Pi: 3.14159 We can use this variable below the definiton. Tau = Pi * 2 => Tau: 6.28318 Results are calculated using Emacs' own calc, so you can use formulas as well. = round(Pi, 2) => 3.14 Later bindings shadow earlier ones: Pi = 3 => Pi: 3 = Pi => 3 Variable names can have spaces as well: Monthly Expenses = 500 => Monthly Expenses: 500 Monthly Income = 1000 => Monthly Income: 1000 Annual Savings = 12 * (Monthly Income - Monthly Expenses) => Annual Savings: 6000 All values are recalculated on every update in a spreadsheet-like fashion.
Roadmap
There are some additional features I’m currently thinking about.
Semantic Highlighting
One of the original inspirations was Tydlig, which does similar things, but also has semantic highlighting. That means, variables are highlighted in different colours, but always the same one for a given variable, so that you can see where it’s used at a glance.
I might steal some code from rainbow-identifiers, which is one of the shorter existing implementations around, and adapt that to our needs.
Export/Import
Currently there is the option to “bake in” results by adding them to the buffer contents. If there is genuine need for exporting calculations with results (so anyone can read them), and then maybe also importing them again, there needs to be more tooling around this.
I’ve also been toying with the idea of integrating with org-babel, so
that you could have babel literate-calc
blocks which evaluate and
export properly.
If you have opinions on this, please feel free to open an issue or send me an email so I can figure out what the best way to do this is.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK