help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: delete lines with overlays [workaround to diff mode]


From: Jens Schmidt
Subject: Re: delete lines with overlays [workaround to diff mode]
Date: Wed, 30 Aug 2023 13:21:55 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.14.0

On 2023-08-30  07:07, Samuel Wales wrote:

> currently 27

How convenient ... having that around here as well.

As Dmitry has noted in your bug#61396, the diff refinement machinery
probably already has what you are looking for, but without
ready-made switches to turn it on.

You might want to try whether the following modified function
`diff--refine-hunk' does what you need.  Not sure how proficient
you are in Elisp hacking, so I provide full instructions to get
it activated.

1. Start Emacs as "emacs -Q" to get reproducible results.  (This
   is required only for the initial tests.)
2. M-x load-library diff-mode RET to load the original library
3. Paste below function definition into *scratch* and press C-M-x
   with point in or after the definition to evaluate the modified
   function definition.
4. Then check whether the result matches your expectation.

The only lines I modified in the function are these:

-              (smerge-refine-regions beg-del beg-add beg-add end-add
-                                     nil #'diff-refine-preproc props-r 
props-a)))))

I replaced that by:

+              (smerge-refine-regions beg-del beg-add beg-add end-add
+                                     props-c nil nil nil)))))

Comments:

- `props-c' is responsible for highlighting changes in yellow
  (compared to `props-r' for removals, `props-a' for additions)
- Function `diff-refine-preproc' referenced in the original function
  strips off the leading '-' and '+' chars from the diffs to avoid
  getting them highlighted as refined diffs.
  When replacing the reference to that function by nil, these leading
  chars get processed by the refinement as well and are highlighted as
  changes if needed.  Hopefully, they did at least in the samples I
  checked.

You might also want to try:

+              (smerge-refine-regions beg-del beg-add beg-add end-add
+                                     props-c nil props-r props-a)))))

Anyway, here is the full modified function:

(defun diff--refine-hunk (start end)
  (require 'smerge-mode)
  (goto-char start)
  (let* ((style (diff-hunk-style))      ;Skips the hunk header as well.
         (beg (point))
         (props-c '((diff-mode . fine) (face . diff-refine-changed)))
         (props-r '((diff-mode . fine) (face . diff-refine-removed)))
         (props-a '((diff-mode . fine) (face . diff-refine-added))))

    (remove-overlays beg end 'diff-mode 'fine)

    (goto-char beg)
    (pcase style
      ('unified
       (while (re-search-forward "^-" end t)
         (let ((beg-del (progn (beginning-of-line) (point)))
               beg-add end-add)
           (when (and (diff--forward-while-leading-char ?- end)
                      ;; Allow for "\ No newline at end of file".
                      (progn (diff--forward-while-leading-char ?\\ end)
                             (setq beg-add (point)))
                      (diff--forward-while-leading-char ?+ end)
                      (progn (diff--forward-while-leading-char ?\\ end)
                             (setq end-add (point))))
             (smerge-refine-regions beg-del beg-add beg-add end-add
                                    props-c nil nil nil)))))
      ('context
       (let* ((middle (save-excursion (re-search-forward "^---" end)))
              (other middle))
         (while (re-search-forward "^\\(?:!.*\n\\)+" middle t)
           (smerge-refine-regions (match-beginning 0) (match-end 0)
                                  (save-excursion
                                    (goto-char other)
                                    (re-search-forward "^\\(?:!.*\n\\)+" end)
                                    (setq other (match-end 0))
                                    (match-beginning 0))
                                  other
                                  (if diff-use-changed-face props-c)
                                  #'diff-refine-preproc
                                  (unless diff-use-changed-face props-r)
                                  (unless diff-use-changed-face props-a)))))
      (_ ;; Normal diffs.
       (let ((beg1 (1+ (point))))
         (when (re-search-forward "^---.*\n" end t)
           ;; It's a combined add&remove, so there's something to do.
           (smerge-refine-regions beg1 (match-beginning 0)
                                  (match-end 0) end
                                  nil #'diff-refine-preproc props-r 
props-a)))))))




reply via email to

[Prev in Thread] Current Thread [Next in Thread]