A Few of my Org Mode Customizations

Org mode is already a part of my life. It’s only natural I’d like to perform various customizations to my liking. Fortunately customizability is exactly Emacs’ core philosophy which means I get to do it with freedom. The following are just a few of them which I think might be helpful to others.

Paste rich format text from web pages into Org mode

When I first started to use Org mode there was a glaring deficit to me, which is the inability to paste any formatted text from web pages. If everything pasted becomes plain, much of the original information would be lost, and this would be a great hindrance for anybody wanting to use Org mode exclusively. Luckily this is Emacs where everything is possible. After I posed the question on StackExchange, user @mankoff was quick to come up with a solution using perl and pandoc. Note that this version of code is tested on OS X, but for other systems the difference should only be the utility to call in order to retrieve text from system clipboard, which in this case is osascript.

(defun kdm/html2org-clipboard ()
  "Convert clipboard contents from HTML to Org and then paste (yank)."
  (interactive)
  (kill-new (shell-command-to-string "osascript -e 'the clipboard as \"HTML\"' | perl -ne 'print chr foreach unpack(\"C*\",pack(\"H*\",substr($_,11,-3)))' | pandoc -f html -t json | pandoc -f json -t org --no-wrap"))
  (yank))

There are actually a couple of points worthy of further attention. First is that pandoc automatically hard-wraps its output at 70 characters per line. I normally just soft-wrap my notes so I added the option --no-wrap command there.

Then note that pandoc automatically uses Non-breaking space instead of normal space when converting formatted inline text(bold, italics, code etc.), which is not recognized by orgmodeby default. Non-breaking space has to be added to org-emphasis-regexp-components in order for those texts to be formatted correctly in orgmode. The following is my code for that. There seems to be two space characters each line because non-breaking space looks exactly the same as normal space as rendered here, but Emacs recognizes it with a underline.

;; This one is for the beginning char
(setcar org-emphasis-regexp-components " \t('\" {")
;; This one is for the ending char.
(setcar (nthcdr 1 org-emphasis-regexp-components) "- \t.,: !?;'\")}\\")

With this function copy-and-pasting in Org mode supports rich format text as other note-taking apps.

Set non-monospace font for normal notes

Nobody likes to read normal notes in monospace. But the source code snippets better be aligned. Is there a way to achieve both? Definitely. The variable-pitch-mode in Emacs is built just for that.

  1. Remember to first set variable-pitch face in your configuration. Just M-x customize-face on variable-pitch would do. I use Helvetica Neue as I think it’s easiest on the eyes when you stare at it all day. It’s easy to change when you want. The following code was added by customize after I set the font:
'(variable-pitch ((t (:family "Helvetica Neue" :height 160))))
  1. Add hook to turn on variable-pitch-mode when the target major modes are enabled. Note I set org-code etc. to be fixed-pitch so that they’re still monospace.
  (defun set-buffer-variable-pitch ()
    (interactive)
    (variable-pitch-mode t)
    (setq line-spacing 3)
     (set-face-attribute 'org-table nil :inherit 'fixed-pitch)
     (set-face-attribute 'org-code nil :inherit 'fixed-pitch)
     (set-face-attribute 'org-block nil :inherit 'fixed-pitch)
     (set-face-attribute 'org-block-background nil :inherit 'fixed-pitch)
    )

  (add-hook 'org-mode-hook 'set-buffer-variable-pitch)
  (add-hook 'eww-mode-hook 'set-buffer-variable-pitch)
  (add-hook 'markdown-mode-hook 'set-buffer-variable-pitch)
  (add-hook 'Info-mode-hook 'set-buffer-variable-pitch)

There is a nice post offering a more detailed configuration and claiming to preserve more font attributes such as org table coloring. However for general purposes the simpler configuration should suffice.

Monospace together with normal font:

variable-pitch-screenshot

Hiding Text-Formatting Markers

This is probably more of a matter of personal taste but I don’t really like seeing text-formatting markers all around. Why display /text/ when you can just have text. Turns out there’s a nice option in Org mode: org-hide-emphasis-markers which helps exactly in that. Just M-x customize-variable and then set org-hide-emphasis-markers to t. I didn’t remember seeing this option when I was reading org manual and only stumbled upon it later, so I thought listing it out might help.

Inserting timestamp for each note.

One potentially useful feature of normal note-taking apps is the ability to have the notes timestamped automatically. In Org mode one possible way of doing so is to utilize org-insert-heading-hook. You can define a function insert-inactive-timestamp as

(defun insert-inactive-timestamp ()
(interactive)
(org-insert-time-stamp nil t t nil nil nil))

I am fine with having the timestamp in the header directly. So I just have

(add-hook 'org-insert-heading-hook 'insert-inactive-timestamp)

There’s another solution in an Org mode mail list discussion which inserts the timestamp into the body:

(defun bh/insert-heading-inactive-timestamp ()
  (save-excursion
    (org-return)
    (org-cycle)
    ('insert-inactive-timestamp)))

(add-hook 'org-insert-heading-hook 'bh/insert-heading-inactive-timestamp)

Automatic pull/push for MobileOrg

Traditionally there’s been open source projects called MobileOrg to bring org mode’s functionality to your smartphones. Both Android and iOS versions utilize the same interface to interact with Emacs, which requires manually “pushing” and “pulling” changes from Emacs. There are a few timer-interval based auto-sync solutions out there. However, as I normally don’t use my phone to take notes with my laptop on, just simply pulling on Emacs initialization and pushing on shutting down works surprisingly well,

(add-hook 'after-init-hook 'org-mobile-pull)
(add-hook 'kill-emacs-hook 'org-mobile-push)

Note: Recently there has been an Android app called Orgzly which directly syncs your .org files instead of going through the push/pull interface defined for MobileOrg. I find it more refined and actively developed than the previous MobileOrg Android. It’s not open-source, but currently it feels a better app and I’ll be using it for now.

Automatically start the next pomodoro with org-pomodoro

I was used to automatically starting the next pomodoro when using other apps, as you’ll often forget to do it manually when a break finishes. There was an issue in org-pomodoro which prevented this from being configured, but now it’s fixed and you can just use org-pomodoro-break-finished-hook to achieve it:

(add-hook 'org-pomodoro-break-finished-hook
  (lambda ()
    (interactive)
    (org-pomodoro '(25)))

I’m only starting to have fun with Org mode so I might find some other interesting customizations later and list them here.