1

I use Emacs 26.1 and would like to compare two files word-by-word. Clicking "Tools -> Compare (Ediff) -> Windows Word-by-word..." allows only to compare the visible parts of two buffers, not the whole buffers. This seems to be intended behavior as per my other question. The response there contains a working solution for comparing two complete buffers. There seem to be no issues with performance.

How can this be used to compare two files? It would be nice if a menu option could be added as well: "Tools -> Compare (Ediff) -> Files Word-by-word...".

1
  • 2
    Visit the files, then use the answer to your other question?
    – Drew
    Commented Nov 25, 2018 at 14:41

1 Answer 1

2

You can copy the following Elisp snippet into your init file.

Disclaimer: The code comes without any guaranty. It references internal functions of ediff. So use it with caution.

The newly defined command ediff-files-wordwise just runs ediff-files with two main modifications:

  • (ediff-word-mode . word-mode) is added to the SETUP-PARAMETERS of ediff-setup.
  • The buffer contents of buf-A and buf-B is wordified and saved in temporary files that are used for the diff-program.

    (eval-after-load "ediff"
      (progn
        (eval
         `(defun ediff-files-wordwise (file-A file-B &optional startup-hooks)
            ,(concat (documentation 'ediff-files)
                     "\nComparison is done wordwise.")
            ,(interactive-form 'ediff-files)
            (cl-letf* ((oldfun (symbol-function 'ediff-setup))
                       ((symbol-function 'ediff-setup)
                        (lambda (buffer-A file-A buffer-B file-B buffer-C file-C startup-hooks setup-parameters &optional merge-buffer-file)
                          (let* ((tmp-buffer (get-buffer-create ediff-tmp-buffer)))
                            (with-current-buffer buffer-A
                              (ediff-wordify (point-min) (point-max) buffer-A tmp-buffer))
                            (setq file-A (ediff-make-temp-file tmp-buffer "regA"))
                            (with-current-buffer buffer-B
                              (ediff-wordify (point-min) (point-max) buffer-B tmp-buffer))
                            (setq file-B (ediff-make-temp-file tmp-buffer "regB"))
                            (setq overl-A (with-current-buffer buffer-A
                                            (ediff-make-bullet-proof-overlay (point-min) (point-max) buffer-A))
                                  overl-B (with-current-buffer buffer-B
                                            (ediff-make-bullet-proof-overlay (point-min) (point-max) buffer-B)))
                            (funcall oldfun
                                     buffer-A file-A buffer-B file-B buffer-C file-C startup-hooks
                                     (append
                                      (list (cons 'ediff-word-mode 'word-mode)
                                            (cons 'ediff-narrow-bounds (list overl-A overl-B)))
                                      setup-parameters)
                                     merge-buffer-file)))))
              (ediff-files file-A file-B startup-hooks))))
        (require 'easymenu)
        (easy-menu-add-item
         menu-bar-tools-menu
         '("Compare (Ediff)")
         ["Two Files Word-by-word..." ediff-files-wordwise t]
         "Two Buffers...")))
    
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.