[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/consult f967184 2/3: Add consult--regexp-join-permutati
From: |
ELPA Syncer |
Subject: |
[elpa] externals/consult f967184 2/3: Add consult--regexp-join-permutations |
Date: |
Mon, 9 Aug 2021 15:57:08 -0400 (EDT) |
branch: externals/consult
commit f96718419d2ab54b447c0cb0def2d951a8e9e72b
Author: Daniel Mendler <mail@daniel-mendler.de>
Commit: Daniel Mendler <mail@daniel-mendler.de>
Add consult--regexp-join-permutations
Automatically disengange for more than three regular expressions.
See the discussion in #393.
---
consult.el | 28 ++++++++++++++++++++++++----
1 file changed, 24 insertions(+), 4 deletions(-)
diff --git a/consult.el b/consult.el
index 4244553..88160c0 100644
--- a/consult.el
+++ b/consult.el
@@ -619,10 +619,30 @@ expression, which can be `basic', `extended', `emacs' or
`pcre'."
(defun consult--join-regexps (regexps type)
"Join REGEXPS of TYPE."
;; Add lookahead wrapper only if there is more than one regular expression
- (if (and (eq type 'pcre) (cdr regexps))
- (concat "^" (mapconcat (lambda (x) (format "(?=.*%s)" x))
- regexps ""))
- (string-join regexps ".*")))
+ (cond
+ ((and (eq type 'pcre) (cdr regexps))
+ (concat "^" (mapconcat (lambda (x) (format "(?=.*%s)" x))
+ regexps "")))
+ ((> (length regexps) 3)
+ (message "Too many regular expressions. Disengaging unordered matching.
Use post-filtering!")
+ (string-join regexps ".*"))
+ (t
+ (consult--regexp-join-permutations regexps
+ (and (memq type '(basic emacs))
"\\")))))
+
+(defun consult--regexp-join-permutations (regexps esc)
+ "Join all permutations of REGEXPS.
+ESC is the escaping string for choice and groups."
+ (pcase regexps
+ ('nil "")
+ (`(,r) r)
+ (`(,r1 ,r2) (concat r1 ".*" r2 esc "|" r2 ".*" r1))
+ (_ (mapconcat
+ (lambda (r)
+ (concat r ".*" esc "("
+ (consult--regexp-join-permutations (remove r regexps) esc)
+ esc ")"))
+ regexps (concat esc "|")))))
(defun consult--valid-regexp-p (re)
"Return t if regexp RE is valid."