updates
[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                                 "magit"))
8
9 (add-to-list 'load-path (concat (file-name-directory
10                                  (directory-file-name
11                                   (file-name-directory
12                                    (or load-file-name
13                                        (when (boundp 'bytecomp-filename) bytecomp-filename)
14                                        buffer-file-name))))
15                                 "git-modes"))
16
17 (add-to-list 'load-path (concat (file-name-directory
18                                  (directory-file-name
19                                   (file-name-directory
20                                    (or load-file-name
21                                        (when (boundp 'bytecomp-filename) bytecomp-filename)
22                                        buffer-file-name))))
23                                 "auto-install"))
24
25 (require 'cl)
26
27 (if (not (functionp 'run-hook-wrapped))
28     (defun run-hook-wrapped (hook wrap-function &rest args)
29       (loop for fn in hook
30             thereis (apply 'wrap-function fn args))))
31
32 (if (not (functionp 'process-live-p))
33     (defun process-live-p (process)
34       (memq (process-status process)
35             '(run open listen connect stop))))
36
37 (require 'magit)
38
39 (when (eq system-type 'windows-nt)
40
41   (require 'advice)
42
43   (setf (symbol-function 'builtin-process-file) (symbol-function 'process-file))
44
45   (defvar my-magit-shell "c:\\Program Files (x86)\\Git\\bin\\sh")
46
47   (defun my-magit-process-file (program &optional infile buffer display &rest args)
48     (builtin-process-file my-magit-shell infile buffer display
49                           "-c" (mapconcat 'shell-quote-argument (cons "/bin/git" args) " ")))
50
51   (defadvice magit-cmd-output (around my-magit-process-file activate)
52     (letf (((symbol-function 'process-file) (symbol-function 'my-magit-process-file)))
53       ad-do-it))
54
55   (defadvice magit-git-exit-code (around my-magit-process-file activate)
56     (letf (((symbol-function 'process-file) (symbol-function 'my-magit-process-file)))
57       ad-do-it))
58
59   (defadvice magit-run (around activate)
60     (letf (((symbol-function 'process-file) (symbol-function 'my-magit-process-file)))
61       ad-do-it))
62
63 ); End Windows-NT
64
65 (defadvice magit-mode-quit-window (around my-magit-mode-quit-window activate)
66   (letf (((symbol-function 'selected-window) (lambda ())))
67     ad-do-it))
68
69 (global-set-key "\C-cGS" 'magit-status)
70
71 (defun find-file-in-git-repo ()
72   (interactive)
73   (let* ((repo (magit-get-top-dir default-directory))
74          (files (shell-command-to-string (format "cd %s && git ls-files" repo))))
75     (find-file
76      (concat repo
77              (ido-completing-read
78               "Find file in git repo: "
79               (remove-if (lambda (x) (string= "" x))
80               (split-string files "\n")))))))
81
82 (defun grep-in-git-repo (regexp &optional words-only)
83   (interactive "sGrep files in Git repo regexp: \np")
84   (let ((default-directory (magit-get-top-dir default-directory)))
85     (if (not default-directory)
86         (error "not a Git directory"))
87     (grep (format "git ls-files -z | xargs -r0 grep -nH -E%s -- %s | cat -"
88                   (if words-only " -w" "") (shell-quote-argument regexp)))))
89
90 (setenv "GIT_PAGER" "cat")
91 (setenv "GIT_MAN_VIEWER" "woman")
92 (setenv "GIT_EDITOR" "emacsclient")
93
94 (defun find-file-maybe-git (&optional nogit)
95   (interactive "P")
96   (if (and (not nogit) (magit-get-top-dir default-directory))
97       (call-interactively 'find-file-in-git-repo)
98     (call-interactively 'ido-find-file)))
99
100 (global-set-key "\C-x\C-f" 'find-file-maybe-git)
101 (global-set-key "\C-cGG" 'grep-in-git-repo)
102
103 (defun git-files-find-symbol (symbol)
104   (interactive (list (read-string "Symbol: " (current-word))))
105   (let ((dir (magit-get-top-dir default-directory)))
106     (if (not dir) (error "No git repository"))
107     (let ((default-directory dir))
108       (grep (format "git ls-files -z | xargs -r0 grep -nwHF %s | cat -" symbol)))))
109
110 (global-set-key "\C-cGF" 'git-files-find-symbol)
111
112 (defun dired-git-files ()
113   (interactive)
114   (let ((default-directory (magit-get-top-dir default-directory))
115         (ls-lisp-use-insert-directory-program t))
116     (dired (cons default-directory (split-string (shell-command-to-string "git ls-files") "\n")))))
117
118 (global-set-key "\C-cGD" 'dired-git-files)
119
120 (defun magit-svn-fetch ()
121   (interactive)
122   (magit-run-git-async "svn" "fetch"))
123
124 (define-key magit-mode-map "Nf" 'magit-svn-fetch)
125
126 (defun magit-quit-window (&optional kill-buffer)
127   (interactive "P")
128   (quit-window kill-buffer))
129
130 (setq magit-diff-options '("-w"))