Subject: bug#79371: [PATCH] Make yank-in-context also can indent the yanked text.

From: Elijah Gabe Pérez <[email-masked]>
Date: Tue, 02 Sep 2025 09:55:55 -0600

Tags: patch

This patch makes `yank-in-context` can optionally indent the yanked
text.

The indentation is done calling `indent-region`, but the function to use
can be changed.

Additionally, i added support for `delete-selection-mode` in
`yank-in-context`.

>From 83c37fb609b40a6cc5cd657b224cfd1db5c14db8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?El=C3=ADas=20Gabriel=20P=C3=A9rez?= <[email-masked]>
Date: Mon, 1 Sep 2025 13:31:49 -0600
Subject: [PATCH] Make yank-in-context can also indent the yanked text.  (Bug#)

* etc/NEWS: Announce changes.
* lisp/delsel.el: Add missing 'yank-in-context'.
* lisp/simple.el (yank-in-context-indent)
(yank-in-context-indent-function): New user options.
(yank-in-context): Rework.
---
 etc/NEWS       | 13 +++++++++++++
 lisp/delsel.el |  1 +
 lisp/simple.el | 28 ++++++++++++++++++++++++++--
 3 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 630d03a1fa0..9aa5436c322 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -2371,6 +2371,19 @@ connected, to facilitate enabling 'strokes-mode' in sessions where the
 availability of a mouse device varies during execution (as is frequently
 observed on Android).
 
+** Yank
+
+---
+*** New user option 'yank-in-context-indent'.
+If this option is non-nil, 'yank-in-context' will indent yanked text,
+according to current major mode indentation.  By default this is
+disabled.
+
+---
+*** New user option 'yank-in-context-indent-function'.
+This option specifies the function to use for indent the yanked text in
+'yank-in-context'.  By default this is set to 'indent-region'.
+
 ** Yank Media
 
 +++
diff --git a/lisp/delsel.el b/lisp/delsel.el
index f5badf62c2c..a94b0f58a36 100644
--- a/lisp/delsel.el
+++ b/lisp/delsel.el
@@ -317,6 +317,7 @@ delete-selection-uses-region-p
 (put 'yank 'delete-selection 'yank)
 (put 'yank-pop 'delete-selection 'yank)
 (put 'yank-from-kill-ring 'delete-selection 'yank)
+(put 'yank-in-context 'delete-selection 'yank)
 (put 'clipboard-yank 'delete-selection 'yank)
 (put 'insert-register 'delete-selection t)
 ;; delete-backward-char and delete-forward-char already delete the selection by
diff --git a/lisp/simple.el b/lisp/simple.el
index 2a13d59e5cd..5d194908e37 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -6416,6 +6416,24 @@ rotate-yank-pointer
   (interactive "p")
   (current-kill arg))
 
+(declare-function indent-region "indent" (start end &optional column))
+
+(defcustom yank-in-context-indent nil
+  "If non-nil, `yank-in-context' will indent yanked text.
+The indentation is done calling the function in
+`yank-in-context-indent-function' (which see)."
+  :type 'boolean
+  :group 'killing
+  :version "31.1")
+
+(defcustom yank-in-context-indent-function #'indent-region
+  "Function to call to indent the yanked region in `yank-in-context'.
+The function is called with 2 arguments, which are the yanked region
+positions."
+  :type 'function
+  :group 'killing
+  :version "31.1")
+
 (defun yank-in-context (&optional arg)
   "Insert the last stretch of killed text while preserving syntax.
 In particular, if point is inside a string, any quote characters
@@ -6425,6 +6443,8 @@ yank-in-context
 If point is inside a comment, ensure that the inserted text is
 also marked as a comment.
 
+If `yank-indent' is non-nil, yanked text will be indented.
+
 This command otherwise behaves as `yank'.  See that command for
 explanation of ARG.
 
@@ -6432,8 +6452,12 @@ yank-in-context
 variable to determine how strings should be escaped."
   (interactive "*P")
   (let ((yank-transform-functions (cons #'yank-in-context--transform
-                                        yank-transform-functions)))
-    (yank arg)))
+                                        yank-transform-functions))
+        (beg (point)) end)
+    (yank arg)
+    (setq end (point))
+    (if yank-in-context-indent
+        (funcall yank-in-context-indent-function beg end))))
 
 (defun yank-in-context--transform (string)
   (let ((ppss (syntax-ppss)))
-- 
2.51.0

-- 
- E.G via Gnus and Org.