emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

psea.el - Peace in the Middle East


From: Emanuel Berg
Subject: psea.el - Peace in the Middle East
Date: Wed, 30 Nov 2022 05:36:33 +0100
User-agent: Gnus/5.13 (Gnus v5.13)

;;; -*- lexical-binding: t -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/psea.el

(require 'cl-lib)
(require 'math) ; https://dataswamp.org/~incal/emacs-init/math.el

(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: ")
                 ,@(when (use-region-p)
                     (list (region-beginning) (region-end)) )))
  (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  42.9%
;; amp.org  5  28.6%
;; this fi  5  28.6%
;; /psea.e  6  14.3%
;; nit/pse  6  14.3%

-- 
underground experts united
https://dataswamp.org/~incal




reply via email to

[Prev in Thread] Current Thread [Next in Thread]