Skip to content

Instantly share code, notes, and snippets.

@jasonm23
Created August 11, 2022 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonm23/e6fc1d31d9abb0169bfa04190d4cd3dc to your computer and use it in GitHub Desktop.
Save jasonm23/e6fc1d31d9abb0169bfa04190d4cd3dc to your computer and use it in GitHub Desktop.
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