## Whitespace Mode

I copy and paste text between lots of different applications. Whitespace mode makes it easy to see all the non printing characters that different apps use to format their notes: Evernote, for example, has a habit of insert non breaking spaces in its notes.

Unfortunately, Doom Emacs uses whitespace mode for tab indents only. In order to restore functionality, I had to read up on whitespace mode. Here are my notes, so you don’t have to do the same.

# Whitespace

Whitespace uses two ways to visualize blanks: Faces and Display Tables.

• Faces are used to highlight the background with a color. Whitespace uses font-lock to highlight blank characters. (FontLockMode, rather confusingly, is used for syntax highlighting.)

### 10.0.4 M-x consult-ripgrep

For a more flexible search try consult-ripgrep. It’s worth reading the documentation, but here’s a taste:

• #alpha beta Search for alpha and beta in any order.
• #alpha.*beta Search for alpha before beta.
• #$$alpha\|beta$$ Search for alpha or beta (Note Emacs syntax!)
• #word -- -C3 Search for word, include 3 lines as context
• #first#second Search for first, quick filter for second.

## Todos and Agenda Views

The following post is part of my new Emacs Writing Setup. You can find the complete setup here on GitHub: https://github.com/ballantony/emacs-writing

On my original Emacs Writing Set Up I had this many states:

(setq org-todo-keywords
(quote ((sequence "TODO(t!)"  "NEXT(n!)" "|" "DONE(d!)")
(sequence "REPEAT(r)"  "WAIT(w!)"  "|"  "PAUSED(p@/!)" "CANCELLED(c@/!)" )
(sequence "IDEA(i!)" "MAYBE(y!)" "STAGED(s!)" "WORKING(k!)" "|" "USED(u!/@)"))))

Now I only have three: TODO, IN PROGRESS and DONE

This is in line with my philosophy that productivity systems are great procrastinators. Thinking of new tagging systems and states for tasks is very absorbing. You can spend hours moving notes around and not doing any work.

Now I capture all my notes as TODOs, I change their state to IN PROGRESS and DONE as projects advance.

Calling org-agenda gives me a bird’s eye view of everything I’m working on. I can then filter down as appropriate.

For convenience, I wrote the following function to restrict the agenda to the current project. ou can see an example in my config.el file

(defun tb/agenda-restrict-this-project ()
"Restrict agenda to current project"
(interactive)
(let ((org-agenda-files (list (projectile-project-root))))
(org-agenda)))


I rely a lot on this function. When writing I hit SPC j p p (my keybinding: see my config.el file) to see the TODOs and IN PROGRESSes for the current project only.

You can read more in My Doom Emacs Writing Set Up

## Capturing and Refiling Notes

The following post is part of my new Emacs Writing Setup. You can find the complete setup here on GitHub: https://github.com/ballantony/emacs-writing

## Capturing Notes

Like any writer I’m always capturing ideas. I used to carry a notebook everywhere, now I capture ideas on my phone using either orgzly or Evernote.

When working in Emacs I use org-capture.

GTD means capturing ideas quickly. I used to have templates to capture to different locations, I realised that this was an unnecessary step. Now I either capture everything as a TODO, either directly to my gtd file, or directly to the story file I’m currently working on.

As org-capture requires you to select a template I wrote the following two functions. The first calls org-capture with the ’t’ template preselected, the second does the same but uses let* to change org-capture-templates to the current buffer for the current capture only.

(defun tb/capture ()
"Capture to do without options"
(interactive)
(org-capture nil "t"))

(defun tb/capture-to-this-buffer ()
"Capture note to this buffer"
(interactive)
(cond  ((not  (eq major-mode 'org-mode))
(message "Can't capture to non org-mode buffer"))
(t
(let* ((this-file buffer-file-name)
(org-capture-templates
(("t" "Todo" entry (file+headline ,this-file "Captured")
"** TODO %?"))))
(org-capture)))))


## 2. Refiling Notes

org-refile makes it easy to refile notes, particularly with a completion system like Vertico. On Doom Emacs this means hitting SPC m r r

## ibuffer changed my life

I wanted a quick way to delete all the buffers that can accumulate in an Emacs session. A quick search threw up this post by Martin Owen.

It turned out all I needed was ibuffer mode. ibuffer has a toggle command which selects all unselected buffers.

But that’s not all. ibuffer will group your buffers by type, just like in the featured image for this post. It also comes with a range of commands for filtering buffers. Here are my five favourite commands:

1. t to toggle files selected
2. / . to filter by extensions
3. / p to remove top level filter
4. * h Mark all help buffers
5. * s Mark all *special* buffers

and here’s my set up: I’ve basically just adapted Martin’s.

(global-set-key (kbd "C-x C-b") 'ibuffer) ; instead of buffer-list
(setq ibuffer-expert t) ; stop yes no prompt on delete

(setq ibuffer-saved-filter-groups
(quote (("default"
("dired" (mode . dired-mode))
("org" (mode . org-mode))
("magit" (name . "^magit"))
("planner" (or
(name . "^\\*Calendar\\*$") (name . "^\\*Org Agenda\\*"))) ("emacs" (or (name . "^\\*scratch\\*$")
(name . "^\\*Messages\\*\$")))))))

(lambda ()
(ibuffer-switch-to-saved-filter-groups "default")))


It took me about ten minutes to do all the above from start to finish. Ten minutes well spent, I say.

## Pattern Matching: pcase

Haskell allows pattern matching. The following function counts one, two or many objects

simpleCount 1 = "One"
simpleCount 2 = "Two"
simpleCount _ = "Many"

You can use pattern matching to set base cases in recursive functions.

factorial 0 = 1
factorial n = n * factorial (n-1)

Haskell also allows guards. This if statement checks if someone is old enough to drive in the UK

canDrive x = if x<18 then  "Too young to drive" else "Old enough to drive"

Here it is using guards:

canDrive x
| x<18 = "Too young to drive"       |
| otherwise = "Old enough to drive" |

## pcase

Emacs Lisp offers similar functionality with the pcase macro. It took me some time to understand the documentation, so here are few examples to get you going. They only scratch the surface, make sure you go back and read up properly afterwards.

(defun simple-count (x)
(pcase x
(1 "one")
(2 "two")
(_ "many")))

(mapcar #'simple-count '(1 2 5))
=> ("one" "two" "many")

Note that _ is used for the don’t care or wildcard case, rather than the more traditional t.

(defun can-drive (x)
(pcase x
((guard (< x 18)) "Too young to drive")
(_ "Old enough to drive")))

(can-drive 12)
=> "Too young to drive"

The following converts a test mark into a grade. Note the use of and to evaluate (pred stringp). If non nil, it binds x to msg. In other words, pcase can distinguish between marks and teacher comments.

(defun student-grade (x)
(pcase x
((and (pred stringp) msg) msg)
((guard (< x 10)) "Fail")
((guard (< x 20)) "C")
((guard (< x 30)) "B")
(_ "A")))

(mapcar #'student-grade '("Absent" 23 12 "off roll" 9 35))
=> ("Absent" "B" "C" "off roll" "Fail" "A")

Take a look at this example from the documentation. Again, it uses and to evaluate (pred stringp). If non nil, it binds x to msg.

So, if x is a string, print it; if x is a recognised symbol, print the associated message; otherwise print unknown return code.

(defun my-errors (x)
(pcase x
;; string
((and (pred stringp) msg)
(message "%s" msg))
;; symbol
('success       (message "Done!"))
('would-block   (message "Sorry, can't do it now"))
('access-denied (message "You do not have the needed rights"))
;; default
(code           (message "Unknown return code %S" code))))

=> ("Unknown return code 1" "The shmliblick is read-only" "hello")

## 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.

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.

## Chessboards

Here’s a chessboard. Each square is 4×4 characters

    XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX


What’s the most Emacsy way of producing the above?

## First Line

XXXX    XXXX    XXXX    XXXX

You could do the following:

C-4 <space> 4 Spaces
C-4 X       4 Xs
C-k         Kill line
C-y         Yank
C-x z z z   Repeat last command three times


Another way is to use a macro:

F3          Start recording
C-4 <space>
C-4 X
F4          Stop recording
F4 F4 F4    Run the macro three times


Once you have one line, you could copy it and then yank it three times to get the first line of squares.

Here’s another way to get a line of squares, this time using rectangles. See this post to remind yourself about rectangles.

C-16 X
Kill and yank to get the following:

XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX


Now insert the white squares:

Go to start of the pattern

Set the region to cover the first square
C-x r o to insert blank space to fill the space of the region-rectangle

XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX

Now move the point forward 8 so its at the correct position to open the next square

C-8 C-f

You can record a macro of the above and then run it 3 times. Don’t forget to add the C-8 C-f at the end to move the point to the correct starting position.

## 2 The Second Line

Once you have the first line of squares, the second is quite easy. Copy one line of squares beneath itself to get the following:

    XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX

And then use C-x r k to kill the white square at the start of the second line.

    XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX


Now you can just kill and yank four times to get the complete chessboard.

Of course, you could just do it in LISP:

(defun one-line(pattern)
"Insert PATTERN 4 times"
(dotimes (count 4)
(insert pattern)))

(defun four-lines(pattern)
"Insert 4 lines of PATTERN"
(dotimes (count 4)
(one-line pattern)
(insert "\n")))

(defun chess-board()
(interactive)
(dotimes (count 4)
(four-lines "    XXXX")
(four-lines "XXXX    ")))

M-x chess-board

Can you think of a more efficient method than the ones above? Why not post it below?

## Emacs Characters 3

I never thought I would write three posts about entering characters in Emacs.

Emacs Characters demonstrates the quickest way to insert characters such as è and ä by using the C-x 8 key combination. So, for example:

C-x 8 ' e prints é
C-x 8 e prints è
C-x 8 ^ e prints ê
C-x 8 " u prints ü
C-x 8 / / prints ÷
C-x 8 C prints © copyright

Emacs Characters 2 shows how C-x 8 [return] allows you to type in the description of a character, so C-x 8 [return] LEFT ARROW gives ←

It’s time for another way. This post demonstrates toggle-input-method. Emacs has a number of input methods, used for entering such things as Arabic characters. You can see the full list using

 M-x list-input-methods

Use C-\ to enable the input method. The first time you do this you’ll be prompted for a method. For the purposes of this post, enter TeX. If you don’t know TeX, this post gives you a flavour.

You can now enter characters using TeX. Here are some examples

\pir^2 → πr²
Z\"urich → Zürich
Caf\'e  → café

I used \rightarrow to get the → used above, by the way.

When you’re done using TeX, use C-\ to disable the current input method

That’s three different methods for entering text. Which one is best? For me, it’s whichever is the most convenient. If I want to type the acute accent in café I’d probably use C-x 8 ‘e. When I was writing my novel Dream Paris I used TeX input for typing in the French dialogue.

As this is the Emacs workout, why not think of the ways you could type the following in Emacs?

Einstein wrote E=mc² on the table whilst eating a rösti in a café in Zürich. As easy as πr², he thought.

If you get stuck

M-x describe-input-method

will give a list of key sequences.