Skip to content

Instantly share code, notes, and snippets.

@NeoCat
Created May 23, 2012 07:40
Show Gist options
  • Save NeoCat/2773738 to your computer and use it in GitHub Desktop.
Save NeoCat/2773738 to your computer and use it in GitHub Desktop.
.emacs to bind "C-c c" to `make' in the directory with the nearest Makefile
(require 'cl)
(defun* get-closest-file-dir (&optional (file "Makefile"))
"Determine the directory of the first instance of FILE starting from the current directory towards root.
This may not do the correct thing in presence of links. If it does not find FILE, then it shall return nil."
(let ((root (expand-file-name "/")))
(loop
for d = default-directory then (expand-file-name ".." d)
if (file-exists-p (expand-file-name file d)) return d
if (equal d root) return nil)))
(require 'compile)
(add-hook 'c-mode-common-hook
'(lambda ()
(define-key c-mode-base-map "\C-cc" 'compile)
(define-key c-mode-base-map "\C-ce" 'next-error)
(c-toggle-hungry-state 1)
(c-set-style "linux")
(setq tab-width 8)
(if (equal compile-command "make -k ") ;; default
(set 'compile-command
(format "make -C %s " (get-closest-file-dir))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment