Alex Bennee left a comment on my dollar editor post:
I then open a number of separate emacs processes for each project I’m actively working on. I mainly do this as each project typically has it’s own make invocation.
I have a similar issue myself where my work projects require different commands but I prefer to avoid running multiple emacs instances if possible. It is not too difficult to fix. The idea is to detect the name of the file and run a different make command depending on which directory the file is in. This can be extended to as many different directories as necessary.
(setq compilation-scroll-output t) (defun make-lib () (compile "make-lib-cmd")) (defun make-app () (compile "make-app-cmd")) (defun run-compile () (interactive) (let ((file (buffer-file-name (current-buffer)))) (cond ((not (stringp file)) (error "[%s] is not a string. Invalid buffer %s ?" f (buffer-name))) ((string-match "/src/lib/" file) (make-lib)) ((string-match "/src/app/" file) (make-app)) (t (error "Invalid location %s" file))))) (define-key c-mode-base-map [f7] 'run-compile)
This is a problem there are many solutions too. I set up a “current-project-root”
which I use as the basis for for decisions about invoking compile
commands. The setup here is actually quite simple and is usually
more complex when I have customised compile commands when working in
proprietary environments.
Many people have had similar
ideas. Maybe one day I’ll either properly rationalise my project code
or use another project package.
I think, that it’s better to look to the EDE package from Cedet (http://cedet.sf.net) – it allows to specify different targets, and build them separately
In Emacs 23 you could use directory local variables to set the build command (and other stuff) per project.
Either add .dir-locals.el in the project root or C-h f dir-locals-set-directory-class and dir-locals-set-class-variables
[…] bookmarks tagged curious A Curious Programmer: Jared saved by 4 others Head1st4Halos7 bookmarked on 02/13/09 | […]
Sounds like an opportunity for me to plug project-root, which is available from:
http://www.shellarchive.co.uk/content/emacs.html
It has a simple, clean and out of the way interface which is really good and will make women like you and stuff.
Seconding the Emacs 23 directory-local variables opinion.
See http://atomized.org/2009/01/emacs-23-directory-local-variables/ for info about setting it up.
You should be able to change compile-command pretty easily with that.
If you prefer your approach, you may as well put the compile commands into run-compile:
(defun run-compile ()
(interactive)
(let ((file (buffer-file-name (current-buffer))))
(compile (cond
((string-match “/src/lib/” file) “make-lib-command”)
((string-match “/src/app/” file) “make-app-command”)
(t (error “Invalid unknown/location %s”
(or file (buffer-name))))))))
Hi folks,
Thanks for the comments – great info as usual.
@Alex B – I had a quick look at your .emacs and the bit I can see that that sets the compile is:
(setq compile-command (format “cd %s && make -k” current-project-root)).
It looks like it is always calling make-k. Or have I missed something?
Thanks for the pointer to ProjectSettings. That looks very handy (although is it superceded by directory local variables?) .
@Alex O – I had a look at CEDET a couple of years ago and I found it very impressive although I decided it wasn’t for me. I don’t really want to change Emacs into a fully-fledged IDE. However, I do like pressing f7 and C-c ` to o to the different errors 🙂
By the way I went over to take a quick look so I could make an informed comment. Is this intended to replace an entire build system?
@Joakim and Ian – Yes, directory-local variables look like what I want. I’ll probably change over to use them instead. And Ian, nice fix to remove the helper functions.
@phil – I really like the look of project-root. I have a home-grown thing that I call aliases for finding various files but I think project-root is much more comprehensive. Cheers
It is simple at the moment because I’m not working with much stuff
that needs complex make invocations. Most FLOSS code is ready to build
with a straight “make -k”. However from time to time when I’m working
at a proprietary shop I usually code up slightly more
complex project-root stuff. One day if I ever have the need I may
generalise the code a bit more and make it deal nicely with buffer
local variables. However I don’t need to at the moment, necessity
being the main driver for such things 🙂
[…] post on providing different compile commands in different directories resulted in some interesting comments on directory local variables and […]
@Alex
Aint that the truth 🙂