chiark / gitweb /
stgit.el: Use a defstruct to store patch data
authorDavid Kågedal <david@virtutech.com>
Fri, 22 May 2009 23:09:31 +0000 (01:09 +0200)
committerDavid Kågedal <david@virtutech.com>
Thu, 30 Jul 2009 15:02:40 +0000 (17:02 +0200)
Signed-off-by: David Kågedal <david@virtutech.com>
contrib/stgit.el

index 09f3a3c8898df191794699f90b5aeae4904d21b5..ea5f6b561f5bc0cae37becb8aba0dd6eab6370e5 100644 (file)
@@ -53,6 +53,8 @@ (defun switch-to-stgit-buffer (dir)
     (switch-to-buffer (if buffers
                           (car buffers)
                         (create-stgit-buffer dir)))))
+(defstruct (stgit-patch)
+  status name desc empty)
 
 (defun create-stgit-buffer (dir)
   "Create a buffer for showing StGit patches.
@@ -124,7 +126,7 @@ (defun stgit-reload ()
   (interactive)
   (let ((inhibit-read-only t)
         (curline (line-number-at-pos))
-        (curpatch (stgit-patch-at-point)))
+        (curpatch (stgit-patch-name-at-point)))
     (erase-buffer)
     (insert "Branch: ")
     (stgit-run-silent "branch")
@@ -377,7 +379,7 @@ (defun stgit-rescan ()
                                   'face 'bold))
               ((looking-at "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
               (setq found-any t)
-               (let ((empty (match-string 1))
+               (let ((empty (string= (match-string 1) "0"))
                     (state (match-string 2))
                      (patchsym (intern (match-string 4))))
                  (put-text-property
@@ -393,10 +395,15 @@ (defun stgit-rescan ()
                    (setq marked (cons patchsym marked)))
                  (add-text-properties (match-beginning 0) (match-end 0)
                                       (list 'stgit-patchsym patchsym
-                                            'entry-type 'patch))
+                                            'entry-type 'patch
+                                            'patch-data (make-stgit-patch
+                                                         :status state
+                                                         :name patchsym
+                                                         :desc (match-string 5)
+                                                         :empty empty)))
                  (when (memq patchsym stgit-expanded-patches)
                    (stgit-expand-patch patchsym))
-                (when (equal "0" empty)
+                (when empty
                   (save-excursion
                     (goto-char (match-beginning 5))
                     (insert "(empty) ")))
@@ -425,7 +432,7 @@ (defun stgit-select-file ()
 
 (defun stgit-select-patch ()
   (let ((inhibit-read-only t)
-        (curpatch (stgit-patch-at-point)))
+        (curpatch (stgit-patch-name-at-point)))
     (if (memq curpatch stgit-expanded-patches)
         (save-excursion
           (setq stgit-expanded-patches (delq curpatch stgit-expanded-patches))
@@ -500,7 +507,7 @@ (defun stgit-next-patch (&optional arg)
     (while (not (zerop arg))
       (setq arg (1- arg))
       (while (progn (stgit-next-line)
-                    (not (stgit-patch-at-point)))))))
+                    (not (stgit-patch-name-at-point)))))))
 
 (defun stgit-previous-patch (&optional arg)
   "Move cursor up ARG patches"
@@ -512,7 +519,7 @@ (defun stgit-previous-patch (&optional arg)
     (while (not (zerop arg))
       (setq arg (1- arg))
       (while (progn (stgit-previous-line)
-                    (not (stgit-patch-at-point)))))))
+                    (not (stgit-patch-name-at-point)))))))
 
 (defvar stgit-mode-hook nil
   "Run after `stgit-mode' is setup.")
@@ -603,13 +610,16 @@ (defun stgit-clear-marks ()
   (setq stgit-marked-patches '()))
 
 (defun stgit-patch-at-point (&optional cause-error)
+  (get-text-property (point) 'patch-data))
+
+(defun stgit-patch-name-at-point (&optional cause-error)
   "Return the patch name on the current line as a symbol.
 If CAUSE-ERROR is not nil, signal an error if none found."
-  (case (get-text-property (point) 'entry-type)
-    ('patch (get-text-property (point) 'stgit-patchsym))
-    (t (if cause-error
-           (error "No patch on this line")
-         nil))))
+  (let ((patch (stgit-patch-at-point)))
+    (cond (patch
+           (stgit-patch-name patch))
+          (cause-error
+           (error "No patch on this line")))))
 
 (defun stgit-patched-file-at-point (&optional both-files)
   "Returns a cons of the patchsym and file name at point. For
@@ -626,7 +636,7 @@ (defun stgit-patched-file-at-point (&optional both-files)
           (t
            (let ((file-sym (save-excursion
                              (stgit-previous-patch)
-                             (unless (eq (stgit-patch-at-point)
+                             (unless (eq (stgit-patch-name-at-point)
                                          patchsym)
                                (error "Cannot find the %s patch" patchsym))
                              (beginning-of-line)
@@ -639,7 +649,7 @@ (defun stgit-patches-marked-or-at-point ()
   "Return the symbols of the marked patches, or the patch on the current line."
   (if stgit-marked-patches
       stgit-marked-patches
-    (let ((patch (stgit-patch-at-point)))
+    (let ((patch (stgit-patch-name-at-point)))
       (if patch
           (list patch)
         '()))))
@@ -663,7 +673,7 @@ (defun stgit-init ()
 (defun stgit-mark ()
   "Mark the patch under point."
   (interactive)
-  (let ((patch (stgit-patch-at-point t)))
+  (let ((patch (stgit-patch-name-at-point t)))
     (stgit-add-mark patch))
   (stgit-next-patch))
 
@@ -671,19 +681,19 @@ (defun stgit-unmark-up ()
   "Remove mark from the patch on the previous line."
   (interactive)
   (stgit-previous-patch)
-  (stgit-remove-mark (stgit-patch-at-point t)))
+  (stgit-remove-mark (stgit-patch-name-at-point t)))
 
 (defun stgit-unmark-down ()
   "Remove mark from the patch on the current line."
   (interactive)
-  (stgit-remove-mark (stgit-patch-at-point t))
+  (stgit-remove-mark (stgit-patch-name-at-point t))
   (stgit-next-patch))
 
 (defun stgit-rename (name)
   "Rename the patch under point to NAME."
   (interactive (list (read-string "Patch name: "
-                                  (symbol-name (stgit-patch-at-point t)))))
-  (let ((old-patchsym (stgit-patch-at-point t)))
+                                  (symbol-name (stgit-patch-name-at-point t)))))
+  (let ((old-patchsym (stgit-patch-name-at-point t)))
     (stgit-capture-output nil
       (stgit-run "rename" old-patchsym name))
     (let ((name-sym (intern name)))
@@ -760,7 +770,7 @@ (defun stgit-applied-at-point ()
 (defun stgit-push-or-pop ()
   "Push or pop the patch on the current line."
   (interactive)
-  (let ((patchsym (stgit-patch-at-point t))
+  (let ((patchsym (stgit-patch-name-at-point t))
         (applied (stgit-applied-at-point)))
     (stgit-capture-output nil
       (stgit-run (if applied "pop" "push") patchsym))
@@ -769,7 +779,7 @@ (defun stgit-push-or-pop ()
 (defun stgit-goto ()
   "Go to the patch on the current line."
   (interactive)
-  (let ((patchsym (stgit-patch-at-point t)))
+  (let ((patchsym (stgit-patch-name-at-point t)))
     (stgit-capture-output nil
       (stgit-run "goto" patchsym))
     (stgit-reload)))
@@ -791,7 +801,7 @@ (defun stgit-show ()
   (stgit-capture-output "*StGit patch*"
     (case (get-text-property (point) 'entry-type)
       ('file
-       (let ((patchsym (stgit-patch-at-point))
+       (let ((patchsym (stgit-patch-name-at-point))
              (patched-file (stgit-patched-file-at-point t)))
          (let ((id (stgit-id (car patched-file))))
            (if (consp (cdr patched-file))
@@ -802,7 +812,8 @@ (defun stgit-show ()
              (stgit-run-git "diff" (concat id "^") id "--"
                             (cdr patched-file))))))
       ('patch
-       (stgit-run "show" "-O" "--patch-with-stat" "-O" "-M" (stgit-patch-at-point)))
+       (stgit-run "show" "-O" "--patch-with-stat" "-O" "-M"
+                  (stgit-patch-name-at-point)))
       (t
        (error "No patch or file at point")))
     (with-current-buffer standard-output
@@ -812,7 +823,7 @@ (defun stgit-show ()
 (defun stgit-edit ()
   "Edit the patch on the current line."
   (interactive)
-  (let ((patchsym (stgit-patch-at-point t))
+  (let ((patchsym (stgit-patch-name-at-point t))
         (edit-buf (get-buffer-create "*StGit edit*"))
         (dir default-directory))
     (log-edit 'stgit-confirm-edit t nil edit-buf)
@@ -903,7 +914,7 @@ (defun stgit-move-patches-target ()
 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-at-point)))
+  (let ((patchsym (stgit-patch-name-at-point)))
     (cond (patchsym patchsym)
          ((save-excursion (re-search-backward "^>" nil t)) :top)
          (t :bottom))))