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