Subject: bug#65380: [PATCH] Add command to copy contents in a diff-mode buffer

From: Gregory Heytings <[email-masked]>
Date: Sun, 20 Aug 2023 23:39:09 +0000

>> Yeah, the question then is: should it be new kill/copy commands or a 
>> new yank command?
>
> I would do something else: a command to move the killed rectangle to the 
> kill-ring.  And perhaps a command to do the opposite: transform the last 
> item of the kill-ring into a killed rectangle.
>

And here are the two commands I had in mind:

(defun move-killed-rectangle-to-kill-ring ()
   "Move the killed rectangle to the `kill-ring'."
   (interactive)
   (if killed-rectangle
       (with-temp-buffer
 	(while killed-rectangle
 	  (insert-for-yank (car killed-rectangle))
 	  (insert "\n")
 	  (setq killed-rectangle (cdr killed-rectangle)))
 	(kill-ring-save (point-min) (point-max)))
     (user-error "No killed rectangle")))

(defun copy-last-kill-to-killed-rectangle ()
   "Transform the current item of `kill-ring' into a killed rectangle"
   (interactive)
   (with-temp-buffer
     (let ((max -1))
       (insert-for-yank (current-kill 0 t))
       (goto-char (point-min))
       (while (not (eobp))
 	(move-end-of-line nil)
 	(let ((col (current-column)))
 	  (when (> col max)
 	    (setq max col)))
 	(forward-line 1))
       (let ((col (current-column)))
 	(when (< col max)
 	  (insert (make-string (- max col) ? ))))
     (kill-rectangle (point-min) (point-max)))))

WDYT?