Monday 30 June 2008

Adding MajorModes to Emacs

In general, you need to implement a major mode in an elisp file (.el) and make the location accessible via the load-path. Example:

;;; Emacs load path
(setq load-path (cons "c:/cygwin/home/windowsjoe/mymode" load-path))

You then need to reload your .emacs ("M-x load-file") . If you get the error "End of file during parsing" it suggests a missing bracket somewhere. Go back and check your code. If successful you will see "Loading...done" in the command buffer. Note: /home/windowsjoe may not work, you may need to actually specify the location on your hard drive where /home/ is located. as in the example.

You also need to byte-compile source files using "M-x byte-compile" in emacs. The bytecode makes your functions faster to execute and byte-code is transportable from one machine to another without recompilation.

Adding Python Mode to Emacs

To associate newly-loaded Python files (and the interpreter name) with the awesome new Python mode, we need to update:
  • auto-mode A List (matches the filename with a MajorMode when file is first opened)
  • interpreter-mode A List (associates interpreter name with a MajorMode)
(setq auto-mode-alist
(cons '("\\.py$" . python-mode) auto-mode-alist))
(setq interpreter-mode-alist
(cons '("python" . python-mode)
interpreter-mode-alist))
(autoload 'python-mode "python-mode" "Python editing mode." t)

python-mode.el does some pretty cool things like:
  • set the default number of spaces for a tab in Python (where indentation is clearly important)
(defcustom py-indent-offset 4
"*Amount of offset per level of indentation.
`\\[py-guess-indent-offset]' can usually guess a good value when
you're editing someone else's Python code."
:type 'integer
:group 'python)

  • Parse classes and functions in the source file and display in the IM-Python menu item
On Windows the Python mode code is normally stored in C:\Program Files\emacs-versionX\modes\python-mode-versionY\. The latest version can be swiped from sourceforge.net/projects/python-mode.

Editing .Emacs

The first thing you need to do is activate highlighting modes. The three fundamental modes are:

(setq-default transient-mark-mode t) ; Active Region highlighting
(global-font-lock-mode t) ; Syntax Highlighting
(setq show-paren-mode t) ; Matching Parentheses highlighting

Then you need to activate various shortcut keys. Some recommended ones:

(global-set-key "\M-g" 'goto-line) ; quickly jump to lines with Meta-X g
(global-set-key [f3] 'query-replace-regexp) ; regex search and replace
(global-set-key [f10] 'load-file) ; load a file (e.g. reload .emacs)

Note: you will sometimes see "\M-g" written as (kbd "M-g") eliminating the need for a backslash.

Finally you need to set some formatting defaults.

(setq c-basic-offset 4)
(setq-default indent-tabs-mode nil)

The last commands here set the factory default for spaces between indentation levels, and sets tabs to spaces, for consistency with source-control systems.


"CBeebs" - The Cygwin, Bash, Emacs Combination

"CBeebs" is a shortened form of the BBC's pre-school channel featuring characters like Andy Pandy, Barnaby Bear, Teletubbies and Bob the Builder. "CBeebs" is also the nickname given to the combination of Cygwin, Bash and Emacs on Windows, a highly potent concoction of Linux/Unix tools on Windows.

Each tool has to be appropriately configured. This section will cover some basic customisations that can be thought of as the "pre-school" level of customisations.

1. Cygwin must be configured to have the applications you want
2. Bash is configured via .bashrc
3. Emacs is configured via .emacs

Some basic customisations:

1. This can be done using the Cywgin installer (requires admin rights to access the list of mirrors)

2. .bashrc is normally located in your home directory e.g. /home/windowsjoe/.bashrc
It should contain useful aliases (shortcuts) to speed up interaction with your computer.

Examples:
alias home='cd /home/windowsjoe' -- quickly jump to home directory
alias cdrive = 'cd /cygdrive/c' -- stop typing cygdrive constantly
alias gi = 'grep -i' -- lazy (case-insensitive) grep
alias find = '/usr/bin/find' -- use UNIX find rather than horrid Windows one
alias ls='ls -Al --color' -- ls displaying full information with colors

3. Your .emacs lives in the same directory as .bashrc. It is best edited with emacs itself.

Editing .emacs will be covered in the next post.

Winning with Cygwin

This is a blog dedicated to Cygwin, Linux emulation in Windows. The aim is to show how Windows developer productivity can be increased massively with Cygwin. More general Windows automation tips and techniques are covered in the related blog windowsjoe.blogspot.com.