unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* psea.el - Peace in the Middle East
@ 2022-11-30  4:36 Emanuel Berg
  0 siblings, 0 replies; only message in thread
From: Emanuel Berg @ 2022-11-30  4:36 UTC (permalink / raw)
  To: emacs-devel

;;; -*- 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




^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-11-30  4:36 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-30  4:36 psea.el - Peace in the Middle East Emanuel Berg

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).