From 2dda6ef9fe93699adb6363c70167242faff95574 Mon Sep 17 00:00:00 2001 From: justbur Date: Mon, 22 Jun 2015 12:51:34 -0400 Subject: [PATCH] Tweak ediff to ignore regions with magic comment. Controlled through new variable `ediff-start-with-ignores`. For use in diffing .spacemacs with template for now. --- core/templates/.spacemacs.template | 3 ++ spacemacs/funcs.el | 53 ++++++++++++++++++++++++++++-- spacemacs/packages.el | 13 +++++++- 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/core/templates/.spacemacs.template b/core/templates/.spacemacs.template index 78e35dac4f76..fccc5f2a1eed 100644 --- a/core/templates/.spacemacs.template +++ b/core/templates/.spacemacs.template @@ -154,13 +154,16 @@ before layers configuration." dotspacemacs-default-package-repository nil ) ;; User initialization goes here + ;; EDIFF-IGNORE ) (defun dotspacemacs/config () "Configuration function. This function is called at the very end of Spacemacs initialization after layers configuration." + ;; EDIFF-IGNORE ) ;; Do not write anything past this comment. This is where Emacs will ;; auto-generate custom variable definitions. +;; EDIFF-IGNORE diff --git a/spacemacs/funcs.el b/spacemacs/funcs.el index 59c5a2003f12..d7946cf5bba4 100644 --- a/spacemacs/funcs.el +++ b/spacemacs/funcs.el @@ -527,11 +527,60 @@ argument takes the kindows rotate backwards." (interactive) (find-file-existing (dotspacemacs/location))) +(defun ediff-hide-regex-and-ignores (n) + "Tweaked version of `ediff-hide-regexp-matches'. This version + automatically matches regions that have the string + \"EDIFF-IGNORE\" in them. It also searches the line just above + the region." + (if (ediff-valid-difference-p n) + (let* ((ctl-buf ediff-control-buffer) + (ignore-regexp "EDIFF-IGNORE") + (regex-A ediff-regexp-hide-A) + (regex-B ediff-regexp-hide-B) + (regex-C ediff-regexp-hide-C) + (reg-A-match (ediff-with-current-buffer ediff-buffer-A + (save-restriction + (goto-char (ediff-get-diff-posn 'A 'beg n ctl-buf)) + (narrow-to-region + (line-beginning-position 0) + (ediff-get-diff-posn 'A 'end n ctl-buf)) + (goto-char (point-min)) + (or (re-search-forward ignore-regexp nil t) + (and (not (string-empty-p regex-A)) + (re-search-forward regex-A nil t)))))) + (reg-B-match (ediff-with-current-buffer ediff-buffer-B + (save-restriction + (goto-char (ediff-get-diff-posn 'B 'beg n ctl-buf)) + (narrow-to-region + (line-beginning-position 0) + (ediff-get-diff-posn 'B 'end n ctl-buf)) + (goto-char (point-min)) + (or (re-search-forward ignore-regexp nil t) + (and (not (string-empty-p regex-B)) + (re-search-forward regex-B nil t)))))) + (reg-C-match (when ediff-3way-comparison-job + (ediff-with-current-buffer ediff-buffer-C + (save-restriction + (goto-char (ediff-get-diff-posn 'C 'beg n ctl-buf)) + (narrow-to-region + (line-beginning-position 0) + (ediff-get-diff-posn 'C 'end n ctl-buf)) + (goto-char (point-min)) + (or (re-search-forward ignore-regexp nil t) + (and (not (string-empty-p regex-C)) + (re-search-forward regex-C nil t)))))))) + (eval (if ediff-3way-comparison-job + (list ediff-hide-regexp-connective + reg-A-match reg-B-match reg-C-match) + (list ediff-hide-regexp-connective reg-A-match reg-B-match))) + ))) + (defun ediff-dotfile-and-template () "ediff the current `dotfile' with the template" (interactive) - (ediff-files (dotspacemacs/location) - (concat dotspacemacs-template-directory ".spacemacs.template"))) + (let ((ediff-start-with-ignores t)) + (ediff-files (dotspacemacs/location) + (concat dotspacemacs-template-directory ".spacemacs.template")))) (defun find-spacemacs-file () (interactive) diff --git a/spacemacs/packages.el b/spacemacs/packages.el index 67920d3222d0..fa7b2c8a13dd 100644 --- a/spacemacs/packages.el +++ b/spacemacs/packages.el @@ -524,7 +524,18 @@ ediff-window-setup-function 'ediff-setup-windows-plain ;; emacs is evil and decrees that vertical shall henceforth be horizontal ediff-split-window-function 'split-window-horizontally - ediff-merge-split-window-function 'split-window-horizontally)))) + ediff-merge-split-window-function 'split-window-horizontally) + (defvar ediff-start-with-ignores nil + "If non-nil start ediff using ediff-hide-regex-and-ignores to select diff + regions to compare.") + (add-hook 'ediff-startup-hook + (lambda () + (when ediff-start-with-ignores + (setq-local + ediff-hide-regexp-matches-function 'ediff-hide-regex-and-ignores) + (setq-local + ediff-skip-diff-region-function 'ediff-hide-regex-and-ignores) + (ediff-update-diffs))))))) (defun spacemacs/init-eldoc () (use-package eldoc