Categories
Emacs Emacs Lisp

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

Categories
Emacs Emacs Setup

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

Categories
Emacs Emacs Setup Emacs Windows

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

Categories
Emacs Emacs Workout

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

Categories
Emacs Emacs Workout

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
Categories
Emacs Emacs Workout

Dired Trick #1

Many people say that once you get the hang of Dired, you never use anything else. How true is this?

From a personal perspective I’d say that was true, but the key is in the first part of the sentence once you’ve got used to using Dired…

Here’s real world use for Dired.

C-x d to start Dired and navigate to a directory.

Here’s a directory into which a CD has been ripped. The track names haven’t been recognised.

/home/*****/Music/Ludwig van Beethoven/Beethoven- Symphonies Nos. 1 & 2:
total used in directory 108088 available 32870008
drwxrwxrwx 2 ***** ***** 4096 Jun 8 2009 .
drwxrwxrwx 4 ***** ***** 4096 Jan 10 2009 ..
-rwxr--r-- 1 ***** ***** 19510080 Sep 21 2010 01 Track 1.mp3
-rwxr--r-- 1 ***** ***** 12268125 Sep 21 2010 02 Track 2.mp3
-rwxr--r-- 1 ***** ***** 7567075 Sep 21 2010 03 Track 3.mp3
-rwxr--r-- 1 ***** ***** 11410580 Sep 21 2010 04 Track 4.mp3
-rwxr--r-- 1 ***** ***** 20195615 Sep 21 2010 05 Track 5.mp3
-rwxr--r-- 1 ***** ***** 19836565 Sep 21 2010 06 Track 6.mp3
-rwxr--r-- 1 ***** ***** 7501945 Sep 21 2010 07 Track 7.mp3
-rwxr--r-- 1 ***** ***** 12329080 Sep 21 2010 08 Track 8.mp3

I could rename each track using a GUI by right clicking on each one and changing the name. Dired mode is faster

C-x C-q to toggle read only. Now simply type the new names of the tracks directly into the buffer. C-c C-c when you’ve finished.

If you want to be more efficient, move the point to the T in Track 1 and press M-z 1 to zap-to-char 1, deleting everything up to including the 1, leaving the .mp3 intact. Now type in the new track name.

If you want to be really efficient, of course, you could use search and replace…

Related Posts

Dired Tricks #2

Categories
Emacs Emacs Workout

Emacs Snooker

The key to snooker isn’t where the coloured balls go, it’s where your white ball stops. You should always be thinking about setting up your next shot. Something similar holds when using Emacs. Where will the point (cursor) be when you’ve completed your current operation?

Consider the following commands:
M-u Convert to uppercase to the end of the word
M-l Convert to lowercase to the end of the word
M-c Capitalize word

Note that the point jumps to the end of the word when each command is called. This makes it easy to jump through a string of words, changing case or capitalizing as you wish.

Now consider the following

M– M-u Convert previous word to uppercase
M– M-l Convert previous word to lowercase
M– M-c Capitalize previous word
M– means (Alt) and –

Note that the point remains where it is, at the end of the word you’ve just converted. You can’t jump back through a series of words converting as you go.

It’s like the difference between hitting C-o and hitting Enter. At first glance they both appear to do the same thing, but look at the point…
(image: www.freeimages.co.uk)