This is a port of python’s difflib to emacs-lisp. It provides tools for computing “human friendly” diffs, which use the notion of the longest contiguous and junk-free matching subsequence.
This package is on melpa. If you have melpa in your package repositories, you
can use M-x RET package-install RET difflib
or install with use-package:
(use-package difflib
:ensure t)
Alternatively, consider installing with straight.el or quelpa-use-package.
Otherwise, download the files to somewhere in your load path, and require difflib:
(require 'difflib)
Use difflib-sequence-matcher
to return a list of the best “good enough”
matches.
(difflib-get-close-matches "appel" '("ape" "apple" "peach" "puppy")) ;; => '("apple" "ape")
(difflib-get-close-matches "wheel" '("if" "while" "with" "except")) ;; => '("while")
(difflib-get-close-matches "apple" '("if" "while" "with" "except")) ;; => nil
(difflib-get-close-matches "accept" '("if" "while" "with" "except")) ;; => '("except")
Return a delta: the difference between a
and b
(lists of strings).
(difflib-ndiff '("one" "two" "three")
'("ore" "tree" "emu"))
;; =>
;; '("- one"
;; "? ^"
;; "+ ore"
;; "? ^"
;; "- two"
;; "- three"
;; "? -"
;; "+ tree"
;; "+ emu")
(difflib-unified-diff (s-split " " "one two three four")
(s-split " " "zero one tree four")
:fromfile "Original"
:tofile "Current"
:fromfiledate "2005-01-26 23:30:50"
:tofiledate "2010-04-02 10:20:52")
;; =>
;; '("--- Original\t2005-01-26 23:30:50"
;; "+++ Current\t2010-04-02 10:20:52"
;; "@@ -1,4 +1,4 @@"
;; "+zero"
;; " one"
;; "-two"
;; "-three"
;; "+tree"
;; " four")
For two lists of strings, return a delta in context diff format.
(difflib-context-diff (s-split " " "one two three four")
(s-split " " "zero one tree four")
:fromfile "Original"
:tofile "Current")
;; =>
;; '("*** Original"
;; "--- Current"
;; "***************"
;; "*** 1,4 ****"
;; " one"
;; "! two"
;; "! three"
;; " four"
;; "--- 1,4 ----"
;; "+ zero"
;; " one"
;; "! tree"
;; " four")
Return one of the two sequences that generated a difflib-ndiff
delta.
(difflib-restore
(difflib-ndiff '("one" "two" "three") '("ore" "tree" "emu"))
1) ;; => '("one" "two" "three")
(difflib-restore
(difflib-ndiff '("one" "two" "three") '("ore" "tree" "emu"))
2) ;; => '("ore" "tree" "emu")
A flexible class for comparing deltas of sequences of any type.
For producing human-readable deltas from sequences of lines of text.