Skip to content

Instantly share code, notes, and snippets.

@hhutch
Created November 6, 2014 05:16
Show Gist options
  • Save hhutch/7e2e6843cf051f6bccc9 to your computer and use it in GitHub Desktop.
Save hhutch/7e2e6843cf051f6bccc9 to your computer and use it in GitHub Desktop.

Org-Mode_Plus_Calc_Plus_Macro_Magic

Know thy tools, to know thyself

Emacs has some incredibly powerful tools by themselves, but combined you can have superpowers.

example

get the data

https://en.wikipedia.org/wiki/Marathon_world_record_progression

load in a web browser and simply highlight and copy one of the main tables

Prepare data for calculation

open a new buffer

C-x b testing

paste in the contents to this buffer

C-y

put buffer in org-mode

M-x org-mode RET

Select contents of buffer

C-x h

Convert to org-table

C-c |

Navigate with <tab> to the `Notes` Column and remove it. To delete column:

M-S-LEFT_ARROW

Use the same method to remove the `Source` and `Events/Place` columns

Add a new empty column at the end of the table

C-e
M-S-RIGHT_ARROW

Create Macro to convert time to seconds and put output in last column

Navigate to beginning of first data line

C-a

Start Recording a Macro

C-x (

Copy hours segment

TAB
C-space
C-s :
C-b
M-w

Enter calc mode and paste

C-x * c
C-y

multiply by 3600 to convert to seconds and quit calc

60 RET
60 RET
*
*
q

copy minutes segment, first move to first digit in minutes segment, and then follow similar procedure

C-f
C-space
C-s :
C-b
M-w

enter calc, paste, and multiply by 60 to get seconds

C-x * c
C-y
60 RET
*
q

copy seconds segment by use ‘move to end of table cell’

C-f
C-space
M-e
M-w

enter calc, and add up total

C-x * c
C-y
+
+
q

move to last column and past using calc paste

C-e
S-tab
C-x * y
tab

now move to beginning of line and save macro

C-a
C-x )

Test macro on new line

C-x e

Did it work?

now test running the macro on multiple lines

let’s do 5 lines

C-u 5 C-x e

Re-prepare the table

undo all of the calculations you’ve done so far

C-/

You want the last column to be empty. If your undo buffer runs out, just delete the values

Now run the macro on all lines of the table

Now select all the data lines of the table and run `count-words` I do this by starting at the first dataline and then:

C-space
M-S->
M-x count-words

You will get the number of lines output in the mini-buffer. My count is 49.

Navigate back to first data line

Now run the macro on all 49 lines

C-u 49 C-x e

PROFIT!

You just used Macros, ORG TABLE and Calc to do a very tedious task.

Practice this over again several times to absorb it. Being able to do this on the fly and knowing these commands is really practical.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment