oetting / markdown-calc.tmbundle

Markdown calc is a TextMate plugin that allows calculation of math statements embedded in markdown code blocks.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Markdown Calc

Markdown calc is a TextMate plugin that allows calculation of math statements embedded in markdown code blocks.

This is heavily inspired by Calca, which is awesome. This plugin does a little of what Calca does, but Calca is much more powerful, and importantly does symbol manipulation, so not just simple calculations like this plugin. If you haven't seen Calca, go have a look at it.

Example

If I buy a house at price = 1,200,000 and a down payment in pct = 20% with a interest rate of interest = 3.7% the following calculation

down payment = down payment in pct * price => 240,000.0
amount to borrow = price - down payment

years = 30
terms pr year = 4

n = years * terms pr year
i = interest / 4

monthly payment = i/(1-(1+i)^-n) * amount to borrow * 
    terms pr year / 12

Shows that my monthly payment will be monthly payment => 4,426.1336

Usage

Calculations in a markdown file are triggered by ctrl+shift+c.

Write your math in markdown code blocks, that is either inline delimited by or indented by a tab or four spaces. Like so:

1 + 1 => 2.0

Add => at the end of a statement if you want to output the result (anything after => will be replaced with the result on each re-calculation).

If the result is an error and there is no => on the line a !=> plus the error will be inserted. !=> and anything after it will be removed if there is no error. For example 1/0 !=> Error: Divide by zero.

Variables are case insensitive, and can contain spaces and any characters (even non ASCII)

You can define functions using the following syntax.

define sum(a, b) {
    return a + b   
}         

sum(2, 1) => 3.0

There are a few built in functions

pi() => 3.1416
sin(pi() / 2) => 1.0
cos(pi()) => -1.0
arctan(1) => 0.7854
sqrt(9) => 3.0

Installation

Install via Git:

mkdir -p ~/Library/Application\ Support/Avian/Bundles
cd ~/Library/Application\ Support/Avian/Bundles
git clone git://github.com/oetting/markdown-calc.tmbundle Markdown\ Calc.tmbundle

Notes

Markdown Calc uses bc to perform the calculations. Some simple transformations are performed to the statements to allow for a less rigid syntax, this includes allowing unicode characters and case insensitive variables.

Piping input to bc and then reading the results has proven difficult. It is hard to know when to wait for output from bc, and if there is no output to read when the plugin expects it, the processing will hang.

If you get in a situation where the plugin hangs you can find and kill the ruby process that the plugin starts.

Future work

This is just a relatively functional hack, so there is room for lots of improvements. Contributions are very welcome.

Ideas

  • Define a grammar and implement a real parser instead the current hack
  • Add symbol manipulation. Maybe have a look at Symbolic

About

Markdown calc is a TextMate plugin that allows calculation of math statements embedded in markdown code blocks.

License:MIT License


Languages

Language:Ruby 100.0%