Here is a different approach, maybe someone will find this useful. This is a minor mode that patches get-text-property
to fake the property 'yank-handler
, which indicates which function to use for inserting. Then I use insert-rectangle
for that. That way, this also works with helm-ring
.
(defvar my/is-rectangle-yanking nil)
(defun my/insert-rectangle-split (string)
(let ((rows (split-string string "\n"))
(my/is-rectangle-yanking t))
(save-excursion (insert (make-string (length rows) ?\n)))
(insert-rectangle rows)
(delete-char 1)))
(defun my/get-text-property-rectangle-yank (fun pos prop &optional object)
(let ((propv (funcall fun pos prop object)))
(if propv propv
(when (and (eq prop 'yank-handler) (stringp object) (not my/is-rectangle-yanking))
'(my/insert-rectangle-split)))))
(define-global-minor-mode my/yank-as-rectangle
"Minor mode to yank as rectangle. This patches `get-text-property'
to put `insert-rectangle' as the insert function (`yank-handler')."
:init-value nil :lighter " Rec"
(if (not my/yank-as-rectangle)
(advice-remove 'get-text-property #'my/get-text-property-rectangle-yank)
(advice-add 'get-text-property :around #'my/get-text-property-rectangle-yank)))
– Tobias Mar 23 '17 at 15:26