chiark / gitweb /
stgit.el: Verify we're in stgit-mode before running commands
authorGustav Hållberg <gustav@virtutech.com>
Fri, 14 Aug 2009 08:54:04 +0000 (10:54 +0200)
committerGustav Hållberg <gustav@virtutech.com>
Sat, 15 Aug 2009 23:52:00 +0000 (01:52 +0200)
Signed-off-by: Gustav Hållberg <gustav@virtutech.com>
contrib/stgit.el

index 2593cf84bb759597345d7f6bcef02d9a80407049..27b17442dfaf960054b420fbc8ca24e837c23698 100644 (file)
@@ -24,6 +24,10 @@ (defun stgit (dir)
   (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."
@@ -253,6 +257,7 @@ (defun stgit-run-series (ewoc)
 (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))
@@ -678,6 +683,7 @@ (defun stgit-expand (&optional patches collapse)
 Non-interactively, operate on PATCHES, and collapse instead of
 expand if COLLAPSE is not nil."
   (interactive (list (stgit-patches-marked-or-at-point)))
+  (stgit-assert-mode)
   (let ((patches-diff (funcall (if collapse #'intersection #'set-difference)
                                patches stgit-expanded-patches)))
     (setq stgit-expanded-patches
@@ -694,6 +700,7 @@ (defun stgit-collapse (&optional patches)
 
 See also `stgit-expand'."
   (interactive (list (stgit-patches-marked-or-at-point)))
+  (stgit-assert-mode)
   (stgit-expand patches t))
 
 (defun stgit-select-patch ()
@@ -707,6 +714,7 @@ (defun stgit-select ()
 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))
@@ -718,22 +726,26 @@ (defun stgit-select ()
 (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))
@@ -751,24 +763,28 @@ (defun stgit-goal-column ()
 (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))
 
@@ -1036,6 +1052,7 @@ (defun stgit-goto-patch (patchsym &optional file)
 (defun stgit-init ()
   "Run stg init."
   (interactive)
+  (stgit-assert-mode)
   (stgit-capture-output nil
     (stgit-run "init"))
   (stgit-reload))
@@ -1043,6 +1060,7 @@ (defun stgit-init ()
 (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)))
@@ -1057,6 +1075,7 @@ (defun stgit-mark ()
 (defun stgit-unmark-up ()
   "Remove mark from the patch on the previous line."
   (interactive)
+  (stgit-assert-mode)
   (stgit-previous-patch)
   (let* ((node (ewoc-locate stgit-ewoc))
          (patch (ewoc-data node)))
@@ -1067,6 +1086,7 @@ (defun stgit-unmark-up ()
 (defun stgit-unmark-down ()
   "Remove mark from the patch on the current line."
   (interactive)
+  (stgit-assert-mode)
   (let* ((node (ewoc-locate stgit-ewoc))
          (patch (ewoc-data node)))
     (stgit-remove-mark (stgit-patch-name patch))
@@ -1078,6 +1098,7 @@ (defun stgit-rename (name)
   (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))
@@ -1097,6 +1118,7 @@ (defun stgit-reload-or-repair (repair)
 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)))
@@ -1104,6 +1126,7 @@ (defun stgit-reload-or-repair (repair)
 (defun stgit-repair ()
   "Run stg repair."
   (interactive)
+  (stgit-assert-mode)
   (stgit-capture-output nil
     (stgit-run "repair"))
   (stgit-reload))
@@ -1123,6 +1146,7 @@ (defun stgit-branch (branch)
   "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))
 
@@ -1148,6 +1172,7 @@ (defun stgit-rebase (new-base)
   "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))
 
@@ -1156,6 +1181,7 @@ (defun stgit-commit (count)
 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))
@@ -1166,6 +1192,7 @@ (defun stgit-uncommit (count)
 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))
@@ -1191,6 +1218,7 @@ (defun stgit-revert-file ()
   "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))
@@ -1230,6 +1258,7 @@ (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)
@@ -1261,6 +1290,7 @@ (defun stgit-revert ()
 (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)))
@@ -1281,6 +1311,7 @@ (defun stgit-push-next (npatches)
   "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))
@@ -1289,6 +1320,7 @@ (defun stgit-pop-next (npatches)
   "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))
@@ -1301,6 +1333,7 @@ (defun stgit-applied-at-point-p ()
 (defun stgit-push-or-pop ()
   "Push or pop the patch on the current line."
   (interactive)
+  (stgit-assert-mode)
   (let ((patchsym (stgit-patch-name-at-point t t))
         (applied (stgit-applied-at-point-p)))
     (stgit-capture-output nil
@@ -1310,6 +1343,7 @@ (defun stgit-push-or-pop ()
 (defun stgit-goto ()
   "Go to the patch on the current line."
   (interactive)
+  (stgit-assert-mode)
   (let ((patchsym (stgit-patch-name-at-point t)))
     (stgit-capture-output nil
       (stgit-run "goto" patchsym))
@@ -1393,6 +1427,7 @@ (defmacro stgit-define-diff (name diff-arg &optional unmerged-action)
                 "")
               name)
      (interactive "p")
+     (stgit-assert-mode)
      (stgit-show-patch ,diff-arg ignore-whitespace)))
 
 (stgit-define-diff stgit-diff
@@ -1440,6 +1475,7 @@ (defun stgit-file-toggle-index ()
 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)))
@@ -1476,6 +1512,7 @@ (defun stgit-toggle-index ()
 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)))
@@ -1498,6 +1535,7 @@ (defun stgit-toggle-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))
@@ -1521,6 +1559,7 @@ (defun stgit-new (add-sign &optional refresh)
 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)
@@ -1551,6 +1590,7 @@ (defun stgit-new-and-refresh (add-sign)
 
 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)
@@ -1580,6 +1620,7 @@ (defun stgit-delete (patchsyms &optional spill-p)
 the work tree and index."
   (interactive (list (stgit-patches-marked-or-at-point)
                      current-prefix-arg))
+  (stgit-assert-mode)
   (unless patchsyms
     (error "No patches to delete"))
   (when (memq :index patchsyms)
@@ -1643,6 +1684,7 @@ (defun stgit-move-patches (patchsyms target-patch)
 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"))
 
@@ -1673,6 +1715,7 @@ (defun stgit-squash (patchsyms)
 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))
@@ -1724,6 +1767,7 @@ (defun stgit-undo (&optional arg)
 
 See also `stgit-redo'."
   (interactive "P")
+  (stgit-assert-mode)
   (stgit-capture-output nil
     (if arg
         (stgit-run "undo" "--hard")
@@ -1736,6 +1780,7 @@ (defun stgit-redo (&optional arg)
 
 See also `stgit-undo'."
   (interactive "P")
+  (stgit-assert-mode)
   (stgit-capture-output nil
     (if arg
         (stgit-run "redo" "--hard")
@@ -1748,6 +1793,7 @@ (defun stgit-refresh (&optional arg)
 
 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)
@@ -1784,6 +1830,7 @@ (defun stgit-toggle-worktree (&optional arg)
 `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)
@@ -1796,6 +1843,7 @@ (defun stgit-toggle-ignored (&optional arg)
 
 Use \\[stgit-toggle-worktree] to show the work tree."
   (interactive)
+  (stgit-assert-mode)
   (setq stgit-show-ignored
         (if (numberp arg)
             (> arg 0)
@@ -1808,6 +1856,7 @@ (defun stgit-toggle-unknown (&optional arg)
 
 Use \\[stgit-toggle-worktree] to show the work tree."
   (interactive)
+  (stgit-assert-mode)
   (setq stgit-show-unknown
         (if (numberp arg)
             (> arg 0)