bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug and region patch for diff-reverse-direction in diff-mode.el


From: James LewisMoss
Subject: bug and region patch for diff-reverse-direction in diff-mode.el
Date: Mon, 08 Apr 2002 19:04:09 -0400
User-agent: Gnus/5.090006 (Oort Gnus v0.06) XEmacs/21.4 (Common Lisp, i386-debian-linux)

So I accidentally hit M-R in diff mode and it attempted to reverse the
patch, but it didn't take into account the --- and +++ and just
swapped the first chars making +-- and -++.  Which afaik isn't a valid
patch.  While I was messing with diff-reverse-direction I noticed that
it only works on the region when you give it a prefix arg which is not
what I've come to expect.  So here's the patch:

2002-04-07  James LewisMoss  <address@hidden>

        * diff-mode.el (diff-reverse-direction): Check for --- and +++
        before reversing the ^- or ^+.  Also extract out the regexp into a
        var for clarity.
        (diff-reverse-direction): if there is a region use that.  If not
        do whole buffer.  Change doc string to go along with this and fix
        typo in doc string.

--- xemacs21-basesupport-2002.03.29.orig/lisp/prog-modes/diff-mode.el
+++ xemacs21-basesupport-2002.03.29/lisp/prog-modes/diff-mode.el
@@ -658,11 +658,32 @@
                                               (string-to-number line2s)
                                               -1)) " @@"))))))))))
 
+(defvar diff-patch-start-regexp
+  (concat
+   ;; start
+   "^\\("
+   ;; Typical start of diff
+   ;; --- file
+   ;; +++ file
+   "\\([-*][-*][-*] \\)\\(.+\\)\n"
+   "\\([-+][-+][-+] \\)\\(.+\\)"
+   ;; or
+   "\\|"
+   ;; I'm not sure
+   "\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\n"
+   "\\*\\*\\* \\(.+\\) \\*\\*\\*\\*"
+   ;; or
+   "\\|"
+   ;; hunk
+   "@@ -\\(.+\\) \\+\\(.+\\) @@"
+   ;; end
+   "\\)$"))
+
 (defun diff-reverse-direction (start end)
   "Reverse the direction of the diffs.
-START and END are either taken from the region (if a prefix arg is given) or
-else cover the whole bufer."
-  (interactive (if current-prefix-arg
+START and END are either taken from the region (if it is set) or
+else cover the whole buffer."
+  (interactive (if (mark)
                   (list (mark) (point))
                 (list (point-min) (point-max))))
   (unless (markerp end) (setq end (copy-marker end)))
@@ -670,7 +691,7 @@
        (inhibit-read-only t))
     (save-excursion
       (goto-char start)
-      (while (and (re-search-forward "^\\(\\([-*][-*][-*] 
\\)\\(.+\\)\n\\([-+][-+][-+] 
\\)\\(.+\\)\\|\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\n\\*\\*\\* \\(.+\\) 
\\*\\*\\*\\*\\|@@ -\\(.+\\) \\+\\(.+\\) @@\\)$" nil t)
+      (while (and (re-search-forward diff-patch-start-regexp nil t)
                  (< (point) end))
        (diff-mode-combine-after-change-calls
          (cond
@@ -711,10 +732,26 @@
            (forward-line 1)
            (let ((c (char-after)) first last)
              (while (case (setq c (char-after))
-                      (?- (setq first (or first (point)))
-                          (delete-char 1) (insert "+") t)
-                      (?+ (setq last (or last (point)))
-                          (delete-char 1) (insert "-") t)
+                      (?- (if (and (char= (char-after (+ 1 (point)))
+                                           ?-)
+                                    (char= (char-after (+ 2 (point)))
+                                           ?-))
+                               nil
+                               (progn
+                                 (setq first (or first (point)))
+                                 (delete-char 1)
+                                 (insert "+")
+                                 t)))
+                      (?+ (if (and (char= (char-after (+ 1 (point)))
+                                           ?+)
+                                    (char= (char-after (+ 2 (point)))
+                                           ?+))
+                               nil
+                               (progn
+                                 (setq last (or last (point)))
+                                 (delete-char 1)
+                                 (insert "-")
+                                 t)))
                       (?\\ t)
                       (t (when (and first last (< first last))
                            (let ((str (buffer-substring first last)))

Jim

-- 
@James LewisMoss <address@hidden>      |  Blessed Be!
@    http://jimdres.home.mindspring.com |  Linux is kewl!
@"Argue for your limitations and sure enough, they're yours." Bach



reply via email to

[Prev in Thread] Current Thread [Next in Thread]