Skip to content

dieggsy/difflib.el

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

89 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

difflib.el

https://melpa.org/packages/difflib-badge.svg https://stable.melpa.org/packages/difflib-badge.svg https://travis-ci.org/dieggsy/difflib.el.svg?branch=master https://coveralls.io/repos/github/dieggsy/difflib.el/badge.svg?branch=master

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.

Installation

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)

Functions

difflib-get-close-matches (word possibilities (:n 3) (:cutoff 0.6))

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")

difflib-ndiff (a b &key linejunk (charjunk #'difflib-is-character-junk-p))

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 (a b &key ...)

(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")

difflib-context-diff (a b &key ...)

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")

difflib-restore (delta which)

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")

Classes

difflib-sequence-matcher

A flexible class for comparing deltas of sequences of any type.

difflib-differ

For producing human-readable deltas from sequences of lines of text.