stgit.el: Disable undo information in stgit output capture buffers
[stgit] / contrib / stgit.el
index 006c082..829669e 100644 (file)
 (require 'git nil t)
 (require 'cl)
 (require 'ewoc)
+(require 'easymenu)
+(require 'format-spec)
+
+(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)
+
+(defcustom stgit-patch-line-format "%s%m%-30n %e%d"
+  "The format string used to format patch lines.
+The format string is passed to `format-spec' and the following
+format characters are recognized:
+
+  %s - A '+', '-', '>' or space, depending on whether the patch is
+       applied, unapplied, top, or something else.
+
+  %m - An asterisk if the patch is marked, and a space otherwise.
+
+  %n - The patch name.
+
+  %e - The string \"(empty) \" if the patch is empty.
+
+  %d - The short patch description.
+
+  %D - The short patch description, or the patch name.
+
+When `stgit-show-patch-names' is non-nil, the `stgit-noname-patch-line-format'
+variable is used instead."
+  :type 'string
+  :group 'stgit
+  :set 'stgit-set-default)
+
+(defcustom stgit-noname-patch-line-format "%s%m%e%D"
+  "The alternate format string used to format patch lines.
+It has the same semantics as `stgit-patch-line-format', and the
+display can be toggled between the two formats using
+\\<stgit-mode-map>>\\[stgit-toggle-patch-names].
+
+The alternate form is used when the patch name is hidden."
+  :type 'string
+  :group 'stgit
+  :set 'stgit-set-default)
+
+(defcustom stgit-default-show-patch-names t
+  "If non-nil, default to showing patch names in a new stgit buffer.
+
+Use \\<stgit-mode-map>\\[stgit-toggle-patch-names] to toggle the
+this setting in an already-started StGit buffer."
+  :type 'boolean
+  :group 'stgit
+  :link '(variable-link stgit-show-patch-names))
+
+(defcustom stgit-file-line-format "    %-11s %-2m %n   %c"
+  "The format string used to format file lines.
+The format string is passed to `format-spec' and the following
+format characters are recognized:
+
+  %s - A string describing the status of the file.
+
+  %m - Mode change information
+
+  %n - The file name.
+
+  %c - A description of file changes."
+  :type 'string
+  :group 'stgit
+  :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.
@@ -24,6 +206,10 @@ See `stgit-mode' for commands available."
   (switch-to-stgit-buffer (git-get-top-dir dir))
   (stgit-reload))
 
+(defun stgit-assert-mode ()
+  "Signal an error if not in an StGit buffer."
+  (assert (derived-mode-p 'stgit-mode) nil "Not an StGit buffer"))
+
 (unless (fboundp 'git-get-top-dir)
   (defun git-get-top-dir (dir)
     "Retrieve the top-level directory of a git tree."
@@ -68,30 +254,40 @@ directory DIR or `default-directory'"
 (defstruct (stgit-patch)
   status name desc empty files-ewoc)
 
+(defun stgit-patch-display-name (patch)
+  (let ((name (stgit-patch-name patch)))
+    (case name
+      (:index "Index")
+      (:work "Work Tree")
+      (t (symbol-name name)))))
+
 (defun stgit-patch-pp (patch)
   (let* ((status (stgit-patch-status patch))
          (start (point))
          (name (stgit-patch-name patch))
-         (face (cdr (assq status stgit-patch-status-face-alist))))
-    (insert (case status
-              ('applied "+")
-              ('top ">")
-              ('unapplied "-")
-              (t " "))
-            (if (memq name stgit-marked-patches)
-                "*" " "))
-    (if (memq status '(index work))
-        (insert (propertize (if (eq status 'index) "Index" "Work tree")
-                            'face face))
-      (insert (format "%-30s"
-                      (propertize (symbol-name name)
-                                  'face face
-                                  'syntax-table (string-to-syntax "w")))
-              "  "
-              (if (stgit-patch-empty patch) "(empty) " "")
-              (propertize (or (stgit-patch-desc patch) "")
-                          'face 'stgit-description-face)))
-    (insert "\n")
+         (face (cdr (assq status stgit-patch-status-face-alist)))
+         (fmt (if stgit-show-patch-names
+                  stgit-patch-line-format
+                stgit-noname-patch-line-format))
+         (spec (format-spec-make
+                ?s (case status
+                     ('applied "+")
+                     ('top ">")
+                     ('unapplied "-")
+                     (t " "))
+                ?m (if (memq name stgit-marked-patches)
+                       "*" " ")
+                ?n (propertize (stgit-patch-display-name patch)
+                               'face face
+                               'syntax-table (string-to-syntax "w"))
+                ?e (if (stgit-patch-empty patch) "(empty) " "")
+                ?d (propertize (or (stgit-patch-desc patch) "")
+                               'face 'stgit-description-face)
+                ?D (propertize (or (stgit-patch-desc patch)
+                                   (stgit-patch-display-name patch))
+                               'face face))))
+
+    (insert (format-spec fmt spec) "\n")
     (put-text-property start (point) 'entry-type 'patch)
     (when (memq name stgit-expanded-patches)
       (stgit-insert-patch-files patch))
@@ -120,6 +316,7 @@ Returns nil if there was no output."
          (stgit-dir default-directory)
          (inhibit-read-only t))
      (with-current-buffer output-buf
+       (buffer-disable-undo)
        (erase-buffer)
        (setq default-directory stgit-dir)
        (setq buffer-read-only t))
@@ -168,6 +365,10 @@ Returns nil if there was no output."
   "Returns non-nil if the index contains no changes from HEAD."
   (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD")))
 
+(defun stgit-work-tree-empty-p ()
+  "Returns non-nil if the work tree contains no changes from index."
+  (zerop (stgit-run-git-silent "diff-files" "--quiet")))
+
 (defvar stgit-index-node)
 (defvar stgit-worktree-node)
 
@@ -249,6 +450,7 @@ Returns nil if there was no output."
 (defun stgit-reload ()
   "Update the contents of the StGit buffer."
   (interactive)
+  (stgit-assert-mode)
   (let ((inhibit-read-only t)
         (curline (line-number-at-pos))
         (curpatch (stgit-patch-name-at-point))
@@ -274,126 +476,6 @@ Returns nil if there was no output."
       (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)
@@ -423,12 +505,11 @@ applied and unapplied patches), and 'bottom (below all patches)."
                      stgit-file-status-code-strings))
          (score (stgit-file-cr-score file)))
     (when code
-      (format "%-11s  "
-              (if (and score (/= score 100))
-                  (format "%s %s" (cdr code)
-                          (propertize (format "%d%%" score)
-                                      'face 'stgit-description-face))
-                (cdr code))))))
+      (if (and score (/= score 100))
+          (format "%s %s" (cdr code)
+                  (propertize (format "%d%%" score)
+                              'face 'stgit-description-face))
+        (cdr code)))))
 
 (defun stgit-file-status-code (str &optional score)
   "Return stgit status code from git status string"
@@ -471,8 +552,8 @@ Cf. `stgit-file-type-string'."
           ((zerop old-type)
            (if (= new-type #o100)
                ""
-             (format "   (%s)" (stgit-file-type-string new-type))))
-          (t (format "   (%s -> %s)"
+             (format "(%s)" (stgit-file-type-string new-type))))
+          (t (format "(%s -> %s)"
                      (stgit-file-type-string old-type)
                      (stgit-file-type-string new-type))))))
 
@@ -545,23 +626,20 @@ Cf. `stgit-file-type-change-string'."
       (concat (stgit-file-cr-from file) arrow (stgit-file-cr-to file)))))
 
 (defun stgit-file-pp (file)
-  (let ((status (stgit-file-status file))
-        (name (if (stgit-file-copy-or-rename file)
-                  (stgit-describe-copy-or-rename file)
-                (stgit-file-file file)))
-        (mode-change (stgit-file-mode-change-string
-                      (stgit-file-old-perm file)
-                      (stgit-file-new-perm file)))
-        (start (point)))
-    (insert (format "    %-12s%s%s%s%s\n"
-                    (stgit-file-status-code-as-string file)
-                    mode-change
-                    (if (zerop (length mode-change)) "" " ")
-                    name
-                    (propertize (stgit-file-type-change-string
-                                 (stgit-file-old-perm file)
-                                 (stgit-file-new-perm file))
-                                'face 'stgit-description-face)))
+  (let ((start (point))
+        (spec (format-spec-make
+               ?s (stgit-file-status-code-as-string file)
+               ?m (stgit-file-mode-change-string
+                   (stgit-file-old-perm file)
+                   (stgit-file-new-perm file))
+               ?n (if (stgit-file-copy-or-rename file)
+                      (stgit-describe-copy-or-rename file)
+                    (stgit-file-file file))
+               ?c (propertize (stgit-file-type-change-string
+                               (stgit-file-old-perm file)
+                               (stgit-file-new-perm file))
+                              'face 'stgit-description-face))))
+    (insert (format-spec stgit-file-line-format spec) "\n")
     (add-text-properties start (point)
                          (list 'entry-type 'file
                                'file-data file))))
@@ -667,13 +745,14 @@ at point."
       (smerge-mode 1))))
 
 (defun stgit-expand (&optional patches collapse)
-  "Show the contents selected patches, or the patch at point.
+  "Show the contents of marked patches, or the patch at point.
 
 See also `stgit-collapse'.
 
 Non-interactively, operate on PATCHES, and collapse instead of
 expand if COLLAPSE is not nil."
-  (interactive (list (stgit-patches-marked-or-at-point)))
+  (interactive (list (stgit-patches-marked-or-at-point t)))
+  (stgit-assert-mode)
   (let ((patches-diff (funcall (if collapse #'intersection #'set-difference)
                                patches stgit-expanded-patches)))
     (setq stgit-expanded-patches
@@ -686,10 +765,11 @@ expand if COLLAPSE is not nil."
   (move-to-column (stgit-goal-column)))
 
 (defun stgit-collapse (&optional patches)
-  "Hide the contents selected patches, or the patch at point.
+  "Hide the contents of marked patches, or the patch at point.
 
 See also `stgit-expand'."
-  (interactive (list (stgit-patches-marked-or-at-point)))
+  (interactive (list (stgit-patches-marked-or-at-point t)))
+  (stgit-assert-mode)
   (stgit-expand patches t))
 
 (defun stgit-select-patch ()
@@ -703,6 +783,7 @@ See also `stgit-expand'."
 With point on a file, open the associated file. Opens the target
 file for (applied) copies and renames."
   (interactive)
+  (stgit-assert-mode)
   (case (get-text-property (point) 'entry-type)
     ('patch
      (stgit-select-patch))
@@ -714,22 +795,26 @@ file for (applied) copies and renames."
 (defun stgit-find-file-other-window ()
   "Open file at point in other window"
   (interactive)
+  (stgit-assert-mode)
   (stgit-find-file t))
 
 (defun stgit-find-file-merge ()
   "Open file at point and merge it using `smerge-ediff'."
   (interactive)
+  (stgit-assert-mode)
   (stgit-find-file t)
   (smerge-ediff))
 
 (defun stgit-quit ()
   "Hide the stgit buffer."
   (interactive)
+  (stgit-assert-mode)
   (bury-buffer))
 
 (defun stgit-git-status ()
   "Show status using `git-status'."
   (interactive)
+  (stgit-assert-mode)
   (unless (fboundp 'git-status)
     (error "The stgit-git-status command requires git-status"))
   (let ((dir default-directory))
@@ -747,24 +832,28 @@ file for (applied) copies and renames."
 (defun stgit-next-line (&optional arg)
   "Move cursor vertically down ARG lines"
   (interactive "p")
+  (stgit-assert-mode)
   (next-line arg)
   (move-to-column (stgit-goal-column)))
 
 (defun stgit-previous-line (&optional arg)
   "Move cursor vertically up ARG lines"
   (interactive "p")
+  (stgit-assert-mode)
   (previous-line arg)
   (move-to-column (stgit-goal-column)))
 
 (defun stgit-next-patch (&optional arg)
   "Move cursor down ARG patches."
   (interactive "p")
+  (stgit-assert-mode)
   (ewoc-goto-next stgit-ewoc (or arg 1))
   (move-to-column goal-column))
 
 (defun stgit-previous-patch (&optional arg)
   "Move cursor up ARG patches."
   (interactive "p")
+  (stgit-assert-mode)
   (ewoc-goto-prev stgit-ewoc (or arg 1))
   (move-to-column goal-column))
 
@@ -775,8 +864,8 @@ file for (applied) copies and renames."
   "Keymap for StGit major mode.")
 
 (unless stgit-mode-map
-  (let ((diff-map   (make-keymap))
-        (toggle-map (make-keymap)))
+  (let ((diff-map   (make-sparse-keymap))
+        (toggle-map (make-sparse-keymap)))
     (suppress-keymap diff-map)
     (mapc (lambda (arg) (define-key diff-map (car arg) (cdr arg)))
           '(("b" .        stgit-diff-base)
@@ -786,14 +875,15 @@ file for (applied) copies and renames."
             ("t" .        stgit-diff-theirs)))
     (suppress-keymap toggle-map)
     (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
-          '(("t" .        stgit-toggle-worktree)
+          '(("n" .        stgit-toggle-patch-names)
+            ("t" .        stgit-toggle-worktree)
             ("i" .        stgit-toggle-ignored)
             ("u" .        stgit-toggle-unknown)))
     (setq stgit-mode-map (make-keymap))
     (suppress-keymap stgit-mode-map)
     (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
-          `((" " .        stgit-mark)
-            ("m" .        stgit-mark)
+          `((" " .        stgit-mark-down)
+            ("m" .        stgit-mark-down)
             ("\d" .       stgit-unmark-up)
             ("u" .        stgit-unmark-down)
             ("?" .        stgit-help)
@@ -817,13 +907,13 @@ file for (applied) copies and renames."
             ("c" .        stgit-new-and-refresh)
             ("\C-c\C-c" . stgit-commit)
             ("\C-c\C-u" . stgit-uncommit)
-            ("U" .        stgit-revert-file)
+            ("U" .        stgit-revert)
             ("R" .        stgit-resolve-file)
             ("\r" .       stgit-select)
             ("+" .        stgit-expand)
             ("-" .        stgit-collapse)
             ("o" .        stgit-find-file-other-window)
-            ("i" .        stgit-file-toggle-index)
+            ("i" .        stgit-toggle-index)
             (">" .        stgit-push-next)
             ("<" .        stgit-pop-next)
             ("P" .        stgit-push-or-pop)
@@ -838,7 +928,126 @@ file for (applied) copies and renames."
             ("\C-c\C-b" . stgit-rebase)
             ("t" .        ,toggle-map)
             ("d" .        ,diff-map)
-            ("q" .        stgit-quit)))))
+            ("q" .        stgit-quit))))
+
+  (let ((at-unmerged-file '(let ((file (stgit-patched-file-at-point)))
+                             (and file (eq (stgit-file-status file)
+                                           'unmerged))))
+        (patch-collapsed-p '(lambda (p) (not (memq p stgit-expanded-patches)))))
+    (easy-menu-define stgit-menu stgit-mode-map
+      "StGit Menu"
+      `("StGit"
+        ["Reload" stgit-reload-or-repair
+         :help "Reload StGit status from disk"]
+        ["Repair" stgit-repair
+         :keys "\\[universal-argument] \\[stgit-reload-or-repair]"
+         :help "Repair StGit metadata"]
+        "-"
+        ["Undo" stgit-undo t]
+        ["Redo" stgit-redo t]
+        "-"
+        ["Git status" stgit-git-status :active (fboundp 'git-status)]
+        "-"
+        ["New patch" stgit-new-and-refresh
+         :help "Create a new patch from changes in index or work tree"
+         :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))]
+        ["New empty patch" stgit-new
+         :help "Create a new, empty patch"]
+        ["(Un)mark patch" stgit-toggle-mark
+         :label (if (memq (stgit-patch-name-at-point nil t)
+                          stgit-marked-patches)
+                    "Unmark patch" "Mark patch")
+         :active (stgit-patch-name-at-point nil t)]
+        ["Expand/collapse patch"
+         (let ((patches (stgit-patches-marked-or-at-point)))
+           (if (member-if ,patch-collapsed-p patches)
+               (stgit-expand patches)
+             (stgit-collapse patches)))
+         :label (if (member-if ,patch-collapsed-p
+                               (stgit-patches-marked-or-at-point))
+                    "Expand patches"
+                  "Collapse patches")
+         :active (stgit-patches-marked-or-at-point)]
+        ["Edit patch" stgit-edit
+         :help "Edit patch comment"
+         :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 (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)]
+        "-"
+        ["Move patches" stgit-move-patches
+         :active stgit-marked-patches
+         :help "Move marked patch(es) to point"]
+        ["Squash patches" stgit-squash
+         :active (> (length stgit-marked-patches) 1)
+         :help "Merge marked patches into one"]
+        "-"
+        ["Refresh top patch" stgit-refresh
+         :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))
+         :help "Refresh the top patch with changes in index or work tree"]
+        ["Refresh this patch" (stgit-refresh t)
+         :keys "\\[universal-argument] \\[stgit-refresh]"
+         :help "Refresh marked patch with changes in index or work tree"
+         :active (and (not (and (stgit-index-empty-p)
+                                (stgit-work-tree-empty-p)))
+                      (stgit-patch-name-at-point nil t))]
+        "-"
+        ["Find file" stgit-select
+         :active (eq (get-text-property (point) 'entry-type) 'file)]
+        ["Open file" stgit-find-file-other-window
+         :active (eq (get-text-property (point) 'entry-type) 'file)]
+        ["Toggle file index" stgit-toggle-index
+         :active (and (eq (get-text-property (point) 'entry-type) 'file)
+                      (memq (stgit-patch-name-at-point) '(:work :index)))
+         :label (if (eq (stgit-patch-name-at-point) :work)
+                    "Move change to index"
+                  "Move change to work tree")]
+        "-"
+        ["Show diff" stgit-diff
+         :active (get-text-property (point) 'entry-type)]
+        ("Merge"
+         :active (stgit-git-index-unmerged-p)
+         ["Combined diff" stgit-diff-combined
+          :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
+         ["Diff against base" stgit-diff-base
+          :help "Show diff against the common base"
+          :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
+         ["Diff against ours" stgit-diff-ours
+          :help "Show diff against our branch"
+          :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
+         ["Diff against theirs" stgit-diff-theirs
+          :help "Show diff against their branch"
+          :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
+         "-"
+         ["Interactive merge" stgit-find-file-merge
+          :help "Interactively merge the file"
+          :active ,at-unmerged-file]
+         ["Resolve file" stgit-resolve-file
+          :help "Mark file conflict as resolved"
+          :active ,at-unmerged-file]
+         )
+        "-"
+        ["Show index & work tree" stgit-toggle-worktree :style toggle
+         :selected stgit-show-worktree]
+        ["Show unknown files" stgit-toggle-unknown :style toggle
+         :selected stgit-show-unknown :active stgit-show-worktree]
+        ["Show ignored files" stgit-toggle-ignored :style toggle
+         :selected stgit-show-ignored :active stgit-show-worktree]
+        ["Show patch names" stgit-toggle-patch-names :style toggle
+         :selected stgit-show-patch-names]
+        "-"
+        ["Switch branches" stgit-branch t
+         :help "Switch to another branch"]
+        ["Rebase branch" stgit-rebase t
+         :help "Rebase the current branch"]
+        ))))
+
+;; disable tool bar editing buttons
+(put 'stgit-mode 'mode-class 'special)
 
 (defun stgit-mode ()
   "Major mode for interacting with StGit.
@@ -865,7 +1074,7 @@ Movement commands:
 \\[stgit-previous-patch]       Move to previous patch
 \\[stgit-next-patch]   Move to next patch
 
-\\[stgit-mark] Mark patch
+\\[stgit-mark-down]    Mark patch and move down
 \\[stgit-unmark-up]    Unmark patch and move up
 \\[stgit-unmark-down]  Unmark patch and move down
 
@@ -874,22 +1083,26 @@ Commands for patches:
 \\[stgit-refresh]      Refresh patch with changes in index or work tree
 \\[stgit-diff] Show the patch log and diff
 
-\\[stgit-expand]       Show changes in selected patches
-\\[stgit-collapse]     Hide changes in selected patches
+\\[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-revert]       Revert all changes in index or work tree
+\\[stgit-toggle-index] Toggle all changes between index and work tree
+
 \\[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)
@@ -899,10 +1112,11 @@ Commands for files:
 \\[stgit-find-file-other-window]       Open the file in another window
 \\[stgit-diff] Show the file's diff
 
-\\[stgit-file-toggle-index]    Toggle change between index and work tree
-\\[stgit-revert-file]  Revert changes to file
+\\[stgit-toggle-index] Toggle change between index and work tree
+\\[stgit-revert]       Revert changes to file
 
 Display commands:
+\\[stgit-toggle-patch-names]   Toggle showing patch names
 \\[stgit-toggle-worktree]      Toggle showing index and work tree
 \\[stgit-toggle-unknown]       Toggle showing unknown files
 \\[stgit-toggle-ignored]       Toggle showing ignored files
@@ -928,6 +1142,7 @@ Commands for branches:
 
 Customization variables:
 `stgit-abbreviate-copies-and-renames'
+`stgit-default-show-patch-names'
 `stgit-default-show-worktree'
 `stgit-find-copies-harder'
 `stgit-show-worktree-mode'
@@ -942,6 +1157,8 @@ See also \\[customize-group] for the \"stgit\" group."
   (set (make-local-variable 'list-buffers-directory) default-directory)
   (set (make-local-variable 'stgit-marked-patches) nil)
   (set (make-local-variable 'stgit-expanded-patches) (list :work :index))
+  (set (make-local-variable 'stgit-show-patch-names)
+       stgit-default-show-patch-names)
   (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
   (set (make-local-variable 'stgit-index-node) nil)
   (set (make-local-variable 'stgit-worktree-node) nil)
@@ -993,14 +1210,16 @@ index or work tree."
 (defun stgit-patched-file-at-point ()
   (get-text-property (point) 'file-data))
 
-(defun stgit-patches-marked-or-at-point ()
-  "Return the symbols of the marked patches, or the patch on the current line."
+(defun stgit-patches-marked-or-at-point (&optional cause-error only-patches)
+  "Return the symbols of the marked patches, or the patch on the current line.
+If CAUSE-ERRROR is not nil, signal an error if none found.
+If ONLY-PATCHES is not nil, do not include index or work tree."
   (if stgit-marked-patches
       stgit-marked-patches
-    (let ((patch (stgit-patch-name-at-point)))
-      (if patch
-          (list patch)
-        '()))))
+    (let ((patch (stgit-patch-name-at-point nil only-patches)))
+      (cond (patch (list patch))
+            (cause-error (error "No patches marked or at this line"))
+            (t nil)))))
 
 (defun stgit-goto-patch (patchsym &optional file)
   "Move point to the line containing patch PATCHSYM.
@@ -1029,13 +1248,23 @@ PATCHSYM."
 (defun stgit-init ()
   "Run stg init."
   (interactive)
+  (stgit-assert-mode)
   (stgit-capture-output nil
     (stgit-run "init"))
   (stgit-reload))
 
+(defun stgit-toggle-mark ()
+  "Toggle mark on the patch under point."
+  (interactive)
+  (stgit-assert-mode)
+  (if (memq (stgit-patch-name-at-point t t) stgit-marked-patches)
+      (stgit-unmark)
+    (stgit-mark)))
+
 (defun stgit-mark ()
   "Mark the patch under point."
   (interactive)
+  (stgit-assert-mode)
   (let* ((node (ewoc-locate stgit-ewoc))
          (patch (ewoc-data node))
          (name (stgit-patch-name patch)))
@@ -1044,26 +1273,39 @@ PATCHSYM."
     (when (eq name :index)
       (error "Cannot mark the index"))
     (stgit-add-mark (stgit-patch-name patch))
-    (ewoc-invalidate stgit-ewoc node))
+    (let ((column (current-column)))
+      (ewoc-invalidate stgit-ewoc node)
+      (move-to-column column))))
+
+(defun stgit-mark-down ()
+  "Mark the patch under point and move to the next patch."
+  (interactive)
+  (stgit-mark)
   (stgit-next-patch))
 
-(defun stgit-unmark-up ()
-  "Remove mark from the patch on the previous line."
+(defun stgit-unmark ()
+  "Remove mark from the patch on the current line."
   (interactive)
-  (stgit-previous-patch)
+  (stgit-assert-mode)
   (let* ((node (ewoc-locate stgit-ewoc))
          (patch (ewoc-data node)))
     (stgit-remove-mark (stgit-patch-name patch))
-    (ewoc-invalidate stgit-ewoc node))
-  (move-to-column (stgit-goal-column)))
+    (let ((column (current-column)))
+      (ewoc-invalidate stgit-ewoc node)
+      (move-to-column column))))
+
+(defun stgit-unmark-up ()
+  "Remove mark from the patch on the previous line."
+  (interactive)
+  (stgit-assert-mode)
+  (stgit-previous-patch)
+  (stgit-unmark))
 
 (defun stgit-unmark-down ()
   "Remove mark from the patch on the current line."
   (interactive)
-  (let* ((node (ewoc-locate stgit-ewoc))
-         (patch (ewoc-data node)))
-    (stgit-remove-mark (stgit-patch-name patch))
-    (ewoc-invalidate stgit-ewoc node))
+  (stgit-assert-mode)
+  (stgit-unmark)
   (stgit-next-patch))
 
 (defun stgit-rename (name)
@@ -1071,6 +1313,7 @@ PATCHSYM."
   (interactive (list
                 (read-string "Patch name: "
                              (symbol-name (stgit-patch-name-at-point t t)))))
+  (stgit-assert-mode)
   (let ((old-patchsym (stgit-patch-name-at-point t t)))
     (stgit-capture-output nil
       (stgit-run "rename" old-patchsym name))
@@ -1090,6 +1333,7 @@ PATCHSYM."
 With a prefix argument, repair the StGit metadata if the branch
 was modified with git commands (`stgit-repair')."
   (interactive "P")
+  (stgit-assert-mode)
   (if repair
       (stgit-repair)
     (stgit-reload)))
@@ -1097,6 +1341,7 @@ was modified with git commands (`stgit-repair')."
 (defun stgit-repair ()
   "Run stg repair."
   (interactive)
+  (stgit-assert-mode)
   (stgit-capture-output nil
     (stgit-run "repair"))
   (stgit-reload))
@@ -1116,6 +1361,7 @@ was modified with git commands (`stgit-repair')."
   "Switch to branch BRANCH."
   (interactive (list (completing-read "Switch to branch: "
                                       (stgit-available-branches))))
+  (stgit-assert-mode)
   (stgit-capture-output nil (stgit-run "branch" "--" branch))
   (stgit-reload))
 
@@ -1141,6 +1387,7 @@ If OMIT-STGIT is not nil, filter out \"resf/heads/*.stgit\"."
   "Rebase to NEW-BASE."
   (interactive (list (completing-read "Rebase to: "
                                       (stgit-available-refs t))))
+  (stgit-assert-mode)
   (stgit-capture-output nil (stgit-run "rebase" new-base))
   (stgit-reload))
 
@@ -1149,6 +1396,7 @@ If OMIT-STGIT is not nil, filter out \"resf/heads/*.stgit\"."
 Interactively, the prefix argument is used as COUNT.
 A negative COUNT will uncommit instead."
   (interactive "p")
+  (stgit-assert-mode)
   (if (< count 0)
       (stgit-uncommit (- count))
     (stgit-capture-output nil (stgit-run "commit" "-n" count))
@@ -1159,6 +1407,7 @@ A negative COUNT will uncommit instead."
 Interactively, the prefix argument is used as COUNT.
 A negative COUNT will commit instead."
   (interactive "p")
+  (stgit-assert-mode)
   (if (< count 0)
       (stgit-commit (- count))
     (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
@@ -1184,6 +1433,7 @@ previous file if point is at the last file within a patch."
   "Revert the file at point, which must be in the index or the
 working tree."
   (interactive)
+  (stgit-assert-mode)
   (let* ((patched-file (or (stgit-patched-file-at-point)
                            (error "No file on the current line")))
          (patch-name   (stgit-patch-name-at-point))
@@ -1219,9 +1469,43 @@ working tree."
         (stgit-reload)
         (stgit-goto-patch patch-name next-file)))))
 
+(defun stgit-revert ()
+  "Revert the change at point, which must be the index, the work
+tree, or a single change in either."
+  (interactive)
+  (stgit-assert-mode)
+  (let ((patched-file (stgit-patched-file-at-point)))
+    (if patched-file
+        (stgit-revert-file)
+      (let* ((patch-name (or (stgit-patch-name-at-point)
+                             (error "No patch or file at point")))
+             (patch-desc (case patch-name
+                           (:index "index")
+                           (:work  "work tree")
+                           (t (error (substitute-command-keys
+                                      "Use \\[stgit-delete] to delete a patch"))))))
+        (when (if (eq patch-name :work)
+                  (stgit-work-tree-empty-p)
+                (stgit-index-empty-p))
+          (error (format "There are no changes in the %s to revert"
+                         patch-desc)))
+        (and (eq patch-name :index)
+             (not (stgit-work-tree-empty-p))
+             (error "Cannot revert index as work tree contains unstaged changes"))
+
+        (when (yes-or-no-p (format "Revert all changes in the %s? "
+                                   patch-desc))
+          (if (eq patch-name :index)
+              (stgit-run-git-silent "reset" "--hard" "-q")
+            (stgit-run-git-silent "checkout" "--" "."))
+          (stgit-refresh-index)
+          (stgit-refresh-worktree)
+          (stgit-goto-patch patch-name))))))
+
 (defun stgit-resolve-file ()
   "Resolve conflict in the file at point."
   (interactive)
+  (stgit-assert-mode)
   (let* ((patched-file (stgit-patched-file-at-point))
          (patch        (stgit-patch-at-point))
          (patch-name   (and patch (stgit-patch-name patch)))
@@ -1242,6 +1526,7 @@ working tree."
   "Push the first unapplied patch.
 With numeric prefix argument, push that many patches."
   (interactive "p")
+  (stgit-assert-mode)
   (stgit-capture-output nil (stgit-run "push" "-n" npatches))
   (stgit-reload)
   (stgit-refresh-git-status))
@@ -1250,27 +1535,50 @@ With numeric prefix argument, push that many patches."
   "Pop the topmost applied patch.
 With numeric prefix argument, pop that many patches."
   (interactive "p")
+  (stgit-assert-mode)
   (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
   (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)
-  (let ((patchsym (stgit-patch-name-at-point t t))
-        (applied (stgit-applied-at-point-p)))
+  (stgit-assert-mode)
+  (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)))
     (stgit-capture-output nil
       (stgit-run "goto" patchsym))
@@ -1354,6 +1662,7 @@ greater than four (e.g., \\[universal-argument] \
                 "")
               name)
      (interactive "p")
+     (stgit-assert-mode)
      (stgit-show-patch ,diff-arg ignore-whitespace)))
 
 (stgit-define-diff stgit-diff
@@ -1386,6 +1695,14 @@ If FORCE is not nil, use --force."
   (stgit-capture-output "*git output*"
     (stgit-run-git "reset" "-q" "--" file)))
 
+(defun stgit-git-index-unmerged-p ()
+  (let (result)
+    (with-output-to-string
+      (setq result (not (zerop (stgit-run-git-silent "diff-index" "--cached"
+                                                     "--diff-filter=U"
+                                                     "--quiet" "HEAD")))))
+    result))
+
 (defun stgit-file-toggle-index ()
   "Move modified file in or out of the index.
 
@@ -1393,6 +1710,7 @@ Leaves the point where it is, but moves the mark to where the
 file ended up. You can then jump to the file with \
 \\[exchange-point-and-mark]."
   (interactive)
+  (stgit-assert-mode)
   (let* ((patched-file   (or (stgit-patched-file-at-point)
                             (error "No file on the current line")))
         (patched-status (stgit-file-status patched-file)))
@@ -1420,9 +1738,39 @@ file ended up. You can then jump to the file with \
       (push-mark nil t t)
       (stgit-goto-patch patch-name point-file))))
 
+(defun stgit-toggle-index ()
+  "Move change in or out of the index.
+
+Works on index and work tree, as well as files in either.
+
+Leaves the point where it is, but moves the mark to where the
+file ended up. You can then jump to the file with \
+\\[exchange-point-and-mark]."
+  (interactive)
+  (stgit-assert-mode)
+  (if (stgit-patched-file-at-point)
+      (stgit-file-toggle-index)
+    (let ((patch-name (stgit-patch-name-at-point)))
+      (unless (memq patch-name '(:index :work))
+        (error "Can only move changes between working tree and index"))
+      (when (stgit-git-index-unmerged-p)
+        (error "Resolve unmerged changes with \\[stgit-resolve-file] first"))
+      (if (if (eq patch-name :index)
+              (stgit-index-empty-p)
+            (stgit-work-tree-empty-p))
+          (message "No changes to be moved")
+        (stgit-capture-output nil
+          (if (eq patch-name :work)
+              (stgit-run-git "add" "--update")
+            (stgit-run-git "reset" "--mixed" "-q")))
+        (stgit-refresh-worktree)
+        (stgit-refresh-index))
+      (stgit-goto-patch (if (eq patch-name :index) :work :index)))))
+
 (defun stgit-edit ()
   "Edit the patch on the current line."
   (interactive)
+  (stgit-assert-mode)
   (let ((patchsym (stgit-patch-name-at-point t t))
         (edit-buf (get-buffer-create "*StGit edit*"))
         (dir default-directory))
@@ -1430,7 +1778,8 @@ file ended up. You can then jump to the file with \
     (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)
@@ -1446,6 +1795,7 @@ file ended up. You can then jump to the file with \
 With a prefix argument, include a \"Signed-off-by:\" line at the
 end of the patch."
   (interactive "P")
+  (stgit-assert-mode)
   (let ((edit-buf (get-buffer-create "*StGit edit*"))
         (dir default-directory))
     (log-edit 'stgit-confirm-new t nil edit-buf)
@@ -1476,6 +1826,7 @@ end of the patch.
 
 This works just like running `stgit-new' followed by `stgit-refresh'."
   (interactive "P")
+  (stgit-assert-mode)
   (stgit-new add-sign t))
 
 (defun stgit-create-patch-name (description)
@@ -1503,8 +1854,9 @@ Interactively, delete the marked patches, or the patch at point.
 
 With a prefix argument, or SPILL-P, spill the patch contents to
 the work tree and index."
-  (interactive (list (stgit-patches-marked-or-at-point)
+  (interactive (list (stgit-patches-marked-or-at-point t t)
                      current-prefix-arg))
+  (stgit-assert-mode)
   (unless patchsyms
     (error "No patches to delete"))
   (when (memq :index patchsyms)
@@ -1530,13 +1882,21 @@ the work tree and index."
   "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
@@ -1568,26 +1928,24 @@ bottom or top of the stack, respectively.
 Interactively, move the marked patches to where the point is."
   (interactive (list stgit-marked-patches
                      (stgit-move-patches-target)))
+  (stgit-assert-mode)
   (unless patchsyms
     (error "Need at least one patch to move"))
 
   (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)
@@ -1598,6 +1956,7 @@ Unless there are any conflicts, the patches will be merged into
 one patch, which will occupy the same spot in the series as the
 deepest patch had before the squash."
   (interactive (list stgit-marked-patches))
+  (stgit-assert-mode)
   (when (< (length patchsyms) 2)
     (error "Need at least two patches to squash"))
   (let ((stgit-buffer (current-buffer))
@@ -1608,8 +1967,9 @@ deepest patch had before the squash."
     (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
@@ -1649,6 +2009,7 @@ With prefix argument, run it with the --hard flag.
 
 See also `stgit-redo'."
   (interactive "P")
+  (stgit-assert-mode)
   (stgit-capture-output nil
     (if arg
         (stgit-run "undo" "--hard")
@@ -1661,6 +2022,7 @@ With prefix argument, run it with the --hard flag.
 
 See also `stgit-undo'."
   (interactive "P")
+  (stgit-assert-mode)
   (stgit-capture-output nil
     (if arg
         (stgit-run "redo" "--hard")
@@ -1673,14 +2035,12 @@ If the index contains any changes, only refresh from index.
 
 With prefix argument, refresh the marked patch or the patch under point."
   (interactive "P")
+  (stgit-assert-mode)
   (let ((patchargs (if arg
-                       (let ((patches (stgit-patches-marked-or-at-point)))
-                         (cond ((null patches)
-                                (error "No patch to update"))
-                               ((> (length patches) 1)
-                                (error "Too many patches selected"))
-                               (t
-                                (cons "-p" patches))))
+                       (let ((patches (stgit-patches-marked-or-at-point nil t)))
+                         (when (> (length patches) 1)
+                           (error "Too many patches marked"))
+                         (cons "-p" patches))
                      nil)))
     (unless (stgit-index-empty-p)
       (setq patchargs (cons "--index" patchargs)))
@@ -1700,6 +2060,9 @@ See also `stgit-show-worktree-mode'.")
 (defvar stgit-show-unknown nil
   "If nil, inhibit showing files not registered with git.")
 
+(defvar stgit-show-patch-names t
+  "If nil, inhibit showing patch names.")
+
 (defun stgit-toggle-worktree (&optional arg)
   "Toggle the visibility of the work tree.
 With ARG, show the work tree if ARG is positive.
@@ -1709,6 +2072,7 @@ Its initial setting is controlled by `stgit-default-show-worktree'.
 `stgit-show-worktree-mode' controls where on screen the index and
 work tree will show up."
   (interactive)
+  (stgit-assert-mode)
   (setq stgit-show-worktree
         (if (numberp arg)
             (> arg 0)
@@ -1721,6 +2085,7 @@ tree. With ARG, show these files if ARG is positive.
 
 Use \\[stgit-toggle-worktree] to show the work tree."
   (interactive)
+  (stgit-assert-mode)
   (setq stgit-show-ignored
         (if (numberp arg)
             (> arg 0)
@@ -1733,10 +2098,24 @@ work tree. With ARG, show these files if ARG is positive.
 
 Use \\[stgit-toggle-worktree] to show the work tree."
   (interactive)
+  (stgit-assert-mode)
   (setq stgit-show-unknown
         (if (numberp arg)
             (> arg 0)
           (not stgit-show-unknown)))
   (stgit-reload))
 
+(defun stgit-toggle-patch-names (&optional arg)
+  "Toggle the visibility of patch names. With ARG, show patch names
+if ARG is positive.
+
+The initial setting is controlled by `stgit-default-show-patch-names'."
+  (interactive)
+  (stgit-assert-mode)
+  (setq stgit-show-patch-names
+        (if (numberp arg)
+            (> arg 0)
+          (not stgit-show-patch-names)))
+  (stgit-reload))
+
 (provide 'stgit)