0

I wrote this code:

(defun compare-string-test (String1 String2)
  (interactive)
  (let ((Temp1 (make-temp-file "gitdiff" nil nil String1))
        (Temp2 (make-temp-file "gitdiff" nil nil String2)))
(insert
 (with-temp-buffer
   (insert
    (shell-command-to-string
     (format
      "git diff --no-index --word-diff=color --word-diff-regex=. %s %s"
      Temp1 Temp2)))
   (ansi-color-apply-on-region (point-min) (point-max))
   (buffer-string)))))

I tried:

(compare-string-test "Ciao" "Miao")

I'd like to insert a "fontified" string but it doesn't work.

If I remove the ansi-color-apply-on-region command I get:

[1mdiff --git a/tmp/gitdiffM8px4W b/tmp/gitdiffb5aHy4[m
[1mindex 11d8bbf..cf44f4e 100644[m
[1m--- a/tmp/gitdiffM8px4W[m
[1m+++ b/tmp/gitdiffb5aHy4[m
[36m@@ -1 +1 @@[m
[31mC[m[32mM[miao

and if I evaluate (ansi-color-apply-on-region (point-min) (point-max)) I get:

enter image description here

that it's what I need (I'm only interested in the last line).

How can I make my function inserting the string with this fontification?

Referring to this https://emacs.stackexchange.com/a/56223/15606, I tried the function both with font-lock-mode active and disabled.

I also tried message instead of insert but the massage in the minibuffer appears without fontification.

I red the buffer-string and insert documentation and I think these functions should keep text properties.

Note. I also opened an issue to ask for a solution using elisp tools only: Is there an Emacs function or package to visually compare two text strings?

Gabriele
  • 1,554
  • 9
  • 21

1 Answers1

0

The issue stemmed from the fact that by default, ansi-color-apply-on-region does not use text-properties to color the text but instead employs overlays. This behavior is controlled by the variable ansi-color-apply-face-function, which on my machine was indeed set by default to ansi-color-apply-overlay-face.

(ansi-color-apply-on-region BEGIN END &optional PRESERVE-SEQUENCES)

Translates SGR control sequences into overlays or extents. Delete all other control sequences without processing them.

SGR control sequences are applied by calling the function specified by ‘ansi-color-apply-face-function’. The default function sets foreground and background colors to the text between BEGIN and END, using overlays. See function ‘ansi-color-apply-sequence’ for details.

The following code works as expected for me:

(defun compare-string-test (String1 String2)
  (interactive)
  (let ((Temp1 (make-temp-file "gitdiff" nil nil String1))
        (Temp2 (make-temp-file "gitdiff" nil nil String2))
        (ansi-color-apply-face-function 'ansi-color-apply-text-property-face))
    (insert
     (with-temp-buffer
       (insert
        (shell-command-to-string
         (format
          "git diff --no-index --word-diff=color --word-diff-regex=. %s %s"
          Temp1 Temp2)))
       (ansi-color-apply-on-region (point-min) (point-max))
       (buffer-string)))))
Gabriele
  • 1,554
  • 9
  • 21