aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/diff-mode.el
diff options
context:
space:
mode:
authorStefan Monnier2000-09-20 16:56:13 +0000
committerStefan Monnier2000-09-20 16:56:13 +0000
commitd868b3bdc32abda0619c3a25eb3e529fe8de307f (patch)
treea045f0ac00af63c7333bd9cdfde7a65ae839205d /lisp/diff-mode.el
parent8b93df0128d3c27cc67aa77885d925ca991c0215 (diff)
downloademacs-d868b3bdc32abda0619c3a25eb3e529fe8de307f.tar.gz
emacs-d868b3bdc32abda0619c3a25eb3e529fe8de307f.zip
(diff-find-source-location):
Move code from diff-apply-hunk. Return buffer rather than file. (diff-apply-hunk): Use the new result from diff-find-source-location. (diff-goto-source): Use the new diff-find-source-location.
Diffstat (limited to 'lisp/diff-mode.el')
-rw-r--r--lisp/diff-mode.el91
1 files changed, 42 insertions, 49 deletions
diff --git a/lisp/diff-mode.el b/lisp/diff-mode.el
index b36500840ce..d1535da386e 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.17 2000/09/19 16:25:43 monnier Exp $ 7;; Revision: $Id: diff-mode.el,v 1.18 2000/09/20 06:40:30 miles Exp $
8 8
9;; This file is part of GNU Emacs. 9;; This file is part of GNU Emacs.
10 10
@@ -952,26 +952,40 @@ If TEXT isn't found, nil is returned."
952 (if (> (- forw orig) (- orig back)) back forw) 952 (if (> (- forw orig) (- orig back)) back forw)
953 (or back forw)))) 953 (or back forw))))
954 954
955(defun diff-find-source-location (&optional other-file) 955(defun diff-find-source-location (&optional other-file reverse)
956 "Find out (FILE LINE)." 956 "Find out (BUF LINE POS SRC DST SWITCHED)."
957 (save-excursion 957 (save-excursion
958 (diff-beginning-of-hunk)
959 (let* ((old (if (not other-file) diff-jump-to-old-file-flag 958 (let* ((old (if (not other-file) diff-jump-to-old-file-flag
960 (not diff-jump-to-old-file-flag))) 959 (not diff-jump-to-old-file-flag)))
960 (orig-point (point))
961 (hunk-line-offset
962 (progn (diff-beginning-of-hunk) (count-lines (point) orig-point)))
961 ;; Find the location specification. 963 ;; Find the location specification.
962 (loc (if (not (looking-at "\\(?:\\*\\{15\\}.*\n\\)?[-@* ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?")) 964 (line (if (not (looking-at "\\(?:\\*\\{15\\}.*\n\\)?[-@* ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?"))
963 (error "Can't find the hunk header") 965 (error "Can't find the hunk header")
964 (if old (match-string 1) 966 (if old (match-string 1)
965 (if (match-end 3) (match-string 3) 967 (if (match-end 3) (match-string 3)
966 (unless (re-search-forward "^--- \\([0-9,]+\\)" nil t) 968 (unless (re-search-forward "^--- \\([0-9,]+\\)" nil t)
967 (error "Can't find the hunk separator")) 969 (error "Can't find the hunk separator"))
968 (match-string 1))))) 970 (match-string 1)))))
969 (file (diff-find-file-name old))) 971 (file (or (diff-find-file-name old) (error "Can't find the file")))
972 (buf (find-file-noselect file))
973 (hunk
974 (buffer-substring (point) (progn (diff-end-of-hunk) (point))))
975 (old (diff-hunk-text hunk reverse hunk-line-offset))
976 (new (diff-hunk-text hunk (not reverse) hunk-line-offset)))
970 ;; Update the user preference if he so wished. 977 ;; Update the user preference if he so wished.
971 (when (> (prefix-numeric-value other-file) 8) 978 (when (> (prefix-numeric-value other-file) 8)
972 (setq diff-jump-to-old-file-flag old)) 979 (setq diff-jump-to-old-file-flag old))
973 (if (null file) (error "Can't find the file") 980 (with-current-buffer buf
974 (list file (string-to-number loc)))))) 981 (goto-line (string-to-number line))
982 (let* ((orig-pos (point))
983 (pos (diff-find-text (car old)))
984 (switched nil))
985 (when (null pos)
986 (setq pos (diff-find-text (car new)) switched t))
987 (list* buf (string-to-number line) pos
988 (if switched (list new old t) (list old new))))))))
975 989
976(defun diff-apply-hunk (&optional reverse other-file dry-run popup noerror) 990(defun diff-apply-hunk (&optional reverse other-file dry-run popup noerror)
977 "Apply the current hunk to the source file. 991 "Apply the current hunk to the source file.
@@ -990,7 +1004,7 @@ If POPUP is non-nil, pop up the patched file in another window; if POPUP
990If NOERROR is non-nil, then no error is signaled in the case where the hunk 1004If NOERROR is non-nil, then no error is signaled in the case where the hunk
991 cannot be found in the source file (other errors may still be signaled). 1005 cannot be found in the source file (other errors may still be signaled).
992 1006
993Return values are `t' if the hunk was sucessfully applied (or could be 1007Return values are t if the hunk was sucessfully applied (or could be
994applied, in the case where DRY-RUN was non-nil), `reversed' if the hunk 1008applied, in the case where DRY-RUN was non-nil), `reversed' if the hunk
995was applied backwards, or nil if the hunk couldn't be found and NOERROR 1009was applied backwards, or nil if the hunk couldn't be found and NOERROR
996was non-nil." 1010was non-nil."
@@ -1004,52 +1018,27 @@ was non-nil."
1004 ;; sense of OTHER-FILE (in `diff-find-source-location') 1018 ;; sense of OTHER-FILE (in `diff-find-source-location')
1005 (setq reverse (not reverse))) 1019 (setq reverse (not reverse)))
1006 1020
1007 (let* ((loc (diff-find-source-location other-file)) 1021 (destructuring-bind (buf patch-line pos old new &optional switched)
1008 (buf (find-file-noselect (car loc))) 1022 (diff-find-source-location other-file reverse)
1009 (patch-line (cadr loc)) 1023
1010 hunk-line-offset 1024 (when (and pos switched popup)
1011 (hunk
1012 (let ((orig-point (point)))
1013 (save-excursion
1014 (diff-beginning-of-hunk)
1015 (setq hunk-line-offset (count-lines (point) orig-point))
1016 (unless (looking-at diff-hunk-header-re)
1017 (error "Malformed hunk"))
1018 (buffer-substring (point) (progn (diff-end-of-hunk) (point))))))
1019 (old (diff-hunk-text hunk reverse hunk-line-offset))
1020 (new (diff-hunk-text hunk (not reverse) hunk-line-offset))
1021 (pos
1022 (with-current-buffer buf
1023 (goto-line patch-line)
1024 (diff-find-text (car old))))
1025 (reversed-pos
1026 (and (null pos)
1027 (with-current-buffer buf
1028 (goto-line patch-line)
1029 (diff-find-text (car new))))))
1030
1031 (when (and reversed-pos popup)
1032 ;; A reversed patch was detected, perhaps apply it in reverse 1025 ;; A reversed patch was detected, perhaps apply it in reverse
1033 ;; (this is only done in `interactive' mode, when POPUP is non-nil). 1026 ;; (this is only done in `interactive' mode, when POPUP is non-nil).
1034 (if (or dry-run 1027 (if (or dry-run
1035 (save-window-excursion 1028 (save-window-excursion
1036 (pop-to-buffer buf) 1029 (pop-to-buffer buf)
1037 (goto-char reversed-pos) 1030 (goto-char pos)
1038 (forward-line (cdr new)) 1031 (forward-line (cdr old))
1039 (if reverse 1032 (if reverse
1040 (y-or-n-p 1033 (y-or-n-p
1041 "Hunk hasn't been applied yet, so can't reverse it; apply it now? ") 1034 "Hunk hasn't been applied yet, so can't reverse it; apply it now? ")
1042 (y-or-n-p "Hunk has already been applied; undo it? ")))) 1035 (y-or-n-p "Hunk has already been applied; undo it? "))))
1043 1036
1044 ;; Set up things to reverse the diff 1037 nil
1045 (let ((swap new))
1046 (setq pos reversed-pos)
1047 (setq old new)
1048 (setq new swap))
1049
1050 ;; The user has chosen not to apply the reversed hunk, but we 1038 ;; The user has chosen not to apply the reversed hunk, but we
1051 ;; don't want to given an error message, so set things up so 1039 ;; don't want to given an error message, so set things up so
1052 ;; nothing else gets done down below 1040 ;; nothing else gets done down below
1041 (setq pos nil)
1053 (message "(Nothing done)") 1042 (message "(Nothing done)")
1054 (setq noerror t))) 1043 (setq noerror t)))
1055 1044
@@ -1058,7 +1047,7 @@ was non-nil."
1058 (unless noerror 1047 (unless noerror
1059 (error "Can't find the text to patch")) 1048 (error "Can't find the text to patch"))
1060 1049
1061 (let ((reversed (if reversed-pos (not reverse) reverse))) 1050 (let ((reversed (if switched (not reverse) reverse)))
1062 (unless dry-run 1051 (unless dry-run
1063 ;; Apply the hunk 1052 ;; Apply the hunk
1064 (with-current-buffer buf 1053 (with-current-buffer buf
@@ -1111,12 +1100,16 @@ is give) determines whether to jump to the old or the new file.
1111If the prefix arg is bigger than 8 (for example with \\[universal-argument] \\[universal-argument]) 1100If the prefix arg is bigger than 8 (for example with \\[universal-argument] \\[universal-argument])
1112 then `diff-jump-to-old-file-flag' is also set, for the next invocations." 1101 then `diff-jump-to-old-file-flag' is also set, for the next invocations."
1113 (interactive "P") 1102 (interactive "P")
1114 (or (diff-apply-hunk nil other-file t 'select t) 1103 (destructuring-bind (buf patch-line pos src &rest ignore)
1115 ;; couldn't actually find the hunk, just obey the hunk line number 1104 (diff-find-source-location other-file)
1116 (let ((loc (diff-find-source-location other-file))) 1105 (pop-to-buffer buf)
1117 (pop-to-buffer (find-file-noselect (car loc))) 1106 (if (null pos)
1118 (goto-line (cadr loc)) 1107 (progn
1119 (message "Hunk text not found")))) 1108 (goto-line patch-line)
1109 (message "Hunk text not found"))
1110 (goto-char pos)
1111 (forward-line (cdr src)))))
1112
1120 1113
1121 1114
1122;; provide the package 1115;; provide the package