emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/speedrect 2aac618746 26/90: speedrect-yank-from-calc: a


From: ELPA Syncer
Subject: [elpa] externals/speedrect 2aac618746 26/90: speedrect-yank-from-calc: added
Date: Fri, 6 Dec 2024 18:59:10 -0500 (EST)

branch: externals/speedrect
commit 2aac6187460dc955fe296f2f115c287f20a39c64
Author: JD Smith <93749+jdtsmith@users.noreply.github.com>
Commit: JD Smith <93749+jdtsmith@users.noreply.github.com>

    speedrect-yank-from-calc: added
---
 README.md    |  5 +++--
 speedrect.el | 44 +++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 46 insertions(+), 3 deletions(-)

diff --git a/README.md b/README.md
index dca0e579e8..3d9317c2bf 100644
--- a/README.md
+++ b/README.md
@@ -13,8 +13,8 @@ SpeedRect is a small Emacs package that provides convenient 
"modal" key bindings
 - Additional command to _invert the deletion_, i.e. delete the _unmarked 
columns_, keeping the marked rectangle.
 - Save and restore the last rectangle's position.
 - Start a new rectangle from point. 
-- Calc grab and sum commands.
-- Column shift: slide a rectangle left or right, 1 or 5 columns at a time (or 
any number, with a prefix). 
+- Two-way interaction with Calc: grab, sum, and yank matrix from top of calc 
stack.
+- Column shift: slide a rectangle left or right, 1 or 5 columns at a time (or 
any number of columns, with a numerical prefix). 
 - A help page (hit `?`). 
 
 ## Installation
@@ -70,6 +70,7 @@ Numerical:
   [#] grab      grab the rectangle as a matrix in calc
   [=] across    sum across rows and grab result in calc as a vector
   [+] down      sum down the columns and grab result in calc
+  [m] yank-mat  yank matrix from top of calc stack, overwriting selected rect
 Etc:
   [?] help      view this Help buffer
   [q] quit      exit rectangle-mark-mode
diff --git a/speedrect.el b/speedrect.el
index ea17787134..1ef133dab2 100644
--- a/speedrect.el
+++ b/speedrect.el
@@ -3,7 +3,7 @@
 
 ;; Author: JD Smith
 ;; Created: 2023
-;; Version: 0.1.2
+;; Version: 0.2.0
 ;; Package-Requires: ((emacs "25.1") (compat "29.1.4.0"))
 ;; Homepage: https://github.com/jdtsmith/speedrect
 ;; Keywords: convenience
@@ -32,6 +32,8 @@
 
 ;;; Code:
 (require 'rect)
+(require 'calc)
+(require 'subr-x)
 (require 'compat)
 (eval-when-compile (require 'cl-lib))
 
@@ -110,6 +112,44 @@ Note that point and mark will not move beyond the end of 
text on their lines."
                   (progn (goto-char end) (line-end-position)))
     (insert (string-join rect "\n"))))
 
+(defun speedrect--replace-with-rect (startcol endcol rect)
+  "Insert RECT's 2nd element, replacing text between STARTCOL and ENDCOL.
+RECT's 2nd element is removed by side-effect."
+  (delete-rectangle-line startcol endcol nil)
+  (insert (pop (cdr rect))))
+
+(defun speedrect-yank-from-calc (start end)
+  "Yank matrix from top of calc stack, overwriting the marked rectangle.
+START and END are the interactively-defined region beginning and
+end.  The (matrix) value at the top of the calc stack is used,
+and must have the same number of rows as the height of the marked
+rectangle.  To avoid copying brackets and commas, v [ and v , may
+be used in calc."
+  (interactive "r")
+  (if-let ((rectangle-mark-mode)
+          (buf (get-buffer "*Calculator*")))
+      (let* ((b (region-beginning))
+            (e (region-end))
+            (height (+ (count-lines b e)
+                       (if (eq (char-before e) ?\n) 1 0)))
+            crect)
+       (save-excursion
+         (with-current-buffer buf
+           (let* ((cstart (progn (calc-cursor-stack-index 1)
+                                 (if (looking-at (if calc-line-numbering 
"[0-9]+: *[^ \n]" " *[^ \n]"))
+                                     (1- (match-end 0))
+                                   (point))))
+                  (cend (progn (calc-cursor-stack-index 0) (line-end-position 
0))))
+             (setq crect (extract-rectangle cstart cend))))
+         (if (eq (length crect) height)
+             (progn
+               (push nil crect) ; dummy, for consuming in apply-on-rectangle
+               (apply-on-rectangle 'speedrect--replace-with-rect start end 
crect)
+               (speedrect-stash))
+           (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")))
+
 (defun speedrect-transient-map-info ()
   "Documentation window for speedrect."
   (interactive)
@@ -142,6 +182,7 @@ Note that point and mark will not move beyond the end of 
text on their lines."
             "  [#] grab      grab the rectangle as a matrix in calc\n"
             "  [=] across    sum across rows and grab result in calc as a 
vector\n"
             "  [+] down      sum down the columns and grab result in calc\n\n"
+            "  [m] yank-mat  yank matrix from top of calc stack, overwriting 
selected rect\n\n"
             "Etc:\n\n"
             "  [?] help      view this Help buffer\n"
             "  [q] quit      exit rectangle-mark-mode"))
@@ -171,6 +212,7 @@ prior to deactivating mark."
      ("M-S-<left>" speedrect-shift-left-fast)
      ;; Calc commands
      ("=" calc-grab-sum-across) ("+" calc-grab-sum-down) ("#" 
calc-grab-rectangle)
+     ("m" speedrect-yank-from-calc)
      ;; Special
      ("n" speedrect-restart) ("l" speedrect-recall-last)
      ("?" speedrect-transient-map-info) ("q" speedrect-quit))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]