unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Juri Linkov <juri@linkov.net>
To: emacsq <laszlomail@protonmail.com>
Cc: 54017@debbugs.gnu.org
Subject: bug#54017: add regexp translation option to read-regexp
Date: Thu, 17 Feb 2022 11:09:21 +0200	[thread overview]
Message-ID: <86fsoheu66.fsf@mail.linkov.net> (raw)
In-Reply-To: <86pmnlew9d.fsf@mail.linkov.net> (Juri Linkov's message of "Thu,  17 Feb 2022 10:24:14 +0200")

[-- Attachment #1: Type: text/plain, Size: 240 bytes --]

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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: regexp-from-function.patch --]
[-- Type: text/x-diff, Size: 1695 bytes --]

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	other threads:[~2022-02-17  9:09 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-15 20:21 bug#54017: add regexp translation option to read-regexp emacsq via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-02-16 18:25 ` Juri Linkov
2022-02-16 19:28   ` emacsq via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-02-17  8:24     ` Juri Linkov
2022-02-17  9:09       ` Juri Linkov [this message]
2022-02-17  9:18       ` Eli Zaretskii
2022-02-17 10:04         ` emacsq via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-02-17 17:40         ` Juri Linkov
2022-02-20 19:06           ` emacsq via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-02-20 20:02             ` Eli Zaretskii
2022-02-20 22:46             ` bug#54017: [External] : " Drew Adams
2022-02-17 10:55       ` emacsq via Bug reports for GNU Emacs, the Swiss army knife of text editors

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=86fsoheu66.fsf@mail.linkov.net \
    --to=juri@linkov.net \
    --cc=54017@debbugs.gnu.org \
    --cc=laszlomail@protonmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).