Re: [Patch] Bug: org-indent-region doesn't restore cursor position when org-src-tab-acts-natively is t [9.1.9 (release_9.1.9-65-g5e4542 @ /usr/share/emacs/26.3/lisp/org/)]

2021-05-16 Thread Bastien
Hi Sébastien,

Sébastien Miquel  writes:

> I think something went wrong though. I've attached as a new patch a
> part of the previous one that wasn't applied. Without it, some
> write-back buffers are never killed.

Sorry I overlooked this, and thanks a lot for the patch, applied now.

-- 
 Bastien



Re: [Patch] Bug: org-indent-region doesn't restore cursor position when org-src-tab-acts-natively is t [9.1.9 (release_9.1.9-65-g5e4542 @ /usr/share/emacs/26.3/lisp/org/)]

2021-05-16 Thread Sébastien Miquel

Hi Bastien,

Bastien writes:

Sorry it took so long to apply this patch, this is now done in maint.

No problem, thank you for getting back on this.

I think something went wrong though. I've attached as a new patch a
part of the previous one that wasn't applied. Without it, some
write-back buffers are never killed.

Regards,

--
Sébastien Miquel

>From f293a9d5808c413ce785646ebf5f480df3a00a2f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Miquel?= 
Date: Sun, 16 May 2021 19:13:53 +0200
Subject: [PATCH] org-src.el (org-edit-src-exit): Fix write-back-buf not
 getting killed

* lisp/org.el (org-edit-src-exit): Fix write-back-buf not getting
killed
---
 lisp/org-src.el | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lisp/org-src.el b/lisp/org-src.el
index df3c76e13..5604e6568 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -1255,8 +1255,8 @@ Throw an error if there is no such buffer."
 	   (narrow-to-region beg end)
 	   (replace-buffer-contents write-back-buf)
 	   (goto-char (point-max
-	 (when (and expecting-bol (not (bolp))) (insert "\n")))
-   (when write-back-buf (kill-buffer write-back-buf
+	 (when (and expecting-bol (not (bolp))) (insert "\n")
+(when write-back-buf (kill-buffer write-back-buf))
 ;; If we are to return to source buffer, put point at an
 ;; appropriate location.  In particular, if block is hidden, move
 ;; to the beginning of the block opening line.
-- 
2.31.1



Re: [Patch] Bug: org-indent-region doesn't restore cursor position when org-src-tab-acts-natively is t [9.1.9 (release_9.1.9-65-g5e4542 @ /usr/share/emacs/26.3/lisp/org/)]

2021-05-16 Thread Bastien
Hi Sébastien,

Sébastien Miquel  writes:

> I've fixed an issue in my previous patch with the write-back buffer
> not getting killed.

Sorry it took so long to apply this patch, this is now done in maint.

> There's also a remaining issue with the ~undo-boundary~ call in
> ~org-edit-src-exit~. Calling ~undo~ after ~indent-region~ or
> ~comment-region~ will send the cursor to the beginning of the src
> block

I believe we can live with it.

Thanks!

-- 
 Bastien



Re: [Patch] Bug: org-indent-region doesn't restore cursor position when org-src-tab-acts-natively is t [9.1.9 (release_9.1.9-65-g5e4542 @ /usr/share/emacs/26.3/lisp/org/)]

2020-10-23 Thread Sébastien Miquel
I've fixed an issue in my previous patch with the write-back buffer not 
getting killed.


Quick recap of the issue for anyone who might be interested:

When ~org-src-tab-acts-natively~ is t (which is now the default) 
functions such as ~indent-region~ and ~comment-region~ acting on a src 
block will call  ~org-edit-src~ and indent/comment from there.


The ~org-edit-src~ machinery replaces the original buffer with the 
src-edit buffer contents, and in doing so screws up the ~save-excursion~ 
calls from ~indent-region~ and ~comment-region~.


Because of this, calling ~indent-region~ or ~comment-region~ inside a 
src-block  sends the cursor to the beginning of the block.



The attached patch uses the ~replace-buffer-contents~ function when 
available in emacs >= 26.1 to fix this issue.



There's also a remaining issue with the ~undo-boundary~ call in 
~org-edit-src-exit~. Calling ~undo~ after ~indent-region~ or 
~comment-region~ will send the cursor to the beginning of the src block




Hi,

The attached patch fixes this issue for emacs >= 26.1.

It also fixes a similar issue where comment-line moves the cursor to 
the beginning of the block aswell.


Can this patch be applied ?


Sébastien

>From 8788ec81130130b538ca1d2f91853c9640040506 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Miquel?= 
Date: Sun, 4 Oct 2020 21:33:30 +0200
Subject: [PATCH] org-edit-src: Use replace-buffer-contents

* lisp/org-src.el (org-src--contents-for-write-back): Use a write back
buffer

* lisp/org-src.el (org-edit-src-exit): Use replace-buffer-contents

* lisp/org-src.el (org-edit-src-save): Use replace-buffer-contents
---
 lisp/org-src.el | 42 +-
 1 file changed, 29 insertions(+), 13 deletions(-)

diff --git a/lisp/org-src.el b/lisp/org-src.el
index 28733d011..3509e148f 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -432,8 +432,8 @@ spaces after it as being outside."
 		(line-end-position)
 	  (point))
 
-(defun org-src--contents-for-write-back ()
-  "Return buffer contents in a format appropriate for write back.
+(defun org-src--contents-for-write-back (write-back-buf)
+  "Populate write-back-buf with contents in a format appropriate for write back.
 Assume point is in the corresponding edit buffer."
   (let ((indentation-offset
 	 (if org-src--preserve-indentation 0
@@ -445,7 +445,7 @@ Assume point is in the corresponding edit buffer."
 	(source-tab-width org-src--tab-width)
 	(contents (org-with-wide-buffer (buffer-string)))
 	(write-back org-src--allow-write-back))
-(with-temp-buffer
+(with-current-buffer write-back-buf
   ;; Reproduce indentation parameters from source buffer.
   (setq indent-tabs-mode use-tabs?)
   (when (> source-tab-width 0) (setq tab-width source-tab-width))
@@ -462,8 +462,7 @@ Assume point is in the corresponding edit buffer."
 	(let ((i (current-column)))
 	  (delete-region (line-beginning-position) (point))
 	  (indent-to (+ i indentation-offset
-	  (forward-line)))
-  (buffer-string
+	  (forward-line))
 
 (defun org-src--edit-element
 (datum name &optional initialize write-back contents remote)
@@ -1189,20 +1188,27 @@ Throw an error if there is no such buffer."
   (interactive)
   (unless (org-src-edit-buffer-p) (user-error "Not in a sub-editing buffer"))
   (set-buffer-modified-p nil)
-  (let ((edited-code (org-src--contents-for-write-back))
+  (let ((write-back-buf (generate-new-buffer "*org-src-write-back*"))
 	(beg org-src--beg-marker)
 	(end org-src--end-marker)
 	(overlay org-src--overlay))
+(org-src--contents-for-write-back write-back-buf)
 (with-current-buffer (org-src-source-buffer)
   (undo-boundary)
   (goto-char beg)
   ;; Temporarily disable read-only features of OVERLAY in order to
   ;; insert new contents.
   (delete-overlay overlay)
-  (delete-region beg end)
   (let ((expecting-bol (bolp)))
-	(insert edited-code)
+	(if (version< emacs-version "26.1")
+	(progn (delete-region beg end)
+		   (insert (with-current-buffer write-back-buf (buffer-string
+	(save-restriction
+	  (narrow-to-region beg end)
+	  (replace-buffer-contents write-back-buf)
+	  (goto-char (point-max
 	(when (and expecting-bol (not (bolp))) (insert "\n")))
+  (kill-buffer write-back-buf)
   (save-buffer)
   (move-overlay overlay beg (point
   ;; `write-contents-functions' requires the function to return
@@ -1219,23 +1225,33 @@ Throw an error if there is no such buffer."
 	 (remote org-src--remote)
 	 (coordinates (and (not remote)
 			   (org-src--coordinates (point) 1 (point-max
-	 (code (and write-back (org-src--contents-for-write-back
+	 (write-back-buf (and write-back (generate-new-buffer "*org-src-write-back*"
+(when write-back (org-src--contents-for-write-back write-back-buf))
 (set-buffer-modified-p nil)
 ;; Switch to source buffer.  Kill sub-editing buffer.
 (let ((edit-buffer (curr

Re: [Patch] Bug: org-indent-region doesn't restore cursor position when org-src-tab-acts-natively is t [9.1.9 (release_9.1.9-65-g5e4542 @ /usr/share/emacs/26.3/lisp/org/)]

2020-10-04 Thread Sébastien Miquel

Hi,

The attached patch fixes this issue for emacs >= 26.1.

It also fixes a similar issue where comment-line moves the cursor to the 
beginning of the block aswell.


Can this patch be applied ?

Regards,
Sébastien Miquel


>From 36f01b8e9d8dc335564f8dc932caf21c43c374eb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Miquel?= 
Date: Sun, 4 Oct 2020 21:33:30 +0200
Subject: [PATCH] org-edit-src: Use replace-buffer-contents

* lisp/org-src.el (org-src--contents-for-write-back): Use a write back
buffer

* lisp/org-src.el (org-edit-src-exit): Use replace-buffer-contents

* lisp/org-src.el (org-edit-src-save): Use replace-buffer-contents
---
 lisp/org-src.el | 40 +++-
 1 file changed, 27 insertions(+), 13 deletions(-)

diff --git a/lisp/org-src.el b/lisp/org-src.el
index 28733d011..4f0044394 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -432,8 +432,8 @@ spaces after it as being outside."
 		(line-end-position)
 	  (point))
 
-(defun org-src--contents-for-write-back ()
-  "Return buffer contents in a format appropriate for write back.
+(defun org-src--contents-for-write-back (write-back-buf)
+  "Populate write-back-buf with contents in a format appropriate for write back.
 Assume point is in the corresponding edit buffer."
   (let ((indentation-offset
 	 (if org-src--preserve-indentation 0
@@ -445,7 +445,7 @@ Assume point is in the corresponding edit buffer."
 	(source-tab-width org-src--tab-width)
 	(contents (org-with-wide-buffer (buffer-string)))
 	(write-back org-src--allow-write-back))
-(with-temp-buffer
+(with-current-buffer write-back-buf
   ;; Reproduce indentation parameters from source buffer.
   (setq indent-tabs-mode use-tabs?)
   (when (> source-tab-width 0) (setq tab-width source-tab-width))
@@ -462,8 +462,7 @@ Assume point is in the corresponding edit buffer."
 	(let ((i (current-column)))
 	  (delete-region (line-beginning-position) (point))
 	  (indent-to (+ i indentation-offset
-	  (forward-line)))
-  (buffer-string
+	  (forward-line))
 
 (defun org-src--edit-element
 (datum name &optional initialize write-back contents remote)
@@ -1189,20 +1188,27 @@ Throw an error if there is no such buffer."
   (interactive)
   (unless (org-src-edit-buffer-p) (user-error "Not in a sub-editing buffer"))
   (set-buffer-modified-p nil)
-  (let ((edited-code (org-src--contents-for-write-back))
+  (let ((write-back-buf (generate-new-buffer "*org-src-write-back*"))
 	(beg org-src--beg-marker)
 	(end org-src--end-marker)
 	(overlay org-src--overlay))
+(org-src--contents-for-write-back write-back-buf)
 (with-current-buffer (org-src-source-buffer)
   (undo-boundary)
   (goto-char beg)
   ;; Temporarily disable read-only features of OVERLAY in order to
   ;; insert new contents.
   (delete-overlay overlay)
-  (delete-region beg end)
   (let ((expecting-bol (bolp)))
-	(insert edited-code)
+	(if (version< emacs-version "26.1")
+	(progn (delete-region beg end)
+		   (insert (with-current-buffer write-back-buf (buffer-string
+	(save-restriction
+	  (narrow-to-region beg end)
+	  (replace-buffer-contents write-back-buf)
+	  (goto-char (point-max
 	(when (and expecting-bol (not (bolp))) (insert "\n")))
+  (kill-buffer write-back-buf)
   (save-buffer)
   (move-overlay overlay beg (point
   ;; `write-contents-functions' requires the function to return
@@ -1219,7 +1225,8 @@ Throw an error if there is no such buffer."
 	 (remote org-src--remote)
 	 (coordinates (and (not remote)
 			   (org-src--coordinates (point) 1 (point-max
-	 (code (and write-back (org-src--contents-for-write-back
+	 (write-back-buf (and write-back (generate-new-buffer "*org-src-write-back*"
+(when write-back (org-src--contents-for-write-back write-back-buf))
 (set-buffer-modified-p nil)
 ;; Switch to source buffer.  Kill sub-editing buffer.
 (let ((edit-buffer (current-buffer))
@@ -1229,13 +1236,20 @@ Throw an error if there is no such buffer."
   (kill-buffer edit-buffer))
 ;; Insert modified code.  Ensure it ends with a newline character.
 (org-with-wide-buffer
- (when (and write-back (not (equal (buffer-substring beg end) code)))
+ (when (and write-back (not (equal (buffer-substring beg end)
+   (with-current-buffer write-back-buf (buffer-string)
(undo-boundary)
(goto-char beg)
-   (delete-region beg end)
(let ((expecting-bol (bolp)))
-	 (insert code)
-	 (when (and expecting-bol (not (bolp))) (insert "\n")
+	 (if (version< emacs-version "26.1")
+	 (progn (delete-region beg end)
+		(insert (with-current-buffer write-back-buf (buffer-string
+	 (save-restriction
+	   (narrow-to-region beg end)
+	   (replace-buffer-contents write-back-buf)
+	   (goto-char (point-max
+	 (when (and expecting-bol (not (bolp))) (insert "\n")))
+   (kill-buffer