GNU bug report logs - #54017
add regexp translation option to read-regexp

Previous Next

Package: emacs;

Reported by: emacsq <laszlomail <at> protonmail.com>

Date: Tue, 15 Feb 2022 20:22:01 UTC

Severity: wishlist

Full log


View this message in rfc822 format

From: Juri Linkov <juri <at> linkov.net>
To: emacsq <laszlomail <at> protonmail.com>
Cc: 54017 <at> debbugs.gnu.org
Subject: bug#54017: add regexp translation option to read-regexp
Date: Thu, 17 Feb 2022 11:09:21 +0200
[Message part 1 (text/plain, inline)]
> It's easy to add regexp translation to the return value of read-regexp.

This patch allows using Rx syntax, so for example, after running 'occur',
you can type an Rx expression:

  List lines matching regexp: (rx (or "e.g." "i.e.") " ")

[regexp-from-function.patch (text/x-diff, inline)]
diff --git a/lisp/emacs-lisp/rx.el b/lisp/emacs-lisp/rx.el
index aa2486b47e..b1e726d025 100644
--- a/lisp/emacs-lisp/rx.el
+++ b/lisp/emacs-lisp/rx.el
@@ -1479,6 +1479,10 @@ rx
 ;; Obsolete internal symbol, used in old versions of the `flycheck' package.
 (define-obsolete-function-alias 'rx-submatch-n 'rx-to-string "27.1")
 
+(defun regexp-from-rx (string)
+  "This translation function can be used by `regexp-from-function'."
+  (rx--to-expr (cons 'seq (cdr (read string)))))
+
 (provide 'rx)
 
 ;;; rx.el ends here
diff --git a/lisp/replace.el b/lisp/replace.el
index 06be597855..a8b850a9ed 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -819,6 +819,15 @@ occur-highlight-overlays
 (defvar occur-collect-regexp-history '("\\1")
   "History of regexp for occur's collect operation.")
 
+(defcustom regexp-from-function nil
+  "Function to translate from a custom regexp to the default regexp syntax."
+  :type '(choice
+          (const :tag "No translation" nil)
+          (function-item :tag "RX" regexp-from-rx)
+          (function :tag "Your choice of function"))
+  :group 'matching
+  :version "29.1")
+
 (defcustom read-regexp-defaults-function nil
   "Function that provides default regexp(s) for `read-regexp'.
 This function should take no arguments and return one of: nil, a
@@ -923,7 +932,9 @@ read-regexp
 	  (when default
 	    (add-to-history (or history 'regexp-history) default)))
       ;; Otherwise, add non-empty input to the history and return input.
-      (prog1 input
+      (prog1 (if (functionp regexp-from-function)
+                 (funcall regexp-from-function input)
+               input)
 	(add-to-history (or history 'regexp-history) input)))))
 
 

This bug report was last modified 3 years and 116 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.