chiark / gitweb /
stgit new: Do not open editor if --save-template was specified
[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
0f076fe6 12(require 'git nil t)
50d88c67 13(require 'cl)
98230edd 14(require 'ewoc)
0f076fe6 15
56d81fe5 16(defun stgit (dir)
a53347d9 17 "Manage StGit patches for the tree in DIR."
56d81fe5 18 (interactive "DDirectory: \n")
52144ce5 19 (switch-to-stgit-buffer (git-get-top-dir dir))
1f0bf00f 20 (stgit-reload))
56d81fe5 21
074a4fb0
GH
22(unless (fboundp 'git-get-top-dir)
23 (defun git-get-top-dir (dir)
24 "Retrieve the top-level directory of a git tree."
25 (let ((cdup (with-output-to-string
26 (with-current-buffer standard-output
27 (cd dir)
28 (unless (eq 0 (call-process "git" nil t nil
29 "rev-parse" "--show-cdup"))
df283a8b 30 (error "Cannot find top-level git tree for %s" dir))))))
074a4fb0
GH
31 (expand-file-name (concat (file-name-as-directory dir)
32 (car (split-string cdup "\n")))))))
33
34(defun stgit-refresh-git-status (&optional dir)
35 "If it exists, refresh the `git-status' buffer belonging to
36directory DIR or `default-directory'"
37 (when (and (fboundp 'git-find-status-buffer)
38 (fboundp 'git-refresh-status))
39 (let* ((top-dir (git-get-top-dir (or dir default-directory)))
40 (git-status-buffer (and top-dir (git-find-status-buffer top-dir))))
41 (when git-status-buffer
42 (with-current-buffer git-status-buffer
43 (git-refresh-status))))))
52144ce5 44
b894e680
DK
45(defun stgit-find-buffer (dir)
46 "Return the buffer displaying StGit patches for DIR, or nil if none."
56d81fe5
DK
47 (setq dir (file-name-as-directory dir))
48 (let ((buffers (buffer-list)))
49 (while (and buffers
50 (not (with-current-buffer (car buffers)
51 (and (eq major-mode 'stgit-mode)
52 (string= default-directory dir)))))
53 (setq buffers (cdr buffers)))
b894e680
DK
54 (and buffers (car buffers))))
55
56(defun switch-to-stgit-buffer (dir)
57 "Switch to a (possibly new) buffer displaying StGit patches for DIR."
58 (setq dir (file-name-as-directory dir))
59 (let ((buffer (stgit-find-buffer dir)))
60 (switch-to-buffer (or buffer
61 (create-stgit-buffer dir)))))
62
2c862b07 63(defstruct (stgit-patch)
3164eec6 64 status name desc empty files-ewoc)
56d81fe5 65
98230edd
DK
66(defun stgit-patch-pp (patch)
67 (let ((status (stgit-patch-status patch))
68 (start (point))
69 (name (stgit-patch-name patch)))
b894e680
DK
70 (case name
71 (:index (insert (propertize " Index" 'face 'italic)))
72 (:work (insert (propertize " Work tree" 'face 'italic)))
73 (t (insert (case status
74 ('applied "+")
75 ('top ">")
76 ('unapplied "-"))
77 (if (memq name stgit-marked-patches)
78 "*" " ")
79 (propertize (format "%-30s"
80 (symbol-name name))
81 'face (case status
82 ('applied 'stgit-applied-patch-face)
83 ('top 'stgit-top-patch-face)
84 ('unapplied 'stgit-unapplied-patch-face)
85 ('index nil)
86 ('work nil)))
87 " "
88 (if (stgit-patch-empty patch) "(empty) " "")
89 (propertize (or (stgit-patch-desc patch) "")
90 'face 'stgit-description-face))))
f9b82d36 91 (put-text-property start (point) 'entry-type 'patch)
98230edd 92 (when (memq name stgit-expanded-patches)
0de6881a 93 (stgit-insert-patch-files patch))
98230edd
DK
94 (put-text-property start (point) 'patch-data patch)))
95
56d81fe5
DK
96(defun create-stgit-buffer (dir)
97 "Create a buffer for showing StGit patches.
98Argument DIR is the repository path."
99 (let ((buf (create-file-buffer (concat dir "*stgit*")))
100 (inhibit-read-only t))
101 (with-current-buffer buf
102 (setq default-directory dir)
103 (stgit-mode)
98230edd
DK
104 (set (make-local-variable 'stgit-ewoc)
105 (ewoc-create #'stgit-patch-pp "Branch:\n" "--"))
56d81fe5
DK
106 (setq buffer-read-only t))
107 buf))
108
109(defmacro stgit-capture-output (name &rest body)
e558a4ab
GH
110 "Capture StGit output and, if there was any output, show it in a window
111at the end.
112Returns nil if there was no output."
94baef5a
DK
113 (declare (debug ([&or stringp null] body))
114 (indent 1))
34afb86c
DK
115 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
116 (stgit-dir default-directory)
117 (inhibit-read-only t))
56d81fe5 118 (with-current-buffer output-buf
34afb86c
DK
119 (erase-buffer)
120 (setq default-directory stgit-dir)
121 (setq buffer-read-only t))
56d81fe5
DK
122 (let ((standard-output output-buf))
123 ,@body)
34afb86c
DK
124 (with-current-buffer output-buf
125 (set-buffer-modified-p nil)
126 (setq buffer-read-only t)
127 (if (< (point-min) (point-max))
128 (display-buffer output-buf t)))))
56d81fe5 129
d51722b7
GH
130(defun stgit-make-run-args (args)
131 "Return a copy of ARGS with its elements converted to strings."
132 (mapcar (lambda (x)
133 ;; don't use (format "%s" ...) to limit type errors
134 (cond ((stringp x) x)
135 ((integerp x) (number-to-string x))
136 ((symbolp x) (symbol-name x))
137 (t
138 (error "Bad element in stgit-make-run-args args: %S" x))))
139 args))
140
9aecd505 141(defun stgit-run-silent (&rest args)
d51722b7 142 (setq args (stgit-make-run-args args))
56d81fe5
DK
143 (apply 'call-process "stg" nil standard-output nil args))
144
9aecd505 145(defun stgit-run (&rest args)
d51722b7 146 (setq args (stgit-make-run-args args))
9aecd505
DK
147 (let ((msgcmd (mapconcat #'identity args " ")))
148 (message "Running stg %s..." msgcmd)
149 (apply 'call-process "stg" nil standard-output nil args)
150 (message "Running stg %s...done" msgcmd)))
151
378a003d 152(defun stgit-run-git (&rest args)
d51722b7 153 (setq args (stgit-make-run-args args))
378a003d
GH
154 (let ((msgcmd (mapconcat #'identity args " ")))
155 (message "Running git %s..." msgcmd)
156 (apply 'call-process "git" nil standard-output nil args)
157 (message "Running git %s...done" msgcmd)))
158
1f60181a 159(defun stgit-run-git-silent (&rest args)
d51722b7 160 (setq args (stgit-make-run-args args))
1f60181a
GH
161 (apply 'call-process "git" nil standard-output nil args))
162
b894e680
DK
163(defun stgit-index-empty-p ()
164 "Returns non-nil if the index contains no changes from HEAD."
165 (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD")))
166
2ecb05c8
GH
167(defvar stgit-index-node)
168(defvar stgit-worktree-node)
210a2a52
DK
169
170(defun stgit-refresh-index ()
171 (when stgit-index-node
172 (ewoc-invalidate (car stgit-index-node) (cdr stgit-index-node))))
173
174(defun stgit-refresh-worktree ()
175 (when stgit-worktree-node
176 (ewoc-invalidate (car stgit-worktree-node) (cdr stgit-worktree-node))))
177
98230edd
DK
178(defun stgit-run-series (ewoc)
179 (let ((first-line t))
180 (with-temp-buffer
181 (let ((exit-status (stgit-run-silent "series" "--description" "--empty")))
182 (goto-char (point-min))
183 (if (not (zerop exit-status))
184 (cond ((looking-at "stg series: \\(.*\\)")
185 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
186 "-- not initialized (run M-x stgit-init)"))
187 ((looking-at ".*")
188 (error "Error running stg: %s"
189 (match-string 0))))
190 (while (not (eobp))
191 (unless (looking-at
192 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
193 (error "Syntax error in output from stg series"))
194 (let* ((state-str (match-string 2))
195 (state (cond ((string= state-str ">") 'top)
196 ((string= state-str "+") 'applied)
197 ((string= state-str "-") 'unapplied))))
198 (ewoc-enter-last ewoc
199 (make-stgit-patch
200 :status state
201 :name (intern (match-string 4))
202 :desc (match-string 5)
203 :empty (string= (match-string 1) "0"))))
204 (setq first-line nil)
ce3b6130 205 (forward-line 1)))))
210a2a52
DK
206 (if stgit-show-worktree
207 (setq stgit-index-node (cons ewoc (ewoc-enter-last ewoc
208 (make-stgit-patch
209 :status 'index
210 :name :index
211 :desc nil
212 :empty nil)))
213 stgit-worktree-node (cons ewoc (ewoc-enter-last ewoc
214 (make-stgit-patch
215 :status 'work
216 :name :work
217 :desc nil
218 :empty nil))))
219 (setq stgit-worktree-node nil))))
98230edd
DK
220
221
1f0bf00f 222(defun stgit-reload ()
a53347d9 223 "Update the contents of the StGit buffer."
56d81fe5
DK
224 (interactive)
225 (let ((inhibit-read-only t)
226 (curline (line-number-at-pos))
2c862b07 227 (curpatch (stgit-patch-name-at-point)))
98230edd
DK
228 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
229 (ewoc-set-hf stgit-ewoc
230 (concat "Branch: "
231 (propertize
232 (with-temp-buffer
233 (stgit-run-silent "branch")
234 (buffer-substring (point-min) (1- (point-max))))
235 'face 'bold)
236 "\n")
ce3b6130
DK
237 (if stgit-show-worktree
238 "--"
239 (propertize
240 (substitute-command-keys "--\n\"\\[stgit-toggle-worktree]\"\
241 shows the working tree\n")
242 'face 'stgit-description-face)))
98230edd 243 (stgit-run-series stgit-ewoc)
56d81fe5
DK
244 (if curpatch
245 (stgit-goto-patch curpatch)
074a4fb0
GH
246 (goto-line curline)))
247 (stgit-refresh-git-status))
56d81fe5 248
8f40753a
GH
249(defgroup stgit nil
250 "A user interface for the StGit patch maintenance tool."
251 :group 'tools)
252
07f464e0
DK
253(defface stgit-description-face
254 '((((background dark)) (:foreground "tan"))
255 (((background light)) (:foreground "dark red")))
8f40753a
GH
256 "The face used for StGit descriptions"
257 :group 'stgit)
07f464e0
DK
258
259(defface stgit-top-patch-face
260 '((((background dark)) (:weight bold :foreground "yellow"))
261 (((background light)) (:weight bold :foreground "purple"))
262 (t (:weight bold)))
8f40753a
GH
263 "The face used for the top patch names"
264 :group 'stgit)
07f464e0
DK
265
266(defface stgit-applied-patch-face
267 '((((background dark)) (:foreground "light yellow"))
268 (((background light)) (:foreground "purple"))
269 (t ()))
8f40753a
GH
270 "The face used for applied patch names"
271 :group 'stgit)
07f464e0
DK
272
273(defface stgit-unapplied-patch-face
274 '((((background dark)) (:foreground "gray80"))
275 (((background light)) (:foreground "orchid"))
276 (t ()))
8f40753a
GH
277 "The face used for unapplied patch names"
278 :group 'stgit)
07f464e0 279
1f60181a
GH
280(defface stgit-modified-file-face
281 '((((class color) (background light)) (:foreground "purple"))
282 (((class color) (background dark)) (:foreground "salmon")))
283 "StGit mode face used for modified file status"
284 :group 'stgit)
285
286(defface stgit-unmerged-file-face
287 '((((class color) (background light)) (:foreground "red" :bold t))
288 (((class color) (background dark)) (:foreground "red" :bold t)))
289 "StGit mode face used for unmerged file status"
290 :group 'stgit)
291
292(defface stgit-unknown-file-face
293 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
294 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
295 "StGit mode face used for unknown file status"
296 :group 'stgit)
297
a6d9a852
GH
298(defface stgit-file-permission-face
299 '((((class color) (background light)) (:foreground "green" :bold t))
300 (((class color) (background dark)) (:foreground "green" :bold t)))
301 "StGit mode face used for permission changes."
302 :group 'stgit)
303
1f60181a
GH
304(defcustom stgit-expand-find-copies-harder
305 nil
306 "Try harder to find copied files when listing patches.
307
308When not nil, runs git diff-tree with the --find-copies-harder
309flag, which reduces performance."
310 :type 'boolean
311 :group 'stgit)
312
313(defconst stgit-file-status-code-strings
314 (mapcar (lambda (arg)
315 (cons (car arg)
a6d9a852
GH
316 (propertize (cadr arg) 'face (car (cddr arg)))))
317 '((add "Added" stgit-modified-file-face)
318 (copy "Copied" stgit-modified-file-face)
319 (delete "Deleted" stgit-modified-file-face)
320 (modify "Modified" stgit-modified-file-face)
321 (rename "Renamed" stgit-modified-file-face)
322 (mode-change "Mode change" stgit-modified-file-face)
323 (unmerged "Unmerged" stgit-unmerged-file-face)
324 (unknown "Unknown" stgit-unknown-file-face)))
1f60181a
GH
325 "Alist of code symbols to description strings")
326
3164eec6
DK
327(defun stgit-file-status-code-as-string (file)
328 "Return stgit status code for FILE as a string"
329 (let* ((code (assq (stgit-file-status file)
330 stgit-file-status-code-strings))
331 (score (stgit-file-cr-score file)))
332 (when code
a6d9a852 333 (format "%-11s "
3164eec6
DK
334 (if (and score (/= score 100))
335 (format "%s %s" (cdr code)
336 (propertize (format "%d%%" score)
a6d9a852 337 'face 'stgit-description-face))
3164eec6 338 (cdr code))))))
1f60181a 339
a6d9a852 340(defun stgit-file-status-code (str &optional score)
1f60181a
GH
341 "Return stgit status code from git status string"
342 (let ((code (assoc str '(("A" . add)
343 ("C" . copy)
344 ("D" . delete)
345 ("M" . modify)
346 ("R" . rename)
347 ("T" . mode-change)
348 ("U" . unmerged)
349 ("X" . unknown)))))
a6d9a852
GH
350 (setq code (if code (cdr code) 'unknown))
351 (when (stringp score)
352 (if (> (length score) 0)
353 (setq score (string-to-number score))
354 (setq score nil)))
355 (if score (cons code score) code)))
356
357(defconst stgit-file-type-strings
358 '((#o100 . "file")
359 (#o120 . "symlink")
360 (#o160 . "subproject"))
361 "Alist of names of file types")
362
363(defun stgit-file-type-string (type)
47271f41
GH
364 "Return string describing file type TYPE (the high bits of file permission).
365Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
a6d9a852
GH
366 (let ((type-str (assoc type stgit-file-type-strings)))
367 (or (and type-str (cdr type-str))
368 (format "unknown type %o" type))))
369
370(defun stgit-file-type-change-string (old-perm new-perm)
47271f41
GH
371 "Return string describing file type change from OLD-PERM to NEW-PERM.
372Cf. `stgit-file-type-string'."
a6d9a852
GH
373 (let ((old-type (lsh old-perm -9))
374 (new-type (lsh new-perm -9)))
375 (cond ((= old-type new-type) "")
376 ((zerop new-type) "")
377 ((zerop old-type)
378 (if (= new-type #o100)
379 ""
380 (format " (%s)" (stgit-file-type-string new-type))))
381 (t (format " (%s -> %s)"
382 (stgit-file-type-string old-type)
383 (stgit-file-type-string new-type))))))
384
385(defun stgit-file-mode-change-string (old-perm new-perm)
47271f41
GH
386 "Return string describing file mode change from OLD-PERM to NEW-PERM.
387Cf. `stgit-file-type-change-string'."
a6d9a852
GH
388 (setq old-perm (logand old-perm #o777)
389 new-perm (logand new-perm #o777))
390 (if (or (= old-perm new-perm)
391 (zerop old-perm)
392 (zerop new-perm))
393 ""
394 (let* ((modified (logxor old-perm new-perm))
395 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
396 (cond ((zerop modified) "")
397 ((and (zerop not-x-modified)
398 (or (and (eq #o111 (logand old-perm #o111))
399 (propertize "-x" 'face 'stgit-file-permission-face))
400 (and (eq #o111 (logand new-perm #o111))
401 (propertize "+x" 'face
402 'stgit-file-permission-face)))))
403 (t (concat (propertize (format "%o" old-perm)
404 'face 'stgit-file-permission-face)
405 (propertize " -> "
406 'face 'stgit-description-face)
407 (propertize (format "%o" new-perm)
408 'face 'stgit-file-permission-face)))))))
1f60181a 409
0de6881a
DK
410(defstruct (stgit-file)
411 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
412
3164eec6 413(defun stgit-file-pp (file)
0de6881a
DK
414 (let ((status (stgit-file-status file))
415 (name (if (stgit-file-copy-or-rename file)
416 (concat (stgit-file-cr-from file)
417 (propertize " -> "
418 'face 'stgit-description-face)
419 (stgit-file-cr-to file))
420 (stgit-file-file file)))
421 (mode-change (stgit-file-mode-change-string
422 (stgit-file-old-perm file)
423 (stgit-file-new-perm file)))
424 (start (point)))
3164eec6
DK
425 (insert (format " %-12s%1s%s%s\n"
426 (stgit-file-status-code-as-string file)
98230edd 427 mode-change
0de6881a
DK
428 name
429 (propertize (stgit-file-type-change-string
430 (stgit-file-old-perm file)
431 (stgit-file-new-perm file))
98230edd 432 'face 'stgit-description-face)))
0de6881a 433 (add-text-properties start (point)
3164eec6
DK
434 (list 'entry-type 'file
435 'file-data file))))
0de6881a
DK
436
437(defun stgit-insert-patch-files (patch)
88134ff7
GH
438 "Expand (show modification of) the patch PATCH after the line
439at point."
3164eec6
DK
440 (let* ((patchsym (stgit-patch-name patch))
441 (end (progn (insert "#") (prog1 (point-marker) (forward-char -1))))
b894e680
DK
442 (args (list "-z" (if stgit-expand-find-copies-harder
443 "--find-copies-harder"
444 "-C")))
3164eec6
DK
445 (ewoc (ewoc-create #'stgit-file-pp nil nil t)))
446 (setf (stgit-patch-files-ewoc patch) ewoc)
0de6881a 447 (with-temp-buffer
b894e680
DK
448 (apply 'stgit-run-git
449 (cond ((eq patchsym :work)
450 `("diff-files" ,@args))
451 ((eq patchsym :index)
452 `("diff-index" ,@args "--cached" "HEAD"))
453 (t
454 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
0de6881a 455 (goto-char (point-min))
b894e680
DK
456 (unless (or (eobp) (memq patchsym '(:work :index)))
457 (forward-char 41))
0de6881a
DK
458 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
459 (let ((old-perm (string-to-number (match-string 1) 8))
460 (new-perm (string-to-number (match-string 2) 8)))
461 (goto-char (match-end 0))
462 (let ((file
463 (cond ((looking-at
464 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
465 (make-stgit-file
466 :old-perm old-perm
467 :new-perm new-perm
468 :copy-or-rename t
469 :cr-score (string-to-number (match-string 2))
470 :cr-from (match-string 3)
471 :cr-to (match-string 4)
472 :status (stgit-file-status-code (match-string 1))
473 :file (match-string 3)))
474 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
475 (make-stgit-file
476 :old-perm old-perm
477 :new-perm new-perm
478 :copy-or-rename nil
479 :cr-score nil
480 :cr-from nil
481 :cr-to nil
482 :status (stgit-file-status-code (match-string 1))
483 :file (match-string 2))))))
3164eec6
DK
484 (ewoc-enter-last ewoc file))
485 (goto-char (match-end 0))))
486 (unless (ewoc-nth ewoc 0)
487 (ewoc-set-hf ewoc "" (propertize " <no files>\n"
488 'face 'stgit-description-face))))
489 (goto-char end)
490 (delete-char -2)))
07f464e0 491
acc5652f 492(defun stgit-select-file ()
3164eec6
DK
493 (let ((filename (expand-file-name
494 (stgit-file-file (stgit-patched-file-at-point)))))
0de6881a
DK
495 (unless (file-exists-p filename)
496 (error "File does not exist"))
497 (find-file filename)))
acc5652f 498
50d88c67 499(defun stgit-select-patch ()
98230edd
DK
500 (let ((patchname (stgit-patch-name-at-point)))
501 (if (memq patchname stgit-expanded-patches)
502 (setq stgit-expanded-patches (delq patchname stgit-expanded-patches))
503 (setq stgit-expanded-patches (cons patchname stgit-expanded-patches)))
504 (ewoc-invalidate stgit-ewoc (ewoc-locate stgit-ewoc)))
505 (move-to-column (stgit-goal-column)))
acc5652f 506
378a003d
GH
507(defun stgit-select ()
508 "Expand or collapse the current entry"
509 (interactive)
50d88c67
DK
510 (case (get-text-property (point) 'entry-type)
511 ('patch
512 (stgit-select-patch))
513 ('file
514 (stgit-select-file))
515 (t
516 (error "No patch or file on line"))))
378a003d
GH
517
518(defun stgit-find-file-other-window ()
519 "Open file at point in other window"
520 (interactive)
521 (let ((patched-file (stgit-patched-file-at-point)))
522 (unless patched-file
523 (error "No file on the current line"))
3164eec6 524 (let ((filename (expand-file-name (stgit-file-file patched-file))))
378a003d
GH
525 (unless (file-exists-p filename)
526 (error "File does not exist"))
527 (find-file-other-window filename))))
528
83327d53 529(defun stgit-quit ()
a53347d9 530 "Hide the stgit buffer."
83327d53
GH
531 (interactive)
532 (bury-buffer))
533
0f076fe6 534(defun stgit-git-status ()
a53347d9 535 "Show status using `git-status'."
0f076fe6
GH
536 (interactive)
537 (unless (fboundp 'git-status)
df283a8b 538 (error "The stgit-git-status command requires git-status"))
0f076fe6
GH
539 (let ((dir default-directory))
540 (save-selected-window
541 (pop-to-buffer nil)
542 (git-status dir))))
543
58f72f16
GH
544(defun stgit-goal-column ()
545 "Return goal column for the current line"
50d88c67
DK
546 (case (get-text-property (point) 'entry-type)
547 ('patch 2)
548 ('file 4)
549 (t 0)))
58f72f16
GH
550
551(defun stgit-next-line (&optional arg)
378a003d 552 "Move cursor vertically down ARG lines"
58f72f16
GH
553 (interactive "p")
554 (next-line arg)
555 (move-to-column (stgit-goal-column)))
378a003d 556
58f72f16 557(defun stgit-previous-line (&optional arg)
378a003d 558 "Move cursor vertically up ARG lines"
58f72f16
GH
559 (interactive "p")
560 (previous-line arg)
561 (move-to-column (stgit-goal-column)))
378a003d
GH
562
563(defun stgit-next-patch (&optional arg)
98230edd 564 "Move cursor down ARG patches."
378a003d 565 (interactive "p")
98230edd
DK
566 (ewoc-goto-next stgit-ewoc (or arg 1))
567 (move-to-column goal-column))
378a003d
GH
568
569(defun stgit-previous-patch (&optional arg)
98230edd 570 "Move cursor up ARG patches."
378a003d 571 (interactive "p")
98230edd
DK
572 (ewoc-goto-prev stgit-ewoc (or arg 1))
573 (move-to-column goal-column))
378a003d 574
56d81fe5
DK
575(defvar stgit-mode-hook nil
576 "Run after `stgit-mode' is setup.")
577
578(defvar stgit-mode-map nil
579 "Keymap for StGit major mode.")
580
581(unless stgit-mode-map
ce3b6130
DK
582 (let ((toggle-map (make-keymap)))
583 (suppress-keymap toggle-map)
584 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
585 '(("t" . stgit-toggle-worktree)))
586 (setq stgit-mode-map (make-keymap))
587 (suppress-keymap stgit-mode-map)
588 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
589 `((" " . stgit-mark)
590 ("m" . stgit-mark)
591 ("\d" . stgit-unmark-up)
592 ("u" . stgit-unmark-down)
593 ("?" . stgit-help)
594 ("h" . stgit-help)
595 ("\C-p" . stgit-previous-line)
596 ("\C-n" . stgit-next-line)
597 ([up] . stgit-previous-line)
598 ([down] . stgit-next-line)
599 ("p" . stgit-previous-patch)
600 ("n" . stgit-next-patch)
601 ("\M-{" . stgit-previous-patch)
602 ("\M-}" . stgit-next-patch)
603 ("s" . stgit-git-status)
604 ("g" . stgit-reload)
605 ("r" . stgit-refresh)
606 ("\C-c\C-r" . stgit-rename)
607 ("e" . stgit-edit)
608 ("M" . stgit-move-patches)
609 ("S" . stgit-squash)
610 ("N" . stgit-new)
611 ("R" . stgit-repair)
612 ("C" . stgit-commit)
613 ("U" . stgit-uncommit)
614 ("\r" . stgit-select)
615 ("o" . stgit-find-file-other-window)
616 ("i" . stgit-file-toggle-index)
617 (">" . stgit-push-next)
618 ("<" . stgit-pop-next)
619 ("P" . stgit-push-or-pop)
620 ("G" . stgit-goto)
621 ("=" . stgit-show)
622 ("D" . stgit-delete)
623 ([(control ?/)] . stgit-undo)
624 ("\C-_" . stgit-undo)
625 ("B" . stgit-branch)
626 ("t" . ,toggle-map)
627 ("q" . stgit-quit)))))
56d81fe5
DK
628
629(defun stgit-mode ()
630 "Major mode for interacting with StGit.
631Commands:
632\\{stgit-mode-map}"
633 (kill-all-local-variables)
634 (buffer-disable-undo)
635 (setq mode-name "StGit"
636 major-mode 'stgit-mode
637 goal-column 2)
638 (use-local-map stgit-mode-map)
639 (set (make-local-variable 'list-buffers-directory) default-directory)
6df83d42 640 (set (make-local-variable 'stgit-marked-patches) nil)
378a003d 641 (set (make-local-variable 'stgit-expanded-patches) nil)
ce3b6130 642 (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
2ecb05c8
GH
643 (set (make-local-variable 'stgit-index-node) nil)
644 (set (make-local-variable 'stgit-worktree-node) nil)
2870f8b8 645 (set-variable 'truncate-lines 't)
b894e680 646 (add-hook 'after-save-hook 'stgit-update-saved-file)
56d81fe5
DK
647 (run-hooks 'stgit-mode-hook))
648
b894e680
DK
649(defun stgit-update-saved-file ()
650 (let* ((file (expand-file-name buffer-file-name))
651 (dir (file-name-directory file))
652 (gitdir (condition-case nil (git-get-top-dir dir)
653 (error nil)))
654 (buffer (and gitdir (stgit-find-buffer gitdir))))
655 (when buffer
656 (with-current-buffer buffer
210a2a52 657 (stgit-refresh-worktree)))))
b894e680 658
d51722b7
GH
659(defun stgit-add-mark (patchsym)
660 "Mark the patch PATCHSYM."
8036afdd 661 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
6df83d42 662
d51722b7
GH
663(defun stgit-remove-mark (patchsym)
664 "Unmark the patch PATCHSYM."
8036afdd 665 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
6df83d42 666
e6b1fdae 667(defun stgit-clear-marks ()
47271f41 668 "Unmark all patches."
e6b1fdae
DK
669 (setq stgit-marked-patches '()))
670
735cb7ec 671(defun stgit-patch-at-point (&optional cause-error)
2c862b07
DK
672 (get-text-property (point) 'patch-data))
673
674(defun stgit-patch-name-at-point (&optional cause-error)
d51722b7 675 "Return the patch name on the current line as a symbol.
735cb7ec 676If CAUSE-ERROR is not nil, signal an error if none found."
2c862b07
DK
677 (let ((patch (stgit-patch-at-point)))
678 (cond (patch
679 (stgit-patch-name patch))
680 (cause-error
681 (error "No patch on this line")))))
378a003d 682
3164eec6
DK
683(defun stgit-patched-file-at-point ()
684 (get-text-property (point) 'file-data))
56d81fe5 685
7755d7f1 686(defun stgit-patches-marked-or-at-point ()
d51722b7 687 "Return the symbols of the marked patches, or the patch on the current line."
7755d7f1 688 (if stgit-marked-patches
d51722b7 689 stgit-marked-patches
2c862b07 690 (let ((patch (stgit-patch-name-at-point)))
7755d7f1
KH
691 (if patch
692 (list patch)
693 '()))))
694
d51722b7
GH
695(defun stgit-goto-patch (patchsym)
696 "Move point to the line containing patch PATCHSYM.
f9b82d36
DK
697If that patch cannot be found, do nothing."
698 (let ((node (ewoc-nth stgit-ewoc 0)))
699 (while (and node (not (eq (stgit-patch-name (ewoc-data node))
700 patchsym)))
701 (setq node (ewoc-next stgit-ewoc node)))
702 (when node
703 (ewoc-goto-node stgit-ewoc node)
d51722b7 704 (move-to-column goal-column))))
56d81fe5 705
1c2426dc 706(defun stgit-init ()
a53347d9 707 "Run stg init."
1c2426dc
DK
708 (interactive)
709 (stgit-capture-output nil
b0424080 710 (stgit-run "init"))
1f0bf00f 711 (stgit-reload))
1c2426dc 712
6df83d42 713(defun stgit-mark ()
a53347d9 714 "Mark the patch under point."
6df83d42 715 (interactive)
8036afdd
DK
716 (let* ((node (ewoc-locate stgit-ewoc))
717 (patch (ewoc-data node)))
718 (stgit-add-mark (stgit-patch-name patch))
719 (ewoc-invalidate stgit-ewoc node))
378a003d 720 (stgit-next-patch))
6df83d42 721
9b151b27 722(defun stgit-unmark-up ()
a53347d9 723 "Remove mark from the patch on the previous line."
6df83d42 724 (interactive)
378a003d 725 (stgit-previous-patch)
8036afdd
DK
726 (let* ((node (ewoc-locate stgit-ewoc))
727 (patch (ewoc-data node)))
728 (stgit-remove-mark (stgit-patch-name patch))
729 (ewoc-invalidate stgit-ewoc node))
730 (move-to-column (stgit-goal-column)))
9b151b27
GH
731
732(defun stgit-unmark-down ()
a53347d9 733 "Remove mark from the patch on the current line."
9b151b27 734 (interactive)
8036afdd
DK
735 (let* ((node (ewoc-locate stgit-ewoc))
736 (patch (ewoc-data node)))
737 (stgit-remove-mark (stgit-patch-name patch))
738 (ewoc-invalidate stgit-ewoc node))
1288eda2 739 (stgit-next-patch))
6df83d42 740
56d81fe5 741(defun stgit-rename (name)
018fa1ac 742 "Rename the patch under point to NAME."
d51722b7 743 (interactive (list (read-string "Patch name: "
2c862b07
DK
744 (symbol-name (stgit-patch-name-at-point t)))))
745 (let ((old-patchsym (stgit-patch-name-at-point t)))
56d81fe5 746 (stgit-capture-output nil
d51722b7
GH
747 (stgit-run "rename" old-patchsym name))
748 (let ((name-sym (intern name)))
749 (when (memq old-patchsym stgit-expanded-patches)
378a003d 750 (setq stgit-expanded-patches
d51722b7
GH
751 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
752 (when (memq old-patchsym stgit-marked-patches)
378a003d 753 (setq stgit-marked-patches
d51722b7
GH
754 (cons name-sym (delq old-patchsym stgit-marked-patches))))
755 (stgit-reload)
756 (stgit-goto-patch name-sym))))
56d81fe5 757
26201d96 758(defun stgit-repair ()
a53347d9 759 "Run stg repair."
26201d96
DK
760 (interactive)
761 (stgit-capture-output nil
b0424080 762 (stgit-run "repair"))
1f0bf00f 763 (stgit-reload))
26201d96 764
adeef6bc
GH
765(defun stgit-available-branches ()
766 "Returns a list of the available stg branches"
767 (let ((output (with-output-to-string
768 (stgit-run "branch" "--list")))
769 (start 0)
770 result)
771 (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start)
772 (setq result (cons (match-string 1 output) result))
773 (setq start (match-end 0)))
774 result))
775
776(defun stgit-branch (branch)
777 "Switch to branch BRANCH."
778 (interactive (list (completing-read "Switch to branch: "
779 (stgit-available-branches))))
780 (stgit-capture-output nil (stgit-run "branch" "--" branch))
781 (stgit-reload))
782
41c1c59c
GH
783(defun stgit-commit (count)
784 "Run stg commit on COUNT commits.
785Interactively, the prefix argument is used as COUNT."
786 (interactive "p")
787 (stgit-capture-output nil (stgit-run "commit" "-n" count))
1f0bf00f 788 (stgit-reload))
c4aad9a7 789
41c1c59c
GH
790(defun stgit-uncommit (count)
791 "Run stg uncommit on COUNT commits.
792Interactively, the prefix argument is used as COUNT."
c4aad9a7 793 (interactive "p")
41c1c59c 794 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
1f0bf00f 795 (stgit-reload))
c4aad9a7 796
0b661144
DK
797(defun stgit-push-next (npatches)
798 "Push the first unapplied patch.
799With numeric prefix argument, push that many patches."
800 (interactive "p")
d51722b7 801 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
074a4fb0
GH
802 (stgit-reload)
803 (stgit-refresh-git-status))
56d81fe5 804
0b661144
DK
805(defun stgit-pop-next (npatches)
806 "Pop the topmost applied patch.
807With numeric prefix argument, pop that many patches."
808 (interactive "p")
d51722b7 809 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
074a4fb0
GH
810 (stgit-reload)
811 (stgit-refresh-git-status))
56d81fe5 812
f9182fca
KH
813(defun stgit-applied-at-point ()
814 "Is the patch on the current line applied?"
815 (save-excursion
816 (beginning-of-line)
817 (looking-at "[>+]")))
818
819(defun stgit-push-or-pop ()
a53347d9 820 "Push or pop the patch on the current line."
f9182fca 821 (interactive)
2c862b07 822 (let ((patchsym (stgit-patch-name-at-point t))
f9182fca
KH
823 (applied (stgit-applied-at-point)))
824 (stgit-capture-output nil
d51722b7 825 (stgit-run (if applied "pop" "push") patchsym))
1f0bf00f 826 (stgit-reload)))
f9182fca 827
c7adf5ef 828(defun stgit-goto ()
a53347d9 829 "Go to the patch on the current line."
c7adf5ef 830 (interactive)
2c862b07 831 (let ((patchsym (stgit-patch-name-at-point t)))
c7adf5ef 832 (stgit-capture-output nil
d51722b7 833 (stgit-run "goto" patchsym))
1f0bf00f 834 (stgit-reload)))
c7adf5ef 835
d51722b7 836(defun stgit-id (patchsym)
50d88c67
DK
837 "Return the git commit id for PATCHSYM.
838If PATCHSYM is a keyword, returns PATCHSYM unmodified."
839 (if (keywordp patchsym)
840 patchsym
841 (let ((result (with-output-to-string
842 (stgit-run-silent "id" patchsym))))
843 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
844 (error "Cannot find commit id for %s" patchsym))
845 (match-string 1 result))))
378a003d 846
56d81fe5 847(defun stgit-show ()
a53347d9 848 "Show the patch on the current line."
56d81fe5
DK
849 (interactive)
850 (stgit-capture-output "*StGit patch*"
50d88c67
DK
851 (case (get-text-property (point) 'entry-type)
852 ('file
3164eec6
DK
853 (let* ((patched-file (stgit-patched-file-at-point))
854 (patch-name (stgit-patch-name-at-point))
855 (patch-id (stgit-id patch-name))
856 (args (append (and (stgit-file-cr-from patched-file)
857 (if stgit-expand-find-copies-harder
858 '("--find-copies-harder")
859 '("-C")))
b894e680
DK
860 (cond ((eq patch-id :index)
861 '("--cached"))
862 ((eq patch-id :work)
863 nil)
864 (t
865 (list (concat patch-id "^") patch-id)))
3164eec6
DK
866 '("--")
867 (if (stgit-file-copy-or-rename patched-file)
868 (list (stgit-file-cr-from patched-file)
869 (stgit-file-cr-to patched-file))
870 (list (stgit-file-file patched-file))))))
871 (apply 'stgit-run-git "diff" args)))
50d88c67 872 ('patch
2c862b07
DK
873 (stgit-run "show" "-O" "--patch-with-stat" "-O" "-M"
874 (stgit-patch-name-at-point)))
50d88c67
DK
875 (t
876 (error "No patch or file at point")))
877 (with-current-buffer standard-output
878 (goto-char (point-min))
879 (diff-mode))))
0663524d 880
fd9fe574 881(defun stgit-move-change-to-index (file)
37cb5766 882 "Copies the workspace state of FILE to index, using git add or git rm"
306b37a6
GH
883 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
884 '("add") '("rm" "-q"))))
37cb5766 885 (stgit-capture-output "*git output*"
5115dea0 886 (apply 'stgit-run-git (append op '("--") (list file))))))
37cb5766 887
fd9fe574 888(defun stgit-remove-change-from-index (file)
37cb5766
DK
889 "Unstages the change in FILE from the index"
890 (stgit-capture-output "*git output*"
891 (stgit-run-git "reset" "-q" "--" file)))
892
893(defun stgit-file-toggle-index ()
894 "Move modified file in or out of the index."
895 (interactive)
896 (let ((patched-file (stgit-patched-file-at-point)))
897 (unless patched-file
898 (error "No file on the current line"))
899 (let ((patch-name (stgit-patch-name-at-point)))
900 (cond ((eq patch-name :work)
fd9fe574 901 (stgit-move-change-to-index (stgit-file-file patched-file)))
37cb5766 902 ((eq patch-name :index)
fd9fe574 903 (stgit-remove-change-from-index (stgit-file-file patched-file)))
37cb5766
DK
904 (t
905 (error "Can only move files in the working tree to index")))))
210a2a52
DK
906 (stgit-refresh-worktree)
907 (stgit-refresh-index))
37cb5766 908
0bca35c8 909(defun stgit-edit ()
a53347d9 910 "Edit the patch on the current line."
0bca35c8 911 (interactive)
2c862b07 912 (let ((patchsym (stgit-patch-name-at-point t))
0780be79 913 (edit-buf (get-buffer-create "*StGit edit*"))
0bca35c8
DK
914 (dir default-directory))
915 (log-edit 'stgit-confirm-edit t nil edit-buf)
d51722b7 916 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
0bca35c8
DK
917 (setq default-directory dir)
918 (let ((standard-output edit-buf))
d51722b7 919 (stgit-run-silent "edit" "--save-template=-" patchsym))))
0bca35c8
DK
920
921(defun stgit-confirm-edit ()
922 (interactive)
923 (let ((file (make-temp-file "stgit-edit-")))
924 (write-region (point-min) (point-max) file)
925 (stgit-capture-output nil
d51722b7 926 (stgit-run "edit" "-f" file stgit-edit-patchsym))
0bca35c8 927 (with-current-buffer log-edit-parent-buffer
1f0bf00f 928 (stgit-reload))))
0bca35c8 929
aa04f831
GH
930(defun stgit-new (add-sign)
931 "Create a new patch.
932With a prefix argument, include a \"Signed-off-by:\" line at the
933end of the patch."
934 (interactive "P")
c5d45b92
GH
935 (let ((edit-buf (get-buffer-create "*StGit edit*"))
936 (dir default-directory))
937 (log-edit 'stgit-confirm-new t nil edit-buf)
aa04f831
GH
938 (setq default-directory dir)
939 (when add-sign
940 (save-excursion
941 (let ((standard-output (current-buffer)))
942 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
64c097a0
DK
943
944(defun stgit-confirm-new ()
945 (interactive)
27b0f9e4 946 (let ((file (make-temp-file "stgit-edit-")))
64c097a0
DK
947 (write-region (point-min) (point-max) file)
948 (stgit-capture-output nil
27b0f9e4 949 (stgit-run "new" "-f" file))
64c097a0 950 (with-current-buffer log-edit-parent-buffer
1f0bf00f 951 (stgit-reload))))
64c097a0
DK
952
953(defun stgit-create-patch-name (description)
954 "Create a patch name from a long description"
955 (let ((patch ""))
956 (while (> (length description) 0)
957 (cond ((string-match "\\`[a-zA-Z_-]+" description)
8439f657
GH
958 (setq patch (downcase (concat patch
959 (match-string 0 description))))
64c097a0
DK
960 (setq description (substring description (match-end 0))))
961 ((string-match "\\` +" description)
962 (setq patch (concat patch "-"))
963 (setq description (substring description (match-end 0))))
964 ((string-match "\\`[^a-zA-Z_-]+" description)
965 (setq description (substring description (match-end 0))))))
966 (cond ((= (length patch) 0)
967 "patch")
968 ((> (length patch) 20)
969 (substring patch 0 20))
970 (t patch))))
0bca35c8 971
9008e45b 972(defun stgit-delete (patchsyms &optional spill-p)
d51722b7 973 "Delete the patches in PATCHSYMS.
9008e45b
GH
974Interactively, delete the marked patches, or the patch at point.
975
976With a prefix argument, or SPILL-P, spill the patch contents to
977the work tree and index."
978 (interactive (list (stgit-patches-marked-or-at-point)
979 current-prefix-arg))
e7231e4f
GH
980 (unless patchsyms
981 (error "No patches to delete"))
d51722b7 982 (let ((npatches (length patchsyms)))
9008e45b 983 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
e7231e4f 984 npatches
9008e45b
GH
985 (if (= 1 npatches) "" "es")
986 (if spill-p
987 " (spilling contents to index)"
988 "")))
989 (let ((args (if spill-p
990 (cons "--spill" patchsyms)
991 patchsyms)))
992 (stgit-capture-output nil
993 (apply 'stgit-run "delete" args))
994 (stgit-reload)))))
d51722b7 995
7cc45294
GH
996(defun stgit-move-patches-target ()
997 "Return the patchsym indicating a target patch for
998`stgit-move-patches'.
999
1000This is either the patch at point, or one of :top and :bottom, if
1001the point is after or before the applied patches."
1002
2c862b07 1003 (let ((patchsym (stgit-patch-name-at-point)))
7cc45294
GH
1004 (cond (patchsym patchsym)
1005 ((save-excursion (re-search-backward "^>" nil t)) :top)
1006 (t :bottom))))
1007
95369f6c
GH
1008(defun stgit-sort-patches (patchsyms)
1009 "Returns the list of patches in PATCHSYMS sorted according to
1010their position in the patch series, bottommost first.
1011
1012PATCHSYMS may not contain duplicate entries."
1013 (let (sorted-patchsyms
1014 (series (with-output-to-string
1015 (with-current-buffer standard-output
1016 (stgit-run-silent "series" "--noprefix"))))
1017 start)
1018 (while (string-match "^\\(.+\\)" series start)
1019 (let ((patchsym (intern (match-string 1 series))))
1020 (when (memq patchsym patchsyms)
1021 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
1022 (setq start (match-end 0)))
1023 (setq sorted-patchsyms (nreverse sorted-patchsyms))
1024
1025 (unless (= (length patchsyms) (length sorted-patchsyms))
1026 (error "Internal error"))
1027
1028 sorted-patchsyms))
1029
7cc45294
GH
1030(defun stgit-move-patches (patchsyms target-patch)
1031 "Move the patches in PATCHSYMS to below TARGET-PATCH.
1032If TARGET-PATCH is :bottom or :top, move the patches to the
1033bottom or top of the stack, respectively.
1034
1035Interactively, move the marked patches to where the point is."
1036 (interactive (list stgit-marked-patches
1037 (stgit-move-patches-target)))
1038 (unless patchsyms
1039 (error "Need at least one patch to move"))
1040
1041 (unless target-patch
1042 (error "Point not at a patch"))
1043
1044 (if (eq target-patch :top)
1045 (stgit-capture-output nil
1046 (apply 'stgit-run "float" patchsyms))
1047
1048 ;; need to have patchsyms sorted by position in the stack
95369f6c 1049 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
7cc45294
GH
1050 (while sorted-patchsyms
1051 (setq sorted-patchsyms
1052 (and (stgit-capture-output nil
1053 (if (eq target-patch :bottom)
1054 (stgit-run "sink" "--" (car sorted-patchsyms))
1055 (stgit-run "sink" "--to" target-patch "--"
1056 (car sorted-patchsyms))))
1057 (cdr sorted-patchsyms))))))
1058 (stgit-reload))
1059
594aa463
KH
1060(defun stgit-squash (patchsyms)
1061 "Squash the patches in PATCHSYMS.
693d179b
GH
1062Interactively, squash the marked patches.
1063
1064Unless there are any conflicts, the patches will be merged into
1065one patch, which will occupy the same spot in the series as the
1066deepest patch had before the squash."
d51722b7
GH
1067 (interactive (list stgit-marked-patches))
1068 (when (< (length patchsyms) 2)
594aa463 1069 (error "Need at least two patches to squash"))
32d7545d
GH
1070 (let ((stgit-buffer (current-buffer))
1071 (edit-buf (get-buffer-create "*StGit edit*"))
693d179b
GH
1072 (dir default-directory)
1073 (sorted-patchsyms (stgit-sort-patches patchsyms)))
594aa463 1074 (log-edit 'stgit-confirm-squash t nil edit-buf)
693d179b 1075 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
ea0def18 1076 (setq default-directory dir)
32d7545d
GH
1077 (let ((result (let ((standard-output edit-buf))
1078 (apply 'stgit-run-silent "squash"
1079 "--save-template=-" sorted-patchsyms))))
1080
1081 ;; stg squash may have reordered the patches or caused conflicts
1082 (with-current-buffer stgit-buffer
1083 (stgit-reload))
1084
1085 (unless (eq 0 result)
1086 (fundamental-mode)
1087 (rename-buffer "*StGit error*")
1088 (resize-temp-buffer-window)
1089 (switch-to-buffer-other-window stgit-buffer)
1090 (error "stg squash failed")))))
ea0def18 1091
594aa463 1092(defun stgit-confirm-squash ()
ea0def18
DK
1093 (interactive)
1094 (let ((file (make-temp-file "stgit-edit-")))
1095 (write-region (point-min) (point-max) file)
1096 (stgit-capture-output nil
594aa463 1097 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
ea0def18 1098 (with-current-buffer log-edit-parent-buffer
e6b1fdae
DK
1099 (stgit-clear-marks)
1100 ;; Go to first marked patch and stay there
1101 (goto-char (point-min))
1102 (re-search-forward (concat "^[>+-]\\*") nil t)
1103 (move-to-column goal-column)
1104 (let ((pos (point)))
1f0bf00f 1105 (stgit-reload)
e6b1fdae 1106 (goto-char pos)))))
ea0def18 1107
0663524d
KH
1108(defun stgit-help ()
1109 "Display help for the StGit mode."
1110 (interactive)
1111 (describe-function 'stgit-mode))
3a59f3db 1112
83e51dbf
DK
1113(defun stgit-undo (&optional arg)
1114 "Run stg undo.
1115With prefix argument, run it with the --hard flag."
1116 (interactive "P")
1117 (stgit-capture-output nil
1118 (if arg
1119 (stgit-run "undo" "--hard")
1120 (stgit-run "undo")))
1f0bf00f 1121 (stgit-reload))
83e51dbf 1122
4d73c4d8
DK
1123(defun stgit-refresh (&optional arg)
1124 "Run stg refresh.
a53347d9 1125With prefix argument, refresh the marked patch or the patch under point."
4d73c4d8
DK
1126 (interactive "P")
1127 (let ((patchargs (if arg
b0424080
GH
1128 (let ((patches (stgit-patches-marked-or-at-point)))
1129 (cond ((null patches)
df283a8b 1130 (error "No patch to update"))
b0424080 1131 ((> (length patches) 1)
df283a8b 1132 (error "Too many patches selected"))
b0424080
GH
1133 (t
1134 (cons "-p" patches))))
1135 nil)))
4d73c4d8 1136 (stgit-capture-output nil
074a4fb0
GH
1137 (apply 'stgit-run "refresh" patchargs))
1138 (stgit-refresh-git-status))
4d73c4d8
DK
1139 (stgit-reload))
1140
ce3b6130
DK
1141(defcustom stgit-default-show-worktree
1142 nil
1143 "Set to non-nil to by default show the working tree in a new stgit buffer.
1144
1145This value is used as the default value for `stgit-show-worktree'."
1146 :type 'boolean
1147 :group 'stgit)
1148
1149(defvar stgit-show-worktree nil
1150 "Show work tree and index in the stgit buffer.
1151
1152See `stgit-default-show-worktree' for its default value.")
1153
1154(defun stgit-toggle-worktree (&optional arg)
1155 "Toggle the visibility of the work tree.
1156With arg, show the work tree if arg is positive.
1157
1158Its initial setting is controlled by `stgit-default-show-worktree'."
1159 (interactive)
1160 (setq stgit-show-worktree
1161 (if (numberp arg)
1162 (> arg 0)
1163 (not stgit-show-worktree)))
1164 (stgit-reload))
1165
3a59f3db 1166(provide 'stgit)