diff options
| author | Stefan Monnier | 2011-01-14 13:10:15 -0500 |
|---|---|---|
| committer | Stefan Monnier | 2011-01-14 13:10:15 -0500 |
| commit | 6d3d61134327fe63fe33f16cf75d160686fd57b6 (patch) | |
| tree | eccbb474b4f9fc08ce0c2443e4340054c8d93d11 | |
| parent | f80aa5bfa90dff5cd75d1f40e4ababa3c86af7fb (diff) | |
| download | emacs-6d3d61134327fe63fe33f16cf75d160686fd57b6.tar.gz emacs-6d3d61134327fe63fe33f16cf75d160686fd57b6.zip | |
* lisp/vc/smerge-mode.el: Resolve comment conflicts more aggressively.
(smerge-resolve--normalize-re): New var.
(smerge-resolve--extract-comment, smerge-resolve--normalize): New funs.
(smerge-resolve): Use them.
* lisp/newcomment.el (comment-only-p): New function.
(comment-or-uncomment-region): Use it.
| -rw-r--r-- | lisp/ChangeLog | 9 | ||||
| -rw-r--r-- | lisp/newcomment.el | 11 | ||||
| -rw-r--r-- | lisp/vc/smerge-mode.el | 73 |
3 files changed, 87 insertions, 6 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 4ce53e701a7..320335c4e7a 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog | |||
| @@ -1,3 +1,12 @@ | |||
| 1 | 2011-01-14 Stefan Monnier <monnier@iro.umontreal.ca> | ||
| 2 | |||
| 3 | * vc/smerge-mode.el: Resolve comment conflicts more aggressively. | ||
| 4 | (smerge-resolve--normalize-re): New var. | ||
| 5 | (smerge-resolve--extract-comment, smerge-resolve--normalize): New funs. | ||
| 6 | (smerge-resolve): Use them. | ||
| 7 | * newcomment.el (comment-only-p): New function. | ||
| 8 | (comment-or-uncomment-region): Use it. | ||
| 9 | |||
| 1 | 2011-01-14 Brent Goodrick <bgoodr@gmail.com> (tiny change) | 10 | 2011-01-14 Brent Goodrick <bgoodr@gmail.com> (tiny change) |
| 2 | 11 | ||
| 3 | * abbrev.el (prepare-abbrev-list-buffer): If listing local abbrev | 12 | * abbrev.el (prepare-abbrev-list-buffer): If listing local abbrev |
diff --git a/lisp/newcomment.el b/lisp/newcomment.el index 0f739513227..6e2955c6607 100644 --- a/lisp/newcomment.el +++ b/lisp/newcomment.el | |||
| @@ -1185,6 +1185,12 @@ end- comment markers additionally to what `comment-add' already specifies." | |||
| 1185 | 'box-multi 'box))) | 1185 | 'box-multi 'box))) |
| 1186 | (comment-region beg end (+ comment-add arg)))) | 1186 | (comment-region beg end (+ comment-add arg)))) |
| 1187 | 1187 | ||
| 1188 | (defun comment-only-p (beg end) | ||
| 1189 | "Return non-nil if the text between BEG and END is all comments." | ||
| 1190 | (save-excursion | ||
| 1191 | (goto-char beg) | ||
| 1192 | (comment-forward (point-max)) | ||
| 1193 | (<= end (point)))) | ||
| 1188 | 1194 | ||
| 1189 | ;;;###autoload | 1195 | ;;;###autoload |
| 1190 | (defun comment-or-uncomment-region (beg end &optional arg) | 1196 | (defun comment-or-uncomment-region (beg end &optional arg) |
| @@ -1193,10 +1199,7 @@ in which case call `uncomment-region'. If a prefix arg is given, it | |||
| 1193 | is passed on to the respective function." | 1199 | is passed on to the respective function." |
| 1194 | (interactive "*r\nP") | 1200 | (interactive "*r\nP") |
| 1195 | (comment-normalize-vars) | 1201 | (comment-normalize-vars) |
| 1196 | (funcall (if (save-excursion ;; check for already commented region | 1202 | (funcall (if (comment-only-p beg end) |
| 1197 | (goto-char beg) | ||
| 1198 | (comment-forward (point-max)) | ||
| 1199 | (<= end (point))) | ||
| 1200 | 'uncomment-region 'comment-region) | 1203 | 'uncomment-region 'comment-region) |
| 1201 | beg end arg)) | 1204 | beg end arg)) |
| 1202 | 1205 | ||
diff --git a/lisp/vc/smerge-mode.el b/lisp/vc/smerge-mode.el index 296ae635d0b..4b85a964fdd 100644 --- a/lisp/vc/smerge-mode.el +++ b/lisp/vc/smerge-mode.el | |||
| @@ -46,7 +46,7 @@ | |||
| 46 | 46 | ||
| 47 | (eval-when-compile (require 'cl)) | 47 | (eval-when-compile (require 'cl)) |
| 48 | (require 'diff-mode) ;For diff-auto-refine-mode. | 48 | (require 'diff-mode) ;For diff-auto-refine-mode. |
| 49 | 49 | (require 'newcomment) | |
| 50 | 50 | ||
| 51 | ;;; The real definition comes later. | 51 | ;;; The real definition comes later. |
| 52 | (defvar smerge-mode) | 52 | (defvar smerge-mode) |
| @@ -455,6 +455,37 @@ BUF contains a plain diff between match-1 and match-3." | |||
| 455 | (insert ">>>>>>> " name3 "\n") | 455 | (insert ">>>>>>> " name3 "\n") |
| 456 | (setq line endline)))))))) | 456 | (setq line endline)))))))) |
| 457 | 457 | ||
| 458 | (defconst smerge-resolve--normalize-re "[\n\t][ \t\n]*\\| [ \t\n]+") | ||
| 459 | |||
| 460 | (defun smerge-resolve--extract-comment (beg end) | ||
| 461 | "Extract the text within the comments that span BEG..END." | ||
| 462 | (save-excursion | ||
| 463 | (let ((comments ()) | ||
| 464 | combeg) | ||
| 465 | (goto-char beg) | ||
| 466 | (while (and (< (point) end) | ||
| 467 | (setq combeg (comment-search-forward end t))) | ||
| 468 | (let ((beg (point))) | ||
| 469 | (goto-char combeg) | ||
| 470 | (comment-forward 1) | ||
| 471 | (save-excursion | ||
| 472 | (comment-enter-backward) | ||
| 473 | (push " " comments) | ||
| 474 | (push (buffer-substring-no-properties beg (point)) comments)))) | ||
| 475 | (push " " comments) | ||
| 476 | (with-temp-buffer | ||
| 477 | (apply #'insert (nreverse comments)) | ||
| 478 | (goto-char (point-min)) | ||
| 479 | (while (re-search-forward smerge-resolve--normalize-re | ||
| 480 | nil t) | ||
| 481 | (replace-match " ")) | ||
| 482 | (buffer-string))))) | ||
| 483 | |||
| 484 | (defun smerge-resolve--normalize (beg end) | ||
| 485 | (replace-regexp-in-string | ||
| 486 | smerge-resolve--normalize-re " " | ||
| 487 | (concat " " (buffer-substring-no-properties beg end) " "))) | ||
| 488 | |||
| 458 | (defun smerge-resolve (&optional safe) | 489 | (defun smerge-resolve (&optional safe) |
| 459 | "Resolve the conflict at point intelligently. | 490 | "Resolve the conflict at point intelligently. |
| 460 | This relies on mode-specific knowledge and thus only works in some | 491 | This relies on mode-specific knowledge and thus only works in some |
| @@ -472,7 +503,8 @@ major modes. Uses `smerge-resolve-function' to do the actual work." | |||
| 472 | (m2e (match-end 2)) | 503 | (m2e (match-end 2)) |
| 473 | (m3e (match-end 3)) | 504 | (m3e (match-end 3)) |
| 474 | (buf (generate-new-buffer " *smerge*")) | 505 | (buf (generate-new-buffer " *smerge*")) |
| 475 | m b o) | 506 | m b o |
| 507 | choice) | ||
| 476 | (unwind-protect | 508 | (unwind-protect |
| 477 | (progn | 509 | (progn |
| 478 | (cond | 510 | (cond |
| @@ -557,6 +589,43 @@ major modes. Uses `smerge-resolve-function' to do the actual work." | |||
| 557 | (narrow-to-region m0b m0e) | 589 | (narrow-to-region m0b m0e) |
| 558 | (smerge-remove-props m0b m0e) | 590 | (smerge-remove-props m0b m0e) |
| 559 | (insert-file-contents m nil nil nil t))) | 591 | (insert-file-contents m nil nil nil t))) |
| 592 | ;; If the conflict is only made of comments, and one of the two | ||
| 593 | ;; changes is only rearranging spaces (e.g. reflowing text) while | ||
| 594 | ;; the other is a real change, drop the space-rearrangement. | ||
| 595 | ((and m2e | ||
| 596 | (comment-only-p m1b m1e) | ||
| 597 | (comment-only-p m2b m2e) | ||
| 598 | (comment-only-p m3b m3e) | ||
| 599 | (let ((t1 (smerge-resolve--extract-comment m1b m1e)) | ||
| 600 | (t2 (smerge-resolve--extract-comment m2b m2e)) | ||
| 601 | (t3 (smerge-resolve--extract-comment m3b m3e))) | ||
| 602 | (cond | ||
| 603 | ((and (equal t1 t2) (not (equal t2 t3))) | ||
| 604 | (setq choice 3)) | ||
| 605 | ((and (not (equal t1 t2)) (equal t2 t3)) | ||
| 606 | (setq choice 1))))) | ||
| 607 | (set-match-data md) | ||
| 608 | (smerge-keep-n choice)) | ||
| 609 | ;; Idem, when the conflict is contained within a single comment. | ||
| 610 | ((save-excursion | ||
| 611 | (and m2e | ||
| 612 | (nth 4 (syntax-ppss m0b)) | ||
| 613 | ;; If there's a conflict earlier in the file, | ||
| 614 | ;; syntax-ppss is not reliable. | ||
| 615 | (not (re-search-backward smerge-begin-re nil t)) | ||
| 616 | (progn (goto-char (nth 8 (syntax-ppss m0b))) | ||
| 617 | (forward-comment 1) | ||
| 618 | (> (point) m0e)) | ||
| 619 | (let ((t1 (smerge-resolve--normalize m1b m1e)) | ||
| 620 | (t2 (smerge-resolve--normalize m2b m2e)) | ||
| 621 | (t3 (smerge-resolve--normalize m3b m3e))) | ||
| 622 | (cond | ||
| 623 | ((and (equal t1 t2) (not (equal t2 t3))) | ||
| 624 | (setq choice 3)) | ||
| 625 | ((and (not (equal t1 t2)) (equal t2 t3)) | ||
| 626 | (setq choice 1)))))) | ||
| 627 | (set-match-data md) | ||
| 628 | (smerge-keep-n choice)) | ||
| 560 | (t | 629 | (t |
| 561 | (error "Don't know how to resolve")))) | 630 | (error "Don't know how to resolve")))) |
| 562 | (if (buffer-name buf) (kill-buffer buf)) | 631 | (if (buffer-name buf) (kill-buffer buf)) |