+;; Rename buffers along with files.
+
+(defvar mdw-inhibit-rename-buffer nil
+ "If non-nil, `rename-file' won't rename the buffer visiting the file.")
+
+(defmacro mdw-advise-to-inhibit-rename-buffer (function)
+ "Advise FUNCTION to set `mdw-inhibit-rename-buffer' while it runs.
+
+This will prevent `rename-file' from renaming the buffer."
+ `(defadvice ,function (around mdw-inhibit-rename-buffer compile activate)
+ "Don't rename the buffer when renaming the underlying file."
+ (let ((mdw-inhibit-rename-buffer t))
+ ad-do-it)))
+(mdw-advise-to-inhibit-rename-buffer recode-file-name)
+(mdw-advise-to-inhibit-rename-buffer set-visited-file-name)
+(mdw-advise-to-inhibit-rename-buffer backup-buffer)
+
+(defadvice rename-file (after mdw-rename-buffers (from to &optional forcep)
+ compile activate)
+ "If a buffer is visiting the file, rename it to match the new name.
+
+Don't do this if `mdw-inhibit-rename-buffer' is non-nil."
+ (unless mdw-inhibit-rename-buffer
+ (let ((buffer (get-file-buffer from)))
+ (when buffer
+ (with-current-buffer buffer
+ (set-visited-file-name to nil t))))))
+
+;;;--------------------------------------------------------------------------
+;;; Improved compilation machinery.
+
+;; Uprated version of M-x compile.
+
+(setq compile-command
+ (let ((ncpu (with-temp-buffer
+ (insert-file-contents "/proc/cpuinfo")
+ (buffer-string)
+ (count-matches "^processor\\s-*:"))))
+ (format "make -j%d -k" (* 2 ncpu))))
+
+(defun mdw-compilation-buffer-name (mode)
+ (concat "*" (downcase mode) ": "
+ (abbreviate-file-name default-directory) "*"))
+(setq compilation-buffer-name-function 'mdw-compilation-buffer-name)
+
+(eval-after-load "compile"
+ '(progn
+ (define-key compilation-shell-minor-mode-map "\C-c\M-g" 'recompile)))
+
+(defadvice compile (around hack-environment compile activate)
+ "Hack the environment inherited by inferiors in the compilation."
+ (let ((process-environment (copy-tree process-environment)))
+ (setenv "LD_PRELOAD" nil)
+ ad-do-it))
+
+(defun mdw-compile (command &optional directory comint)
+ "Initiate a compilation COMMAND, maybe in a different DIRECTORY.
+The DIRECTORY may be nil to not change. If COMINT is t, then
+start an interactive compilation.
+
+Interactively, prompt for the command if the variable
+`compilation-read-command' is non-nil, or if requested through
+the prefix argument. Prompt for the directory, and run
+interactively, if requested through the prefix.
+
+Use a prefix of 4, 6, 12, or 14, or type C-u between one and three times, to
+force prompting for a directory.
+
+Use a prefix of 2, 6, 10, or 14, or type C-u three times, to force
+prompting for the command.
+
+Use a prefix of 8, 10, 12, or 14, or type C-u twice or three times,
+to force interactive compilation."
+ (interactive
+ (let* ((prefix (prefix-numeric-value current-prefix-arg))
+ (command (eval compile-command))
+ (dir (and (plusp (logand prefix #x54))
+ (read-directory-name "Compile in directory: "))))
+ (list (if (or compilation-read-command
+ (plusp (logand prefix #x42)))
+ (compilation-read-command command)
+ command)
+ dir
+ (plusp (logand prefix #x58)))))
+ (let ((default-directory (or directory default-directory)))
+ (compile command comint)))
+
+;; Flymake support.
+
+(defun mdw-find-build-dir (build-file)
+ (catch 'found
+ (let* ((src-dir (file-name-as-directory (expand-file-name ".")))
+ (dir src-dir))
+ (loop
+ (when (file-exists-p (concat dir build-file))
+ (throw 'found dir))
+ (let ((sub (expand-file-name (file-relative-name src-dir dir)
+ (concat dir "build/"))))
+ (catch 'give-up
+ (loop
+ (when (file-exists-p (concat sub build-file))
+ (throw 'found sub))
+ (when (string= sub dir) (throw 'give-up nil))
+ (setq sub (file-name-directory (directory-file-name sub))))))
+ (when (string= dir
+ (setq dir (file-name-directory
+ (directory-file-name dir))))
+ (throw 'found nil))))))
+
+(defun mdw-flymake-make-init ()
+ (let ((build-dir (mdw-find-build-dir "Makefile")))
+ (and build-dir
+ (let ((tmp-src (flymake-init-create-temp-buffer-copy
+ #'flymake-create-temp-inplace)))
+ (flymake-get-syntax-check-program-args
+ tmp-src build-dir t t
+ #'flymake-get-make-cmdline)))))
+
+(setq flymake-allowed-file-name-masks
+ '(("\\.\\(?:[cC]\\|cc\\|cpp\\|cxx\\|c\\+\\+\\)\\'"
+ mdw-flymake-make-init)
+ ("\\.\\(?:[hH]\\|hh\\|hpp\\|hxx\\|h\\+\\+\\)\\'"
+ mdw-flymake-master-make-init)
+ ("\\.p[lm]" flymake-perl-init)))
+
+(setq flymake-mode-map
+ (let ((map (if (boundp 'flymake-mode-map)
+ flymake-mode-map
+ (make-sparse-keymap))))
+ (define-key map [?\C-c ?\C-f ?\C-p] 'flymake-goto-prev-error)
+ (define-key map [?\C-c ?\C-f ?\C-n] 'flymake-goto-next-error)
+ (define-key map [?\C-c ?\C-f ?\C-c] 'flymake-compile)
+ (define-key map [?\C-c ?\C-f ?\C-k] 'flymake-stop-all-syntax-checks)
+ (define-key map [?\C-c ?\C-f ?\C-e] 'flymake-popup-current-error-menu)
+ map))
+