From 5e9a94b016b497d1743bc1b396293072d3bc6cde Mon Sep 17 00:00:00 2001 From: Tom Dalziel Date: Mon, 22 Jul 2024 22:06:22 +0200 Subject: [PATCH] Allow `\l`, `\U`, etc. override case-replace in substitution replacements --- evil-commands.el | 10 ++++++++-- evil-common.el | 28 ++++++++++++++++------------ 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/evil-commands.el b/evil-commands.el index cd8a66d6..ff81181a 100644 --- a/evil-commands.el +++ b/evil-commands.el @@ -3939,6 +3939,10 @@ replacements made or found." nreplaced (if (/= nreplaced 1) "s" "")))))) +(defvar evil--substitute-fixed-case (not case-replace) + "Whether the replacement for a substitution respects case. +By default, the oposite of `case-replace', but can be overridden by flags.") + (evil-define-operator evil-ex-substitute (beg end pattern replacement flags) "The Ex substitute command. @@ -4014,7 +4018,8 @@ replacements made or found." (when (member response '(?y ?a ?l)) (unless count-only (set-match-data match-data) - (replace-match next-replacement (not case-replace))) + (replace-match next-replacement evil--substitute-fixed-case) + (setq evil--substitute-fixed-case (not case-replace))) (cl-incf nreplaced) (evil-ex-hl-set-region 'evil-ex-substitute @@ -4032,7 +4037,8 @@ replacements made or found." (if (stringp replacement) replacement (funcall (car replacement) (cdr replacement) nreplaced)))) (set-match-data match-data) - (replace-match next-replacement (not case-replace)))) + (replace-match next-replacement evil--substitute-fixed-case) + (setq evil--substitute-fixed-case (not case-replace)))) (cl-incf nreplaced)) (setq last-point (point)) (cond ((>= (point) end-marker) diff --git a/evil-common.el b/evil-common.el index 7f60bdfb..78659d4d 100644 --- a/evil-common.el +++ b/evil-common.el @@ -3524,6 +3524,7 @@ transformations, usually `regexp-quote' or `replace-quote'." (evil-magic evil-magic) (quote (or quote #'identity)) result stop) + (setq evil--substitute-fixed-case (not case-replace)) (while (and (not stop) str (string-match regexp str)) (unless (zerop (match-beginning 1)) (push (substring str 0 (match-beginning 1)) result)) @@ -3687,10 +3688,12 @@ REST is the unparsed remainder of TO." (?u . evil-upcase-first) (?U . upcase)))))) (list `(,func - (replace-quote - (evil-match-substitute-replacement - ,(car result) - (not case-replace)))) + (progn + (setq evil--substitute-fixed-case t) + (replace-quote + (evil-match-substitute-replacement + ,(car result) + nil)))) (cdr result)))) ((eq char ?=) (when (or (zerop (length rest)) @@ -3733,7 +3736,7 @@ REST is the unparsed remainder of TO." Return a list suitable for `perform-replace' if necessary, the original string if not. Currently the following magic characters in replacements are supported: 0-9&#lLuUrnbt, -The magic character , (comma) start an Emacs-lisp expression." +The magic character , (comma) starts an Emacs-lisp expression." (when (stringp to) (save-match-data (cons 'replace-eval-replacement @@ -3743,13 +3746,14 @@ The magic character , (comma) start an Emacs-lisp expression." "Return REPLACEMENT as it will be inserted by `evil-replace-match'. If REPLACEMENT is an expression it will be evaluated to compute the replacement text first." - (match-substitute-replacement - (if (stringp replacement) - replacement - (funcall (car replacement) - (cdr replacement) - 0)) - fixedcase nil string)) + (cl-flet ((f-replacement () (funcall (car replacement) + (cdr replacement) + 0))) + (match-substitute-replacement + (if (stringp replacement) replacement (f-replacement)) + (or evil--substitute-fixed-case fixedcase) + nil + string))) ;;; Alignment