• Stars
    star
    323
  • Rank 129,304 (Top 3 %)
  • Language
    Emacs Lisp
  • License
    GNU General Publi...
  • Created over 4 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿงฎ Literate programming for M-x calc

Literate Calc Mode

https://melpa.org/packages/literate-calc-mode-badge.svg

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.

./scrot.png

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.

Configuration

There is a M-x customize group named after the mode which contains the following options:

literate-calc-mode-idle-timeHow long to wait after typing to recalculate results
literate-calc-mode-inhibit-line-functionsHook functions called for each line to test whether to inhibit calculation
literate-calc-mode-radixDefault radix/base for results
literate-calc-max-buffer-sizeMaximum buffer size to activate the minor mode

The idle time prevents lag due to constant recalculation in the middle of typing, defaulting to 1 second.

These are the available inhibitors, which are all enabled by default:

literate-calc-mode-inhibit-in-src-blocks
Prevents evaluation inside org-mode src blocks
literate-calc-mode-inhibit-in-latex
Prevents evaluation inside org-mode LaTeX fragments

Of course you can also just setq the options directly.

Calc Configuration

Because LCM uses Emacs calc under the hood, a lot of configuration options for calc also affect LCM operation.

An interesting one is calc-multiplication-has-precedence, which when non-nil means that multiplication will have higher precedence than division, which can lead to surprising results.

Use

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-lineEvaluates a single line
M-x literate-calc-eval-bufferEvaluates the whole buffer
M-x literate-calc-insert-resultsEvaluates the whole buffer and inserts results
M-x literate-calc-clear-overlaysRemoves all overlays and clears variables
M-x literate-calc-remove-resultsRemoves all results and clears variables
M-x literate-calc-set-radixSets the radix/base output for the current buffer

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

Evaluation in Org

Org-mode source blocks can be evaluated (C-c C-c by default).

If :results is set to value, which is the default, a block returns its last result. If :results is set to output, it will return the entire block, annotated with results.

Local variables can be defined in header arguments as :var a=38 b=4.

Changing radix/base

You can change the literate-calc-mode-radix custom variable to set the default base for number output globally for all literate-calc-mode buffers, but itโ€™s also possible to change the output radix of the current buffer by calling the interactive function literate-calc-set-radix in your desired buffer. For example, M-x literate-calc-set-radix 16 will display results with base 16 (hex).

Example output with radix set to 16:

a0 = 2#11001100 => a0: 16#CC
a1 = 2#11110000 => a1: 16#F0
= and(a0, a1) => 16#C0

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.

Calc also has a lot of advanced features, like arrays:

Numbers = [1 2 3] => Numbers: [1, 2, 3]

= 3 Numbers => [3, 6, 9]

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.

More Repositories

1

FrankenWM

๐Ÿ–ผ๏ธ Fast dynamic tiling X11 window manager
C
261
star
2

wm

[WIP] Minimal floating Wayland window manager
C
34
star
3

blog

โœ๏ธ My blog. Custom static site generator.
Rust
28
star
4

dotfiles

๐Ÿ  ~/.everything : everything = mostly Emacs
Emacs Lisp
23
star
5

feed2maildir

๐Ÿ“ฌ Read RSS/Atom feeds in your favourite, maildir-compatible email client.
Python
14
star
6

arduino-nano-33

๐Ÿฆพ Rust on the Arduino Nano 33 IoT
Rust
12
star
7

three-bodies

๐Ÿช Three body simulation in Wasm
Rust
11
star
8

streamo

๐Ÿ“ฝ๏ธ CLI *nix to Twitch.tv streaming solution using ffmpeg
Shell
7
star
9

hmenu

โŒจ๏ธ Fuzzy finder and launcher for your command line
Haskell
5
star
10

spielwiese

experimental stuff
Haskell
5
star
11

tinfoilsub

Allows to follow YouTube subscribtions w/o an account
Haskell
4
star
12

emacs-package-template

๐Ÿงฐ Emacs Package Template for Cookiecutter
Emacs Lisp
4
star
13

circleci-api.el

๐Ÿ”— CircleCI API bindings for Emacs
Emacs Lisp
4
star
14

calc

๐Ÿงฎ Terminal RPN calculator
Rust
4
star
15

rx

Standalone version of Emacs' rx macro
Rust
3
star
16

advent-of-code-2022

๐ŸŽ„๐Ÿฆ€ Advent of Code in Rust - all in < 1000 ms
Rust
3
star
17

farm

Brutal medieval farming game in the browser
Clojure
2
star
18

serial-monitor

๐Ÿ–ฅ Simple Arduino Serial Monitor
Rust
2
star
19

hGM

A Yesod-based web tool for tabletop GMs and other writers
Haskell
2
star
20

matcha

๐Ÿต A peaceful package manager
Rust
2
star
21

cvshs

Automated C vs. Haskell performance comparison
C
1
star
22

nozdormu

Python benchmarking for humans and dragons
Python
1
star
23

floppy-bird

It's a bird, and it flops
Clojure
1
star
24

hcached

Haskell drop-in replacement for memcached
Haskell
1
star
25

pelican-theme

A flat and simple pelican (Jinja2) blog theme using my CyberCopy colorscheme
CSS
1
star
26

cryptopals

Terrible Rust learning ground
Rust
1
star
27

diet.vim

Diet template language syntax highlighting for vim
Vim Script
1
star
28

sloap

An interactive powernap wakeup timer / alarm clock
C
1
star
29

lein-deps-repl

Start a Clojure REPL with dependencies. Zero setup.
Shell
1
star
30

Medusa

Modular, nagios compatible monitoring system
Python
1
star
31

circleci-cookbook

โœ… CircleCI example configs for advanced use cases
1
star
32

danketsu

A simple and fast event-based, RESTful microservice communication microservice
Go
1
star