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

Ubuntu Emacs Org-Mode Setup

Emacs works straight out of the box on Ubuntu however, at the time of writing, Ubuntu 12.04 still only comes with org-version 6.33.  It’s worth installing the latest version.  The installation instructions are on the org-mode site http://orgmode.org/manual/Installation.html, but they’re not quite complete.

 Install the latest version of Org-Mode

  1. Download the org-mode files and copy to a suitable location (I put them in the Ubuntu One folder so they’re easily shared between PCs)
  2. sudo apt-get install texinfo.  This is the missing step that ensures the next part works correctly
  3. Navigate to the org-8.x folder and sudo make autoloads and then sudo make install
  4. Finally, add (add-to-list ‘load-path “/usr/share/emacs/site-lisp/org”) to your .emacs file

Unity Keybindings

Some of the Unity keybindings overwrite those of standard org-mode.  I get particularly frustrated not being able to use S-M-<UP> to sort lines.  The following sorts this out:

  1. sudo apt-get install compizconfig-settings-manager
  2. Launch compiz-config-settings-manager
  3. Dash Home -> CompizConfig Settings Manager-> Scale(icon) under Windows Management Category -> Bindings(tab) -> Initiate Windows Picker -> change to <Shift><Super>Up or similar

Alt and Alt Gr

I don’t make use of the way Ubuntu distinguishes between these two keys, and I prefer to set the Alt Gr key to act just like the Alt.  For one thing, it makes it easier on the hands to type M-f and M-b when moving forward and backwards through words (something I do a lot when editing) .  Making this change on Ubuntu 12.04 is easy

  • Open Keyboard Layout from the dash.  Choose Options, Alt/Win Key behaviour and select Alt and Meta are on Alt Keys

Note you you can also swap the Ctrl and Caps lock this way if you prefer.
For older versions of Ubuntu, the Keyboard Layout preferences are found on a tab in Keyboard in System Settings

Related Posts

Emacs Windows Setup

Installing Emacs on Windows

  1. Download a copy of Emacs for Windows from here: http://ftp.gnu.org/gnu/emacs/windows/  Emacs comes as a zip file looking something like this:  emacs-24.3-bin-i386.zip         18-Mar-2013 22:43   47M 
  2. Unzip the folder to a suitable location, e.g. C:/Program Files
  3. That’s it.  There is no other installation required.
  4. To launch Emacs, run the runemacs.exe file in the emacs-XX.X\bin\ folder
  5. You will now have a functioning copy of Emacs.

Follow this link to my Emacs Tutorial

…You’ll probably find, however, that not all features are present.  Follow the steps below to add the remaining features.

If you’re looking for how to get Ediff or the spell checker to work in Windows, you’ve come to the right place.

Ispell (Spell Checker) on Windows Emacs

  1. Download Ispell: http://www.filewatcher.com/m/ispell.zip.352502-0.html
  2. M-x customize-variable and enter exec-path to include the path to ispell.exe
  3. Copy english.hash to emacs home folder. (You can find the path to your home folder by pasting the following into Emacs: (getenv “HOME”)  and pressing C-x C-e after the final bracket.)

M-x flyspell to turn on flyspell mode, which underlines misspelled words. Click with the centre mouse button on the misspelled word for a menu suggested changes.

I like to add the following to my .emacs file.  It maps the menu select option to the right mouse button.

(eval-after-load "flyspell" '(define-key flyspell-mode-map [down-mouse-3] 'flyspell-correct-word))

Install Cygwin

Cygwin is “a collection of tools which provide a Linux look and feel environment for Windows.”

Installing Cygwin is the easiest way to enable all those extra features in Emacs

  1. Go to http://cygwin.com/ and run the setup.exe file on the website
  2. Install the default set of packages
  3. If you want to be able to use org-mode to export to ODT documents in Windows, you’ll need to install zip and unzip from the archive package.
  4. On Emacs, set exec-path to c:\cygwin\bin (or to wherever you installed Cygwin) (If you don’t know how to set exec-path, the easiest way is M-x customize-variable, enter exec-path and then insert the path in one of the fields.  Don’t forget to save the changes)
  5. Add c:\cygwin\bin to your Windows path and restart the machine

Done.  Emacs should now by fully working on your Windows machine.

Related Posts

Regexp Builder

Emacs has an interactive regex mode that shows matches as you type.

To go into the mode, M-x regexp-builder.  Type C-c C-q to exit the mode.

Watch out for escape characters.  Emacs requires you to escape \, so type \b for \b

Copy the following text into emacs and then M-x regexp-builder

Lots of Grey things
50 shades of Grey
Earl Grey Tea
Lady Grey
Graybeard the pirate
Greyhound buses
"You're looking grey," he said.
"That's the greyest greyhound I ever saw," said Earl Grey.
Grey1: Dark Gray
Grey35: Mid grey
Grey44: Battleship grey
Grey100: The colour of TV tuned to a dead channel

In regexp-builder, type…

Grey to find all the Greys
Gray to find all the Grays
Gr[ae]y to find all the Greys and Grays
\\bGray\\b to find Gray on its on and not part of another word
^Gray to find lines beginning with Gray
grey$ to find lines ending in grey
grey\\(hound\\)? to find all appearances of grey and greyhound
grey[0-9] to find all the shades of grey ending with a digit
grey[0-9]\\{2\\} to find all the shades of grey with exactly two digits

Follow the link for some Regexp Exercises to try out

Read more about Regexps at the Emacs Wiki

Rectangles

I never use the rectangle commands as often as I should.

Here’s my shopping list:

  • Bread
  • Cheese
  • Milk
  • Yoghurt

I want to make the above into a list. There are all sorts of ways I could so this, one way is to use the string-rectangle command. Set (or highlight) the region to zero width in front of the list and then C-x r t – to insert a dash before each of the words.

You can use the kill-rectangle command to remove unwanted bullet points (particularly useful when importing text from PDFs)

Set the region to cover the bullet symbols and then C-x r k to kill the rectangle. Don’t forget you can C-x r y yank that rectangle back.

C-x r o or open-rectangle is a quick way to indent a block of text to where you want it, especially if you can’t remember the other commands.

And lastly C-x r c will clear a rectangular space. I can’t remember ever using that one, to be honest…

If you find pressing C-x r a keystroke too far, read this post on remapping C-x r  to C-`

Summary of Commands

C-x r k    Kill rectangle
C-x r y     yank last killed rectangle
C-x r c     Clear rectangle
C-x r o     open rectangle and shift text right
C-x r t spoons  fill rectangle with spoons
C-x r r q   Copy rectangle to register q
C-x r i q   Insert rectangle from register q