MyKitaab Podcast

The mission of the MyKitaab podcast series is to help answer the question “I have written a book, how do I get it published in India?”
Here, host Amar Vyas talks to me about writing, blogging and Open Source software, especially  Emacs!
You can access the podcast as follows:

Or listen to it here:

Finding my Place

(This post appears as part of the document My Emacs Writing Setup: a guide to how I write novels and short stories using Emacs Org Mode)

It can be a nuisance finding where things are even when writing a short story. When writing a novel, it’s easy to get lost amongst the story, the notes, the character sketches…

Fortunately, Emacs and Org-Mode have a number of features to help you find your way around.

The most basic and most useful method, of course, is Emacs search. I’ve written more about this here.

Similar to search are

M-x             occur, and
C-c / r         org-occur

These highlight all occurrences of the search string. This can be handy, for example, when searching to see which chapters a character appears in.

Quite often I find myself jumping up and down a file, adding text here and there. You can use the standard mark-ring commands:

C-<SPC> C-<SPC>         Set the mark to the mark ring
C-u C-<SPC>             Move point to where the mark was

… but org-mode has two commands that respectively push your current position to the mark-ring and jump to the last position on the mark ring. For convenience, I’ve bound them to the f7 key as follows.

(global-set-key (kbd "<f7>") 'org-mark-ring-push)
(global-set-key (kbd "C-<f7>") 'org-mark-ring-goto)

Now, I simply hit f7 to remember my position before heading off to edit elsewhere in the file, then hit C-f7 to return to where I started.

Bookmarks

I’m assuming you already know how to use bookmarks, but if you don’t, here’s a link to the Emacs Wiki for a crash course.
As I write on multiple machines, I keep my bookmarks file on Dropbox so that I have a consistent set of bookmarks wherever I happen to be working. I’ve added the following command to my .emacs file to let Emacs know where my bookmarks are.

(setq bookmark-default-file "~/Dropbox/common/emacs/bookmarks.bmk" bookmark-save-flag 1)

I’ve got into the habit of having a bookmark named here. I try to set this mark when I finish working. When I start work, I simply jump to here.

Mouse and Emacs

Okay, if you’re using Emacs properly you won’t be using the mouse. You know that you waste time moving your hand from the keyboard to the mouse and back again.

But there will be times when you have the mouse in your hand anyway, so whilst it’s there you might want to remember these handy shortcuts…

Buffers

Ctrl and mouse 1 brings up the buffer list. Slide that mouse down context menu to quickly change buffer

Selecting Text

The following are worth remembering:
  • Double clicking on a parenthesis selects up to the matching parenthesis
  • Double clicking on a word selects it
  • Triple clicking selects a line

Mouse-1, Mouse-2, Mouse-3

I’m right handed. On my computer, Mouse-1 is left click, Mouse-2 is the scroll wheel and Mouse-3 is right click
Mouse-1 sets the point
Dragging the mouse not only sets the region, it also copies it to the Primary Selection, ready to be inserted by Mouse-2
Mouse-3 extends the current selection up to the point clicked. Mouse-3 a second time deletes the selection. This doesn’t work in all modes, however.

Searching

See the aardvark, a few lines down? It’s worth remembering that it’s often faster get to it by using C-s to search than by moving the cursor. Emacs expects you to search frequently, that’s why searching takes less key presses than C-x s, the save command.

Here’s a reminder of the search commands:

C-s C-w         Search for word after point
C-s C-s         Repeat last search
C-s C-y         Search line
C-s M-y         Search last kill
C-u C-s         Regexp Search
C-u C-r         Regexp Reverse Search

Emacs incremental search can occasionally be a pain. Don’t forget you can search non-incrementally for a specific word such as elephant using

M-s w <RET> elephant <RET>

Here’s the aardvark, by the way.

Calc Mode 1: Binary Numbers

Thanks to Andrew Hyatt for inspiring this entry

Here’s how to use Emacs to convert 8 numbers to binary. Why? Read on…

  1. Write the numbers: 127, 54, 32, 178, 199, 244, 3, 255
  2. Mark the region of the numbers and hit C-x * g. This grabs the numbers into calc mode as a vector
  3. In calc mode, hit d 2 to display the numbers in binary
  4. Still in calc mode, hit d z to show the leading zeroes. This shows them as 32 bit words
  5. Hit b w 8 to set the word size to 8 bits.
  6. Finally, and still in calc mode, hit y to yank the numbers back into the last buffer you were using.
  7. Add a table, tidy things up, and you’ve got a worksheet like the one below for a set of students and the answers for yourself.

Convert the 8 bit unsigned numbers to denary

  1. 01111111
  2. 00110110
  3. 00100000
  4. 10110010
  5. 11000111
  6. 11110100
  7. 00000011
  8. 11111111

Emacs Characters

Who would have thought that three dots could cause so many problems?

Most of the Aethernet Magazine writers use the ellipsis …

The Guardian Style Guide suggests leaving a space before and after the ellipsis.

In the Aethernet style guide the ellipsis is always followed by a space

“Is it… ?”

“Yes… ”

Preparing texts proves to be an interesting Emacs exercise.

It’s easy to search for three dots … , but there also exists a unicode character …
Now, as everyone knows, Emacs characters are saved as integers. You can insert any character on Emacs by hitting C-q and then the appropriate code in octal, in this case:

C-q 20046       gives …

If you want to enter the codes in denary, evaluate the following: (setq read-quoted-char-radix 10)

Evaluate the above and now…

C-q 8230 gives  …

Better yet, place the code in your .emacs file. Set the value to 16 if you want to quote in hexadecimal.

It’s easy to find a character’s value:

C-x = gives information about a character under the point.

For example, the ellipsis gives

Char: … (8230, #o20046, #x2026, file ...)

There are easier ways to insert non-keyboard characters using C-x 8

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

The above are just a few examples

C-x 8 C-h to see all characters

If you want to hurry (dépêcher in French) you might get tired of typing C-x 8 all the time. The following allows you to omit the C-x 8 section

M-x set-input-method latin-9-prefix

Okay, what now… ?

Repeating Commands

Repeating Commands

C-x z   Repeats the last command.  Hitting z repeatedly keeps on repeating that command.

Once you know this trick, many other apparently complicated Emacs commands suddenly become clear. For example, why is the command to widen a window the extremely time consuming C-x }? That takes four keypresses on my keyboard, as I have to hold down shift to access the }. Surely it’s faster to use a mouse?
Not if you remember C-x z. Try it now:

C-x 3   Split into two vertical windows
C-x }   Widen the window one space
C-x z   Repeat the last command.  Now hit z until the window is wide enough.

Granted, this is still pretty complicated at first sight. If you’ve stumbled across this page whilst looking for something else you’re probably wondering why on Earth anyone would want to use Emacs, but that’s to miss the point. C-x z is another useful command that you can chain together with others to produce new results.

Whilst we’re here, here’s some useful window commands to remember

C-x -   Shrink the window to the size of the buffer text, if it's smaller
C-x +   Make windows the same size
C-x 4 0 Kill buffer and window
C-M-v   Scroll other window down
S-C-M-v Scroll other window up

 

3 Interactive Functions that work on Regions

1 Start Point, End Point and Contents of a Region

The following function prints the contents of the selected region, together with its start and end points.

1: (defun region-contents (rStart rEnd)
2:   "Prints region contents of region selected, and the start and end positions of that region"
3:   (interactive "r")
4:   (setq rStr (buffer-substring rStart rEnd))
5:   (message "The string of the selected region is %s.  It starts at  %d and ends at %d"  rStr  rStart rEnd)
6: )

2 Iterate through the words in a region

You might want to iterate through the words in a selected region. The following function gives an example of this, it prints out the words in the region in reverse order.

1: (defun reverse-region-message (rStart rEnd)
2:   "Reverses order of words"
3:   (interactive "r")
4:   (setq myStr (buffer-substring rStart rEnd))
5:   (setq myWords (split-string myStr))
6:   (setq reversed (reverse myWords))
7:   (message (mapconcat 'identity reversed " "))
8: )

Note the use of the following functions:

  • split-string: splits the string into a list of words
  • reverse: reverses a list
  • mapconcat: Convert a list into a string

It’s interesting to compare the mapconcat function with the concat function. Note that concat does not accept lists, as you might expect at first glance. You can get round this using the apply function.

(concat "this" "that" "other") => "thisthatother"
(concat '("this" "that" "other")) => error
(apply 'concat '("this" "that"  "other")) => "thisthatother"
(mapconcat 'identity '("this" "that" "other") " ") => "thisthatother"

Here’s the function, rewritten to delete the selected region and to replace it with the words in reverse order.

1: (defun reverse-region (rStart rEnd)
2:   "Reverses order of words"
3:   (interactive "r")
4:   (setq myStr (buffer-substring rStart rEnd))
5:   (delete-region rStart rEnd)
6:   (setq myWords (split-string myStr))
7:   (setq reversed (reverse myWords))
8:   (insert (mapconcat 'identity reversed " "))
9:  )

3 Iterate through the letters in a region

You can spend a lot of time writing code to solve a problem only to find that Emacs Lisp already provides a function to do what you want.
For example, suppose you want to iterate through the letters in a region to convert SEPARATE to S-E-P-A-R-A-T-E. The easiest way is to use the mapconcat function, as seen in the previous section.

(mapconcat 'string "SEPARATE" "-")

Here’s the separator as a function that operates on the selected region.

1: (defun region-seperator (rStart rEnd)
2:   "Reverses order of words"
3:   (interactive "r")
4:   (setq rStr (buffer-substring rStart rEnd))
5:   (delete-region rStart rEnd)
6:   (insert (mapconcat 'string rStr "-"))
7:  )

3.1 string-to-list, mapcar and apply

The function string-to-list converts a string to characters.

(setq rStr (string-to-list "example")) => (101 120 97 109 112 108 101)

use char-to-string to convert a character code to a string

(char-to-string 101) => "e"

Note the use of apply, as discussed previously.

(apply 'string (reverse (string-to-list "example"))) => "elpmaxe"

Note that apply returns a value. If you want a list of characters, use mapcar

(mapcar 'char-to-string (string-to-list "example")) => ("e" "x" "a" "m" "p" "l" "e")

Remember, mapcar returns a list, apply returns a value. Many of the errors you make when beginning come down to confusing the two return types.
Finally, use a lambda function (discussed later) for complex functions in mapcar

(mapcar 'char-to-string (mapcar '(lambda (x) (+ x 1)) (string-to-list "foo")))

4 Enclose a Region

This tutorial has been written using emacs org mode. The code samples are marked up using the tags

#+BEGIN_SRC emacs-lisp -n
and
#+END_SRC

I wrote the following function to make the markup easier. Now I just select the region and run the function. The appropriate tags are placed at the start and end of the region.

1: (defun myMark-elisp-region (rStart rEnd)
2:   "Mark region as Elisp source code for org mode export."
3:   (interactive "r")
4:   (save-excursion
5:     (goto-char rEnd) (insert "\n#+END_SRC\n")
6:     (goto-char rStart) (insert "#+BEGIN_SRC emacs-lisp -n\n"))
7: )

Note the use of the save-excursion function to return the point to where it was before the function was run. Note also the way the above function is laid out. An interesting exercise is to reverse the order of lines 5 and 6. See if you can figure out why the function doesn’t work as you might expect.

5 Find and Replace in a Region: Strip Smart Quotes in a Region

These are smart quotes: “ ” ‘ ’
For editing purposes I sometimes need to replace these with the plain quotes ” and ‘. I only want this to replacement to occur on a specified region of text, not on the whole buffer.
Here’s a function to do this. It narrows to a region, uses save-restriction to remember how things were before the narrowing and then uses two regex searches to find first double quotes and then single quotes, replacing both with plain quotes.

 1: (defun strip-smart-quotes (rStart rEnd)
 2:   "Replace smart quotes with plain quotes in text"
 3:   (interactive "r")
 4:   (save-restriction
 5:   (narrow-to-region rStart rEnd)
 6:   (goto-char (point-min))
 7:   (while (re-search-forward "[“”]" nil t) (replace-match "\"" nil t))
 8:   (goto-char (point-min))
 9:   (while (re-search-forward "[‘’]" nil t) (replace-match "'" nil t))
10: ))

6 Choosing between Operating on a Region or the Whole Buffer

The previous example only stripped smart quotes from the selected region. This function uses the (when (uses-region-p) …) construction to determine whether or not a region is selected. If no region is selected, the whole buffer is operated upon.

 1: (defun region-or-buffer ()
 2:  "Strip smart quotes from region, or whole buffer if region not set"
 3:  (interactive)
 4:  (save-excursion
 5:    (save-restriction
 6:      (when (use-region-p) (narrow-to-region (region-beginning) (region-end)))
 7:      (goto-char (point-min))
 8:      (while (re-search-forward "[“”]" nil t) (replace-match "\"" nil t))
 9:      (goto-char (point-min))
10:      (while (re-search-forward "[‘’]" nil t) (replace-match "'" nil t))
11: )))

Next: Yodaizer – Manipulating Strings Example

2 Functions in Elisp

Here’s how to define a simple LISP function

1: (defun pi ()
2:   "A sample non-interactive function"
3:   3.1415
4: )

The above is a non-interactive function that simply returns 3.1415. Evaluate it (C-x C-e, remember?) and you will see the word pi appear in the echo area. Try M-x pi, though, and Emacs won’t find the function. If you want to be able to call a function using M-x, you have to make it interactive, as follows.

1: (defun print-pi()
2:     "Insert an approximation to pi"
3:     (interactive)
4:     (insert "3.1415")
5: )

So why would you want a non-interactive function? Perhaps because you want it to be called from another function, as follows:

1: (defun circumference-of-circle()
2:     (interactive)
3:     (message "The circumference of a circle diameter 3 is %f" (* pi 3))
4: )

Before evaluating the above function, make sure that you have evaluated the non-interactive function pi.

There are lots of different types of interactive functions. The next interactive function is more useful in that it prompts for the diameter to be input (the n at the start of “nInput diameter of circle:” is what tells Emacs to prompt for a number)

1: (defun circumference-of-circle(diameter)
2:     "Calculate the circumference of a circle given the diameter"
3:     (interactive "nInput diameter of circle:")
4:     (message "The circumference of a circle diameter %d is %f" diameter (* 3.1415 diameter))
5: )

Here’s the same function but this time set up to receive the parameter from the universal argument. That is to say, in the form C-u 4 M-x circumference-of-circle.

1: (defun circumference-of-circle(diameter)
2: (interactive "p")
3: (message "The circumference of a circle diameter %d is %f" diameter (* 3.1415 diameter))
4: )

Here’s an example of a function that reads strings and tests your knowledge of capital cities.

1: (defun capital-of-france(answer)
2:     "Simple quiz example."
3:     (interactive "sWhat's the Capital of France?")
4:     (if (string= answer "paris") (message "Correct!") (message "Wrong!"))
5: )

Argument codes for interactive functions can be found here http://www.gnu.org/software/emacs/manual/html_node/elisp/Interactive-Codes.html#Interactive-Codes

Next: Interactive Functions that work on Regions

1 Beginning Emacs Lisp

LISP is derived from the term LISt Processing.

A list in LISP looks like this:

(Knife Fork Spoon)

or like these two examples
(set-background-color "yellow")    <- C-x C-e
(set-background-color "white")    <- C-x C-e

If the first item in the list is a function you can evaluate the list by placing the cursor just after the bracket at the end of the list and pressing C-x C-e. Try it with the two lists above. Copy them into Emacs and then C-x C-e where indicated to turn the Emacs background yellow and then to set it white again.

If you try to evaluate the (Knife Fork Spoon) list you’ll get an error telling you that Knife is a void function.

Try evaluating the following lists in Emacs by typing C-x C-e after the closing bracket:
(linum-mode)
(message "This is the echo area")
(* 2 3)
(+ 4 5)

The last three will output their results in the echo area, the area at the bottom of Emacs.

You can also evaluate a function by typing M-x (function name). So M-x visual-line-mode will turn word wrap on and off.

Emacs supports TAB completion, so typing M-x visu and pressing TAB is enough to fill in the function name.

You set a variable as follows:

(set 'name 'John) C-x C-e to set the variable

name C-x C-e to see the contents of the variable “name”

If you press C-x C-e after (name)you’ll get an error. Remember, name is a variable, (name) is a function, and you haven’t defined a function called name.
It’s a nuisance typing in ‘ all the time, so the following is often used
(setq animal 'cat)

Evaluate the above and then evaluate animal …

C-u C-x C-e will insert any output directly in the text area, rather than in the echo area.

Here is a list of cheeses called cheese:
(setq cheese '(Stilton Wensleydale Cheddar Cheshire))

Evaluate the list.

The first item in a list is called the car, the remaining items are called the cdr (pronounced could-er) The Emacs Lisp tutorial will tell you why. Evaluate the following:

(car cheese)
(cdr cheese)

… and there you are

Next: Functions in Elisp