emacs-elpa-diffs
[Top][All Lists]
Advanced

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

[elpa] externals/clipboard-collector 083e349 07/11: Allow transform usin


From: Stefan Monnier
Subject: [elpa] externals/clipboard-collector 083e349 07/11: Allow transform using match group number instead of a function
Date: Wed, 17 Mar 2021 18:40:41 -0400 (EDT)

branch: externals/clipboard-collector
commit 083e349f05a35d81c0cf9a54acb43647d4be1991
Author: Clemens Radermacher <clemera@posteo.net>
Commit: Clemens Radermacher <clemera@posteo.net>

    Allow transform using match group number instead of a function
---
 README.org             | 14 ++++++++++++++
 clipboard-collector.el | 42 ++++++++++++++++++++++++------------------
 2 files changed, 38 insertions(+), 18 deletions(-)

diff --git a/README.org b/README.org
index 2eaa6bf..f3d328e 100644
--- a/README.org
+++ b/README.org
@@ -66,6 +66,20 @@ regex in that function, too:
  (("https?://\\([^/]*\\)"  "Url: %s" (lambda (item) (match-string 1 item)))))
 #+END_SRC
 
+If you just want to apply a matched group to the format string you can provide
+the match group number instead of using a function, too:
+
+#+BEGIN_SRC elisp
+(clipboard-collector-create
+ cc-youtube-rss
+ (("https://www.youtube.com/user/\\(.*\\)"
+   "https://www.youtube.com/feeds/videos.xml?user=%s";
+   1)
+  ("https://www.youtube.com/channel/\\(.*\\)"
+   "https://www.youtube.com/feeds/videos.xml?channel_id=%s";
+   1)))
+#+END_SRC
+
 * Related projects
 
 There is [[https://github.com/bburns/clipmon][clipmon]] but I wanted something 
more flexible and I wanted it to work for text
diff --git a/clipboard-collector.el b/clipboard-collector.el
index 1f6bdf6..a74dd64 100644
--- a/clipboard-collector.el
+++ b/clipboard-collector.el
@@ -63,18 +63,21 @@
 
 Uses the following list format:
 
-    (MATCH-REGEX [TRANSFORM-FORMAT-STRING] [TRANSFORM-CLIPBOARD-FUNC])
+    (MATCH-REGEX [TRANSFORM-FORMAT-STRING] [TRANSFORM-CLIPBOARD])
 
 MATCH-REGEX is the triggering regex, if clipboard contents match
 this regex the clipboard entry will be collected.
 
 Optional TRANSFORM-FORMAT-STRING should be a format string where
-the placeholder is replaced by the clipboard contents.
+the '%s' placeholder is replaced by the clipboard contents.
 
-If you want to transform the clipboard contents using a function
-specify TRANSFORM-CLIPBOARD-FUNC. This is applied before contents
-are applied to TRANSFORM-FORMAT-STRING and can use match-data of
-the matched regex.")
+Additionally the matched candidated can be transformed by
+specifying TRANSFORM-CLIPBOARD. If it's a function it gets called
+with the matched candidated and its return value will be applied
+to TRANSFORM-FORMAT-STRING. The function can use match-data of
+MATCH-REGEX. In case TRANSFORM-CLIPBOARD is a number the match
+string of that number will be applied to
+TRANSFORM-FORMAT-STRING.")
 
 (defvar clipboard-collector--finish-function
   #'clipboard-collector-finish-default
@@ -145,18 +148,21 @@ is active."
 
 Returns cons of matching regex of used rule and clipboard
 contents transformed according to matched rule."
-  (cl-dolist (rules (or rules clipboard-collector--rules))
-    (when (string-match (car rules) clip)
-      (let ((main (cond ((functionp (car (cddr rules)))
-                         (funcall (car (cddr rules)) clip))
-                        (t clip)))
-            (format (cond ((functionp (cadr rules))
-                           (funcall (cadr rules) (match-string 1 clip)))
-                          ((stringp (cadr rules))
-                           (cadr rules))
-                          (t "%s"))))
-        (cl-return (cons (car rules)
-                         (format format main)))))))
+  (cl-dolist (rule (or rules clipboard-collector--rules))
+    (when (string-match (car rule) clip)
+      (let* ((converter (car (cddr rule)))
+             (transformed (cond ((functionp converter)
+                                 (funcall converter clip))
+                                ((numberp converter)
+                                 (match-string converter clip))
+                                (t clip)))
+             (format (cond ((functionp (cadr rule))
+                            (funcall (cadr rule) (match-string 1 clip)))
+                           ((stringp (cadr rule))
+                            (cadr rule))
+                           (t "%s"))))
+        (cl-return (cons (car rule)
+                         (format format transformed)))))))
 
 
 (defun clipboard-collector--try-collect-last-kill ()



reply via email to

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