Tag Archives: Emacs

Leveraging the Org Mode Structure with CSS

Org-mode is an incredible tool for editing text. May it be for HTML, Latex, doc, or even plain text format.

I’ve already told that I use it to edit this blog, but I also use it for almost every text file I have to do. For example, all my readme on Github are in .org. (Even if they don’t support the org format very well. Screw Markown, org-mode is superior!)

Without any surprises, I also use it if I have to write long texts for my commercial solution BACnet Help. I can write thousands of words without having to think once about any HTML tags (DIV, SPAN, BR…). This is all handled automatically when exporting to HTML.

This is all very unsurprising for any Org-mode user. However while inspecting a page in the Chromium browser, I noticed how an Org-mode document is so well structured when exported to HTML.

Let ‘s take the Table of Contents as an example. You get it for free, with all the links and anchors. But the more important part is how the TOC is made: with successive stages of <UL> and <LI>. The structure itself is giving information. With it, we can easily determine what is a subset of a section, and what is a subset of that subset.

Here is how the plain TOC looks: https://frozenlock.files.wordpress.com/2012/10/wpid-toc.png

Now let’s leverage the <UL> and <LI> structure with a pinch of CSS.

 #text-table-of-contents ul > li > ul {
    margin-left: 0.5em;
    border-left: 1px dotted #779898;
    border-bottom-left-radius: 1em;
    padding: 0.5em;

If you don’t read CSS, just know this: We select the element identified by “text-table-of-contents”, find any children that’s an unlisted list (<UL>), inside a list item (<LI>), inside another unlisted list. Then we apply some simple decorations to those.


Such a simple change, and yet a tremendous impact!

Emacs: Unit Converter for the Engineer

How many cubic centimeters in a liter? How many feet in a kilometer? What about gallons per minute and liters per second?

Every single engineer needs to convert units. What may change is how he does it.

  1. Find an online converter / ask Google;
  2. Use a desktop application, such as convert; https://frozenlock.files.wordpress.com/2012/08/wpid-convert.png
  3. Or do it on-the-fly with Emacs!

Now, you may ask:

“But why on earth would I ever consider Emacs to convert my units?”

Because Emacs has an amazing hidden gem inside it: Calc. Fear not, there is no need to learn yet another tool only to convert some units; I’ve package everything needed in a little .el file.

Once this is installed, you can ask Emacs, on-the-fly, to convert some units. Here is an example:

Old value and unit (type C-z for unit list) :

100oz RET

to unit (type C-z for unit list) : g


100oz converted to g = 2834.9523125 g

Pretty nice, isn’t it?

But this is far from it. What’s really handy is how you can input usual prefixes, such as k (kilo), m (milli), y (yocto)… without any special care.

Old value and unit (type C-z for unit list) :

kg RET

to unit (type C-z for unit list) :


kg converted to g = 1000 g


What if you need units not yet defined in Calc? Simply add your own!

Here are some units I’ve added to be more helpful in a HVAC context:

;;; Add some custom units often used in HVAC
(eval-after-load "calc-units"
     (setq math-additional-units
           '((gpm "gal / min" "Gallons per minute")
             (ls "l / s" "Liters per second")
             (cfm "ft^3 / min" "Cubic feet per minute"))
           math-units-table nil)))

Finally, to make sure you take Emacs and Calc seriously, here is the unit list (obtained with M-x calc-view-units-table):

Calculator Units Table:

(All definitions are exact unless marked with an asterisk (*).)

Unit    Type  Definition                  Description

 gpm      U   gal / min                   Gallons per minute
 ls       U   l / s                       Liters per second
 cfm      U   ft^3 / min                  Cubic feet per minute

 m            m                           Meter (base unit)
 in           2.54 cm                     Inch
 ft           12 in                       Foot
 yd           3 ft                        Yard
 mi           5280 ft                     Mile
 au           149597870691 m (*)          Astronomical Unit
 lyr          c yr                        Light Year
 pc           3.0856775854 10^16 m (*)    Parsec
 nmi          1852 m                      Nautical Mile
 fath         6 ft                        Fathom
 fur          660 ft                      Furlong
 mu           1 um                        Micron
 mil          (1/1000) in                 Mil
 point        (1/72) in                   Point (1/72 inch)
 Ang          10^-10 m                    Angstrom
 mfi          mi + ft + in                Miles + feet + inches
 texpt        (100/7227) in               Point (TeX conventions)
 texpc        12 texpt                    Pica
 texbp        point                       Big point (TeX conventions)
 texdd        (1238/1157) texpt           Didot point
 texcc        12 texdd                    Cicero
 texsp        (1/65536) texpt             Scaled TeX point

 hect         10000 m^2                   Hectare
 a            100 m^2                     Are
 acre         (1/640) mi^2                Acre
 b            10^-28 m^2                  Barn

 L            10^-3 m^3                   Liter
 l            L                           Liter
 gal          4 qt                        US Gallon
 qt           2 pt                        Quart
 pt           2 cup                       Pint
 cup          8 ozfl                      Cup
 ozfl         2 tbsp                      Fluid Ounce
 floz         2 tbsp                      Fluid Ounce
 tbsp         3 tsp                       Tablespoon
 tsp          4.92892159375 ml            Teaspoon
 vol          tsp+tbsp+ozfl+cup+pt+qt+gal Gallons + ... + teaspoons
 galC         galUK                       Canadian Gallon
 galUK        4.54609 L                   UK Gallon

 s            s                           Second (base unit)
 sec          s                           Second
 min          60 s                        Minute
 hr           60 min                      Hour
 day          24 hr                       Day
 wk           7 day                       Week
 hms          wk + day + hr + min + s     Hours, minutes, seconds
 yr           365.25 day                  Year (Julian)
 Hz           1 / s                       Hertz

 mph          mi / hr                     Miles per hour
 kph          km / hr                     Kilometers per hour
 knot         nmi / hr                    Knot
 c            299792458 m / s             Speed of light

 ga           9.80665 m / s^2             "g" acceleration

 g            g                           Gram (base unit)
 lb           16 oz                       Pound (mass)
 oz           28.349523125 g              Ounce (mass)
 ton          2000 lb                     Ton
 tpo          ton + lb + oz               Tons + pounds + ounces (mass)
 t            1000 kg                     Metric ton
 tonUK        1016.0469088 kg             UK ton
 lbt          12 ozt                      Troy pound
 ozt          31.10347680 g               Troy ounce
 ct           0.2 g                       Carat
 u            1.660538782 10^-27 kg (*)   Unified atomic mass

 N            m kg / s^2                  Newton
 dyn          10^-5 N                     Dyne
 gf           ga g                        Gram (force)
 lbf          ga lb                       Pound (force)
 kip          1000 lbf                    Kilopound (force)
 pdl          0.138254954376 N            Poundal

 J            N m                         Joule
 erg          10^-7 J                     Erg
 cal          4.1868 J                    International Table Calorie
 calth        4.184 J                     Thermochemical Calorie
 Cal          1000 cal                    Large Calorie
 Btu          1055.05585262 J             International Table Btu
 eV           ech V                       Electron volt
 ev           eV                          Electron volt
 therm        105506000 J                 EEC therm
 invcm        h c / cm                    Energy in inverse centimeters
 Kayser       invcm                       Kayser (inverse centimeter energy)
 men          100 / invcm                 Inverse energy in meters
 Hzen         h Hz                        Energy in Hertz
 Ken          k K                         Energy in Kelvins
 Wh           W hr                        Watt hour
 Ws           W s                         Watt second

 W            J / s                       Watt
 hp           550 ft lbf / s              Horsepower
 hpm          75 m kgf / s                Metric Horsepower

 K        K   K                           Degree Kelvin (base unit)
 dK       K   K                           Degree Kelvin
 degK     K   K                           Degree Kelvin
 dC       C   K                           Degree Celsius
 degC     C   K                           Degree Celsius
 dF       F   (5/9) K                     Degree Fahrenheit
 degF     F   (5/9) K                     Degree Fahrenheit

 Pa           N / m^2                     Pascal
 bar          10^5 Pa                     Bar
 atm          101325 Pa                   Standard atmosphere
 Torr         (1/760) atm                 Torr
 mHg          1000 Torr                   Meter of mercury
 inHg         25.4 mmHg                   Inch of mercury
 inH2O        2.490889 10^2 Pa (*)        Inch of water
 psi          lbf / in^2                  Pounds per square inch

 P            (1/10) Pa s                 Poise
 St           10^-4 m^2 / s               Stokes

 A            A                           Ampere (base unit)
 C            A s                         Coulomb
 Fdy          ech Nav                     Faraday
 e            ech                         Elementary charge
 ech          1.602176487 10^-19 C (*)    Elementary charge
 V            W / A                       Volt
 ohm          V / A                       Ohm
 mho          A / V                       Mho
 S            A / V                       Siemens
 F            C / V                       Farad
 H            Wb / A                      Henry
 T            Wb / m^2                    Tesla
 Gs           10^-4 T                     Gauss
 Wb           V s                         Weber

 cd           cd                          Candela (base unit)
 sb           10000 cd / m^2              Stilb
 lm           cd sr                       Lumen
 lx           lm / m^2                    Lux
 ph           10000 lx                    Phot
 fc           lm / ft^2                   Footcandle
 lam          10000 lm / m^2              Lambert
 flam         (1 / pi) cd / ft^2          Footlambert

 Bq           1 / s                       Becquerel
 Ci           37 10^9 Bq                  Curie
 Gy           J / kg                      Gray
 Sv           Gy                          Sievert
 R            258 10^-6 C / kg            Roentgen
 rd           (1/100) Gy                  Rad
 rem          rd                          Rem

 mol          mol                         Mole (base unit)

 rad          rad                         Radian (base unit)
 circ         2 pi rad                    Full circle
 rev          circ                        Full revolution
 deg          circ / 360                  Degree
 arcmin       deg / 60                    Arc minute
 arcsec       arcmin / 60                 Arc second
 grad         circ / 400                  Grade
 rpm          rev / min                   Revolutions per minute

 sr           sr                          Steradian (base unit)

 h            6.62606896 10^-34 J s (*)   Planck's constant
 hbar         h / 2 pi                    Planck's constant
 mu0          4 pi 10^-7 H / m            Permeability of vacuum
 eps0         1 / mu0 c^2                 Permittivity of vacuum
 G            6.67428 10^-11 m^3/(kg s^2) (*) Gravitational constant
 Nav          6.02214179 10^23 / mol (*)  Avogadro's constant
 me           9.10938215 10^-31 kg (*)    Electron rest mass
 mp           1.672621637 10^-27 kg (*)   Proton rest mass
 mn           1.674927211 10^-27 kg (*)   Neutron rest mass
 mmu          1.88353130 10^-28 kg (*)    Muon rest mass
 Ryd          10973731.568527 /m (*)      Rydberg's constant
 k            1.3806504 10^-23 J/K (*)    Boltzmann's constant
 alpha        7.2973525376 10^-3 (*)      Fine structure constant
 muB          927.400915 10^-26 J/T (*)   Bohr magneton
 muN          5.05078324 10^-27 J/T (*)   Nuclear magneton
 mue          -928.476377 10^-26 J/T (*)  Electron magnetic moment
 mup          1.410606662 10^-26 J/T (*)  Proton magnetic moment
 R0           8.314472 J/(mol K) (*)      Molar gas constant
 V0           22.710981 10^-3 m^3/mol (*) Standard volume of ideal gas

Unit Prefix Table:

 Y     10^24      Yotta
 Z     10^21      Zetta
 E     10^18      Exa
 P     10^15      Peta
 T     10^12      Tera
 G     10^9       Giga
 M     10^6       Mega
 k K   10^3       Kilo
 h H   10^2       Hecto
 D     10^1       Deka
 d     10^-1      Deci
 c     10^-2      Centi
 m     10^-3      Milli
 u     10^-6      Micro
 n     10^-9      Nano
 p     10^-12     Pico
 f     10^-15     Femto
 a     10^-18     Atto
 z     10^-21     zepto
 y     10^-24     yocto

Url-retrieve and JSON API

Update 2012-10-25 Thu

Philipp Moeller posted a better version of the code in the comments. You might want to take a look.


This thing is everywhere. Perhaps one day it will be Clojure maps, which would be quite an improvement. But until then, we have to cope with it.

But what is it really?

An application programming interface (API) is a specification
intended to be used as an interface by software components to
communicate with each other.

Here is an example to help you wrap your head around it. Say I have an awesome website which offers you to scan a BACnet network and keep the data in a nice database (shameless plug https://bacnethelp.com). Not everyone want to go to the site to consult their data. Some want to be able to manipulate it as they wish, which is quite hard when the data is stuck in an html format. The solution is to offer some URLs intended only for machines. No pretty formatting, no page layout, no nothing. Simply the pure data (which, to programmers, is like pron).

JSON is the format in which this data is represented. You can find more on this website, but this is mostly irrelevant for this post, because we will discover that Emacs already has everything it needs!

Emacs can get data directly from URLs

Yes, I know, this is common knowledge… and yet when I researched the url-retrieve function recently, I found my blog in the top results, only because I mentioned it when I spoke about ELPA.

With this limited information, it is possible that some poor souls are missing on this great Emacs’ feature. Say you want to get some info from google.com… no, let’s strech it a little, https://google.ca (that’s right, with encrypted connection)! An easy way would be to do this:

(url-retrieve-synchronously "https://google.ca")

This function will go to the given URL, retrieve the data and stuff it in a buffer; in this case #<buffer *http http://www.google.ca:443*>. Of course, we rarely want to leave this buffer alone, we need this data. To catch the returned buffer, simply use a let function

  (let ((buffer (url-retrieve-synchronously url))

Now as long as we remain in this let function, we can get the google buffer with the symbol buffer. Let’s skip some steps and go to the interesting part:

(defun get-json (url)
  (let ((buffer (url-retrieve-synchronously url))
        (json nil))
      (set-buffer buffer)
      (goto-char (point-min))
      (re-search-forward "^$" nil 'move)
      (setq json (buffer-substring-no-properties (point) (point-max)))
      (kill-buffer (current-buffer)))

This function will return any content from a given url.

Ok, we have the JSON formatted data, what can we do with it? Of course, Emacs already has a JSON parser. Simply make sure you require ‘json before going any further.

(defun get-and-parse-json (url)
  (let ((json-object-type 'plist))
     (get-json url))))

Here I’ve choosen to parse it as a PLIST, be it could as well be an ALIST, or even an hash-table!

And because we are in Emacs, we loooove lists. Here is a little function from my bhelp.el to convert any vectors into lists:

(defun bhelp-listify (arg)
  "Convert any remaining vector into a list"
  (mapcar (lambda (x) (if (or (vectorp x) (listp x))
                          (bhelp-listify x)
                        x)) arg))

That’s it! All the tools required to get data from a JSON API from inside Emacs! You can look to the source code of bhelp.el if you need to see how these functions are used in a real live application, but there really isn’t much more. Getting the data from within Emacs is really straightforward.

Another shamless plug: I use this to get info from https://bacnethelp.com. I can then use the data in various Emacs mode (such as Org-mode) and compare the BACnet networks with the initial designs. I can also make customized reports to send to the client, or the IT department (they might want to make sure we didn’t take any IP address).

Emacs make me feel like a have a humongous reproductive instrument.

Copying multiple files with different filenames (with Emacs)

Situation: you have to make multiple copies of a file, but with a precise and different name each time.

And to be sure you don’t try to make your smart ass by right clicking, copying, pasting and renaming, let’s say you have a hundred of them. How to do it? Well Emacs of course! In fact my example is so constrained, that I will even add that the list of precise names is already in Emacs! (It’s actually what happened to me…)

(defun copy-file-multiple-output (original-file awesome-filename-list &optional destination-path)
  (let ((path (or destination-path (file-name-directory original-file)))
        (extension (file-name-extension original-file)))
    (dolist (current-name awesome-filename-list)
      (copy-file (concat original-file) (concat path current-name "." extension)))))

That’s it! 5 lines of code to save tens of minutes of work, which was boring and prone to human error!

The following line will take my test.txt file and create 3 copies of it, named: abc.txt, def.txt and ghi.txt

(copy-file-multiple-output "/home/frozenlock/test.txt" '("abc" "def" "ghi"))

This could probably have been done in Powershell, Bash or any other scripting tool, but… well… I live in Emacs!

Sometimes I wonder how normal people can get things done.

Back to the Future with Emacs and Undo Tree

Undo is broken almost everywhere

It really is. Forget everything you know about software. When was the first time you used one? You probably took the mouse or the keyboard and were amazed by how the machine obeyed to your most incredible wishes. Want to move the mouse to the upper right? Zooooom! Want to open a directory? Click! And when you learned about undo! Oh I remember! It was almost the best feature of a computer. You know when you draw or paint something, you need to know where to stop, otherwise you may end up ruining your chef d’oeuvre. No more with a computer! Want to try a little tree in this corner? Go on! You can UNDO! https://frozenlock.files.wordpress.com/2012/04/wpid-636x460design_01.jpg

But wait, what if I want to redo? So let’s say you undo 3 times, change a word, then realize you shouldn’t have changed it in the first place. The horror! For most software, when you make any action after an undo, it’s goodbye data! But why? For any new computer user, this is surprising. “So you mean I can access X number of previous states, but only if I don’t undo something? Then my history will have gaps in it?” If you follow the principle of least astonishment1, this is bad! Really really bad! Most users now try to patch this horrible design by saving more often. How often did you do this yourself? “Oh I want to try X, I should save now so I could come back to this state later.” No! Nononono! Computers are tools! They should do what you want and what you expect!

Undoing the undo

For Emacs, undo is like any other action. Now think about it. If you can undo an action, then you can also undo an undo. You could also undo the undo of an undo. This quickly becomes confusing, in particular when you throw some changes in the middle of all this. This is confusing, but very, very powerful. Keep in mind that Emacs now has the history of every action you’ve made. Want to return to a previous state? Turn back the time machine…

Back to the future

Here is where Emacs really shines. When you undo X number of times and make a change, you can still return to any previous states, even those forbidden by all the other ill designed softwares. See it as alternate timelines. Each time you make a different action at the same “time”, you create an alternate history. Don’t like the result? Jump back in your Delorean… eh I mean Emacs… and go to your favorite branch. There’s a terrific mode for using this abstraction: Undo Tree. In addition, you can save all your undos and load them back when you open the file again. How wonderful is that!?! Here is a little Undo Tree from the redaction of this very article. https://frozenlock.files.wordpress.com/2012/04/wpid-undo-tree.png

This is why Emacs seems weird when you first use it: because it behaves like you expect it to!

Clojure on Windows 7

The installation of Clojure on Windows is quite an accomplishement in itself. The following steps are those I finally isolated from multiple forums, blogs and painful discoveries. It’s mostly a note for myself.


https://frozenlock.files.wordpress.com/2012/03/wpid-leiningen-banner.png Grab the last stable version of Leiningen’s .bat file. If you don’t have Wget installed on your machine, install it or put the binary (wget.exe) in the same folder as lein.bat. In this directory, open the command window and type lein self-install. This will install leiningen-1.x.x-standalone.jar on your machine. It will probably be in your user directory, depending of your configurations.

Environment variables and the PATH

Copy the path to your new lein.bat. Then type <win><pause> (windows key and the key to the right of screen lock). Now go in Advanced system setting, Environment Variables. Find the PATH variable and click edit. Now type a semicolon and paste the path to your lein.bat file. This will make sure you can call Leiningen from anywhere in your command line with the simple “lein”. You should try it now, just to make sure everything is ok.


At this point you have a working installation of Clojure. But surely you want more, don’t you?

Emacs (SLIME)

Assuming you want to use Clojure in its most natural environment, you should use Emacs (yay!).

Install clojure-mode from git or Marmalade.


Update 2012-03-07 mer.: Be sure to restart Emacs if you changed the environment variables in the previous step!


Swank Clojure is a server that allows SLIME (the Superior Lisp Interaction Mode for Emacs) to connect to Clojure projects.

To install Swank-clojure, open once again the command line and type

lein plugin install swank-clojure 1.4.0

(Where 1.4.0 is the version you want to install).

If Clojars.org isn’t blocked, (which it is, at my office, God only knows why…) you should see the plugin being downloaded and installed. That’s it.

No, really, that’s it!

Beginners’ traps

Before you try jack-in, you should make a project as described in Leiningen instructions. The most important part before trying anything else is to have a .clj ready. You must have this file opened in Emacs before you do the magic M-x clojure-jack-in!!

If you get an error message complaining about “$SHELL”, type M-x customize-variable RET clojure-swank-command RET and enter “lein jack-in %s” instead of the current value.

How to toggle minor modes in Emacs 24

There is a little underdocumented change in Emacs 24 about minor modes toggling. (To be fair, it still isn’t the official release.)

One of the beauty of Emacs is how easy it is to bind it to your will. Say I want to have flyspell-mode and auto-fill-mode activated when I write for this blog. I could M-x each of them… or I can bind <f7> to a lambda function, composed of a call to every mode I need. Here is a little example I use for my daily work:

(global-set-key (kbd "<f7>") ; F7 - Activate auto-fill-mode, flyspell-mode and abbrev-mode
                   (message "Toggle Fill Flyspell Abbrev")))

Bam! 3 minor modes activated instantly! Unfortunately, this doesn’t work for Emacs 24. The reason is a little change in how the modes can be activated. In all previous version, this was the normal behavior:

  • Argument NIL -> toggle
  • Argument 1 -> On
  • Argument -1 -> Off

For example, (auto-fill-mode 1) would have activated the mode no matter what.

The new behavior is quite different and might break some of your code, so you should check into it. Here is what changed:

  • (When called from Lisp) Argument nil -> On

See what they did there? Now you can’t toggle in a program like you would if called interactively (which is to say, with M-x).


Here is what I got after asking to the Emacs developers’ mailing list:

  1. (foo-mode (if foo-mode -1 1))
  2. (foo-mode ‘toggle)

I would be careful with the solution #1, because the variable foo-mode might not have the same name as the function foo-mode.

My recommendation is to use #2.

Now my code looks like this:

(global-set-key (kbd "<f7>") ; F7 - Activate auto-fill-mode, flyspell-mode and abbrev-mode
                   (auto-fill-mode 'toggle)
                   (flyspell-mode 'toggle)
                   (abbrev-mode 'toggle)
                   (message "Toggle Fill Flyspell Abbrev")))