Feeds:
Posts
Comments

Archive for January, 2009

Emacs users, what is your $EDITOR?

I suspect that even for many hardcore emacs users it is something other than emacs.

The $EDITOR environment variable determines which editor is run to change the contents of files by various unix commands1. Until a few weeks ago, my $EDITOR and $P4EDITOR variables were both set to vi. Why?

Starting my emacs takes a few seconds on a local server and up to a minute on a remote server (because of X). I could use emacsclient which connects to an already running emacs. I would also have to change my work habits and access remote servers using tramp. Doing that would be inconvenient and a better solution for me is to start a stripped-down emacs-in-a-terminal.

The first thing to do is to set up a simple file which loads your basic configuration. I call this one basic-emacs.el.

basic-emacs.el

(add-to-list 'load-path (expand-file-name "~/emacs-files"))

(require 'my-defaults)
(require 'my-utils)

Then set your EDITOR variables in .bashrc for example.

.bashrc

EDITOR='emacs -nw -q --load ~/emacs-files/basic-emacs.el'
P4EDITOR=$EDITOR

export EDITOR P4EDITOR
  • The q flag prevents emacs from loading your .emacs
  • The nw flag runs emacs in a terminal

1. e.g. crontab -e

Read Full Post »

How To Disable Indent-Tabs-Mode

Emacs has a feature where it can automatically convert leading spaces to tabs. This is controlled by the indent-tabs-mode variable. I find mixing tabs and spaces extremely annoying as graphical diff tools will often display a tab and an equivalent number of spaces differently. Therefore I like to disable this behaviour. However, the obvious solution doesn’t work.

(setq indent-tabs-mode nil)

The documentation explains the reason

indent-tabs-mode is a variable defined in `C source code’.
Its value is nil

Automatically becomes buffer-local when set in any fashion.

I’m surprised it works like this. Fortunately, there is a simple fix:

(customize-set-variable 'indent-tabs-mode nil)

If it wasn’t a customizable variable1 you might use something similar to the following.

(add-hook 'first-change-hook
          (lambda () (setq indent-tabs-mode nil)))

Update: (setq-default ...) looks like a better solution. Thanks to Ron for the tip.

(setq-default 'indent-tabs-mode nil)

1. Is this even possible for a variable that automatically becomes buffer-local when set?

Read Full Post »

The Time Poor Blogger has a post about creating a new emacs major modearticle mode. For implementing a major mode, the wiki provides some useful hints but I think this is a useful case study. The mode adds word counting and keyword density checking and is designed for writing search engine optimised articles.

Major modes are generally for significant pieces of functionality, for example helping with editing a new type of file or providing a unified mechanism for editing directories (dired).

Article mode doesn’t really fit into either category which indicates that perhaps it would be better implemented as a minor mode. Also it might be useful in conjuction with a number of different major modes, e.g. HTML mode, Text mode or Latex mode.

As major modes are mutually exclusive it wouldn’t be possible to use Article mode in this way with the current implementation. I would recommend to change this to a minor mode.

The mode demonstrates the following interesting emacs features:

  • adding text properties – it provides a mechanism for highlighting keywords
  • timers – it only updates the information buffer when emacs has been idle for half a second
  • hooks – it demonstrates a slightly unusual technique of waiting for a second keypress after a particular keychord using post-command-hook.

Finally, I like the fact that it uses (define-derived-mode CHILD PARENT NAME <DOCSTRING>).

(define-derived-mode article-mode text-mode "Article"
  "Major mode for editing articles
Special commands:
\\{article-mode-map}")

Read Full Post »

An Ediff Tutorial

tech-ranting.blogspot.com has an ediff tutorial. ediff is something I use extensively as it is so nice. It integrates with your source control system so you can ask it what changes you have made since last check-in (ediff-revision).

One thing that isn’t well highlighted (so to speak) is how well ediff drills down on the exact change. It doesn’t just say “this line is different to this line”, it focuses on the exact change within the line. You can easily see here the tags that have been added to an old revision of one of my earlier posts.

The merging facility is also great. It has saved me on a number of occasions when clear^H^H^H^H^Ha well known source control system has dismally failed to merge branches.

What did I like about this tutorial? I learned something new (and Vedang, the tutorial author, had only looked at it for a day – I guess familiarity does breed contempt). So far I haven’t used ediff-directories as I didn’t know what it did. I imagined it would be similar to a recursive diff from the shell but it sounds like it does a lot more.

Read Full Post »

Emacs Abbreviations

Emacs includes an abbreviation package that allows you to type a shortcut e.g. oc which it will expand it to (say) oversight committee.

And a good thing too. I’ve had to type rel="nofollow" a few too many times in the past few days. I’d much rather type, for example "rnf" which I can add using (define-abbrev ...).

Another advantage is that I can include my inline styles in abbreviations which is very handy when wordpress.com makes it a little bit inconvenient to fix the master css.

(define-abbrev global-abbrev-table "rnf" "rel=\"nofollow\"")

(define-abbrev global-abbrev-table "stcod"
  "style=\"font-size: 130%; background: #eee; padding: 3px;\"")

(define-abbrev global-abbrev-table "stsrc"
  (concat "<pre style=\"font-size: 130%; border: 1px solid #bbb;"
                       "background: #eee; margin: 15px 5px; padding: 5px;\">"))

Then add a hook to enable abbrev mode and you are good to go.

(add-hook 'text-mode-hook (lambda () (abbrev-mode 1)))

Read Full Post »

Emacs and External Processes

Emacs has some nice facilities for interacting with external
processes. Recently I was working with an extremely long line with no
spaces that I wanted to split into lines of 72 characters. I banged
this out in about 20 seconds:

M-| (shell-command-on-region)
perl -e '$_=<>;s/(.{72})/$1\n/g;print'

Now,

  • Maybe I could have written this to a file and run perl on it using
    the shell
  • Maybe I could have used a keyboard macro, something like C-x ( C-u 7 2 C-f <RET> C-x ) C-x C-e e e e e ...
  • perhaps $_<>= can be replaced with just <> or an appropriate command line switch, reducing my character count by two
  • Maybe there is a function within emacs that does this already -
    perhaps something similar to (let ((sentence-end "."))
    (fill-paragraph nil))
    (which doesn’t work)

However, interacting with the external process

a) worked first time and
b) made me feel clever

and sometimes, particularly in this kind of job, that is the most important thing.

Read Full Post »

New Management

News: curiousprogrammer.wordpress.com is "under new management".

Hi, I’m Jared. I’m not much of a schemer, but I am a curious programmer. I do write a fair bit of emacs-lisp which I think is quite similar. You don’t have to agree :)

Read Full Post »

Follow

Get every new post delivered to your Inbox.