Coder Social home page Coder Social logo

units-mode's Introduction

https://melpa.org/packages/units-mode-badge.svg https://stable.melpa.org/packages/units-mode-badge.svg

Introduction

This mode uses gnu units (which you need to install) to facilitate unit related conversions/calculations inside emacs.

I wrote units-mode so that I can do quick conversion and see the values, as well as get possible units as completion to choose from so I know which units are similar. And gnu units being lot more powerful for units, you’ll get the full power of gnu units inside emacs.

Emacs supports unit conversion in =calc= by default, so if you use calc then you can do conversions there too however units-mode will make the conversion available anywhere, and easily callable functions to convert for your packages too.

This package is a work in progress it may not handle all the errors or perform all the checks yet.

Contents

Features

  • convert region to other units (e.g 1 mile ⇒ 5280 ft)
  • check --conformable units for completion
  • Early error out for non-existent units
  • Errors out when target unit isn’t the same dimension
  • reduce expressions to standard unit form (e.g 1ft + 12 inch ⇒ 0.6096 m)
  • Calculate molecular weight from chemical formula (e.g. CH3 ⇒ 15.03482)

Installation

First clone this repo into your local machine or install it from melpa, then you can load it.

An example config using use-package is like this:

(use-package units-mode
  ;; :load-path "/path/to/units-mode"
  :hook text-mode
  :config
  (local-set-key (kbd "C-c u") 'units-convert-region-and-insert))

Customizations

customize variables

Available variables to customize are:

  • units-binary-path [default: “units”] “Path to the units binary.”

    Change this if units isn’t in your path, it should point to the units binary

  • units-user-args [default: “”] “Extra args for user to add to units command.”

    If there is any args necessary for user. Default arg used by system is “-t” to get terse results so it depends on that to parse the results. Look at man units for possible args.

  • units-insert-separator [default: ” = “] “Separator to insert before inserting the `units’ outputs.”

    When using the convert and insert variety of functions the converted value is separated with this separator before being inserted. for eg 1 m = 3.2808399 ft.

Keybindings

feel free to set your own keybindings, example is shown in Installation. The functions you can bind are in Interactive Functions.

Interactive Functions

  • units-convert-region Convert the marked region to a unit by asking units interactively. The interactive prompt has completions for defined units with same dimension but complex ones can be entered too.
  • units-convert-region-and-insert Convert region to unit and insert the results. Same as units-convert-region but insert the results.
  • units-reduce-region Reduce the region to standard units.
  • units-reduce-region-and-insert Reduce the region to standard units and insert the results.
  • units-quick-convert Quickly convert an expression into a unit interactively.

Examples

Simple conversion

If you have

L = 23 ft

And you ran units-convert-region-and-insert while selecting 23 ft you’ll be asked for target unit and with mm you get:

L = 23 ft = 7010.4 mm

There is completion for the target unit, that is non-exhaustive. So feel free to type whatever unit you want to. But pressing tab will help you see some of them.

Derived units

Similar to previous, you can use derived units that are product of other units, or expression with numbers with units.

g = 9.81 m/s^2
g = 9.81 m/s^2 = 32.185039 ft/s^2

and,

g = 9.81 m/s^2 + 12 N / 4kg
g = 9.81 m/s^2 + 12 N / 4kg = 42.027559 ft/s^2

do note that the expression needs to be valid, if you try 1m + 2 gram You’ll get error.

Completion only includes defined units and not derived ones like ft/s^2, so you need to type it fully.

Running units-convert-region will just show the converted results in the minibuffer.

If there is errors, like units aren’t matched then it’ll end with the error from units

Multiple units

Since units can allow you to convert to multiple units, this package also can.

For example using ft;in in 1m here returns this:

L = 1m
L = 1m = 3 ft + 3.3700787 in
L = 1m = 3 ft + 3.3700787 in = 100 cm

As you can see in the third line, you can use that value again to convert to something else as units supports simple calculations on units.

Also note that it’ll remove the unit with 0 coefficient, for example converting 1mile to ft;in will result in this:

L = 1mile
L = 1mile = 5280 ft

Reduce to standard units

You can reduce a expression to standard units, for example running units-reduce-region-and-insert on region after = in these examples we get:

L = 1 miles
L = 1 miles = 1609.344 m
g' = 1.9 force
g' = 1.9 force = 18.632635 m / s^2
area = 5 acre
area = 5 acre = 20234.282 m^2

Package Functions to use in your code

Elisp

You can load and then directly use the functions in your code. Most useful ones are:

(list (units-convert-simple (/ 1.0 2) "m" "ft")
      (units-convert "2 m" "ft")
      (units-reduce "1 m + 24 in")
      (units-ignore 5 "ft"))
1.64041996.56167981.6096 m5

Slime Integration (example is for sbcl; adapt to whatever clisp you use)

Although not part of emacs package there is a file clisp/units.lisp with functions that do similar things in sbcl. You can load/evaluate the functions there in slime process to use those same functions in slime.

You can also put the contents in clisp/units.lisp to ~/.sbclrc so it’s evaluated in sbcl startup, similarly for any other dialect you use.

The slime integration means you’ll have advantages of clisp rational numbers and other things while using the same syntax as that of elisp.

Same example using sbcl. (Note that here you don’t have to use 1.0/2 coz 1/2 ≠ 0 in sbcl)

(list (units-convert-simple (/ 1 2) "m" "ft")
      (units-convert "2 m" "ft")
      (units-reduce "1 m + 24 in")
      (units-ignore 5 "ft"))
1.64041986.561681.60965

units-mode's People

Contributors

atreyagaurav avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

emacsmirror

units-mode's Issues

Easier way to get the molecular weights of the compounds

Gnu units can give the molecular weights given the element composition, like units "hydrogen" gives 1.00794, or units "hydrogen 2 + oxygen" gives 18.01528, so maybe we can make it so H or just H2O will give that instead of having to type out the whole names. since there are alredy units that have same symbols we need a different method that knowing it is chemical formula can convert it into gnu units format.

Let's see \(\([A-Z][a-z]*\)\([0-9]*\)[+-]*\) Seems to work for individual parts, we need to split by the dot, and then extract the number (multiplier) at the beginning before parsing this way.

Tested on: H2O CO2 C6H12O6 CaCO3 2SO2 2 H2SO4 SO4-- Cl- MgCO3.2H2O

Could be fun lol.

And then we'll need a dictionary to point all the symbols to their names, and once we have the name, we can use gnu units to get their molar weight, that also makes debugging easier as we can see which elements it has parsed into.

Support for the isotopes maybe later. Just need this to work for now.

A method to quickly convert or reduce

A function to do quick convert without modifying the buffer, the inputs can come completely from the input prompt instead of from the buffer content.

Also, this could be added to the current interactive function when there is prefix argument then it asks with a prompt instead of read from buffer. (or if the selection is empty it asks.)

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.