Skip to content

Commit

Permalink
Allow \l, \U, etc. to override case-replace in substitution repla…
Browse files Browse the repository at this point in the history
…cements
  • Loading branch information
tomdl89 committed Jul 22, 2024
1 parent 30ebe6d commit bf3511b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
10 changes: 8 additions & 2 deletions evil-commands.el
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
17 changes: 11 additions & 6 deletions evil-common.el
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand All @@ -3749,7 +3752,9 @@ replacement text first."
(funcall (car replacement)
(cdr replacement)
0))
fixedcase nil string))
(or evil--substitute-fixed-case fixedcase)
nil
string))

;;; Alignment

Expand Down

0 comments on commit bf3511b

Please sign in to comment.