;;;;
;;;;;; Version Control / Ediff
;;;;

(defadvice ediff-window-display-p (after eaw-ediff-window-display-p)
  "Force window display instead of frame display"
  (setq ad-return-value nil))
(ad-activate 'ediff-window-display-p)

;; ediff should use a smaller diff
(setq ediff-diff-options "-d")

;; ediff should split horizontally
(setq ediff-split-window-function 'split-window-horizontally)

;; ediff shouldn't create a new frame
(setq ediff-window-setup-function 'ediff-setup-windows-plain)

;; magit
(autoload 'magit-status "magit" nil t)
(defvar eaw-magit-prefix-map
  (let ((map (make-sparse-keymap)))
    (define-key map "o" 'magit-status)
    map)
  "The Prefix for Magit Commands.")
(define-key global-map "\C-xj" eaw-magit-prefix-map)
(setq magit-repository-directories `((,eaw-src-home . 2)))
(setq magit-buffer-name-format "*%M%v: %t*")
(setq magit-delete-by-moving-to-trash nil)
(setq magit-fetch-arguments '("--prune"))
(defun magit-submodule-update-recursive ()
  (interactive)
  (magit-run-git-async "submodule" "update" "--init" "--recursive"))
(eval-after-load "magit"
  '(progn
     (magit-define-popup-action 'magit-submodule-popup
       ?U "Update all (recursively)" 'magit-submodule-update-recursive)
     (if (boundp 'magit-file-mode-map)
         (define-key magit-file-mode-map (kbd "C-x g") eaw-proj-map))
     (remove-hook 'magit-refs-sections-hook 'magit-insert-tags)
     (define-key eaw-proj-map "C" 'magit-clean)))
(if (file-accessible-directory-p "~/.git")
    (magit-status "~/"))

;; debian does this for me, mac does not
(autoload 'svn-status "psvn" nil t)

;; it's crazy that it steals C-x C-j for dired
(add-hook 'svn-status-mode-hook '(lambda () (define-key svn-status-mode-map (kbd "C-x C-j") nil)))

;; remove temporary files
(setq svn-status-ediff-delete-temporary-files t)

;; give a warning when following symlinks, don't ask
(setq vc-follow-symlinks nil)

;; C-x v - to use ediff to diff
(defun ediff-current-buffer-revision ()
  (interactive)
  (ediff-load-version-control)
  (if (buffer-file-name)
      (funcall (intern
        (format "ediff-%S-internal" ediff-version-control-package)) "" ""
        `((lambda ()
            (make-local-variable 'ediff-cleanup-hook)
            (add-hook 'ediff-cleanup-hook '(lambda () (kill-buffer ediff-buffer-A))))))))
(define-key vc-prefix-map "-" 'ediff-current-buffer-revision)
(autoload 'ediff-load-version-control "ediff" nil t)

(provide 'ew-vc)