82f0d3858c7b8fc786d6dc895b6702edbf0660b4
[stgit] / contrib / stgit.el
1 ;; stgit.el: An emacs mode for StGit
2 ;;
3 ;; Copyright (C) 2007 David Kågedal <davidk@lysator.liu.se>
4 ;;
5 ;; To install: put this file on the load-path and place the following
6 ;; in your .emacs file:
7 ;;
8 ;; (require 'stgit)
9 ;;
10 ;; To start: `M-x stgit'
11
12 (defun stgit (dir)
13 "Manage stgit patches"
14 (interactive "DDirectory: \n")
15 (switch-to-stgit-buffer dir)
16 (stgit-refresh))
17
18 (defun switch-to-stgit-buffer (dir)
19 "Switch to a (possibly new) buffer displaying StGit patches for DIR"
20 (setq dir (file-name-as-directory dir))
21 (let ((buffers (buffer-list)))
22 (while (and buffers
23 (not (with-current-buffer (car buffers)
24 (and (eq major-mode 'stgit-mode)
25 (string= default-directory dir)))))
26 (setq buffers (cdr buffers)))
27 (switch-to-buffer (if buffers
28 (car buffers)
29 (create-stgit-buffer dir)))))
30
31 (defun create-stgit-buffer (dir)
32 "Create a buffer for showing StGit patches.
33 Argument DIR is the repository path."
34 (let ((buf (create-file-buffer (concat dir "*stgit*")))
35 (inhibit-read-only t))
36 (with-current-buffer buf
37 (setq default-directory dir)
38 (stgit-mode)
39 (setq buffer-read-only t))
40 buf))
41
42 (defmacro stgit-capture-output (name &rest body)
43 "Capture StGit output and show it in a window at the end"
44 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
45 (stgit-dir default-directory)
46 (inhibit-read-only t))
47 (with-current-buffer output-buf
48 (erase-buffer)
49 (setq default-directory stgit-dir)
50 (setq buffer-read-only t))
51 (let ((standard-output output-buf))
52 ,@body)
53 (with-current-buffer output-buf
54 (set-buffer-modified-p nil)
55 (setq buffer-read-only t)
56 (if (< (point-min) (point-max))
57 (display-buffer output-buf t)))))
58 (put 'stgit-capture-output 'lisp-indent-function 1)
59
60 (defun stgit-run (&rest args)
61 (apply 'call-process "stg" nil standard-output nil args))
62
63 (defun stgit-refresh ()
64 "Update the contents of the stgit buffer"
65 (interactive)
66 (let ((inhibit-read-only t)
67 (curline (line-number-at-pos))
68 (curpatch (stgit-patch-at-point)))
69 (erase-buffer)
70 (insert "Branch: ")
71 (stgit-run "branch")
72 (stgit-run "series" "--description")
73 (if curpatch
74 (stgit-goto-patch curpatch)
75 (goto-line curline))))
76
77 (defvar stgit-mode-hook nil
78 "Run after `stgit-mode' is setup.")
79
80 (defvar stgit-mode-map nil
81 "Keymap for StGit major mode.")
82
83 (unless stgit-mode-map
84 (setq stgit-mode-map (make-keymap))
85 (suppress-keymap stgit-mode-map)
86 (define-key stgit-mode-map "?" 'stgit-help)
87 (define-key stgit-mode-map "h" 'stgit-help)
88 (define-key stgit-mode-map "g" 'stgit-refresh)
89 (define-key stgit-mode-map "r" 'stgit-rename)
90 (define-key stgit-mode-map ">" 'stgit-push-next)
91 (define-key stgit-mode-map "<" 'stgit-pop-next)
92 (define-key stgit-mode-map "P" 'stgit-push-or-pop)
93 (define-key stgit-mode-map "G" 'stgit-goto)
94 (define-key stgit-mode-map "=" 'stgit-show))
95
96 (defun stgit-mode ()
97 "Major mode for interacting with StGit.
98 Commands:
99 \\{stgit-mode-map}"
100 (kill-all-local-variables)
101 (buffer-disable-undo)
102 (setq mode-name "StGit"
103 major-mode 'stgit-mode
104 goal-column 2)
105 (use-local-map stgit-mode-map)
106 (set (make-local-variable 'list-buffers-directory) default-directory)
107 (set-variable 'truncate-lines 't)
108 (run-hooks 'stgit-mode-hook))
109
110 (defun stgit-patch-at-point ()
111 "Return the patch name on the current line"
112 (save-excursion
113 (beginning-of-line)
114 (if (looking-at "[>+-] \\([^ ]*\\)")
115 (match-string 1)
116 nil)))
117
118 (defun stgit-goto-patch (patch)
119 "Move point to the line containing PATCH"
120 (let ((p (point)))
121 (goto-char (point-min))
122 (if (re-search-forward (concat "[>+-] " (regexp-quote patch) " ") nil t)
123 (progn (move-to-column goal-column)
124 t)
125 (goto-char p)
126 nil)))
127
128 (defun stgit-rename (name)
129 "Rename the patch under point"
130 (interactive (list (read-string "Patch name: " (stgit-patch-at-point))))
131 (let ((old-name (stgit-patch-at-point)))
132 (unless old-name
133 (error "No patch on this line"))
134 (stgit-capture-output nil
135 (stgit-run "rename" old-name name))
136 (stgit-refresh)
137 (stgit-goto-patch name)))
138
139 (defun stgit-push-next ()
140 "Push the first unapplied patch"
141 (interactive)
142 (stgit-capture-output nil (stgit-run "push"))
143 (stgit-refresh))
144
145 (defun stgit-pop-next ()
146 "Pop the topmost applied patch"
147 (interactive)
148 (stgit-capture-output nil (stgit-run "pop"))
149 (stgit-refresh))
150
151 (defun stgit-applied-at-point ()
152 "Is the patch on the current line applied?"
153 (save-excursion
154 (beginning-of-line)
155 (looking-at "[>+]")))
156
157 (defun stgit-push-or-pop ()
158 "Push or pop the patch on the current line"
159 (interactive)
160 (let ((patch (stgit-patch-at-point))
161 (applied (stgit-applied-at-point)))
162 (stgit-capture-output nil
163 (stgit-run (if applied "pop" "push") patch))
164 (stgit-refresh)))
165
166 (defun stgit-goto ()
167 "Go to the patch on the current line"
168 (interactive)
169 (let ((patch (stgit-patch-at-point)))
170 (stgit-capture-output nil
171 (stgit-run "goto" patch))
172 (stgit-refresh)))
173
174 (defun stgit-show ()
175 "Show the patch on the current line"
176 (interactive)
177 (stgit-capture-output "*StGit patch*"
178 (stgit-run "show" (stgit-patch-at-point))
179 (with-current-buffer standard-output
180 (goto-char (point-min))
181 (diff-mode))))
182
183 (defun stgit-help ()
184 "Display help for the StGit mode."
185 (interactive)
186 (describe-function 'stgit-mode))
187
188 (provide 'stgit)