Category Archives: emacs

Using Emacs Org Mode tables to calculate doses to buy

I got tired of manually calculating how many I needed to buy based on a daily protocol and how many I had in stock, so I wrote a little bit of Emacs Lisp to figure it out. You can specify the type, daily dose, start and end dates (inclusive; defaults to the last specified date if blank), and how many you have in stock.

First, define a table of this form, and give it a name.

#+NAME: input
| Type         | Per day |      Start |        End | Stock |
|--------------+---------+------------+------------+-------|
| Medication A |       2 | 2015-06-09 | 2015-06-16 |     5 |
| Medication B |       1 |            |            |     0 |
| Medication C |     0.1 | 2015-06-12 | 2015-06-16 |   0.2 |
Type Per day Start End Stock
Medication A 2 2015-06-09 2015-06-16 5
Medication B 1 0
Medication C 0.1 2015-06-12 2015-06-16 0.2

To call the code from the bottom of this post, use something like:

#+CALL: calculate-meds-needed(meds=input) :hlines yes :colnames yes
Type Total In stock Needed
Medication A 16 5 11
Medication B 8 0 8
Medication C 0.5 0.2 1

Here’s the code that processes it:

#+name: calculate-meds-needed :var meds=meds :colnames yes :hlines yes
#+begin_src emacs-lisp
(let (start end)
  (append
   (list (list "Type" "Total" "In stock" "Needed"))
   (list 'hline)
   (sort (delq nil (mapcar
                    (lambda (row)
                      (unless (or (eq row 'hline) (string= (elt row 0) "Type"))
                        (let (total)
                          (setq start (if (string< "" (elt row 3)) (elt row 3) start)
                                end (if (string< "" (elt row 2)) (elt row 2) end)
                                total (* (elt row 1)
                                         (- (calendar-absolute-from-gregorian (org-date-to-gregorian start))
                                            (calendar-absolute-from-gregorian (org-date-to-gregorian end))
                                            -1)))
                          (list
                           (elt row 0)
                           total
                           (elt row 4)
                           (max 0 (ceiling (- total (elt row 4))))))))
                    meds)) (lambda (a b) (string< (car a) (car b))))))
#+end_src

Adding calculations based on time to the Org Agenda clock report

Duplicating this answer on my blog in case StackOverflow goes away. =)

Leo asked:

I’m trying to make the Agenda Clockreport show how many pomodoros I’ve invested in a task. A Pomodoro is 25 minutes. For example, 1:15 hours of work is 3 pomodoros.

I’m trying to customize org-agenda-clockreport-paramater-plist, and I would like to extract “Time” and convert it to a pomodoro. I.e., (time in minutes / 25) = pomodoro.

I wrote:

This will create a column in your clocktable report that sums the hours from columns 3 and 4, and then another column that shows you the round number of pomodoros that took up.

(setq org-agenda-clockreport-parameter-plist
      '(:link t :maxlevel 2 :formula "$5=$3+$4;t::$6=ceil($5*60/25);N"))

If you don’t want in-between columns, here’s a totally hackish approach:

(defun my/org-minutes-to-clocksum-string (m)
  "Format number of minutes as a clocksum string.
Shows the number of 25-minute pomodoros."
  (format "%dp" (ceiling (/ m 25))))
(fset 'org-minutes-to-clocksum-string 'my/org-minutes-to-clocksum-string)

Alternatively, you can use :formatter, but the formatting function looks very long and annoying to change.

Leo eventually configured it with:

(setq org-agenda-clockreport-parameter-plist
 '(:fileskip0 t :link t :maxlevel 2 :formula "$5=($3+$4)*(60/25);t"))

(He didn’t mind the decimals, I guess! =) )

2015-05-13 Emacs Hangout

Console Emacs vs GUI Emacs, keybindings, Org Mode, cooking, nyan, window management, calendars, SuperCollider Usual disclaimer: times are approximate, and the note-taker often gets distracted. =)

  • 0:00:00 Emacs configuration
  • 0:11:22 Console Emacs vs GUI Emacs? iTerm integration, mouse support, 256 colours, drop-down menus (although you can get a text one), …
  • 0:14:59 multihop TRAMP
  • 0:16:01 keybinding philosophies, Hyper and Super
  • 0:22:15 Remapping keys on Mac OS X (dealing with separate Alt and Meta)
  • 0:28:04 Org and mobile
  • 0:30:25 emulating hyper and super keys
  • 0:32:15 orgzly
  • 0:33:33 Org Mode and cooking, org-map-entries
  • 0:39:31 nyan
  • 0:43:04 One window, workgroups
  • 0:46:56 winner-mode
  • 0:53:30 rinari, zeus, ruby
  • 0:54:53 neotree
  • 0:58:22 keyboards
  • 1:03:24 conference
  • 1:09:22 calw; also, something about rainbow-mode, and palette, and then later Org Mode
  • 1:23:13 SuperCollider, Overtone, yasnippet
  • 1:45:13 blackink?

Text chat: Here’s the gif I have as my nyan http://vignette3.wikia.nocookie.net/donkeykong/images/9/91/Cranky_walk.gif/revision/latest?cb=20100930155227

Sahil Sinha 9:23 PM http://emacswiki.org/emacs/MetaKeyProblems
Jack G. 9:24 PM (setq mac-right-command-modifier ‘hyper)<br>(setq mac-right-option-modifier ‘super) http://ergoemacs.org/emacs/emacs_hyper_super_keys.html (global-set-key (kbd “H-h”) ‘er/expand-region
George Jones 9:32 PM https://github.com/heikkil/org-dropbox
Jack G. 9:36 PM nyan Cranky_walk.gif
Jack G. 9:42 PM
me 9:42 PM http://www.emacswiki.org/emacs/OneWindow ?
Daniel H 9:46 PM https://github.com/tlh/workgroups.el
me 9:48 PM http://askubuntu.com/questions/4820/keeping-emacs-from-splitting-the-window-when-openning-multiple-files winner-mode
George Jones 9:59 PM http://ergoemacs.org/emacs/emacs_best_keyboard.html
George Jones 9:59 PM Xah Lee writes a LOT about keyboards
Jack G. 10:02 PM http://www.razerzone.com/ca-en/store/razer-anansi
Bogdan Popa 10:10 PM https://github.com/kiwanami/emacs-calfw#for-ical-google-calendar-users
me 10:11 PM org-gcal
Daniel H 10:12 PM https://github.com/kiwanami/emacs-calfw
George Jones 10:12 PM having real trouble hearing…
George Jones 10:20 PM when you open a PDF in docview you can get the text with ^C^T (default bindings)
Jack G. 10:21 PM Thanks George!
George Jones 10:21 PM C-c C-t runs the command doc-view-open-text
me 10:27 PM http://doc.norang.ca/org-mode.html
sai tejaa Cluri 10:27 PM hi
Jack G. 10:37 PM link
me 10:37 PM This was a fun demo of Org Mode and SuperCollider http://pages.sachachua.com/emacs-chats/chat-iannis-zannos.html
Levi Strope 10:40 PM Jack your audio is crystal clear now… whatever that change was
Jack G. 10:45 PM http://blackink.bleank.com/
me 10:48 PM http://emacslife.com/baby-steps-org.html

2015-04-30 Emacs Hangout – hosted by Philip Stark

Thanks to Philip Stark for organizing an Emacs Hangout that’s more conducive to European timezones! Here’s the video and the notes.

You can add more comments on the event page. For more about upcoming Hangouts, check out our Google+ page.

Show notes (times might need a little adjustment):

  • 0:00:03 Introductions!
  • 0:03:03 A couple of Emacs semi-newbies =)
  • 0:03:52 Java and C# language support (autocomplete, refactoring, etc.); bridging the gap between Emacs and the runtime (Unity, Android, etc.). Batch mode for the latter. OmniSharp actually went pretty darn well this time around!
  • 0:06:10 OmniSharp demo
  • 0:06:13 https://github.com/MmmCurry/.emacs.d/
  • 0:07:23 OmniSharp + company config, demo of completion. Includes API. Jump to definition as well.
  • 0:10:59 Cool refactoring stuff. Ex: intelligent rename. Watch out for bugs. Still neat!
  • 0:12:30 MS Visual Studio Code seems to run on the same backend =)
  • 0:13:18 OmniSharp background info
  • 0:14:53 New participant, working out the tech issues
  • 0:16:28 Java? Haven’t looked into it much yet, lower priority. Pain point: Eclipse project build chain. eclim? May give it a second chance.
  • 0:19:37 Wishlist: batch mode Unity for headless testing?
  • 0:20:05 Emacs and Python – working through the Google Code Jam problems. C-c C-c to execute code in the REPL, so much fun. Suggestion: org-babel blocks? =)
  • 0:21:37 Discussion about Scala and Ensime. Ooh, Ensime does Java too. Neat!
  • 0:22:49 New to Emacs Lisp. Discovering things and implementing them – good enough, but not well-polished. Writing. Helm, etc. So many things to learn! Balancing studying the Emacs Lisp intro and manual, and discovering things day to day.
  • 0:25:00 Separate Lisp file loading for experimental stuff.
  • 0:25:30 Woodnotes guide? http://therandymon.com/woodnotes/emacs-for-writers/emacs-for-writers.html
  • 0:26:32 Emacs StackExchange, Reddit.com/r/emacs, Planet Emacsen
  • 0:29:17 Spacemacs, packaged defaults. Learning with index cards. Learning curve. Emacs community is obsessed with documentation. Phenomenal! =)
  • 0:34:23 Documented conventions, nicely-designed keybindings etc. for Spacemacs
  • 0:35:50 Spacemacs setup asks you which tradition you want to follow
  • 0:36:46 nerdtree replacement – neotree
  • 0:37:27 Goal is to not rely on Spacemacs, but for it to be a stepping-stone / scaffold
  • 0:38:30 Differences between Linux window managers; simplified workflows
  • 0:40:24 Looking at configuration frameworks piecemeal, learning workflows
  • 0:43:05 Discoverability is a big issue. helm-c-yasnippet has helm-yas-complete, helm-yas-create-snippet-on-region . Can be configured to display the keys. (setq helm-yas-display-key-on-candidate t) Has additional actions if you TAB.
  • 0:50:24 Hydra demo. Ex: moving lines up and down. Hydra for Helm?
  • 0:57:25 Lispy-mnemonic
  • 1:02:58 Usability
  • 1:05:30 Lispy-mnemonic workflow – minor mode
  • 1:06:15 back-to-indentation and restoring the binding in Lispy
  • 1:07:36 org-timer and meeting notes
  • 1:08:14 Make timestamps better! =)
  • 1:10:53 Cognitive overhead of new IDEs. Ex: SublimeText C-d marks a thing (Emacs equivalent: expand-region)? More organic, flexible commands versus specific ones, staying within your mental model.
  • 1:13:00 multiple-cursors, transpose-chars versus backward-kill-word.
  • 1:17:30 helm-swoop
  • 1:21:06 micro-optimizations, command-log-mode, keyfreq, mc/mark-all-like-this(-dwim), guru-mode
  • 1:26:06 Dealing with Eclipse wizards, things that shift you out of your mental model. Discussion of Helm and Ido. Also, helm-show-kill-ring.
  • 1:31:59 Hydra and leader keys.
  • 1:32:31 Dan’s intro. Figuring out workflow. Export Org Mode to HTML. Yasnippet for HTML5 declarations? Org Mode publishing project support (org) Publishing options
  • 1:37:20 Magit, git-timemachine, git-gutter-fringe, git-wip (for committing work in progress each time you save; https://github.com/bartman/git-wip), git-wip-timemachine (forked git-wip, https://github.com/itsjeyd/git-wip-timemachine)
  • 1:41:44 undo-tree
  • 1:42:57 git-messenger
  • 1:43:48 C-x v g, vc-annotate, colour-coding
  • 1:45:13 Emacs load times, profiling
  • 1:47:59 markdown and flycheck not finding an external command, checking the *Messages* buffer

Text chat:

M. Ian Graham 2:06 PM https://github.com/MmmCurry/.emacs.d/blob/master/lisp/init-csharp.el
M. Ian Graham 2:14 PM https://github.com/OmniSharp/omnisharp-server https://github.com/OmniSharp/omnisharp-emacs
Tim K 2:15 PM should be ok
Tim K 2:15 PM maybe someone has to unmute me
M. Ian Graham 2:15 PM https://code.visualstudio.com/
Tim K 2:15 PM i’ll just keep lurking for now then
me 2:18 PM https://github.com/senny/emacs-eclim
Tim K 2:19 PM tangentially related: ENSIME I used it for developing a web play framework project
M. Ian Graham 2:20 PM Ooo, scala goodness https://github.com/ensime/ensime-server
Tim K 2:21 PM yeah it targets scala BUT it works for java as well !!
Philip Stark 2:23 PM Excellent.. Thank you Tim !
M. Ian Graham 2:25 PM http://therandymon.com/woodnotes/emacs-for-writers/emacs-for-writers.html
Tim K 2:25 PM @Will: Are you on Emacs.SE?
Philip Stark 2:26 PM right?
Tim K 2:26 PM yes There’s lots of good content for non-programmers there
Philip Stark 2:26 PM cool. I gotta check that out.
me 2:27 PM Yakshaving: http://sachachua.com/blog/2015/03/the-balance-between-doing-and-improving-evaluating-yak-shaving/
Tim K 2:32 PM For people who know their way around some of the starter kits: You could definitely score some points answering questions on Emacs.SE. My impression is that there usually aren’t that many people around who can answer these types of questions.
me 2:32 PM Good point!
Will Monroe 2:33 PM Thanks, Tim. That sounds like a good place for someone like me to start.
Tim K 2:33 PM Prelude is probably the one you’re thinking of
M. Ian Graham 2:45 PM https://github.com/emacs-helm/helm-c-yasnippeta https://github.com/emacs-helm/helm-c-yasnippet
me 2:47 PM (setq helm-yas-display-key-on-candidate t)
Will Monroe 2:58 PM Hey everyone, I’ve really enjoyed listening to and talking with each of you. Have to go. See you all next time!
Tim K 3:03 PM Bye Will!
me 3:04 PM For the text chat: https://github.com/itsjeyd/lispy-mnemonic You might like https://github.com/magnars/expand-region.el
Tim K 3:12 PM Also: multiple cursors
me 3:23 PM keyfreq?
Tim K 3:25 PM guru-mode ?
me 3:41 PM https://github.com/bartman/git-wip
Tim K 3:41 PM https://github.com/itsjeyd/git-wip-timemachine
me 3:43 PM https://github.com/syohex/emacs-git-messenger
Philip Stark 3:43 PM https://github.com/syohex/emacs-git-messenger ah thx

Thanks, everyone!

Emacs Hangout 2015-04-15 show notes

We chatted about packages, packaging, databases, and the upcoming Emacs conference.

Event page: https://plus.google.com/events/c3igul0rj6cfc1qbcar91lrcj0s

What’s this Emacs Hangout thing about? This is an informal way for Emacs geeks to get together and swap tips/notes/questions. You can find the previous Hangouts or sign up for the mailing list at http://sachachua.com/blog/tag/emacs-hangout/ .

Upcoming Emacs Hangouts:

Emacs Lisp Development Tips with John Wiegley
April 28, 2015 Tue 4:00 PM Toronto time (8 PM GMT)

Emacs Hangout
April 30, 2015, at 2 PM Toronto time (6 PM GMT, 8 PM CET):

Want to find out about upcoming hangouts? You can sign up for notifications at http://eepurl.com/bbi-Ir or follow the Emacs conferences and hangouts page.

Timestamps are approximate since I was a little distracted. =)

  • 0:25:14 8:26 PM whoops, forgot to keep track of stuff. Before this: conversation about Unity/C#/etc. in Emacs; Spacemacs
  • 0:26:33 Howard demonstrates literate database queries with a remote SQL server. SSH tunnel + –protocol=tcp
  • 0:28:55 Naming the results of queries and then using them in a different calculation
  • 0:30:39 Hmm, might be a good technique for exploring old databases
  • 0:31:13 sx.el – StackExchange client
  • 0:34:16 howdoi-emacs
  • 0:34:57 sx setup, sx-tab-all-questions
  • 0:35:59 hydra sample use cases: opening files and directories, changing the quotation of strings, mode switching
  • 0:37:30 clone indirect buffers
  • 0:39:35 Cask
  • 0:43:27 use-package
  • 0:44:57 paradox
  • 0:49:11 packages
  • 0:56:02 Emacs conference stuff
  • 1:05:11 weird Emacs things =)
  • 1:07:43 Next up: literate config (Dylan, Sean)
  • 1:13:19 wrapped up

Text chat and links:

me 8:08 PM By the way, we can use this text chat as a backchannel. After the chat, I’ll copy it and share it with the show notes so that other people can grab links.
Howard Abrams 8:14 PM Here is my investigation of my save hooks in case you can kick off some sort of script: http://howardism.org/Technical/Emacs/save-hooks.html
me 8:15 PM External to Emacs, but possibly interesting: https://github.com/infospace/guard-java
Dylan Thiedeke 8:16 PM You said there was a ruby-guard? I will have to look into that for authoring and editing cookbooks and recipes for use with Chef
me 8:17 PM https://github.com/guard/guard ?
Dylan Thiedeke 8:18 PM Awesome! Will definitely be looking at that thanks Sacha
Swaroop C H 8:20 PM csharp layer – https://github.com/syl20bnr/spacemacs/blob/064a598bff56f7cef1ac2ddf1c43684357dde56a/contrib/lang/csharp/README.md ?
M. Ian Graham 8:21 PM Nice link Swaroop, I’ll see if I can pull it in
me 8:25 PM http://emacswiki.org/emacs/GnuGlobal
Swaroop C H 8:26 PM https://github.com/syl20bnr/spacemacs/pull/1169
Sod Oscarfono 8:32 PM +1 for being a bassist!
M. Ian Graham 8:32 PM https://github.com/vermiculus/sx.el
Howard Abrams 8:32 PM Here is the blog post about the literate database work: http://howardism.org/Technical/Emacs/literate-database.html
me 8:34 PM https://github.com/arthurnn/howdoi-emacs
Zachary Kanfer 8:48 PM There’s an Emacs song: https://www.gnu.org/fun/jokes/welcome-to-gnu-emacs.html and http://www.emacswiki.org/emacs/LyricMode
Samer Masterson 8:53 PM gimmie a sec, getting headphones
me 8:56 PM Question from a viewer: Why would someone use both Cask and use-package at the same time https://raw.githubusercontent.com/rejeep/emacs/master/init.el ?
Sod Oscarfono 8:57 PM i’m an ex-event manager… i’d be keen on helping get one happening in oceania
me 8:57 PM Oooooh
Sod Oscarfono 8:57 PM nz or aus maybe
Samer Masterson 8:57 PM https://goo.gl/forms/tv0sDuApd8
Dylan Thiedeke 8:57 PM Sod I’m in AU. Not qualified enough to present but would help out if I could
Sod Oscarfono 8:59 PM thanks Dylan. perfect/ any ideas on a rough idea of numbers of emacs users globally? by region? hard to quantify i realise but are we talking hundreds, thousands, hundreds of thousands do you think? keen as Samer… i’m nowhere near as proficient with emacs, or programming as most here but i have many other skills.. event management, audio engineer, graphic design and close connection with large format commercial printer
Dylan Thiedeke 9:03 PM Sod in Australia I couldn’t even imagine a number. Maybe poll the #emacs IRC channel on friend and the emacs group on G+
me 9:03 PM Sod: Woohoo! Awesomeness.
Dylan Thiedeke 9:04 PM Sod I’m not a programmer either but use emacs for documentations and starting to use it for project management with org-mode etc
Sod Oscarfono 9:04 PM community is the magic word when talking to me
Samer Masterson 9:04 PM emacsconf.github.io/emacsconf2015
Sod Oscarfono 9:05 PM feel free to add me Dylan we can fire some ideas back and forth. maybe a poll of interest in a local conf or meetup
me 9:06 PM Sod, Dylan: Neato!
Howard Abrams 9:07 PM I’m sorry, but I have to leave as well. Thanks for the fun and I will listen to the rest later.

2015-04-08 Emacs Lisp Development Tips with John Wiegley

You can find John Wiegley on Twitter (@jwiegley) or at http://newartisans.com/.

0:00:00 Paredit mode. Start with it from day 1! Matching pairs of parentheses, won’t let you delete one without the other. Inserts appropriate newlines, too
0:03:56 Emacs as a Lisp environment. (Also, Helm is what’s responsible for the display.) Evaluating a function makes it available in the global scope, which has all these functions and commands you can do. This makes it easy to iteratively develop your functions, because you can just execute things directly.
0:05:08 Without (interactive), you can’t call functions with M-x. You can use M-: or put the call in your scratch buffer.
0:06:00 command-log-mode
0:06:47 pp-eval-last-sexp. Check out http://github.com/jwiegley/dot-emacs for other config things
0:07:14 debugging. e to evaluate within the current context. Also, stepping, quit.
0:08:09 Edebug with C-u C-M-x. Interactive debugging. SPC moves you forward, one Lisp form at a time. It shows you results in the minibuffer. You can descend into Lisp forms or go into functions. ? shows keybindings. Check out the Emacs Lisp chapter on EDebug, highly recommendeg.
0:09:25 You can also use the (debug) form to go to the debugger.
0:10:26 eldoc: Seeing arguments in the minibuffer as you type, because no one remembers all the arguments anyway. eldoc-mode, or add (turn-on-eldoc-mode) to your config.
0:11:30 What functions should you call in the first place? What concepts? Emacs predates many standard terms, so that’s why things are a little confusing. Ex: “frames” and “windows” are not what you might think they are. OS window = frame. Area within Emacs = window. Opposite of HTML. Use the Emacs tutorial C-h t.
0:13:04 Read the Emacs Lisp intro, which you can get to with C-h i (which lists the manuals that are available). Read the Emacs Lisp manual too.
0:14:03 Other weird terms: point, mark, marker. (point) vs (point-marker).
0:15:35 C-h f (describe-function) shows the help for the function. Nearly all functions you’ll probably call are documented well. Lots of options. Check out C-h f for interactive, for example.
0:17:17 C-h v (describe-variable).
0:17:46 More in-depth documentation: C-h i, go to the Emacs Lisp manual, then use i to view the index.
0:18:22 info-lookmore shows you the Info documentation for the symbol under point. Works for other Lisps too (ex: Common Lisp)
0:19:46 Sanity-checking paired parentheses with M-x check-parens. Handy for adding to your after-save-hook in Emacs Lisp mode.
0:20:40 Paredit editing capabilities. Ex: C-k kills the current sexp. paredit-raise-sexp replaces the parent sexp with the following sexp. slurping and barfing. Barfing – spitting out an element from the list form. C-{ or C-} (with suggested keybindings). C-( and C-) are slurping, which pulls forms in. Works for strings, too.
0:22:38 Maximum barfage and slurpage. Useful for slurping everything in, for example. paredit-slurp-all-the-way-forward.
0:24:13 redshank (companion to paredit) for refactoring. Ex: redshank-condify-form converts an if to a cond for when you realize you’ve got more than two conditions.
0:25:05 M-1 M-( surround the next one thing with parens
0:25:25 redshank: wrap a let, change if to a when, etc.
0:25:52 C-h k (describe-key) shows what a keyboard shortcut or menu item will do.
0:27:26 Took a while to get used to paredit, but you eventually get into the zen of paredit.
0:27:54 Linter – M-x elint-current-buffer. Loads every module that your code depends on (so the first time is slow), and then shows you style notes.
0:28:50 C-q for manually inserting parentheses
0:29:10 Helm, which shows you all the other stuff that matches your query. Lets you select by regex, multiple patterns, etc. Much nicer and more interactive.
0:30:29 Profiler M-x elp-instrument-function, then call the function, then elp-results will show you the time it took to execute. Results aggregate, and are reset when you call elp-results.
0:32:30 Measuring memory consumption. Also, internal representation of lists. reverse vs. nreverse. Like nconc, nreverse, setcar, setcdr. This can greatly speed up your code, if you can avoid using the garbage collector. EmacsWiki – memory-use-counts, but not particularly helpful? Another package that extends the Emacs Lisp profiler? Avoid premature optimization.
0:38:55 elint and flycheck? flycheck’s designed for external processes, so that might be a challenge. Possibility: use async to spawn another Emacs? Doesn’t seem to be available yet.
0:40:40 ert
0:48:11 testcover, coveralls.io, undercover.el
0:48:13 Read Emacs Lisp manual, etc.
0:48:20 Creating a mode. You don’t have to make it from scartch – start by copying someone else, and then strip away everything you don’t want.
0:49:58 checkdoc – checks the style of your documentation strings.
0:51:30 defining a minor mode
0:56:08 when to define a major mode – structure of your buffer