chiark / gitweb /
Emacs mode: Bind n and p
[stgit] / contrib / stgit.el
CommitLineData
3a59f3db
KH
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
56d81fe5
DK
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.
33Argument 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"
34afb86c
DK
44 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
45 (stgit-dir default-directory)
46 (inhibit-read-only t))
56d81fe5 47 (with-current-buffer output-buf
34afb86c
DK
48 (erase-buffer)
49 (setq default-directory stgit-dir)
50 (setq buffer-read-only t))
56d81fe5
DK
51 (let ((standard-output output-buf))
52 ,@body)
34afb86c
DK
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)))))
56d81fe5
DK
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")
2870f8b8 72 (stgit-run "series" "--description")
56d81fe5
DK
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)
0663524d
KH
86 (define-key stgit-mode-map "?" 'stgit-help)
87 (define-key stgit-mode-map "h" 'stgit-help)
9a3bb1f1
DK
88 (define-key stgit-mode-map "p" 'previous-line)
89 (define-key stgit-mode-map "n" 'next-line)
56d81fe5
DK
90 (define-key stgit-mode-map "g" 'stgit-refresh)
91 (define-key stgit-mode-map "r" 'stgit-rename)
92 (define-key stgit-mode-map ">" 'stgit-push-next)
82a12e6e 93 (define-key stgit-mode-map "<" 'stgit-pop-next)
f9182fca 94 (define-key stgit-mode-map "P" 'stgit-push-or-pop)
c7adf5ef 95 (define-key stgit-mode-map "G" 'stgit-goto)
56d81fe5
DK
96 (define-key stgit-mode-map "=" 'stgit-show))
97
98(defun stgit-mode ()
99 "Major mode for interacting with StGit.
100Commands:
101\\{stgit-mode-map}"
102 (kill-all-local-variables)
103 (buffer-disable-undo)
104 (setq mode-name "StGit"
105 major-mode 'stgit-mode
106 goal-column 2)
107 (use-local-map stgit-mode-map)
108 (set (make-local-variable 'list-buffers-directory) default-directory)
2870f8b8 109 (set-variable 'truncate-lines 't)
56d81fe5
DK
110 (run-hooks 'stgit-mode-hook))
111
112(defun stgit-patch-at-point ()
113 "Return the patch name on the current line"
114 (save-excursion
115 (beginning-of-line)
2870f8b8 116 (if (looking-at "[>+-] \\([^ ]*\\)")
56d81fe5
DK
117 (match-string 1)
118 nil)))
119
120(defun stgit-goto-patch (patch)
121 "Move point to the line containing PATCH"
122 (let ((p (point)))
123 (goto-char (point-min))
2870f8b8 124 (if (re-search-forward (concat "[>+-] " (regexp-quote patch) " ") nil t)
56d81fe5
DK
125 (progn (move-to-column goal-column)
126 t)
127 (goto-char p)
128 nil)))
129
130(defun stgit-rename (name)
131 "Rename the patch under point"
132 (interactive (list (read-string "Patch name: " (stgit-patch-at-point))))
133 (let ((old-name (stgit-patch-at-point)))
134 (unless old-name
135 (error "No patch on this line"))
136 (stgit-capture-output nil
137 (stgit-run "rename" old-name name))
138 (stgit-refresh)
139 (stgit-goto-patch name)))
140
141(defun stgit-push-next ()
82a12e6e 142 "Push the first unapplied patch"
56d81fe5 143 (interactive)
82a12e6e
KH
144 (stgit-capture-output nil (stgit-run "push"))
145 (stgit-refresh))
56d81fe5 146
82a12e6e
KH
147(defun stgit-pop-next ()
148 "Pop the topmost applied patch"
56d81fe5 149 (interactive)
82a12e6e
KH
150 (stgit-capture-output nil (stgit-run "pop"))
151 (stgit-refresh))
56d81fe5 152
f9182fca
KH
153(defun stgit-applied-at-point ()
154 "Is the patch on the current line applied?"
155 (save-excursion
156 (beginning-of-line)
157 (looking-at "[>+]")))
158
159(defun stgit-push-or-pop ()
160 "Push or pop the patch on the current line"
161 (interactive)
162 (let ((patch (stgit-patch-at-point))
163 (applied (stgit-applied-at-point)))
164 (stgit-capture-output nil
165 (stgit-run (if applied "pop" "push") patch))
166 (stgit-refresh)))
167
c7adf5ef
KH
168(defun stgit-goto ()
169 "Go to the patch on the current line"
170 (interactive)
171 (let ((patch (stgit-patch-at-point)))
172 (stgit-capture-output nil
173 (stgit-run "goto" patch))
174 (stgit-refresh)))
175
56d81fe5
DK
176(defun stgit-show ()
177 "Show the patch on the current line"
178 (interactive)
179 (stgit-capture-output "*StGit patch*"
180 (stgit-run "show" (stgit-patch-at-point))
181 (with-current-buffer standard-output
182 (goto-char (point-min))
183 (diff-mode))))
0663524d
KH
184
185(defun stgit-help ()
186 "Display help for the StGit mode."
187 (interactive)
188 (describe-function 'stgit-mode))
3a59f3db
KH
189
190(provide 'stgit)