Showing posts with label emacs. Show all posts
Showing posts with label emacs. Show all posts

Wednesday, 22 October 2008

In Praise of Emacs

Emacs is praiseworthy. You can create your own development platform inside emacs. All you need is the will to learn elisp. Elisp is a dialect of LISP, the language developed at MIT in the 1950s for AI research (for inspiration check out some old-school programs written in LISP here).

Emacs Folding Mode

Embrace this awesome emacs minor mode. More here. Rusty on the concept of minor modes? No worries, read this and become enlightened.

Thursday, 11 September 2008

Emacs: Multi-line Macro Application

M-x apply-macro-to-region-lines

Friday, 25 July 2008

Emacs 22.2

22.2 comes with a built-in Python mode. No need to install this separately.

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.