(require 'ewoc)
(require 'easymenu)
+(defun stgit-set-default (symbol value)
+ "Set default value of SYMBOL to VALUE using `set-default' and
+reload all StGit buffers."
+ (set-default symbol value)
+ (dolist (buf (buffer-list))
+ (with-current-buffer buf
+ (when (eq major-mode 'stgit-mode)
+ (stgit-reload)))))
+
+(defgroup stgit nil
+ "A user interface for the StGit patch maintenance tool."
+ :group 'tools
+ :link '(function-link stgit)
+ :link '(url-link "http://www.procode.org/stgit/"))
+
+(defcustom stgit-abbreviate-copies-and-renames t
+ "If non-nil, abbreviate copies and renames as \"dir/{old -> new}/file\"
+instead of \"dir/old/file -> dir/new/file\"."
+ :type 'boolean
+ :group 'stgit
+ :set 'stgit-set-default)
+
+(defcustom stgit-default-show-worktree t
+ "Set to non-nil to by default show the working tree in a new stgit buffer.
+
+Use \\<stgit-mode-map>\\[stgit-toggle-worktree] to toggle the this setting in an already-started StGit buffer."
+ :type 'boolean
+ :group 'stgit
+ :link '(variable-link stgit-show-worktree))
+
+(defcustom stgit-find-copies-harder nil
+ "Try harder to find copied files when listing patches.
+
+When not nil, runs git diff-tree with the --find-copies-harder
+flag, which reduces performance."
+ :type 'boolean
+ :group 'stgit
+ :set 'stgit-set-default)
+
+(defcustom stgit-show-worktree-mode 'center
+ "This variable controls where the \"Index\" and \"Work tree\"
+will be shown on in the buffer.
+
+It can be set to 'top (above all patches), 'center (show between
+applied and unapplied patches), and 'bottom (below all patches)."
+ :type '(radio (const :tag "above all patches (top)" top)
+ (const :tag "between applied and unapplied patches (center)"
+ center)
+ (const :tag "below all patches (bottom)" bottom))
+ :group 'stgit
+ :link '(variable-link stgit-show-worktree)
+ :set 'stgit-set-default)
+
+(defface stgit-branch-name-face
+ '((t :inherit bold))
+ "The face used for the StGit branch name"
+ :group 'stgit)
+
+(defface stgit-top-patch-face
+ '((((background dark)) (:weight bold :foreground "yellow"))
+ (((background light)) (:weight bold :foreground "purple"))
+ (t (:weight bold)))
+ "The face used for the top patch names"
+ :group 'stgit)
+
+(defface stgit-applied-patch-face
+ '((((background dark)) (:foreground "light yellow"))
+ (((background light)) (:foreground "purple"))
+ (t ()))
+ "The face used for applied patch names"
+ :group 'stgit)
+
+(defface stgit-unapplied-patch-face
+ '((((background dark)) (:foreground "gray80"))
+ (((background light)) (:foreground "orchid"))
+ (t ()))
+ "The face used for unapplied patch names"
+ :group 'stgit)
+
+(defface stgit-description-face
+ '((((background dark)) (:foreground "tan"))
+ (((background light)) (:foreground "dark red")))
+ "The face used for StGit descriptions"
+ :group 'stgit)
+
+(defface stgit-index-work-tree-title-face
+ '((((supports :slant italic)) :slant italic)
+ (t :inherit bold))
+ "StGit mode face used for the \"Index\" and \"Work tree\" titles"
+ :group 'stgit)
+
+(defface stgit-unmerged-file-face
+ '((((class color) (background light)) (:foreground "red" :bold t))
+ (((class color) (background dark)) (:foreground "red" :bold t)))
+ "StGit mode face used for unmerged file status"
+ :group 'stgit)
+
+(defface stgit-unknown-file-face
+ '((((class color) (background light)) (:foreground "goldenrod" :bold t))
+ (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
+ "StGit mode face used for unknown file status"
+ :group 'stgit)
+
+(defface stgit-ignored-file-face
+ '((((class color) (background light)) (:foreground "grey60"))
+ (((class color) (background dark)) (:foreground "grey40")))
+ "StGit mode face used for ignored files")
+
+(defface stgit-file-permission-face
+ '((((class color) (background light)) (:foreground "green" :bold t))
+ (((class color) (background dark)) (:foreground "green" :bold t)))
+ "StGit mode face used for permission changes."
+ :group 'stgit)
+
+(defface stgit-modified-file-face
+ '((((class color) (background light)) (:foreground "purple"))
+ (((class color) (background dark)) (:foreground "salmon")))
+ "StGit mode face used for modified file status"
+ :group 'stgit)
+
(defun stgit (dir)
"Manage StGit patches for the tree in DIR.
(goto-line curline)))
(stgit-refresh-git-status))
-(defun stgit-set-default (symbol value)
- "Set default value of SYMBOL to VALUE using `set-default' and
-reload all StGit buffers."
- (set-default symbol value)
- (dolist (buf (buffer-list))
- (with-current-buffer buf
- (when (eq major-mode 'stgit-mode)
- (stgit-reload)))))
-
-(defgroup stgit nil
- "A user interface for the StGit patch maintenance tool."
- :group 'tools
- :link '(function-link stgit)
- :link '(url-link "http://www.procode.org/stgit/"))
-
-(defcustom stgit-abbreviate-copies-and-renames t
- "If non-nil, abbreviate copies and renames as \"dir/{old -> new}/file\"
-instead of \"dir/old/file -> dir/new/file\"."
- :type 'boolean
- :group 'stgit
- :set 'stgit-set-default)
-
-(defcustom stgit-default-show-worktree t
- "Set to non-nil to by default show the working tree in a new stgit buffer.
-
-Use \\<stgit-mode-map>\\[stgit-toggle-worktree] to toggle the this setting in an already-started StGit buffer."
- :type 'boolean
- :group 'stgit
- :link '(variable-link stgit-show-worktree))
-
-(defcustom stgit-find-copies-harder nil
- "Try harder to find copied files when listing patches.
-
-When not nil, runs git diff-tree with the --find-copies-harder
-flag, which reduces performance."
- :type 'boolean
- :group 'stgit
- :set 'stgit-set-default)
-
-(defcustom stgit-show-worktree-mode 'center
- "This variable controls where the \"Index\" and \"Work tree\"
-will be shown on in the buffer.
-
-It can be set to 'top (above all patches), 'center (show between
-applied and unapplied patches), and 'bottom (below all patches)."
- :type '(radio (const :tag "above all patches (top)" top)
- (const :tag "between applied and unapplied patches (center)"
- center)
- (const :tag "below all patches (bottom)" bottom))
- :group 'stgit
- :link '(variable-link stgit-show-worktree)
- :set 'stgit-set-default)
-
-(defface stgit-branch-name-face
- '((t :inherit bold))
- "The face used for the StGit branch name"
- :group 'stgit)
-
-(defface stgit-top-patch-face
- '((((background dark)) (:weight bold :foreground "yellow"))
- (((background light)) (:weight bold :foreground "purple"))
- (t (:weight bold)))
- "The face used for the top patch names"
- :group 'stgit)
-
-(defface stgit-applied-patch-face
- '((((background dark)) (:foreground "light yellow"))
- (((background light)) (:foreground "purple"))
- (t ()))
- "The face used for applied patch names"
- :group 'stgit)
-
-(defface stgit-unapplied-patch-face
- '((((background dark)) (:foreground "gray80"))
- (((background light)) (:foreground "orchid"))
- (t ()))
- "The face used for unapplied patch names"
- :group 'stgit)
-
-(defface stgit-description-face
- '((((background dark)) (:foreground "tan"))
- (((background light)) (:foreground "dark red")))
- "The face used for StGit descriptions"
- :group 'stgit)
-
-(defface stgit-index-work-tree-title-face
- '((((supports :slant italic)) :slant italic)
- (t :inherit bold))
- "StGit mode face used for the \"Index\" and \"Work tree\" titles"
- :group 'stgit)
-
-(defface stgit-unmerged-file-face
- '((((class color) (background light)) (:foreground "red" :bold t))
- (((class color) (background dark)) (:foreground "red" :bold t)))
- "StGit mode face used for unmerged file status"
- :group 'stgit)
-
-(defface stgit-unknown-file-face
- '((((class color) (background light)) (:foreground "goldenrod" :bold t))
- (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
- "StGit mode face used for unknown file status"
- :group 'stgit)
-
-(defface stgit-ignored-file-face
- '((((class color) (background light)) (:foreground "grey60"))
- (((class color) (background dark)) (:foreground "grey40")))
- "StGit mode face used for ignored files")
-
-(defface stgit-file-permission-face
- '((((class color) (background light)) (:foreground "green" :bold t))
- (((class color) (background dark)) (:foreground "green" :bold t)))
- "StGit mode face used for permission changes."
- :group 'stgit)
-
-(defface stgit-modified-file-face
- '((((class color) (background light)) (:foreground "purple"))
- (((class color) (background dark)) (:foreground "salmon")))
- "StGit mode face used for modified file status"
- :group 'stgit)
-
(defconst stgit-file-status-code-strings
(mapcar (lambda (arg)
(cons (car arg)
:active (stgit-patch-name-at-point nil t)]
["Rename patch" stgit-rename :active (stgit-patch-name-at-point nil t)]
["Push/pop patch" stgit-push-or-pop
- :label (if (stgit-applied-at-point-p) "Pop patch" "Push patch")
- :active (stgit-patch-name-at-point nil t)]
+ :label (if (subsetp (stgit-patches-marked-or-at-point nil t)
+ (stgit-applied-patchsyms t))
+ "Pop patches" "Push patches")]
["Delete patches" stgit-delete
:active (stgit-patches-marked-or-at-point nil t)]
"-"
\\[stgit-expand] Show changes in marked patches
\\[stgit-collapse] Hide changes in marked patches
-\\[stgit-new] Create a new, empty patch
\\[stgit-new-and-refresh] Create a new patch from index or work tree
+\\[stgit-new] Create a new, empty patch
+
\\[stgit-rename] Rename patch
\\[stgit-edit] Edit patch description
\\[stgit-delete] Delete patch(es)
\\[stgit-push-next] Push next patch onto stack
\\[stgit-pop-next] Pop current patch from stack
-\\[stgit-push-or-pop] Push or pop patch at point
-\\[stgit-goto] Make current patch current by popping or pushing
+\\[stgit-push-or-pop] Push or pop marked patches
+\\[stgit-goto] Make patch at point current by popping or pushing
\\[stgit-squash] Squash (meld together) patches
-\\[stgit-move-patches] Move patch(es) to point
+\\[stgit-move-patches] Move marked patches to point
\\[stgit-commit] Commit patch(es)
\\[stgit-uncommit] Uncommit patch(es)
(stgit-reload)
(stgit-refresh-git-status))
-(defun stgit-applied-at-point-p ()
- "Return non-nil if the patch at point is applied."
- (let ((patch (stgit-patch-at-point t)))
- (not (eq (stgit-patch-status patch) 'unapplied))))
+(defun stgit-applied-patches (&optional only-patches)
+ "Return a list of the applied patches.
+
+If ONLY-PATCHES is not nil, exclude index and work tree."
+ (let ((states (if only-patches
+ '(applied top)
+ '(applied top index work)))
+ result)
+ (ewoc-map (lambda (patch) (when (memq (stgit-patch-status patch) states)
+ (setq result (cons patch result))))
+ stgit-ewoc)
+ result))
+
+(defun stgit-applied-patchsyms (&optional only-patches)
+ "Return a list of the symbols of the applied patches.
+
+If ONLY-PATCHES is not nil, exclude index and work tree."
+ (mapcar #'stgit-patch-name (stgit-applied-patches only-patches)))
(defun stgit-push-or-pop ()
- "Push or pop the patch on the current line."
+ "Push or pop the marked patches."
(interactive)
(stgit-assert-mode)
- (let ((patchsym (stgit-patch-name-at-point t t))
- (applied (stgit-applied-at-point-p)))
+ (let* ((patchsyms (stgit-patches-marked-or-at-point t t))
+ (applied-syms (stgit-applied-patchsyms t))
+ (unapplied (set-difference patchsyms applied-syms)))
(stgit-capture-output nil
- (stgit-run (if applied "pop" "push") patchsym))
- (stgit-reload)))
+ (apply 'stgit-run
+ (if unapplied "push" "pop")
+ "--"
+ (stgit-sort-patches (if unapplied unapplied patchsyms)))))
+ (stgit-reload))
(defun stgit-goto ()
- "Go to the patch on the current line."
+ "Go to the patch on the current line.
+
+Pops or pushes patches to make this patch topmost."
(interactive)
(stgit-assert-mode)
(let ((patchsym (stgit-patch-name-at-point t)))
(set (make-local-variable 'stgit-edit-patchsym) patchsym)
(setq default-directory dir)
(let ((standard-output edit-buf))
- (stgit-run-silent "edit" "--save-template=-" patchsym))))
+ (save-excursion
+ (stgit-run-silent "edit" "--save-template=-" patchsym)))))
(defun stgit-confirm-edit ()
(interactive)
"Return the patchsym indicating a target patch for
`stgit-move-patches'.
-This is either the patch at point, or one of :top and :bottom, if
-the point is after or before the applied patches."
-
- (let ((patchsym (stgit-patch-name-at-point nil t)))
- (cond (patchsym patchsym)
- ((save-excursion (re-search-backward "^>" nil t)) :top)
- (t :bottom))))
+This is either the first unmarked patch at or after point, or one
+of :top and :bottom if the point is after or before the applied
+patches."
+
+ (save-excursion
+ (let (result)
+ (while (not result)
+ (let ((patchsym (stgit-patch-name-at-point)))
+ (cond ((memq patchsym '(:work :index)) (setq result :top))
+ (patchsym (if (memq patchsym stgit-marked-patches)
+ (stgit-next-patch)
+ (setq result patchsym)))
+ ((re-search-backward "^>" nil t) (setq result :top))
+ (t (setq result :bottom)))))
+ result)))
(defun stgit-sort-patches (patchsyms)
"Returns the list of patches in PATCHSYMS sorted according to
(unless target-patch
(error "Point not at a patch"))
- (if (eq target-patch :top)
- (stgit-capture-output nil
- (apply 'stgit-run "float" patchsyms))
-
- ;; need to have patchsyms sorted by position in the stack
- (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
- (while sorted-patchsyms
- (setq sorted-patchsyms
- (and (stgit-capture-output nil
- (if (eq target-patch :bottom)
- (stgit-run "sink" "--" (car sorted-patchsyms))
- (stgit-run "sink" "--to" target-patch "--"
- (car sorted-patchsyms))))
- (cdr sorted-patchsyms))))))
+ ;; need to have patchsyms sorted by position in the stack
+ (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
+ (stgit-capture-output nil
+ (if (eq target-patch :top)
+ (apply 'stgit-run "float" sorted-patchsyms)
+ (apply 'stgit-run
+ "sink"
+ (append (unless (eq target-patch :bottom)
+ (list "--to" target-patch))
+ '("--")
+ sorted-patchsyms)))))
(stgit-reload))
(defun stgit-squash (patchsyms)
(set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
(setq default-directory dir)
(let ((result (let ((standard-output edit-buf))
- (apply 'stgit-run-silent "squash"
- "--save-template=-" sorted-patchsyms))))
+ (save-excursion
+ (apply 'stgit-run-silent "squash"
+ "--save-template=-" sorted-patchsyms)))))
;; stg squash may have reordered the patches or caused conflicts
(with-current-buffer stgit-buffer