6100 comments
2357 subscribers
6264 on Twitter
Subscribe! Feed reader E-mail

Emacs: Working with multiple source trees

As a developer, I often find myself working with multiple trees of source code. Sometimes, I’m comparing the trunk and the branches. Sometimes, I’m copying ideas from one place to another. Sometimes, I’m working on one project and an urgent defect comes in for a different project. With good development environments such as Eclipse, I’d still need to click on the project or directory in order to change my context, and then use a keyboard shortcut to find the resource in that tree.

With an awesome development environment like my customized Emacs, however, I can easily juggle multiple source trees. Here’s the macro I wrote to make setting this up much easier:

(defmacro sacha/file-cache-setup-tree (prefix shortcut directories)
  "Set up the file-cache tree for PREFIX using the keyboard SHORTCUT.
DIRECTORIES should be a list of directory names."
  `(let ((file-cache-alist nil)
	 (directories ,directories))
     (file-cache-clear-cache)
     (while directories
       (file-cache-add-directory-using-find (car directories))
       (setq directories (cdr directories)))
     (setq ,(intern (concat "sacha/file-cache-" prefix "-alist")) file-cache-alist)
     (defun ,(intern (concat "sacha/file-cache-ido-find-" prefix)) ()
       (interactive)
       (let ((file-cache-alist ,(intern (concat "sacha/file-cache-" prefix "-alist"))))
	 (call-interactively 'file-cache-ido-find-file)))
     (global-set-key (kbd ,shortcut)
		     (quote ,(intern (concat "sacha/file-cache-ido-find-" prefix))))))

With that, I can use the following to map C-c p to finding files in my personal directories:

(sacha/file-cache-setup-tree
 "personal"
 "C-c p"
 '("/var/www/html/drupal"
   "~/elisp"
   "~/personal"))

I’ve also set up keyboard shortcuts for the other trees I’m working on.

You’ll need file-cache, ido, and probably something like this:

(require 'filecache)
(require 'ido)
(defun file-cache-ido-find-file (file)
  "Using ido, interactively open file from file cache'.
First select a file, matched using ido-switch-buffer against the contents
in `file-cache-alist'. If the file exist in more than one
directory, select directory. Lastly the file is opened."
  (interactive (list (file-cache-ido-read "File: "
                                          (mapcar
                                           (lambda (x)
                                             (car x))
                                           file-cache-alist))))
  (let* ((record (assoc file file-cache-alist)))
    (find-file
     (expand-file-name
      file
      (if (= (length record) 2)
          (car (cdr record))
        (file-cache-ido-read
         (format "Find %s in dir: " file) (cdr record)))))))

(defun file-cache-ido-read (prompt choices)
  (let ((ido-make-buffer-list-hook
	 (lambda ()
	   (setq ido-temp-list choices))))
    (ido-read-buffer prompt)))
(add-to-list 'file-cache-filter-regexps "docs/html")
(add-to-list 'file-cache-filter-regexps "\\.svn-base$")
(add-to-list 'file-cache-filter-regexps "\\.dump$")
Short URL: http://sachachua.com/blog/p/5463

On This Day...

  • 2012: Quantified Self: Learning from a year of time data and planning what to tweak in 2013 — Last year, I decided to move from tracking my time using off-the-shelf applications (Time Recording, then Tap Log Record) to [...]
  • 2011: Decision review: Decision review — In September, I wrote about how I could get better at making decisions. I switched to organizing my decisions in [...]
  • 2010: Reflecting on life as an experiment, gender gaps, and privilege — Is there a gender gap for self-experimentation? Maybe. I’m not sure. But I can tell you about the things I [...]
  • 2009: After the tea party — Almost six hours of conversation over tea and assorted goodies. (I made home-baked vegan apple pie and non-vegan biscuits!) Somehow, the [...]
  • 2009: Book: The Hamster Revolution for Meetings — The Hamster Revolution for Meetings: How to Meet Less and Get More DoneMike Song, Vicki Halsey, and Tim Buress, [...]
  • 2007: How to talk to execs and clients about social media — “Know the differences between Technology, Features, Benefits, and Value,” Jeremiah Owyang advises in his blog post about effectively talking to executives [...]
  • 2007: Dec 17: Awesome!
  • 2006: Ah, found wallet — Yay! Random Emacs symbol: delete-frame – Command: Delete FRAME, permanently eliminating it from use.
  • 2006: Not panicking — My wallet exists somewhere in my suite. I know because I had to take my room key out of it in [...]
  • 2003: kernel-janitor-discuss — The kernel-janitor-discuss archives on http://sourceforge.net/mailarchive/forum.php?forum_id=2314 seem to be spam-filled. I’m tempted to work on the balancing-functions task, as that doesn’t require too much [...]
  • 2003: Open source for school projects — I think the idea of using open source for class projects as suggested by http://www.kegel.com/academy/opensource.html is a good thing. The page also [...]
  • 2003: Prompting — Although unconditional prompting requires an extra keystroke, it makes blogging more consistent (no need to think about if you’re on a [...]
  • 2003: Mirrored blogs — I’m thinking of making it easier for me to blog to my http://www.advogato.org/diary/ . I should look at the blogging Emacs modules [...]
  • 2003: Blogging contests? — As requested by Marcelle, have checked out blog competition as he is in running for Best Philippine Blog – but by very [...]

Get the highlights as a PDF!

Stories from my Twenties: Highlights from a Decade of Blogging

Free sample!