summaryrefslogtreecommitdiff
path: root/lisp/replace.el
diff options
context:
space:
mode:
authorLars Ingebrigtsen <larsi@gnus.org>2022-07-05 18:26:52 +0200
committerLars Ingebrigtsen <larsi@gnus.org>2022-07-05 18:27:42 +0200
commit3868c3aa3e4f1be4863f2a382174347b8cfd3d6c (patch)
treec98fd787e7acd3ce5e9bf7caebfdb94bbc6fad70 /lisp/replace.el
parent3bd889cba06b8511856559afa31511b8d67f4b49 (diff)
downloademacs-3868c3aa3e4f1be4863f2a382174347b8cfd3d6c.tar.gz
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'.
Diffstat (limited to 'lisp/replace.el')
-rw-r--r--lisp/replace.el52
1 files changed, 29 insertions, 23 deletions
diff --git a/lisp/replace.el b/lisp/replace.el
index 163d5821486..54ee64f64a5 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)