comparison lisp/diff-mode.el @ 29423:12e89e9b65f6

(diff-font-lock-keywords, diff-hunk-header-re) (diff-goto-source, diff-unified->context, diff-context->unified) (diff-reverse-direction, diff-fixup-modifs): Fix the regexps to understand the format output by the `-p' argument to diff.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Mon, 05 Jun 2000 07:30:09 +0000
parents 85d4cc0b8741
children 7f824fa01e10
comparison
equal deleted inserted replaced
29422:be5a54fc840d 29423:12e89e9b65f6
2 2
3 ;; Copyright (C) 1998-1999 Free Software Foundation, Inc. 3 ;; Copyright (C) 1998-1999 Free Software Foundation, Inc.
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.6 2000/03/21 16:59:17 monnier Exp $ 7 ;; Revision: $Id: diff-mode.el,v 1.7 2000/05/10 22:12:46 monnier Exp $
8 8
9 ;; This file is part of GNU Emacs. 9 ;; This file is part of GNU Emacs.
10 10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify 11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by 12 ;; it under the terms of the GNU General Public License as published by
197 "diff-mode face used to highlight changed lines." 197 "diff-mode face used to highlight changed lines."
198 :group 'diff-mode) 198 :group 'diff-mode)
199 (defvar diff-changed-face 'diff-changed-face) 199 (defvar diff-changed-face 'diff-changed-face)
200 200
201 (defvar diff-font-lock-keywords 201 (defvar diff-font-lock-keywords
202 '(("^@@ .+ @@$" . diff-hunk-header-face) ;unified 202 '(("^@@ -[0-9,]+ \\+[0-9,]+ @@.*$" . diff-hunk-header-face) ;unified
203 ("^--- .+ ----$" . diff-hunk-header-face) ;context 203 ("^--- .+ ----$" . diff-hunk-header-face) ;context
204 ("^\\*\\*\\*.+\\*\\*\\*\n" . diff-hunk-header-face) ;context 204 ("^\\*\\*\\*\\(.+\\*\\*\\*\\|\\*\\{12\\}.*\\)\n" . diff-hunk-header-face) ;context
205 ("^\\(---\\|\\+\\+\\+\\|\\*\\*\\*\\) .*\n" . diff-file-header-face) 205 ("^\\(---\\|\\+\\+\\+\\|\\*\\*\\*\\) .*\n" . diff-file-header-face)
206 ("^[0-9,]+[acd][0-9,]+$" . diff-hunk-header-face) 206 ("^[0-9,]+[acd][0-9,]+$" . diff-hunk-header-face)
207 ("^!.*\n" . diff-changed-face) ;context 207 ("^!.*\n" . diff-changed-face) ;context
208 ("^[+>].*\n" . diff-added-face) 208 ("^[+>].*\n" . diff-added-face)
209 ("^[-<].*\n" . diff-removed-face) 209 ("^[-<].*\n" . diff-removed-face)
228 228
229 ;;;; 229 ;;;;
230 ;;;; Movement 230 ;;;; Movement
231 ;;;; 231 ;;;;
232 232
233 (defconst diff-hunk-header-re "^\\(@@ .+ @@\\|\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\n\\*\\*\\* .+ \\*\\*\\*\\*\\|[0-9]+\\(,[0-9]+\\)?[acd][0-9]+\\(,[0-9]+\\)?\\)$") 233 (defconst diff-hunk-header-re "^\\(@@ -[0-9,]+ \\+[0-9,]+ @@.*\\|\\*\\{15\\}.*\n\\*\\*\\* .+ \\*\\*\\*\\*\\|[0-9]+\\(,[0-9]+\\)?[acd][0-9]+\\(,[0-9]+\\)?\\)$")
234 (defconst diff-file-header-re (concat "^\\(--- .+\n\\+\\+\\+\\|\\*\\*\\* .+\n---\\|[^-+!<>0-9@* ]\\).+\n" (substring diff-hunk-header-re 1))) 234 (defconst diff-file-header-re (concat "^\\(--- .+\n\\+\\+\\+\\|\\*\\*\\* .+\n---\\|[^-+!<>0-9@* ]\\).+\n" (substring diff-hunk-header-re 1)))
235 (defvar diff-narrowed-to nil) 235 (defvar diff-narrowed-to nil)
236 236
237 (defun diff-end-of-hunk (&optional style) 237 (defun diff-end-of-hunk (&optional style)
238 (if (looking-at diff-hunk-header-re) (goto-char (match-end 0))) 238 (if (looking-at diff-hunk-header-re) (goto-char (match-end 0)))
431 (let ((old (if (not other-file) diff-jump-to-old-file-flag 431 (let ((old (if (not other-file) diff-jump-to-old-file-flag
432 (not diff-jump-to-old-file-flag)))) 432 (not diff-jump-to-old-file-flag))))
433 (when (> (prefix-numeric-value other-file) 8) 433 (when (> (prefix-numeric-value other-file) 8)
434 (setq diff-jump-to-old-file-flag old)) 434 (setq diff-jump-to-old-file-flag old))
435 (diff-beginning-of-hunk) 435 (diff-beginning-of-hunk)
436 (let* ((loc (if (not (looking-at "[-@*\n ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?")) 436 (let* ((loc (if (not (looking-at "\\(?:\\*\\{15\\}.*\n\\)?[-@* ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?"))
437 (error "Can't find the hunk header") 437 (error "Can't find the hunk header")
438 (if old (match-string 1) 438 (if old (match-string 1)
439 (if (match-end 3) (match-string 3) 439 (if (match-end 3) (match-string 3)
440 (unless (re-search-forward "^--- \\([0-9,]+\\)" nil t) 440 (unless (re-search-forward "^--- \\([0-9,]+\\)" nil t)
441 (error "Can't find the hunk separator")) 441 (error "Can't find the hunk separator"))
482 (unless (markerp end) (setq end (copy-marker end))) 482 (unless (markerp end) (setq end (copy-marker end)))
483 (let (;;(diff-inhibit-after-change t) 483 (let (;;(diff-inhibit-after-change t)
484 (inhibit-read-only t)) 484 (inhibit-read-only t))
485 (save-excursion 485 (save-excursion
486 (goto-char start) 486 (goto-char start)
487 (while (and (re-search-forward "^\\(\\(---\\) .+\n\\(\\+\\+\\+\\) .+\\|@@ -\\([0-9]+\\),\\([0-9]+\\) \\+\\([0-9]+\\),\\([0-9]+\\) @@\\)$" nil t) 487 (while (and (re-search-forward "^\\(\\(---\\) .+\n\\(\\+\\+\\+\\) .+\\|@@ -\\([0-9]+\\),\\([0-9]+\\) \\+\\([0-9]+\\),\\([0-9]+\\) @@.*\\)$" nil t)
488 (< (point) end)) 488 (< (point) end))
489 (combine-after-change-calls 489 (combine-after-change-calls
490 (if (match-beginning 2) 490 (if (match-beginning 2)
491 ;; we matched a file header 491 ;; we matched a file header
492 (progn 492 (progn
569 (unless (markerp end) (setq end (copy-marker end))) 569 (unless (markerp end) (setq end (copy-marker end)))
570 (let (;;(diff-inhibit-after-change t) 570 (let (;;(diff-inhibit-after-change t)
571 (inhibit-read-only t)) 571 (inhibit-read-only t))
572 (save-excursion 572 (save-excursion
573 (goto-char start) 573 (goto-char start)
574 (while (and (re-search-forward "^\\(\\(\\*\\*\\*\\) .+\n\\(---\\) .+\\|\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\n\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]+\\) \\*\\*\\*\\*\\)$" nil t) 574 (while (and (re-search-forward "^\\(\\(\\*\\*\\*\\) .+\n\\(---\\) .+\\|\\*\\{15\\}.*\n\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]+\\) \\*\\*\\*\\*\\)$" nil t)
575 (< (point) end)) 575 (< (point) end))
576 (combine-after-change-calls 576 (combine-after-change-calls
577 (if (match-beginning 2) 577 (if (match-beginning 2)
578 ;; we matched a file header 578 ;; we matched a file header
579 (progn 579 (progn
641 (unless (markerp end) (setq end (copy-marker end))) 641 (unless (markerp end) (setq end (copy-marker end)))
642 (let (;;(diff-inhibit-after-change t) 642 (let (;;(diff-inhibit-after-change t)
643 (inhibit-read-only t)) 643 (inhibit-read-only t))
644 (save-excursion 644 (save-excursion
645 (goto-char start) 645 (goto-char start)
646 (while (and (re-search-forward "^\\(\\([-*][-*][-*] \\)\\(.+\\)\n\\([-+][-+][-+] \\)\\(.+\\)\\|\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\n\\*\\*\\* \\(.+\\) \\*\\*\\*\\*\\|@@ -\\(.+\\) \\+\\(.+\\) @@\\)$" nil t) 646 (while (and (re-search-forward "^\\(\\([-*][-*][-*] \\)\\(.+\\)\n\\([-+][-+][-+] \\)\\(.+\\)\\|\\*\\{15\\}.*\n\\*\\*\\* \\(.+\\) \\*\\*\\*\\*\\|@@ -\\([0-9,]+\\) \\+\\([0-9,]+\\) @@.*\\)$" nil t)
647 (< (point) end)) 647 (< (point) end))
648 (combine-after-change-calls 648 (combine-after-change-calls
649 (cond 649 (cond
650 ;; a file header 650 ;; a file header
651 ((match-beginning 2) (replace-match "\\2\\5\n\\4\\3" nil)) 651 ((match-beginning 2) (replace-match "\\2\\5\n\\4\\3" nil))
706 (let ((inhibit-read-only t)) 706 (let ((inhibit-read-only t))
707 (save-excursion 707 (save-excursion
708 (goto-char end) (diff-end-of-hunk) 708 (goto-char end) (diff-end-of-hunk)
709 (let ((plus 0) (minus 0) (space 0) (bang 0)) 709 (let ((plus 0) (minus 0) (space 0) (bang 0))
710 (while (and (= (forward-line -1) 0) (<= start (point))) 710 (while (and (= (forward-line -1) 0) (<= start (point)))
711 (if (not (looking-at "\\(@@ .+ @@\\|[-*][-*][-*] .+ [-*][-*][-*][-*]\\)$")) 711 (if (not (looking-at "\\(@@ -[0-9,]+ \\+[0-9,]+ @@.*\\|[-*][-*][-*] .+ [-*][-*][-*][-*]\\)$"))
712 (case (char-after) 712 (case (char-after)
713 (?\ (incf space)) 713 (?\ (incf space))
714 (?+ (incf plus)) 714 (?+ (incf plus))
715 (?- (incf minus)) 715 (?- (incf minus))
716 (?! (incf bang)) 716 (?! (incf bang))
717 ((?\\ ?#) nil) 717 ((?\\ ?#) nil)
718 (t (setq space 0 plus 0 minus 0 bang 0))) 718 (t (setq space 0 plus 0 minus 0 bang 0)))
719 (cond 719 (cond
720 ((looking-at "@@ -[0-9]+,\\([0-9]*\\) \\+[0-9]+,\\([0-9]*\\) @@$") 720 ((looking-at "@@ -[0-9]+,\\([0-9]*\\) \\+[0-9]+,\\([0-9]*\\) @@.*$")
721 (let* ((old1 (match-string 1)) 721 (let* ((old1 (match-string 1))
722 (old2 (match-string 2)) 722 (old2 (match-string 2))
723 (new1 (number-to-string (+ space minus))) 723 (new1 (number-to-string (+ space minus)))
724 (new2 (number-to-string (+ space plus)))) 724 (new2 (number-to-string (+ space plus))))
725 (unless (string= new2 old2) (replace-match new2 t t nil 2)) 725 (unless (string= new2 old2) (replace-match new2 t t nil 2))
754 ;; from a post-command-hook doesn't pose much problems 754 ;; from a post-command-hook doesn't pose much problems
755 (defvar diff-unhandled-changes nil) 755 (defvar diff-unhandled-changes nil)
756 (defun diff-after-change-function (beg end len) 756 (defun diff-after-change-function (beg end len)
757 "Remember to fixup the hunk header. 757 "Remember to fixup the hunk header.
758 See `after-change-functions' for the meaning of BEG, END and LEN." 758 See `after-change-functions' for the meaning of BEG, END and LEN."
759 ;; Ignoring changes when inhibit-read-only is set is strictly speaking
760 ;; incorrect, but it turns out that inhibit-read-only is normally not set
761 ;; inside editing commands, while it tends to be set when the buffer gets
762 ;; updated by an async process or by a conversion function, both of which
763 ;; would rather not be uselessly slowed down by this hook.
759 (when (and (not undo-in-progress) (not inhibit-read-only)) 764 (when (and (not undo-in-progress) (not inhibit-read-only))
760 (if diff-unhandled-changes 765 (if diff-unhandled-changes
761 (setq diff-unhandled-changes 766 (setq diff-unhandled-changes
762 (cons (min beg (car diff-unhandled-changes)) 767 (cons (min beg (car diff-unhandled-changes))
763 (max beg (cdr diff-unhandled-changes)))) 768 (max beg (cdr diff-unhandled-changes))))
834 ;; provide the package 839 ;; provide the package
835 (provide 'diff-mode) 840 (provide 'diff-mode)
836 841
837 ;;; Change Log: 842 ;;; Change Log:
838 ;; $Log: diff-mode.el,v $ 843 ;; $Log: diff-mode.el,v $
844 ;; Revision 1.7 2000/05/10 22:12:46 monnier
845 ;; (diff-font-lock-keywords): Recognize comments.
846 ;; (diff-font-lock-defaults): Explicitly turn off multiline.
847 ;; (diff-end-of-hunk): Handle comments and fix end-of-buffer bug.
848 ;; (diff-ediff-patch): Fix call to ediff-patch-file.
849 ;; (diff-end-of-file, diff-reverse-direction, diff-fixup-modifs):
850 ;; Handle comments.
851 ;;
839 ;; Revision 1.6 2000/03/21 16:59:17 monnier 852 ;; Revision 1.6 2000/03/21 16:59:17 monnier
840 ;; (diff-mode-*-map): use `easy-mmode-defmap'. 853 ;; (diff-mode-*-map): use `easy-mmode-defmap'.
841 ;; (diff-end-of-hunk): Return the end position for use in 854 ;; (diff-end-of-hunk): Return the end position for use in
842 ;; `easy-mmode-define-navigation'. 855 ;; `easy-mmode-define-navigation'.
843 ;; (diff-recenter): Remove. 856 ;; (diff-recenter): Remove.