[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/iedit c00a8ba805 272/301: Add a new feature - preserve cas
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/iedit c00a8ba805 272/301: Add a new feature - preserve case |
Date: |
Mon, 10 Jan 2022 22:59:09 -0500 (EST) |
branch: elpa/iedit
commit c00a8ba80579b74349187b78efa88945f398ba9f
Author: Victor <victorhge@gmail.com>
Commit: Victor <victorhge@gmail.com>
Add a new feature - preserve case
The behavior of this feature is the same as `query-replace'.
If `iedit-case-sensitive' is nil and `case-replace' is non-nil, iedit will
preserve case in occurrences.
Quoted from doc-string of `replace-match':
"maybe capitalize the whole text, or maybe just word initials, based on the
replaced text. If the replaced text has only capital letters and has at
least
one multiletter word, convert NEWTEXT to all caps. Otherwise if all words
are
capitalized in the replaced text, capitalize each word in NEWTEXT."
---
iedit-lib.el | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++--------
iedit-tests.el | 25 +++++++++++++++++
2 files changed, 100 insertions(+), 12 deletions(-)
diff --git a/iedit-lib.el b/iedit-lib.el
index d3936f93cd..5f9a3e4bac 100755
--- a/iedit-lib.el
+++ b/iedit-lib.el
@@ -431,7 +431,9 @@ occurrences if the user starts typing."
(overlay-put occurrence 'insert-behind-hooks '(iedit-update-occurrences))
(overlay-put occurrence 'modification-hooks '(iedit-update-occurrences))
(overlay-put occurrence 'priority iedit-overlay-priority)
- (overlay-put occurrence 'category 'iedit-overlay)
+ (overlay-put occurrence 'category (if (and (not iedit-case-sensitive)
case-replace)
+
(iedit-case-action begin end)
+
'no-change))
occurrence))
(defun iedit-make-read-only-occurrence-overlay (begin end)
@@ -548,7 +550,7 @@ part to apply it to all the other occurrences."
(setq iedit-after-change-list nil)))))
(defun iedit-update-occurrences-3 (occurrence beg end &optional change)
- "The third part of updateing occurrences.
+ "The third part of updating occurrences.
Apply the change to all the other occurrences. "
(let ((iedit-updating t)
(offset (- beg (overlay-start occurrence)))
@@ -558,15 +560,28 @@ Apply the change to all the other occurrences. "
(inhibit-modification-hooks (memq #'c-before-change
before-change-functions)))
(save-excursion
(iedit-move-conjoined-overlays occurrence)
+ (when (/= beg end)
+ (case (overlay-get occurrence 'category)
+ ('all-caps
+ (upcase-region beg end))
+ ('cap-initial
+ (when (= 0 offset) (capitalize-region beg end )))))
(dolist (another-occurrence iedit-occurrences-overlays)
- (let* ((beginning (+ (overlay-start another-occurrence) offset))
- (ending (+ beginning (- end beg))))
- (when (not (eq another-occurrence occurrence))
- (when change (delete-region beginning (+
beginning change))) ;; delete
- (when (/= beg end) ;; insert
- (goto-char beginning)
- (insert-and-inherit value)))
- (iedit-move-conjoined-overlays another-occurrence))))
+ (when (not (eq another-occurrence occurrence))
+ (let* ((beginning (+ (overlay-start another-occurrence) offset))
+ (ending (+ beginning (- end beg))))
+ (when (/= 0 change) (delete-region beginning (+
beginning change))) ;; delete
+ (when (/= beg end) ;; insert
+ (goto-char beginning)
+ (insert-and-inherit
+ (case (overlay-get another-occurrence 'category)
+ ('no-change value)
+ ('all-caps
+ (upcase value))
+ ('cap-initial
+ (if (= 0 offset) (capitalize value)
+ value))))))
+ (iedit-move-conjoined-overlays another-occurrence))))
(when inhibit-modification-hooks
;; run the after change functions only once. It seems OK for c-mode
(run-hook-with-args 'after-change-functions beg end change))))
@@ -842,7 +857,7 @@ FORMAT."
(lambda (beg end from-string to-string)
(goto-char beg)
(search-forward from-string end)
- (replace-match to-string t))
+ (replace-match to-string (not (and (not iedit-case-sensitive)
case-replace))))
from-string to-string)
(goto-char (+ (overlay-start ov) offset))))
@@ -892,6 +907,47 @@ be applied to other occurrences when buffering is off."
(buffer-disable-undo)
(message "Start buffering editing..."))
+(defun iedit-case-action (beg end)
+ "Decide how to casify by examining the text between `beg' and `end'.
+
+Maybe capitalize the whole text, or maybe just word initials,
+based on the text between `beg' and `end'. If the text has only
+capital letters and has at least one multiletter word, convert
+`new-text' to all caps. Otherwise if all words are capitalized
+in the replaced text, capitalize each word in `new-text'"
+ (let ((some-word nil)
+ (some-lowercase nil)
+ (some-uppercase nil)
+ (some-non-uppercase-init nil)
+ (previous-char ?\n)
+ (char nil)
+ (index 0))
+ (while (< index (- end beg))
+ (setq char (elt (buffer-substring beg end) index))
+ (if (and (= char (downcase char))
+ (/= char (upcase char)))
+ (progn
+ (setq some-lowercase t)
+ (if (/= ?w (char-syntax previous-char))
+ (setq some-non-uppercase-init t)
+ (setq some-word t)))
+ (if (/= char (downcase char))
+ (progn
+ (setq some-uppercase t)
+ (when (= ?w (char-syntax previous-char))
+ (setq some-word t)))
+ (when (/= ?w (char-syntax previous-char))
+ (setq some-non-uppercase-init t))))
+ (setq previous-char char)
+ (setq index (1+ index)))
+ (if (and (null some-lowercase) some-word)
+ 'all-caps
+ (if (and (null some-non-uppercase-init) some-word)
+ 'cap-initial
+ (if (and (null some-non-uppercase-init) some-uppercase)
+ 'all-caps
+ 'no-change)))))
+
(defun iedit-stop-buffering ()
"Stop buffering and apply the modification to other occurrences.
If current point is not at any occurrence, the buffered
@@ -921,7 +977,14 @@ modification is not going to be applied to other
occurrences."
(delete-region beginning ending)
(unless (eq beg end) ;; replacement
(goto-char beginning)
- (insert-and-inherit modified-string))
+ (insert-and-inherit
+ (case (overlay-get occurrence 'category)
+ ('all-caps
+ (upcase modified-string))
+ ('cap-initial
+ (if (= 0 offset) (capitalize
modified-string)
+ modified-string))
+ (t modified-string))))
(iedit-move-conjoined-overlays occurrence))))
(goto-char (+ (overlay-start ov) offset))))))
(setq iedit-buffering nil)
diff --git a/iedit-tests.el b/iedit-tests.el
index 4b322e097b..43f1af9a3f 100644
--- a/iedit-tests.el
+++ b/iedit-tests.el
@@ -73,12 +73,14 @@
(with-iedit-test-buffer "* iedit transient mark *"
(transient-mark-mode t)
(setq iedit-transient-mark-sensitive t)
+ (setq iedit-case-sensitive t)
(insert input-buffer-string)
(goto-char 1)
(iedit-mode)
(funcall body))
(with-iedit-test-buffer "* iedit NO transient mark *"
(setq iedit-transient-mark-sensitive nil)
+ (setq iedit-case-sensitive t)
(transient-mark-mode -1)
(insert input-buffer-string)
(goto-char 1)
@@ -433,6 +435,29 @@ fob")))))
(iedit-toggle-case-sensitive)
(should (= 1 (length iedit-occurrences-overlays))))))
+(ert-deftest iedit-case-preserve-test ()
+ (with-iedit-test-fixture
+"foo
+ Foo
+ barFoo
+ FOO"
+(lambda ()
+ (iedit-mode)
+ (goto-char 1)
+ (set-mark-command nil)
+ (forward-char 3)
+ (let ((iedit-case-sensitive nil)
+ (case-replace t))
+ (iedit-mode)
+ (goto-char 1)
+ (insert "bar")
+ (run-hooks 'post-command-hook)
+ (should (string= (buffer-string)
+"barfoo
+ BarFoo
+ barBarFoo
+ BARFOO"))))))
+
(ert-deftest iedit-apply-on-occurrences-test ()
"Test functions deal with the whole occurrences"
(with-iedit-test-fixture
- [nongnu] elpa/iedit abcc27a9f0 231/301: Add Installation instruction, (continued)
- [nongnu] elpa/iedit abcc27a9f0 231/301: Add Installation instruction, ELPA Syncer, 2022/01/10
- [nongnu] elpa/iedit 8b620efacb 262/301: Replace `inhibit-modification-hooks' with `iedit-updating', ELPA Syncer, 2022/01/10
- [nongnu] elpa/iedit 6d2694356c 253/301: Move "declare-function" invocations outside of "when" statement., ELPA Syncer, 2022/01/10
- [nongnu] elpa/iedit f6682186f0 242/301: Replace eval-after-load with require check, ELPA Syncer, 2022/01/10
- [nongnu] elpa/iedit 14376a97e0 223/301: Ignore error in `iedit-mark-sgml-pair', ELPA Syncer, 2022/01/10
- [nongnu] elpa/iedit 9fd1b43170 229/301: Fix failed test cases for Emacs 25.1, ELPA Syncer, 2022/01/10
- [nongnu] elpa/iedit 658c10a249 244/301: fix issue of iedit-restrict-function doesn't update mode-line text correctly, ELPA Syncer, 2022/01/10
- [nongnu] elpa/iedit 06f1f50939 224/301: Merge pull request #65 from pstray/master, ELPA Syncer, 2022/01/10
- [nongnu] elpa/iedit c11ecd9080 273/301: Update copyright notice, ELPA Syncer, 2022/01/10
- [nongnu] elpa/iedit e5b51ac0af 278/301: Move hooks setup to iedit-lib to reduce code duplication, ELPA Syncer, 2022/01/10
- [nongnu] elpa/iedit c00a8ba805 272/301: Add a new feature - preserve case,
ELPA Syncer <=
- [nongnu] elpa/iedit 4ef9bf6323 287/301: prefix-arg for `iedit-next-occurrence` and `iedit-prev-occurrence`, ELPA Syncer, 2022/01/10
- [nongnu] elpa/iedit 3d71392852 276/301: Fix as well issue #118, ELPA Syncer, 2022/01/10
- [nongnu] elpa/iedit f7184f81d6 284/301: Clean up some lines of unnecessary code, ELPA Syncer, 2022/01/10
- [nongnu] elpa/iedit 2e1fd5b1b2 295/301: fix a byte compile issue, ELPA Syncer, 2022/01/10
- [nongnu] elpa/iedit 012de2e8d8 293/301: A few rephase in doc, ELPA Syncer, 2022/01/10
- [nongnu] elpa/iedit 6bb8d63a97 297/301: Fix several typo errors, ELPA Syncer, 2022/01/10
- [nongnu] elpa/iedit 345431294a 294/301: Unify prefix augument handling, ELPA Syncer, 2022/01/10
- [nongnu] elpa/iedit 61689f6ba4 057/301: Add arg handle for iedit-mode to exclude some occurrences., ELPA Syncer, 2022/01/10
- [nongnu] elpa/iedit 7479d4c947 079/301: Merge branch 'topic/multi-interface', ELPA Syncer, 2022/01/10
- [nongnu] elpa/iedit 6cc86d58e6 086/301: Merge pull request #6 from gorakhargosh/master, ELPA Syncer, 2022/01/10