Posts Tagged ‘aliases’

Directory Aliases is one of my favourite emacs packages, well, out of the ones I have written at least. It provides a way to go from an alias to a directory location using ido and dired respectively. On Linux, I can use shell aliases and the emacs daemon but for my sins, a lot of my time is spent developing on Windows and there it is invaluable. As I find the various Windows shells are lacking it is nice to an alternative interface to the OS – emacs.

alias realias='$EDITOR ~/.aliases; source ~/.aliases'

Inspired by chromatic’s post mentioning realias (attributed to Damian Conway) I added a couple of features to directory aliases. The first is the ability to open a regular file in addition to the directory. The second is the ability to reload the aliases so you have access to them immediately after adding them.

Future plans include a facility to add an alias with a keystroke while visiting a file (maybe I should be using bookmarks instead… but I like ido too much).

(require 'ido)
(require 'dired)

(defconst *home* "c:/home/jared")
(defconst *packages* "c:/packages")

(defconst *file:dir-aliases* (concat *elisp-dir* "dir-aliases.el"))

(defsubst home (path) (concat *home* path))
(defsubst packages (path) (concat *packages* path))

The preamble has some convenience functions for referring to commonly used areas.

(defconst *file-aliases*
  (list (cons "dir-aliases" *file:dir-aliases*)

(defconst *dired-aliases*
  (list (cons "project" (home "/project"))
        (cons "curious" (home "/websites/curious"))
        (cons "plack" (home "/plack-tests"))
        (cons "site-lisp" (packages "/emacs/site-lisp"))

I main separate alists for file aliases and dired aliases.

(defun my-open-alias (aliases fn &optional alias)
  (unless alias
    (setq alias
          (ido-completing-read "Alias: "
                               (mapcar (lambda (e) (car e)) aliases)
                               nil t)))
  (if (and (stringp alias) (> (length alias) 0 ))
      (let ((pair (assoc alias aliases)))
        (if pair
            (funcall fn (cdr pair))
          (error "Invalid alias %s" alias)))
    (error "Invalid alias %s" alias)))

The main my-open-alias function reads in the alias with ido and then applies the passed in function.

(defun file-open-alias (&optional alias)
  (my-open-alias *file-aliases* #'find-file alias))

(defun dired-open-alias (&optional alias)
  (my-open-alias *dired-aliases* #'dired alias))

(defun reload-aliases ()
  (load *file:dir-aliases*))

More convenience functions using #'find-file and #'dired and the alias reloader I mentioned earlier.

(defvar f2-prefix-map nil)
(setq f2-prefix-map (make-sparse-keymap))

(global-set-key [f2] f2-prefix-map)
(global-set-key (kbd " d") 'dired-open-alias)
(global-set-key (kbd " f") 'file-open-alias)

(provide 'dir-aliases)

Maybe dir-aliases isn’t the best name anymore.

Read Full Post »

Customizing Emacs Muse – Part 3

This is part 3 in the customizing emacs muse series.

In part 1 (Creating Squidoo Lenses With Emacs Muse) we talked about adding hooks to transform the generated HTML to cope with the impoverished tag set that Squidoo provides.

In part 2 (Updating Elements Within Association Lists) we talked about adding a function that can replace values in association lists to manipulate muse-html-markup-strings.

In this post, we will talk about how to select between different output styles if you’re writing for, say, a wordpress.com blog, an article directory and squidoo.

Note: muse already allows for different styles, but I’m not aware of a sub-style mechanism which is what I am really describing here.

Let’s get started. First of all, it is handy to have a mechanism for getting back to the default state. I provide a variable to store a function that will undo the changes that made the current sub-style. Returning to the default is just a matter of calling this function if it is defined.

(defvar muse-style-remove-fn nil)

(defun muse-style-default ()
  (when muse-style-remove-fn (funcall muse-style-remove-fn))
  (setq muse-style-remove-fn nil))

muse-style-select enables me to choose between the various styles using ido. You may recognise this selection mechanism from my directory aliases post.

(defconst muse-style-mapping
  (list (cons "squidoo" 'muse-style-squidoo-enable)
        (cons "curious" 'muse-style-curious-enable)
        (cons "default" 'muse-style-default)))

(defconst muse-style-aliases
  (mapcar (lambda (e) (car e)) muse-style-mapping))

(defun muse-style-select (&optional style)
  (unless style
    (setq style
          (ido-completing-read "Muse Style: "
                               nil t)))
  (if (and (stringp style) (> (length style) 0 ))
      (let ((pair (assoc style muse-style-mapping)))
        (if pair
            (funcall (cdr pair))
          (error "Invalid style %s" style)))
    (error "Invalid style %s" style)))

I have a helper function to replace the keys within muse-html-markup-strings.

(defun muse-strings-replace (mapping)
  (setq muse-html-markup-strings
        (assoc-replace muse-html-markup-strings mapping)))

The disable function resets begin-literal and end-literal to the original state. Possibly I should have copied the entire original associative list instead. It also removes the hooks which have been added.

(defun muse-style-squidoo-disable ()
  (muse-strings-replace (list (kv 'begin-literal "<code>")
                              (kv 'end-literal "</code>")))
  (remove-hook 'muse-html-after-htmlize-hook 'squidoo-htmlize)
  (remove-hook 'muse-after-publish-hook 'squidoo-fix-para))

The enable function returns us to the default style first, just in case there is already a sub-style selected. It then overwrites begin-literal and end-literal and adds the hooks.

(defun muse-style-squidoo-enable ()

  (when muse-style-remove-fn (funcall muse-style-remove-fn))
  (setq muse-style-remove-fn 'muse-style-squidoo-disable)

  (muse-strings-replace (list (kv 'begin-literal sq-code-prefix)
                              (kv 'end-literal "</b>")))
  (add-hook 'muse-html-after-htmlize-hook 'squidoo-htmlize)
  (add-hook 'muse-after-publish-hook 'squidoo-fix-para))

Read Full Post »

An Extensive, But Not Comprehensive, Look At Finding Files

The Problem

Finding files can be a tricky business. If you have a number of different directories where your files can be stored, how can you open a particular file as quickly as possible. The standard find-file interface is pretty good if you’re already in the correct directory as it will do tab completion on anything you have already typed in similar to how a decent shell will.

ido improves this interface. If you enter part of a filename that has already been seen, even if it is in a different directory it will be offered to you. I enable flex-matching so that ido will match files containing the characters which have been entered anywhere so long as they are in the correct sequence. e.g. eod will match hello-world.c.

(ido-mode t)
(setq ido-enable-flex-matching t)

(setq ido-create-new-buffer 'always)

I understand that icicles does even better here, enabling you to filter out matches but as I’m not (yet) using it myself, I can’t say much more about it.

If you need more help in finding your files, what other options are there?


I hadn’t heard of Bookmarks before I saw a a post on them recently. I use them in conjunction with directory aliases1.

From the emacs manual:

Bookmarks are somewhat like registers in that they record positions you can jump to. Unlike registers, they have long names, and they persist automatically from one Emacs session to the next. The prototypical use of bookmarks is to record “where you were reading” in various files.


Sacha Chua wrote a post about navigating your source tree using ido and filecache. I’m not sure what additional benefits
filecache offers over vanilla ido as ido finds files in other directories too.


The traditional way for finding function definitions is with Emacs Tags. Once you have generated the tags file you can go from call site to function definition with M-. (find-tag).

When I go on holiday I leave a link to the current method I am working on in an orgfile. Then when I come back, I can click on the current work link and press M-. and I’ll be right back where I left off.

[[SomeClass::SomeMethod][Current Work]]

* Work Projects
*** A Specific Project
Some notes about what I was doing...

I set the tags file so I don’t need to find that file.

(setq tags-file-name "/path/to/tags/file")

1. Directory Aliases

(require 'ido)
(require 'dired)

(defconst *web-top* "~/websites")

(defun web (path)
  (concat *web-top* path))

dired-dirs is an associating list mapping aliases to directories. I need to use (list ...) to construct it so that (web ...) expands correctly.

(defconst *dired-dirs*
  (list (cons "mrc-theme" (web "/blog/mrc/wp-content/themes/mrc"))
        (cons "mrc-pages" (web "/pages/mrc"))
        (cons "webtest"   (web "/test"))))

(defconst *dired-aliases*
  (mapcar (lambda (e) (car e)) *dired-dirs*))

dired-open-alias uses ido-completing-read to choose between the aliases I defined earlier.

(defun dired-open-alias (&optional alias)
  (unless alias
    (setq alias
          (ido-completing-read "Alias: "
                               nil t)))
  (if (and (stringp alias) (> (length alias) 0 ))
      (let ((pair (assoc alias *dired-dirs*)))
        (if pair
            (dired (cdr pair))
          (error "Invalid alias %s" alias)))
    (error "Invalid alias %s" alias)))

I really like f2 as a prefix key. The default binding is related to two-column mode, which I never use, and you can use it without pressing shift, ctrl or alt. I have this defined in my-defaults.el (which probably isn’t strictly necessary but at least it names the prefix map).

(defvar f2-prefix-map nil)
(setq f2-prefix-map (make-sparse-keymap))
(global-set-key [f2] f2-prefix-map)

<f2> d calls up the list of directory aliases.

(global-set-key (kbd "<f2> d") 'dired-open-alias)

Read Full Post »