I'm currently trying to move from vim to emacs so be gentle with me ;)
In Vim, one of my favorite features is auto commands, I'll have one for each project for example:
au BufEnter c:/projects/foo/* :call FooSetup()
au BufEnter c:/projects/bar/* :call BarSetup()
This will call the correct setup whenever I switch buffers and change setting like the root directory, make program, error format etc.
The closest emacs equivalent I've been able to find is mode hooks, which seem much more limited.
Edit - The solution, by combining both answers below:
(defun my/test ()
(when (and buffer-file-name (string-match "c:/projects/foo/.*" (buffer-file-name)))
(message "foo")
(setq default-directory "c:/projects/foo"))
(when (and buffer-file-name (string-match "c:/projects/bar/.*" (buffer-file-name)))
(message "bar")
(setq default-directory "c:/projects/bar")))
(add-hook 'buffer-list-update-hook #'my/test)
.dir-locals.el
or Directory Variables. – Kaushal Modi Jan 14 '16 at 02:50