Really Simple Scrivener Mode

Here’s a screenshot of a really simple Scrivener type view for org mode files. I set this up following my simple sidebar set up.

Scriv Sample

I’ve copied the code below (I’ve also joined the 21st Century and started uploading code to GitHub)

The code is actually very simple. To make notes appear in a side window, simply put the letters TR (for top right), BR (for bottom right) or HD (for heading) at the start of a title and then call org-tree-to-indirect-buffer on those headings. I’ve added a key binding to M-s i to make this easy. I like to have certain notes always visible while I’m typing, this system allows me to vary just which notes they are.

And that’s it. Very simple, but I’ve found this very useful when writing.

(defun my-sidebars()
  (setq fit-window-to-buffer-horizontally t)
  (setq window-resize-pixelwise t)

  (setq display-buffer-alist
        `(("\\*Occur\\*" display-buffer-in-side-window
           (side . left) (slot . 0)
           (window-width . fit-window-to-buffer)
           (preserve-size . (t . nil)) 
           (window-parameters . ((no-delete-other-windows . t))))
          (".*\\.org-HD." display-buffer-in-side-window
           (side . top) (slot . 0) 
           (preserve-size . (t . nil)) 
           (window-parameters . ((no-delete-other-windows . t))))
          (".*\\.org-TR." display-buffer-in-side-window
           (side . right) (slot . -1) 
           (preserve-size . (t . nil)) 
           (window-parameters . ((no-delete-other-windows . t))))
          (".*\\.org-BR." display-buffer-in-side-window
           (side . right) (slot . 1) 
           (preserve-size . (t . nil)) 
           (window-parameters . ((no-delete-other-windows . t)))))))

(defun my-indirect-buffer ()
  (interactive)
  (let ((current-prefix-arg 4))                       ;; emulate C-u
    (call-interactively 'org-tree-to-indirect-buffer)))

(defun scriv()
  (interactive)
  (when (require 'wc-mode nil t)
    (wc-mode))
  (toggle-frame-maximized)
  (my-sidebars)
  (global-set-key (kbd "M-s i") 'my-indirect-buffer))

Sidebar for Emacs Org Mode

It would be nice to have a sidebar when using org mode. The sidebar would display the headlines of an org file. When a headline is selected, the subheadings and text would be displayed in another buffer. 

You can currently do this by using C-c C-x b which is bound to (org-tree-to-indirect-buffer). The command opens a subtree in an indirect buffer which is sort of what I’m looking for, but you have to enter the command each time you land on a new headline.

You can make the process automatic by adding a hook as follows:

(add-hook 'post-command-hook #'org-tree-to-indirect-buffer nil :local)

The solution works, but it’s not quite there.

Searching the internet I found a useful suggestion from the delightfully named My Other Soup’s a Borscht:

M-x occur then search for the regexp "*+ " (note the space at the end)

This gives more of the functionality I want and has the advantage of being customizable. One problem: I wanted the sidebar to appear on the left hand side.

So I looked a little further and discovered side windows

My first thought was so what? I can already do that by splitting windows. The advantage of side windows is that you can set them to stay in position and to fix the buffer they display. No more losing your layout when you hit C-x 1.

If the above seems a little confusing (and it did to me at first) there’s an example of what you can do here in the Emacs Manual 

So I combined the two things I’d learned and came up with the following function:

(defun my-sidebar-occur()
 (interactive)
 (setq fit-window-to-buffer-horizontally t)
        (setq window-resize-pixelwise t)

        (setq
       display-buffer-alist
       `(("\\*Occur\\*" display-buffer-in-side-window
          (side . left) (slot . 0) (window-width . fit-window-to-buffer)
          (preserve-size . (t . nil)) 
          (window-parameters . ((no-delete-other-windows . t)))))))

Here’s a video of the process in action.

One last thing. org-sidebar appears to solve my problem, but at the time of writing it’s still a little buggy. It’s nicely done though and could well become the standard in the future. Until then, I’ll use my workaround.