bug-gnu-emacs
[Top][All Lists]
Advanced

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

bug#54017: add regexp translation option to read-regexp


From: Juri Linkov
Subject: bug#54017: add regexp translation option to read-regexp
Date: Thu, 17 Feb 2022 11:09:21 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (x86_64-pc-linux-gnu)

> 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.") " ")

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)))))
 
 

reply via email to

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