[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/speedrect 0fa8964ce2 46/90: wrap commands for stash and
From: |
ELPA Syncer |
Subject: |
[elpa] externals/speedrect 0fa8964ce2 46/90: wrap commands for stash and restart |
Date: |
Fri, 6 Dec 2024 18:59:13 -0500 (EST) |
branch: externals/speedrect
commit 0fa8964ce2f4e5d66cea76a4e0f2b6bf48fd2fb2
Author: JD Smith <93749+jdtsmith@users.noreply.github.com>
Commit: JD Smith <93749+jdtsmith@users.noreply.github.com>
wrap commands for stash and restart
---
speedrect.el | 77 +++++++++++++++++++++++++++++++++++++-----------------------
1 file changed, 47 insertions(+), 30 deletions(-)
diff --git a/speedrect.el b/speedrect.el
index e3fe5d24aa..a6cf25b914 100644
--- a/speedrect.el
+++ b/speedrect.el
@@ -37,6 +37,11 @@
(require 'compat)
(eval-when-compile (require 'cl-lib))
+(defcustom speedrect-continue t
+ "Stay in speedrect until quit."
+ :type 'boolean
+ :group 'rectangle)
+
(defun speedrect-linecol ()
"Return line and column as list."
(list (line-number-at-pos) (current-column)))
@@ -45,16 +50,14 @@
"Last rectangle position.
Stored as (point-line point-col mark-line mark-col)")
-(defun speedrect-stash (&rest _r)
- "Stash the line and column of point and mark.
-Used as :before advice for commands which operate on the marked
-rect and exit `rectangle-mark-mode'."
- (if rectangle-mark-mode
- (setq speedrect-last
- (append (speedrect-linecol)
- (save-excursion
- (goto-char (mark))
- (speedrect-linecol))))))
+(defun speedrect-stash ()
+ "Stash the line and column of point and mark."
+ (when rectangle-mark-mode
+ (setq speedrect-last
+ (append (speedrect-linecol)
+ (save-excursion
+ (goto-char (mark))
+ (speedrect-linecol))))))
(defun speedrect-recall-last ()
"Restore last saved rectangle position."
@@ -67,7 +70,8 @@ rect and exit `rectangle-mark-mode'."
(set-mark (point))
(forward-line (- pl ml))
(move-to-column pc)
- (message "Restored last rectangle %d %d, %d %d" ml mc pl pc))
+ (if (called-interactively-p 'interactive)
+ (message "Restored last rectangle %d %d, %d %d" ml mc pl pc)))
(_ (message "No stored rectangle position"))))
(defun speedrect-restart ()
@@ -134,7 +138,6 @@ Note that point and mark will not move beyond the end of
text on their lines."
(defun speedrect-delete-rest (start end)
"Keep rectangle between START and END, deleting the rest of the affected
lines."
(interactive "r")
- (speedrect-stash)
(let ((rect (extract-rectangle start end)))
(delete-region (progn (goto-char start) (line-beginning-position))
(progn (goto-char end) (line-end-position)))
@@ -189,8 +192,7 @@ each side of the inserted text."
(apply-on-rectangle 'speedrect--replace-with-rect
start end crect
(max 0 (1- (car lr)))
- (min 0 (- (1- (cdr lr)))))
- (speedrect-stash))
+ (min 0 (- (1- (cdr lr))))))
(user-error "Row count of calc matrix (%d) does not match rectangle
height (%d)"
(length crect) height))))
(user-error "Calc rectangle yank not possible here")))
@@ -242,18 +244,39 @@ each side of the inserted text."
(interactive)
(deactivate-mark))
+(defun speedrect--wrap-command
+ (command &optional after)
+ "Wrap an interactive COMMAND to store rect and (posibly) reenter.
+Many/most rectangle commands deactivate mark and exit
+`rectangle-mark-mode'. This ensure the rectangle is stashed
+before such commands, and, if custom option `speedrect-continue'
+is non-nil, restarts with the same rectangle. If AFTER is
+non-nil, stash the rectangle after the command runs."
+ (lambda ()
+ (interactive)
+ (unless after (speedrect-stash))
+ (call-interactively command)
+ (when after (speedrect-stash))
+ (when speedrect-continue
+ (run-at-time 0 nil
+ (lambda ()
+ (let ((rectangle-mark-mode-hook nil))
+ (activate-mark)
+ (rectangle-mark-mode 1)
+ (speedrect-recall-last)))))))
+
(defun speedrect-create-bindings ()
"Create the bindings for speedrect.
Also adds :before advice to rectangle commands to stash the rect
prior to deactivating mark."
(cl-loop
- for (key def) in
+ for (key def wrap) in
'(;; Rectangle basics
- ("k" kill-rectangle) ("t" string-rectangle)
- ("o" open-rectangle) ("w" copy-rectangle-as-kill)
- ("y" yank-rectangle) ("c" clear-rectangle)
- ("d" delete-rectangle) ("N" rectangle-number-lines)
- ("r" speedrect-delete-rest) ("SPC" delete-whitespace-rectangle)
+ ("k" kill-rectangle after) ("t" string-rectangle after)
+ ("o" open-rectangle t) ("w" copy-rectangle-as-kill t)
+ ("y" yank-rectangle t) ("c" clear-rectangle t)
+ ("d" delete-rectangle after) ("N" rectangle-number-lines t)
+ ("r" speedrect-delete-rest t) ("SPC" delete-whitespace-rectangle t)
;; Shift rect
("S-<right>" speedrect-shift-right)
("S-<left>" speedrect-shift-left)
@@ -265,24 +288,18 @@ prior to deactivating mark."
("M-S-<down>" speedrect-shift-down-fast)
;; Calc commands
("_" calc-grab-sum-across) (":" calc-grab-sum-down) ("#"
calc-grab-rectangle)
- ("m" speedrect-yank-from-calc)
+ ("m" speedrect-yank-from-calc t)
;; Special
("n" speedrect-restart) ("l" speedrect-recall-last)
("?" speedrect-transient-map-info) ("q" speedrect-quit))
- for dname = (symbol-name def)
- do
- (define-key rectangle-mark-mode-map (kbd key) def)
- (unless (seq-some (lambda (name)
- (string-prefix-p name dname))
- '("speedrect" "calc"))
- (advice-add def :before #'speedrect-stash)))
+ for bind = (if wrap (speedrect--wrap-command def (eq wrap 'after)) def)
+ do (define-key rectangle-mark-mode-map (kbd key) bind))
(put 'rectangle-mark-mode-map 'speedrect t))
(defun speedrect-hook ()
"Setup speedrect for `rectangle-mark-mode'."
(when rectangle-mark-mode
- (unless (get 'rectangle-mark-mode-map 'speedrect)
- (speedrect-create-bindings))
+ (speedrect-create-bindings)
(message "%s: [?] for help%s"
(propertize "SpeedRect" 'face 'success)
(if speedrect-last
- [elpa] externals/speedrect 3cab9dfcc5 35/90: Update README.md, (continued)
- [elpa] externals/speedrect 3cab9dfcc5 35/90: Update README.md, ELPA Syncer, 2024/12/06
- [elpa] externals/speedrect d9d7f79977 36/90: calc-grab-sum-down/across: switch to `_` and `:` to mirror calc, ELPA Syncer, 2024/12/06
- [elpa] externals/speedrect 1e732a5b35 44/90: bump version, ELPA Syncer, 2024/12/06
- [elpa] externals/speedrect a9086d65e5 45/90: speedrect-right-char: correct rectangle-right logic, ELPA Syncer, 2024/12/06
- [elpa] externals/speedrect 6867945e0b 54/90: Update README.md, ELPA Syncer, 2024/12/06
- [elpa] externals/speedrect 2a4d4f0bcd 59/90: Properly handle point/mark crutches for saving rects, ELPA Syncer, 2024/12/06
- [elpa] externals/speedrect d2af235ca3 60/90: stash: save point and mark crutches to stash, ELPA Syncer, 2024/12/06
- [elpa] externals/speedrect 6dc047f54b 64/90: bump version, ELPA Syncer, 2024/12/06
- [elpa] externals/speedrect 4321fef672 40/90: Mention phi-rectangle, ELPA Syncer, 2024/12/06
- [elpa] externals/speedrect 4f9d3020e2 32/90: Update README.md, ELPA Syncer, 2024/12/06
- [elpa] externals/speedrect 0fa8964ce2 46/90: wrap commands for stash and restart,
ELPA Syncer <=
- [elpa] externals/speedrect e7c0ca81cc 49/90: Bump version, ELPA Syncer, 2024/12/06
- [elpa] externals/speedrect 540dc4e790 58/90: Update README.md, ELPA Syncer, 2024/12/06
- [elpa] externals/speedrect 0f00af4c67 62/90: docstring tweak, ELPA Syncer, 2024/12/06
- [elpa] externals/speedrect 6982a08d25 11/90: doc: open uses tabs/spaces, ELPA Syncer, 2024/12/06
- [elpa] externals/speedrect 7bb4633c90 29/90: Update README.md, ELPA Syncer, 2024/12/06
- [elpa] externals/speedrect b743b42208 55/90: Add x:corners and M:multiple-cursors support, ELPA Syncer, 2024/12/06
- [elpa] externals/speedrect 85b281d045 61/90: f - fill-text in rectangle, ELPA Syncer, 2024/12/06
- [elpa] externals/speedrect 6886e5c604 66/90: Update README.md, ELPA Syncer, 2024/12/06
- [elpa] externals/speedrect 932d983f02 65/90: Document fill, ELPA Syncer, 2024/12/06
- [elpa] externals/speedrect 1e1b713d65 57/90: Update README.md, ELPA Syncer, 2024/12/06