1c2b2f078620da08708311f9193d7c74a76ead98
[emacs-init.git] / setup / magit.el
1 (add-to-list 'load-path "~/.emacs.d/magit")
2 (require 'magit)
3 (require 'advice)
4 (require 'cl)
5
6 (setf (symbol-function 'builtin-process-file) (symbol-function 'process-file))
7
8 (defvar my-magit-shell "c:\\Program Files (x86)\\Git\\bin\\sh")
9
10 (defun my-magit-process-file (program &optional infile buffer display &rest args)
11   (builtin-process-file my-magit-shell infile buffer display 
12                         "-c" (mapconcat 'shell-quote-argument (cons "/bin/git" args) " ")))
13
14 (defadvice magit-cmd-output (around my-magit-process-file activate)
15   (letf (((symbol-function 'process-file) (symbol-function 'my-magit-process-file)))
16     ad-do-it))
17
18 (defadvice magit-git-exit-code (around my-magit-process-file activate)
19   (letf (((symbol-function 'process-file) (symbol-function 'my-magit-process-file)))
20     ad-do-it))
21
22 (defadvice magit-run (around activate)
23   (letf (((symbol-function 'process-file) (symbol-function 'my-magit-process-file)))
24     ad-do-it))
25
26 (global-set-key "\C-cGS" 'magit-status)
27
28 (defun find-file-in-git-repo ()
29   (interactive)
30   (let* ((repo (magit-get-top-dir default-directory))
31          (files (shell-command-to-string (format "cd %s && git ls-files" repo))))
32     (find-file
33      (concat repo
34              (ido-completing-read
35               "Find file in git repo: "
36               (remove-if (lambda (x) (string= "" x))
37               (split-string files "\n")))))))
38
39 (defun grep-in-git-repo (regexp)
40   (interactive "sGrep files in Git repo regexp: ")
41   (let ((default-directory (magit-get-top-dir default-directory)))
42     (if (not default-directory)
43         (error "not a Git directory"))
44     (grep (format "git ls-files -z | xargs -r0 grep -nH -E %s | cat -" (shell-quote-argument regexp)))))
45
46 (defun find-file-maybe-git (&optional nogit)
47   (interactive "P")
48   (if (and (not nogit) (magit-get-top-dir default-directory))
49       (call-interactively 'find-file-in-git-repo)
50     (call-interactively 'ido-find-file)))
51
52 (global-set-key "\C-x\C-f" 'find-file-maybe-git)
53 (global-set-key "\C-cGG" 'grep-in-git-repo)
54
55 (defun git-files-find-symbol (&optional arg)
56   (interactive "P")
57   (let ((symbol (current-word))
58         (dir (magit-get-top-dir default-directory)))
59     (if (not dir) (error "No git repository"))
60     (if arg (setq symbol (read-string "Symbol: " nil nil symbol)))
61     (let ((default-directory dir))
62       (grep (format "git ls-files -z | xargs -r0 grep -nHF %s | cat -" symbol)))))
63
64 (global-set-key "\C-cGF" 'git-files-find-symbol)