[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] master 4e1162e: el-search: New pattern type `l'

From: Michael Heerdegen
Subject: [elpa] master 4e1162e: el-search: New pattern type `l'
Date: Sat, 09 Jan 2016 03:26:12 +0000

branch: master
commit 4e1162ea060c45d5eb50382ec34307ab1ea51fc3
Author: Michael Heerdegen <address@hidden>
Commit: Michael Heerdegen <address@hidden>

    el-search: New pattern type `l'
 packages/el-search/el-search.el |   53 +++++++++++++++++++++++++++++++++++++++
 1 files changed, 53 insertions(+), 0 deletions(-)

diff --git a/packages/el-search/el-search.el b/packages/el-search/el-search.el
index 22a20a4..4157887 100644
--- a/packages/el-search/el-search.el
+++ b/packages/el-search/el-search.el
@@ -659,6 +659,59 @@ matches any of these expressions:
                                  "argument not a string or vector")
   `(pred (el-search--match-key-sequence ,key-sequence)))
+(defun el-search--s (expr)
+  (cond
+   ((symbolp expr) `(symbol ,(symbol-name expr)))
+   ((stringp expr) `(string ,expr))
+   (t expr)))
+(el-search-defpattern l (&rest lpats)
+  "Alternative pattern type for matching lists.
+Match any list with subsequent elements matched by all LPATS in
+The idea is to be able to search for pieces of code (i.e. lists)
+with very brief input by using a specialized syntax.
+An LPAT can take the following forms:
+SYMBOL  Matches any symbol matched by SYMBOL's name interpreted as
+        a regexp
+STRING  Matches any string matched by STRING interpreted as a
+        regexp
+_       Matches any list element
+__      Matches any number of list elements (including zero)
+^       Matches zero elements, but only at the beginning of a list
+$       Matches zero elements, but only at the end of a list
+PAT     Anything else is interpreted as a normal pcase pattern, and
+        matches one list element matched by it
+^ is only valid as the first, $ as the last of the LPATS.
+Example: To match defuns that contain \"hl\" in their name and
+have at least one mandatory, but also optional arguments, you
+could use this pattern:
+    (l ^ 'defun hl (l _ &optional))"
+  (let ((match-start nil) (match-end nil))
+    (when (eq (car-safe lpats) '^)
+      (setq match-start t)
+      (cl-callf cdr lpats))
+    (when (eq (car-safe (last lpats)) '$)
+      (setq match-end t)
+      (cl-callf butlast lpats 1))
+    `(append ,@(if match-start '() '(_))
+             ,@(mapcar
+                (lambda (elt)
+                  (pcase elt
+                    ('__ '_)
+                    ('_ '`(,_))
+                    ('_? '(or '() `(,_))) ;FIXME: useful - document? or should 
we provide a (? PAT)
+                                          ;thing?
+                    (_ `(,'\` ((,'\, ,(el-search--s elt)))))))
+                lpats)
+             ,@(if match-end '() '(_)))))
 ;;;; Highlighting

reply via email to

[Prev in Thread] Current Thread [Next in Thread]