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