GithubHelp home page GithubHelp logo

progo / literate-calc-mode.el Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sulami/literate-calc-mode.el

0.0 0.0 0.0 136 KB

🧮 Literate programming for M-x calc

Home Page: https://melpa.org/#/literate-calc-mode

License: GNU General Public License v3.0

Emacs Lisp 100.00%

literate-calc-mode.el's Introduction

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 literate-calc-mode
  :ensure t)
(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

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

There is currently a single inhibitor, which comes installed by default:

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

Of course you can also just setq the options directly.

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

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.

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.

literate-calc-mode.el's People

Contributors

sulami avatar chrisbarrett avatar progo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.