[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/multiple-cursors 00896cf 030/434: Refactor: simplify savin
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/multiple-cursors 00896cf 030/434: Refactor: simplify saving of state for cursors. |
Date: |
Sat, 7 Aug 2021 09:19:48 -0400 (EDT) |
branch: elpa/multiple-cursors
commit 00896cf47fac16cd600e5b16d5cb14c9b81af932
Author: Magnar Sveen <magnars@gmail.com>
Commit: Magnar Sveen <magnars@gmail.com>
Refactor: simplify saving of state for cursors.
---
multiple-cursors.el | 80 +++++++++++++++++++++++++++++------------------------
todo.org | 5 ++--
2 files changed, 46 insertions(+), 39 deletions(-)
diff --git a/multiple-cursors.el b/multiple-cursors.el
index cd08fc3..da22c5c 100644
--- a/multiple-cursors.el
+++ b/multiple-cursors.el
@@ -110,20 +110,46 @@ highlights the entire width of the window."
(overlay-put overlay 'type 'additional-region)
overlay))
-(defun mc/add-cursor-at-point ()
- "Add a fake cursor where point is.
-Also makes a copy of the kill-ring to be used by this cursor."
+(defun mc/store-current-state-in-overlay (o)
+ (overlay-put o 'point (point))
+ (overlay-put o 'kill-ring kill-ring)
+ (overlay-put o 'mark (set-marker (make-marker) (mark)))
+ (overlay-put o 'mark-ring mark-ring)
+ (overlay-put o 'mark-active mark-active)
+ (overlay-put o 'er/history er/history)
+ o)
+
+(defun mc/restore-state-from-overlay (o)
+ (goto-char (overlay-get o 'point))
+ (setq kill-ring (overlay-get o 'kill-ring))
+ (set-marker (mark-marker) (overlay-get o 'mark))
+ (setq mark-ring (overlay-get o 'mark-ring))
+ (setq mark-active (overlay-get o 'mark-active))
+ (setq er/history (overlay-get o 'er/history)))
+
+(defun mc/clean-up-state-overlay (o)
+ (set-marker (overlay-get o 'mark) nil)
+ (mc/delete-region-overlay o)
+ (delete-overlay o))
+
+(defun mc/pop-state-from-overlay (o)
+ (mc/restore-state-from-overlay o)
+ (mc/clean-up-state-overlay o))
+
+(defun mc/delete-region-overlay (o)
+ (ignore-errors
+ (delete-overlay (overlay-get o 'region-overlay))))
+
+(defun mc/create-fake-cursor-at-point ()
+ "Add a fake cursor and possibly a fake active region overlay based on point
and mark.
+Saves the current state in the overlay to be restored later."
(let ((overlay (mc/make-cursor-overlay-at-point)))
(overlay-put overlay 'type 'additional-cursor)
- (overlay-put overlay 'kill-ring kill-ring)
- (overlay-put overlay 'mark-ring mark-ring)
- (overlay-put overlay 'mark-active mark-active)
- (overlay-put overlay 'mark (set-marker (make-marker) (mark)))
- (overlay-put overlay 'er/history er/history)
+ (overlay-put overlay 'priority 100)
+ (mc/store-current-state-in-overlay overlay)
(when (use-region-p)
(overlay-put overlay 'region-overlay
- (mc/make-region-overlay-between-point-and-mark)))
- (overlay-put overlay 'priority 100)))
+ (mc/make-region-overlay-between-point-and-mark)))))
(defun mc/execute-command-for-all-fake-cursors (cmd)
"Calls CMD interactively for each cursor.
@@ -131,36 +157,19 @@ It works by moving point to the fake cursor, setting
up the proper kill-ring, and then removing the cursor.
After executing the command, it sets up a new fake
cursor with updated info."
- (let ((current-kill-ring kill-ring)
- (current-mark-ring mark-ring)
- (current-mark-active mark-active)
- (current-er/history er/history)
+ (let ((current-state (mc/store-current-state-in-overlay
+ (make-overlay (point) (point) nil nil t)))
(annoying-arrows-mode nil))
(save-excursion
(mapc #'(lambda (o)
(when (eq (overlay-get o 'type) 'additional-cursor)
- (goto-char (overlay-start o))
- (setq kill-ring (overlay-get o 'kill-ring))
- (set-marker (mark-marker) (overlay-get o 'mark))
- (set-marker (overlay-get o 'mark) nil)
- (setq mark-ring (overlay-get o 'mark-ring))
- (setq mark-active (overlay-get o 'mark-active))
- (setq er/history (overlay-get o 'er/history))
- (delete-region-overlay o)
- (delete-overlay o)
+ (mc/pop-state-from-overlay o)
(ignore-errors
(call-interactively cmd)
(when deactivate-mark (deactivate-mark))
- (mc/add-cursor-at-point))))
+ (mc/create-fake-cursor-at-point))))
(overlays-in (point-min) (point-max))))
- (setq kill-ring current-kill-ring)
- (setq mark-ring current-mark-ring)
- (setq mark-active current-mark-active)
- (setq er/history current-er/history)))
-
-(defun delete-region-overlay (o)
- (ignore-errors
- (delete-overlay (overlay-get o 'region-overlay))))
+ (mc/pop-state-from-overlay current-state)))
(defun mc/execute-this-command-for-all-cursors ()
"Used with post-command-hook to execute supported commands for
@@ -183,8 +192,7 @@ Do not use to conclude editing with multiple cursors. For
that
you should disable multiple-cursors-mode."
(mapc #'(lambda (o)
(when (eq (overlay-get o 'type) 'additional-cursor)
- (delete-region-overlay o)
- (delete-overlay o)))
+ (mc/clean-up-state-overlay o)))
(overlays-in (point-min) (point-max))))
(defun mc/keyboard-quit ()
@@ -227,7 +235,7 @@ mark-multiple if point and mark is on different columns."
(navigation-func (if (< point-line mark-line) 'previous-line
'next-line)))
(deactivate-mark)
(while (not (eq (line-number-at-pos) point-line))
- (mc/add-cursor-at-point)
+ (mc/create-fake-cursor-at-point)
(funcall navigation-func))
(multiple-cursors-mode)))
@@ -253,7 +261,7 @@ mark-multiple if point and mark is on different columns."
(save-excursion
(dolist (mirror mm/mirrors)
(goto-char (+ offset (overlay-start mirror)))
- (mc/add-cursor-at-point)))
+ (mc/create-fake-cursor-at-point)))
(mm/clear-all)
(multiple-cursors-mode)))
diff --git a/todo.org b/todo.org
index c8f4ef4..cb95d40 100644
--- a/todo.org
+++ b/todo.org
@@ -1,9 +1,8 @@
-* TODO: [3/9]
+* TODO: [4/9]
** DONE (set-marker MARKER nil) for performance
** DONE C-g deactivates regions first, before disabling multiple-cursors
** DONE more state to save per cursor: er/history
-** TODO refactor
- an object with all the current state, used for both overlays and current
+** DONE refactor: an object with all the current state, used for both overlays
and current
** TODO add tests
** TODO collapse cursors at same point
** TODO unknown command: (t)ry all or (i)gnore -> (did that work ok? (k)eep
doing that or (d)on't)
- [nongnu] elpa/multiple-cursors ca0e0ae 084/434: Tests for rectangular-region-mode, (continued)
- [nongnu] elpa/multiple-cursors ca0e0ae 084/434: Tests for rectangular-region-mode, ELPA Syncer, 2021/08/07
- [nongnu] elpa/multiple-cursors a4c4714 081/434: Remove multiple cursors when reverting buffer., ELPA Syncer, 2021/08/07
- [nongnu] elpa/multiple-cursors 36b262c 086/434: Added mc-version of mark-next-like-this, ELPA Syncer, 2021/08/07
- [nongnu] elpa/multiple-cursors 491b206 103/434: Use forward-line instead of next/previous-line in lisp code., ELPA Syncer, 2021/08/07
- [nongnu] elpa/multiple-cursors b1ea7f2 095/434: Add licences to all el-files., ELPA Syncer, 2021/08/07
- [nongnu] elpa/multiple-cursors f6403ea 112/434: Fix assertion, ELPA Syncer, 2021/08/07
- [nongnu] elpa/multiple-cursors 6481972 113/434: Update ecukes, ELPA Syncer, 2021/08/07
- [nongnu] elpa/multiple-cursors 241e34e 123/434: Remove mark-multiple integration, ELPA Syncer, 2021/08/07
- [nongnu] elpa/multiple-cursors f040a33 143/434: Add execute-extended-command to run-once list, ELPA Syncer, 2021/08/07
- [nongnu] elpa/multiple-cursors ef7eddf 021/434: Started adding feature tests., ELPA Syncer, 2021/08/07
- [nongnu] elpa/multiple-cursors 00896cf 030/434: Refactor: simplify saving of state for cursors.,
ELPA Syncer <=
- [nongnu] elpa/multiple-cursors c07d761 009/434: Some thoughts about combining with mark-multiple., ELPA Syncer, 2021/08/07
- [nongnu] elpa/multiple-cursors d8f5116 013/434: Add more commands to multiple-cursors whitelist., ELPA Syncer, 2021/08/07
- [nongnu] elpa/multiple-cursors 1fcf7ed 016/434: More white-listing of commands., ELPA Syncer, 2021/08/07
- [nongnu] elpa/multiple-cursors b51ee01 017/434: More white-listing., ELPA Syncer, 2021/08/07
- [nongnu] elpa/multiple-cursors 53dd119 032/434: Add docstring., ELPA Syncer, 2021/08/07
- [nongnu] elpa/multiple-cursors c2bb81f 042/434: Dont forget to require rectangular-region-mode, ELPA Syncer, 2021/08/07
- [nongnu] elpa/multiple-cursors ba11b12 051/434: Make interprogram-paste work for all cursors., ELPA Syncer, 2021/08/07
- [nongnu] elpa/multiple-cursors 8c8d52f 049/434: Run pre-command-hooks for each cursor, ELPA Syncer, 2021/08/07
- [nongnu] elpa/multiple-cursors c1fba2c 111/434: Add travis badge, ELPA Syncer, 2021/08/07
- [nongnu] elpa/multiple-cursors f73569f 125/434: Update documentation., ELPA Syncer, 2021/08/07