From 4fd6ec538e4e1b73645569283ed1431bbf3a3a98 Mon Sep 17 00:00:00 2001 From: Davis Herring Date: Thu, 18 Feb 2016 07:47:45 -0700 Subject: [PATCH 1/6] Clean up context diff header regexps * lisp/vc/diff-mode.el (diff-hunk-header-re-context): New const. (diff-hunk-header-re,diff-context->unified,diff-reverse-direction, diff-sanity-check-hunk): Use it. (diff-context-mid-hunk-header-re): Add missing ^. --- lisp/vc/diff-mode.el | 34 +++++++++++++++++++++++----------- 1 files changed, 23 insertions(+), 11 deletions(-) diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el index 58498cb..10a2734 100644 --- a/lisp/vc/diff-mode.el +++ b/lisp/vc/diff-mode.el @@ -359,8 +359,10 @@ well." (defconst diff-hunk-header-re-unified "^@@ -\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? \\+\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? @@") +(defconst diff-hunk-header-re-context + "\\*\\{15\\}\\(?: .*\\)?\n\\*\\*\\* \\(\\([0-9]+\\)\\(?:,\\(-?[0-9]+\\)\\)?\\) \\*\\*\\*\\*") (defconst diff-context-mid-hunk-header-re - "--- \\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? ----$") + "^--- \\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? ----$") (defvar diff-use-changed-face (and (face-differs-from-default-p 'diff-changed) (not (face-equal 'diff-changed 'diff-added)) @@ -435,7 +437,9 @@ empty lines. This makes the format less robust, but is tolerated. See http://lists.gnu.org/archive/html/emacs-devel/2007-11/msg01990.html") (defconst diff-hunk-header-re - (concat "^\\(?:" diff-hunk-header-re-unified ".*\\|\\*\\{15\\}.*\n\\*\\*\\* .+ \\*\\*\\*\\*\\|[0-9]+\\(,[0-9]+\\)?[acd][0-9]+\\(,[0-9]+\\)?\\)$")) + (concat "^\\(?:" diff-hunk-header-re-unified ".*\\|" + diff-hunk-header-re-context + "\\|[0-9]+\\(,[0-9]+\\)?[acd][0-9]+\\(,[0-9]+\\)?\\)$")) (defconst diff-file-header-re (concat "^\\(--- .+\n\\+\\+\\+ \\|\\*\\*\\* .+\n--- \\|[^-+!<>0-9@* \n]\\).+\n" (substring diff-hunk-header-re 1))) (defvar diff-narrowed-to nil) @@ -1028,7 +1032,11 @@ With a prefix argument, convert unified format to context format." (inhibit-read-only t)) (save-excursion (goto-char start) - (while (and (re-search-forward "^\\(\\(\\*\\*\\*\\) .+\n\\(---\\) .+\\|\\*\\{15\\}.*\n\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]+\\) \\*\\*\\*\\*\\)\\(?: \\(.*\\)\\|$\\)" nil t) + (while (and (re-search-forward + (concat "^\\(\\(\\*\\*\\*\\) .+\n\\(---\\) .+\\|" + diff-hunk-header-re-context + "\\)\\(?: \\(.*\\)\\|$\\)") + nil t) (< (point) end)) (combine-after-change-calls (if (match-beginning 2) @@ -1038,15 +1046,15 @@ With a prefix argument, convert unified format to context format." (replace-match "+++" t t nil 3) (replace-match "---" t t nil 2)) ;; we matched a hunk header - (let ((line1s (match-string 4)) - (line1e (match-string 5)) + (let ((line1s (match-string 5)) + (line1e (match-string 6)) (pt1 (match-beginning 0)) ;; Variables to use the special undo function. (old-undo buffer-undo-list) (old-end (marker-position end)) ;; We currently throw away the comment that can follow ;; the hunk header. FIXME: Preserve it instead! - (reversible (not (match-end 6)))) + (reversible (not (match-end 7)))) (replace-match "") (unless (re-search-forward diff-context-mid-hunk-header-re nil t) @@ -1124,7 +1132,11 @@ else cover the whole buffer." (inhibit-read-only t)) (save-excursion (goto-char start) - (while (and (re-search-forward "^\\(\\([-*][-*][-*] \\)\\(.+\\)\n\\([-+][-+][-+] \\)\\(.+\\)\\|\\*\\{15\\}.*\n\\*\\*\\* \\(.+\\) \\*\\*\\*\\*\\|@@ -\\([0-9,]+\\) \\+\\([0-9,]+\\) @@.*\\)$" nil t) + (while (and (re-search-forward + (concat "^\\(\\([-*][-*][-*] \\)\\(.+\\)\n\\([-+][-+][-+] \\)\\(.+\\)\\|" + diff-hunk-header-re-context + "\\|@@ -\\([0-9,]+\\) \\+\\([0-9,]+\\) @@.*\\)$") + nil t) (< (point) end)) (combine-after-change-calls (cond @@ -1477,13 +1489,13 @@ Only works for unified diffs." ;; A context diff. ((eq (char-after) ?*) - (if (not (looking-at "\\*\\{15\\}\\(?: .*\\)?\n\\*\\*\\* \\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? \\*\\*\\*\\*")) + (if (not (looking-at diff-hunk-header-re-context)) (error "Unrecognized context diff first hunk header format") (forward-line 2) (diff-sanity-check-context-hunk-half - (if (match-end 2) - (1+ (- (string-to-number (match-string 2)) - (string-to-number (match-string 1)))) + (if (match-end 3) + (1+ (- (string-to-number (match-string 3)) + (string-to-number (match-string 2)))) 1)) (if (not (looking-at diff-context-mid-hunk-header-re)) (error "Unrecognized context diff second hunk header format") -- 1.7.1