--- replace.el 2015-06-02 23:21:42.631715000 -0400
+++ replace-editted.el 2015-06-02 23:32:47.754001000 -0400
@@ -1834,6 +1834,8 @@
(define-key map [M-next] 'scroll-other-window)
(define-key map [?\C-\M-\S-v] 'scroll-other-window-down)
(define-key map [M-prior] 'scroll-other-window-down)
+ (define-key map "\M-c" 'toggle-query-case)
+ (define-key map "\M-r" 'toggle-replace-preserve-case)
;; Binding ESC would prohibit the M-v binding. Instead, callers
;; should check for ESC specially.
;; (define-key map "\e" 'exit-prefix)
@@ -2100,12 +2102,14 @@
;; (match-data); otherwise it is t if a match is possible at point.
(match-again t)
- (message
+ (query-replace-in-progress-message
(if query-flag
(apply 'propertize
(substitute-command-keys
- "Query replacing %s with %s: (\\<query-replace-map>\\[help] for help) ")
- minibuffer-prompt-properties))))
+ (concat "Query replacing %s with %s: "
+ "(\\<query-replace-map>\\[help] for help) %s "))
+ minibuffer-prompt-properties)))
+ (query-message-momentary ""))
;; If region is active, in Transient Mark mode, operate on region.
(if backward
@@ -2251,7 +2255,7 @@
noedit real-match-data backward)
replace-count (1+ replace-count)))
(undo-boundary)
- (let (done replaced key def)
+ (let (done replaced key def replacement-presentation)
;; Loop reading commands until one of them sets done,
;; which means it has finished handling this
;; occurrence. Any command that sets `done' should
@@ -2266,17 +2270,18 @@
regexp-flag delimited-flag case-fold-search backward)
;; Bind message-log-max so we don't fill up the message log
;; with a bunch of identical messages.
- (let ((message-log-max nil)
- (replacement-presentation
- (if query-replace-show-replacement
- (save-match-data
- (set-match-data real-match-data)
- (match-substitute-replacement next-replacement
- nocasify literal))
- next-replacement)))
- (message message
+ (let ((message-log-max nil))
+ (setq replacement-presentation
+ (if query-replace-show-replacement
+ (save-match-data
+ (set-match-data real-match-data)
+ (match-substitute-replacement next-replacement
+ nocasify literal))
+ next-replacement))
+ (message query-replace-in-progress-message
(query-replace-descr from-string)
- (query-replace-descr replacement-presentation)))
+ (query-replace-descr replacement-presentation)
+ query-message-momentary))
(setq key (read-event))
;; Necessary in case something happens during read-event
;; that clobbers the match data.
@@ -2404,6 +2409,51 @@
(replace-dehighlight)
(save-excursion (recursive-edit))
(setq replaced t))
+
+ ((eq def 'toggle-query-case)
+ (setq case-fold-search (not case-fold-search))
+ (let ((message-log-max nil)
+ (query-message-momentary
+ (concat "["
+ (if case-fold-search
+ "case insensitive search"
+ "Case Sensitive Search")
+ "]")))
+ (message query-replace-in-progress-message
+ (query-replace-descr from-string)
+ (query-replace-descr replacement-presentation)
+ query-message-momentary)
+ (sit-for 1)))
+
+ ((eq def 'toggle-replace-preserve-case)
+ (let ((message-log-max nil)
+ (nocasify-value-reason "")
+ query-message-momentary)
+ (setq nocasify (not nocasify))
+ (cond
+ ((null case-fold-search)
+ (setq nocasify nil)
+ (setq nocasify-value-reason ", as case-fold-search is nil"))
+ ((null (isearch-no-upper-case-p from-string regexp-flag))
+ (setq nocasify nil)
+ (setq nocasify-value-reason ", as FROM-STRING has an upper case char."))
+ ((null (isearch-no-upper-case-p next-replacement regexp-flag))
+ (setq nocasify t)
+ (setq nocasify-value-reason ", as REPLACEMENT has an upper case char.")))
+ (setq query-message-momentary
+ (concat "[Replaced text case will "
+ (if nocasify "NOT " "")
+ "be preserved"
+ nocasify-value-reason
+ "]"))
+ (message query-replace-in-progress-message
+ (query-replace-descr from-string)
+ (query-replace-descr replacement-presentation)
+ query-message-momentary)
+ (sit-for 1.5)))
+
+ (def (call-interactively def)) ; User-defined key, invoke it.
+
;; Note: we do not need to treat `exit-prefix'
;; specially here, since we reread
;; any unrecognized character.