aboutsummaryrefslogtreecommitdiffstats
path: root/lisp
diff options
context:
space:
mode:
authorStefan Monnier2000-09-21 16:52:30 +0000
committerStefan Monnier2000-09-21 16:52:30 +0000
commit6e4e8a3b59a7f81c0611bebf08e38f98b4d3f5a6 (patch)
treea1e4351cec1ebb569be7b9ef816e9ab40696a605 /lisp
parent5768681d58479ef65f2bfb7b0311db675a06c183 (diff)
downloademacs-6e4e8a3b59a7f81c0611bebf08e38f98b4d3f5a6.tar.gz
emacs-6e4e8a3b59a7f81c0611bebf08e38f98b4d3f5a6.zip
(diff-hunk-text): Properly handle one-sided context diffs.
(diff-apply-hunk): When done, advance to the next hunk.
Diffstat (limited to 'lisp')
-rw-r--r--lisp/ChangeLog13
-rw-r--r--lisp/diff-mode.el151
2 files changed, 92 insertions, 72 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 608bdec8adb..7f3d49ba90d 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,16 @@
12000-09-21 Stefan Monnier <monnier@cs.yale.edu>
2
3 * diff-mode.el (diff-file-header-face): Reset to its previous value.
4 (diff-hunk-text): Correctly use offsets rather than buffer-positions.
5 (diff-xor): New function.
6 (diff-find-source-location): Use it. Fix a stupid name clash.
7 (diff-hunk-status-msg): New function.
8 (diff-apply-hunk): Drop args OTHER-FILE, DRY-RUN, POPUP and NOERROR.
9 (diff-test-hunk): Use diff-find-source-location.
10 (diff-goto-source): Favor the `reverse'.
11 (diff-hunk-text): Properly handle one-sided context diffs.
12 (diff-apply-hunk): When done, advance to the next hunk.
13
12000-09-21 Gerd Moellmann <gerd@gnu.org> 142000-09-21 Gerd Moellmann <gerd@gnu.org>
2 15
3 * add-log.el (change-log-date-face, change-log-name-face) 16 * add-log.el (change-log-date-face, change-log-name-face)
diff --git a/lisp/diff-mode.el b/lisp/diff-mode.el
index 17ef4697faf..1d00f696452 100644
--- a/lisp/diff-mode.el
+++ b/lisp/diff-mode.el
@@ -4,7 +4,7 @@
4 4
5;; Author: Stefan Monnier <monnier@cs.yale.edu> 5;; Author: Stefan Monnier <monnier@cs.yale.edu>
6;; Keywords: patch diff 6;; Keywords: patch diff
7;; Revision: $Id: diff-mode.el,v 1.20 2000/09/20 22:36:23 monnier Exp $ 7;; Revision: $Id: diff-mode.el,v 1.21 2000/09/21 16:15:32 monnier Exp $
8 8
9;; This file is part of GNU Emacs. 9;; This file is part of GNU Emacs.
10 10
@@ -876,75 +876,80 @@ If CHAR-OFFSET is non-nil, it should be a char-offset in
876HUNK, and instead of a string, a cons cell is returned whose car is the 876HUNK, and instead of a string, a cons cell is returned whose car is the
877appropriate text, and whose cdr is the corresponding char-offset in that text." 877appropriate text, and whose cdr is the corresponding char-offset in that text."
878 (with-temp-buffer 878 (with-temp-buffer
879 (insert hunk) 879 (insert hunk)
880 (goto-char (point-min)) 880 (goto-char (point-min))
881 (let ((src-pos nil) 881 (let ((src-pos nil)
882 (dst-pos nil) 882 (dst-pos nil)
883 (divider-pos nil) 883 (divider-pos nil)
884 (num-pfx-chars 2)) 884 (num-pfx-chars 2))
885 ;; Set the following variables: 885 ;; Set the following variables:
886 ;; SRC-POS buffer pos of the source part of the hunk or nil if none 886 ;; SRC-POS buffer pos of the source part of the hunk or nil if none
887 ;; DST-POS buffer pos of the destination part of the hunk or nil 887 ;; DST-POS buffer pos of the destination part of the hunk or nil
888 ;; DIVIDER-POS buffer pos of any divider line separating the src & dst 888 ;; DIVIDER-POS buffer pos of any divider line separating the src & dst
889 ;; NUM-PFX-CHARS number of line-prefix characters used by this format" 889 ;; NUM-PFX-CHARS number of line-prefix characters used by this format"
890 (cond ((looking-at "^@@") 890 (cond ((looking-at "^@@")
891 ;; unified diff 891 ;; unified diff
892 (setq num-pfx-chars 1) 892 (setq num-pfx-chars 1)
893 (forward-line 1) 893 (forward-line 1)
894 (setq src-pos (point) dst-pos (point))) 894 (setq src-pos (point) dst-pos (point)))
895 ((looking-at "^\\*\\*") 895 ((looking-at "^\\*\\*")
896 ;; context diff 896 ;; context diff
897 (forward-line 2) 897 (forward-line 2)
898 (setq src-pos (point)) 898 (setq src-pos (point))
899 (re-search-forward "^--- " nil t) 899 (re-search-forward "^--- " nil t)
900 (forward-line 0) 900 (forward-line 0)
901 (setq divider-pos (point)) 901 (setq divider-pos (point))
902 (forward-line 1) 902 (forward-line 1)
903 (setq dst-pos (point))) 903 (setq dst-pos (point)))
904 ((looking-at "^[0-9]+a[0-9,]+$") 904 ((looking-at "^[0-9]+a[0-9,]+$")
905 ;; normal diff, insert 905 ;; normal diff, insert
906 (forward-line 1) 906 (forward-line 1)
907 (setq dst-pos (point))) 907 (setq dst-pos (point)))
908 ((looking-at "^[0-9,]+d[0-9]+$") 908 ((looking-at "^[0-9,]+d[0-9]+$")
909 ;; normal diff, delete 909 ;; normal diff, delete
910 (forward-line 1) 910 (forward-line 1)
911 (setq src-pos (point))) 911 (setq src-pos (point)))
912 ((looking-at "^[0-9,]+c[0-9,]+$") 912 ((looking-at "^[0-9,]+c[0-9,]+$")
913 ;; normal diff, change 913 ;; normal diff, change
914 (forward-line 1) 914 (forward-line 1)
915 (setq src-pos (point)) 915 (setq src-pos (point))
916 (re-search-forward "^---$" nil t) 916 (re-search-forward "^---$" nil t)
917 (forward-line 0) 917 (forward-line 0)
918 (setq divider-pos (point)) 918 (setq divider-pos (point))
919 (forward-line 1) 919 (forward-line 1)
920 (setq dst-pos (point))) 920 (setq dst-pos (point)))
921 (t 921 (t
922 (error "Unknown diff hunk type"))) 922 (error "Unknown diff hunk type")))
923 (if (if destp (null dst-pos) (null src-pos)) 923
924 ;; Implied empty text 924 (if (if destp (null dst-pos) (null src-pos))
925 (if char-offset '("" . 0) "") 925 ;; Implied empty text
926 926 (if char-offset '("" . 0) "")
927 (when char-offset (goto-char (+ (point-min) char-offset))) 927
928 928 ;; For context diffs, either side can be empty, (if there's only
929 ;; Get rid of anything except the desired text. 929 ;; added or only removed text). We should then use the other side.
930 (save-excursion 930 (cond ((equal src-pos divider-pos) (setq src-pos dst-pos))
931 ;; Delete unused text region 931 ((equal dst-pos (point-max)) (setq dst-pos src-pos)))
932 (let ((keep (if destp dst-pos src-pos)) 932
933 (kill (or divider-pos (if destp src-pos dst-pos)))) 933 (when char-offset (goto-char (+ (point-min) char-offset)))
934 (when (and kill (> kill keep)) 934
935 (delete-region kill (point-max))) 935 ;; Get rid of anything except the desired text.
936 (delete-region (point-min) keep)) 936 (save-excursion
937 ;; Remove line-prefix characters, and unneeded lines (unified diffs). 937 ;; Delete unused text region
938 (let ((kill-char (if destp ?- ?+))) 938 (let ((keep (if destp dst-pos src-pos)))
939 (goto-char (point-min)) 939 (when (and divider-pos (> divider-pos keep))
940 (while (not (eobp)) 940 (delete-region divider-pos (point-max)))
941 (if (eq (char-after) kill-char) 941 (delete-region (point-min) keep))
942 (delete-region (point) (progn (forward-line 1) (point))) 942 ;; Remove line-prefix characters, and unneeded lines (unified diffs).
943 (delete-char num-pfx-chars) 943 (let ((kill-char (if destp ?- ?+)))
944 (forward-line 1))))) 944 (goto-char (point-min))
945 945 (while (not (eobp))
946 (let ((text (buffer-substring-no-properties (point-min) (point-max)))) 946 (if (eq (char-after) kill-char)
947 (if char-offset (cons text (- (point) (point-min))) text)))))) 947 (delete-region (point) (progn (forward-line 1) (point)))
948 (delete-char num-pfx-chars)
949 (forward-line 1)))))
950
951 (let ((text (buffer-substring-no-properties (point-min) (point-max))))
952 (if char-offset (cons text (- (point) (point-min))) text))))))
948 953
949 954
950(defun diff-find-text (text) 955(defun diff-find-text (text)
@@ -1010,7 +1015,7 @@ If TEXT isn't found, nil is returned."
1010 1015
1011 1016
1012(defun diff-apply-hunk (&optional reverse) 1017(defun diff-apply-hunk (&optional reverse)
1013 "Apply the current hunk to the source file. 1018 "Apply the current hunk to the source file and go to the next.
1014By default, the new source file is patched, but if the variable 1019By default, the new source file is patched, but if the variable
1015`diff-jump-to-old-file-flag' is non-nil, then the old source file is 1020`diff-jump-to-old-file-flag' is non-nil, then the old source file is
1016patched instead (some commands, such as `diff-goto-source' can change 1021patched instead (some commands, such as `diff-goto-source' can change
@@ -1034,7 +1039,8 @@ hunk was applied backwards and nil if the hunk wasn't applied."
1034 (if reverse 1039 (if reverse
1035 "Hunk hasn't been applied yet; apply it now? " 1040 "Hunk hasn't been applied yet; apply it now? "
1036 "Hunk has already been applied; undo it? "))))) 1041 "Hunk has already been applied; undo it? ")))))
1037 (message "(Nothing done)")) 1042 (message "(Nothing done)")
1043 nil)
1038 (t 1044 (t
1039 (let ((reversed (diff-xor switched reverse))) 1045 (let ((reversed (diff-xor switched reverse)))
1040 ;; Apply the hunk 1046 ;; Apply the hunk
@@ -1046,6 +1052,7 @@ hunk was applied backwards and nil if the hunk wasn't applied."
1046 (let ((win (display-buffer buf))) 1052 (let ((win (display-buffer buf)))
1047 (set-window-point win (+ pos (cdr new)))) 1053 (set-window-point win (+ pos (cdr new))))
1048 (diff-hunk-status-msg line-offset reversed nil) 1054 (diff-hunk-status-msg line-offset reversed nil)
1055 (diff-hunk-next)
1049 (if reversed 'reversed t)))))) 1056 (if reversed 'reversed t))))))
1050 1057
1051 1058