all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Emanuel Berg <incal@dataswamp.org>
To: emacs-devel@gnu.org
Subject: psea.el - Peace in the Middle East
Date: Wed, 30 Nov 2022 05:36:33 +0100	[thread overview]
Message-ID: <8735a12h4e.fsf@dataswamp.org> (raw)

;;; -*- 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	other threads:[~2022-11-30  4:36 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8735a12h4e.fsf@dataswamp.org \
    --to=incal@dataswamp.org \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.