Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Order a list of substring matches from s.el's s-matched-positions-all
(defun order-substring-matches (raw-substring-matches)
"Order a set of RAW-SUBSTRING-MATCHES.
Ordered substrings can then be used to perform replacements
on the original source string.
The list is sorted last to first, so that string replacements
don't invalidate replacements using subsequent substring indexes.
Raw substring matches are in the form:
'((\"s1\" ((10 . 12) (30 . 32) ...))...)
(\"s2\" ((15 . 17) (20 . 22) ...))...)
This would become:
'((\"s1\" (30 . 33)))
(\"s2\" (20 . 22)))
(\"s2\" (15 . 17)))
(\"s1\" (10 . 13)))
"
(sort (cl-reduce (lambda (acc entry)
(let* ((indexes (cadr entry))
(str (car entry))
(mapped (mapcar (lambda (it) (list str it)) indexes)))
(seq-concatenate 'list acc mapped)))
raw-substring-matches
:initial-value '())
(lambda (it other) (> (caadr it)) (caadr other))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment