;;; -*- lexical-binding: t -*- ;; ;; this file: ;; https://dataswamp.org/~incal/emacs-init/psea.el (require 'cl-lib) (require 'dwim) (require 'math) (defun string-distance-percentage (str1 str2 &optional str-out) (let*((len1 (length str1)) (len2 (length str2)) (long (max len1 len2)) (dist (string-distance str1 str2)) ) (percent (- long dist) long str-out) )) (defun pattern-search (str &optional beg end) (interactive `(,(read-string "search: ") ,(use-region))) (or beg (setq beg (point-min))) (or end (setq end (point-max))) (cl-loop with len = (length str) for c from beg to (- end len) with hit with fallout do (setq hit (buffer-substring-no-properties c (+ c len))) (cl-pushnew (list hit (string-distance hit str) (string-distance-percentage str hit t) ) fallout :test #'equal) finally return (message "%s" (seq-take (sort fallout (lambda (a b) (< (cadr a) (cadr b)))) 5) ))) (defalias 'psea #'pattern-search) ;; (psea "history" (point-min) (+ 100 (point-min))) ;; ;; his fil 4 43% ;; amp.org 5 29% ;; this fi 5 29% ;; /psea.e 6 14% ;; nit/pse 6 14% (provide 'psea)