emacs-diffs
[Top][All Lists]
Advanced

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

master 3868c3aa3e 3/3: Don't hard-code `M-c' in `read-regexp'


From: Lars Ingebrigtsen
Subject: master 3868c3aa3e 3/3: Don't hard-code `M-c' in `read-regexp'
Date: Tue, 5 Jul 2022 12:27:49 -0400 (EDT)

branch: master
commit 3868c3aa3e4f1be4863f2a382174347b8cfd3d6c
Author: Lars Ingebrigtsen <larsi@gnus.org>
Commit: Lars Ingebrigtsen <larsi@gnus.org>

    Don't hard-code `M-c' in `read-regexp'
    
    * lisp/replace.el (read-regexp-map): New map.
    (read-regexp--case-fold, read-regexp-toggle-case-folding)
    (read-regexp): Factor out to avoid hard-coding `M-c'.
---
 lisp/replace.el | 52 +++++++++++++++++++++++++++++-----------------------
 1 file changed, 29 insertions(+), 23 deletions(-)

diff --git a/lisp/replace.el b/lisp/replace.el
index 163d582148..54ee64f64a 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -895,6 +895,23 @@ by this function to the end of values available via
    (regexp-quote (or (car search-ring) ""))
    (car (symbol-value query-replace-from-history-variable))))
 
+(defvar-keymap read-regexp-map
+  :parent minibuffer-local-map
+  "M-c" #'read-regexp-toggle-case-folding)
+
+(defvar read-regexp--case-fold nil)
+
+(defun read-regexp-toggle-case-folding ()
+  (interactive)
+  (setq read-regexp--case-fold
+        (if (or (eq read-regexp--case-fold 'fold)
+                (and read-regexp--case-fold
+                     (not (eq read-regexp--case-fold 'inhibit-fold))))
+            'inhibit-fold
+          'fold))
+  (minibuffer-message "Case folding is now %s"
+                      (if (eq read-regexp--case-fold 'fold) "on" "off")))
+
 (defun read-regexp (prompt &optional defaults history)
   "Read and return a regular expression as a string.
 Prompt with the string PROMPT.  If PROMPT ends in \":\" (followed by
@@ -931,11 +948,14 @@ in \":\", followed by optional whitespace), DEFAULT is 
added to the prompt.
 The optional argument HISTORY is a symbol to use for the history list.
 If nil, use `regexp-history'.
 
-If the user has used the \\`M-c' command to specify case
+If the user has used the 
\\<read-regexp-map>\\[read-regexp-toggle-case-folding] command to specify case
 sensitivity, the returned string will have a text property named
 `case-fold' that has a value of either `fold' or
 `inhibit-fold'.  (It's up to the caller of `read-regexp' to
-respect this or not; see `read-regexp-case-fold-search'.)"
+respect this or not; see `read-regexp-case-fold-search'.)
+
+This command uses the `read-regexp-map' keymap while reading the
+regexp from the user."
   (let* ((defaults
           (if (and defaults (symbolp defaults))
               (cond
@@ -951,29 +971,15 @@ respect this or not; see `read-regexp-case-fold-search'.)"
         (suggestions (delete-dups (delq nil (delete "" suggestions))))
         ;; Do not automatically add default to the history for empty input.
         (history-add-new-input nil)
-         (case-fold case-fold-search)
+         ;; `read-regexp--case-fold' dynamically bound and may be
+         ;; altered by `M-c'.
+         (read-regexp--case-fold case-fold-search)
         (input (read-from-minibuffer
                  (if (string-match-p ":[ \t]*\\'" prompt)
                      prompt
                    (format-prompt prompt (and (length> default 0)
                                               (query-replace-descr default))))
-                nil
-                 (define-keymap
-                   :parent minibuffer-local-map
-                   "M-c" (lambda ()
-                           (interactive)
-                           (setq case-fold
-                                 (if (or (eq case-fold 'fold)
-                                         (and case-fold
-                                              (not (eq case-fold
-                                                       'inhibit-fold))))
-                                     'inhibit-fold
-                                   'fold))
-                           (minibuffer-message
-                            "Case folding is now %s"
-                            (if (eq case-fold 'fold)
-                                "on"
-                              "off"))))
+                nil read-regexp-map
                  nil (or history 'regexp-history) suggestions t))
          (result (if (equal input "")
                     ;; Return the default value when the user enters
@@ -983,9 +989,9 @@ respect this or not; see `read-regexp-case-fold-search'.)"
     (when result
       (add-to-history (or history 'regexp-history) result))
     (if (and result
-             (or (eq case-fold 'fold)
-                 (eq case-fold 'inhibit-fold)))
-        (propertize result 'case-fold case-fold)
+             (or (eq read-regexp--case-fold 'fold)
+                 (eq read-regexp--case-fold 'inhibit-fold)))
+        (propertize result 'case-fold read-regexp--case-fold)
       (or result input))))
 
 (defun read-regexp-case-fold-search (regexp)



reply via email to

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