emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* 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-02-02 10:22 Sébastien Miquel
  2020-02-03 18:47 ` Bastien
  0 siblings, 1 reply; 13+ messages in thread
From: Sébastien Miquel @ 2020-02-02 10:22 UTC (permalink / raw)
  To: emacs-orgmode

Given the following src block in org-mode:

#+BEGIN_SRC emacs-lisp
(defun my-function ()
   (instruction1)
   (instruction2)
;;  (instruction3)
   )
#+END_SRC

Steps to reproduce (with emacs -Q):
  1) set org-src-tab-acts-natively to t
  2) set the mark at the beginning of the function definition inside the 
src block
  3) move point to the last line of the src block
  4) call org-indent-region

Effect: the code is indented, but the cursor is moved to first line of
the block.

Expected: the code is indented, and the cursor doesn't move.

Emacs  : GNU Emacs 26.3 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.10)
  of 2019-08-29
Package: Org mode version 9.1.9 (release_9.1.9-65-g5e4542 @ 
/usr/share/emacs/26.3/lisp/org/)

current state:
==============
(setq
  org-src-mode-hook '(org-src-babel-configure-edit-buffer
              org-src-mode-configure-edit-buffer)
  org-after-todo-state-change-hook '(org-clock-out-if-current)
  org-metadown-hook '(org-babel-pop-to-session-maybe)
  org-clock-out-hook '(org-clock-remove-empty-clock-drawer)
  org-mode-hook '(#[0 "\300\301\302\303\304$\207"
            [add-hook change-major-mode-hook org-show-block-all append
             local]
            5]
          #[0 "\300\301\302\303\304$\207"
            [add-hook change-major-mode-hook org-babel-show-result-all
             append local]
            5]
          org-babel-result-hide-spec org-babel-hide-all-hashes)
  org-archive-hook '(org-attach-archive-delete-maybe)
  org-confirm-elisp-link-function 'yes-or-no-p
  org-agenda-before-write-hook '(org-agenda-add-entry-text)
  org-metaup-hook '(org-babel-load-in-session-maybe)
  org-bibtex-headline-format-function #[257 "\300\x01\236A\207" [:title] 3 
"\n\n(fn ENTRY)"]
  org-babel-pre-tangle-hook '(save-buffer)
  org-tab-first-hook '(org-babel-hide-result-toggle-maybe
               org-babel-header-arg-expand)
  org-occur-hook '(org-first-headline-recenter)
  org-cycle-hook '(org-cycle-hide-archived-subtrees org-cycle-hide-drawers
           org-cycle-show-empty-lines
           org-optimize-window-after-visibility-change)
  org-speed-command-hook '(org-speed-command-activate
               org-babel-speed-command-activate)
  org-confirm-shell-link-function 'yes-or-no-p
  org-link-parameters '(("id" :follow org-id-open)
                ("rmail" :follow org-rmail-open :store
             org-rmail-store-link)
                ("mhe" :follow org-mhe-open :store org-mhe-store-link)
                ("irc" :follow org-irc-visit :store org-irc-store-link)
                ("info" :follow org-info-open :export org-info-export
             :store org-info-store-link)
                ("gnus" :follow org-gnus-open :store
             org-gnus-store-link)
                ("docview" :follow org-docview-open :export
             org-docview-export :store org-docview-store-link)
                ("bibtex" :follow org-bibtex-open :store
             org-bibtex-store-link)
                ("bbdb" :follow org-bbdb-open :export org-bbdb-export
             :complete org-bbdb-complete-link :store
             org-bbdb-store-link)
                ("w3m" :store org-w3m-store-link) ("file+sys")
                ("file+emacs") ("doi" :follow org--open-doi-link)
                ("elisp" :follow org--open-elisp-link)
                ("file" :complete org-file-complete-link)
                ("ftp" :follow
             (lambda (path) (browse-url (concat "ftp:" path))))
                ("help" :follow org--open-help-link)
                ("http" :follow
             (lambda (path) (browse-url (concat "http:" path))))
                ("https" :follow
             (lambda (path) (browse-url (concat "https:" path))))
                ("mailto" :follow
             (lambda (path) (browse-url (concat "mailto:" path))))
                ("news" :follow
             (lambda (path) (browse-url (concat "news:" path))))
                ("shell" :follow org--open-shell-link))
  )

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: 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-02-02 10:22 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/)] Sébastien Miquel
@ 2020-02-03 18:47 ` Bastien
  2020-02-04  6:47   ` Sébastien Miquel
  0 siblings, 1 reply; 13+ messages in thread
From: Bastien @ 2020-02-03 18:47 UTC (permalink / raw)
  To: Sébastien Miquel; +Cc: emacs-orgmode

Hi Sébastien,

I cannot reproduce this bug with latest Org version.

If you can, please test with the latest Org version and
report any problem.

Thanks,

-- 
 Bastien

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: 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-02-03 18:47 ` Bastien
@ 2020-02-04  6:47   ` Sébastien Miquel
  2020-02-04 16:07     ` Bastien
  0 siblings, 1 reply; 13+ messages in thread
From: Sébastien Miquel @ 2020-02-04  6:47 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-orgmode

Hi,

Thank you for your reply.

I can still reproduce with latest Org: what I did was start emacs with 
emacs -q --load empty_init.el, with empty_init.el setting the org 
package-archive to http://orgmode.org/elpa/, update org to 9.3.2 then 
proceed with the previous steps. The result is the same.

I'd be happy to provide any additional info.


> Hi Sébastien,
>
> I cannot reproduce this bug with latest Org version.
>
> If you can, please test with the latest Org version and
> report any problem.
>
> Thanks,
>

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: 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-02-04  6:47   ` Sébastien Miquel
@ 2020-02-04 16:07     ` Bastien
  2020-02-09 15:26       ` Sébastien Miquel
  0 siblings, 1 reply; 13+ messages in thread
From: Bastien @ 2020-02-04 16:07 UTC (permalink / raw)
  To: Sébastien Miquel; +Cc: emacs-orgmode

Hi Sébastien,

Sébastien Miquel <sebastien.miquel@posteo.eu> writes:

> Thank you for your reply.
>
> I can still reproduce with latest Org

Mh, yes, I can reproduce this now, and this is fixed in maint.

Thanks!

-- 
 Bastien

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: 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-02-04 16:07     ` Bastien
@ 2020-02-09 15:26       ` Sébastien Miquel
  2020-02-10  6:25         ` Bastien
  0 siblings, 1 reply; 13+ messages in thread
From: Sébastien Miquel @ 2020-02-09 15:26 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-orgmode

Thank you for looking into this.

As far as I can tell, this is still broken in 9.3.3, which has your fix 
included.

I've updated org to 9.3.3, describe-function brings me to the org.el 
file with your save-window-excursion modification, and I've deleted the 
.elc file just in case.


> Hi Sébastien,
>
> Sébastien Miquel <sebastien.miquel@posteo.eu> writes:
>
>> Thank you for your reply.
>>
>> I can still reproduce with latest Org
> Mh, yes, I can reproduce this now, and this is fixed in maint.
>
> Thanks!
>

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: 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-02-09 15:26       ` Sébastien Miquel
@ 2020-02-10  6:25         ` Bastien
  2020-02-29 20:24           ` Sébastien Miquel
  0 siblings, 1 reply; 13+ messages in thread
From: Bastien @ 2020-02-10  6:25 UTC (permalink / raw)
  To: Sébastien Miquel; +Cc: emacs-orgmode

Hi Sébastien,

Sébastien Miquel <sebastien.miquel@posteo.eu> writes:

> As far as I can tell, this is still broken in 9.3.3, which has your
> fix included.

yes, the initial fix was wrong.

The current behavior when the region is active in a src block is to
indent the whole source block, which is not what the user expect.  The
problem with the cursor going back to the beginning of the block comes
on top of this (bigger?) problem.

I could not find a proper fix yet, I will try later this week.

Thanks,

-- 
 Bastien

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: 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-02-10  6:25         ` Bastien
@ 2020-02-29 20:24           ` Sébastien Miquel
  2020-09-04 10:31             ` Bastien
  0 siblings, 1 reply; 13+ messages in thread
From: Sébastien Miquel @ 2020-02-29 20:24 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-orgmode

Hi Bastien,

Thank you again for looking into this. I guess you couldn't find an easy 
fix.

This bug is quite the pain, since it gets triggered by the comment-line 
function: every time I try to comment something, the cursor goes to the 
beginning of the block.

With org-src-tab-acts-natively being set to t by default, it may impact 
a lot of people.

Sorry to be nagging you, my elisp-fu is barely above kill/yank level, 
but I'm still curious why save-excursion seemingly isn't doing its job here.


> Hi Sébastien,
>
> yes, the initial fix was wrong.
>
> The current behavior when the region is active in a src block is to
> indent the whole source block, which is not what the user expect.  The
> problem with the cursor going back to the beginning of the block comes
> on top of this (bigger?) problem.
>
> I could not find a proper fix yet, I will try later this week.
>
> Thanks,
>

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: 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-02-29 20:24           ` Sébastien Miquel
@ 2020-09-04 10:31             ` Bastien
  2020-10-04 21:08               ` [Patch] " Sébastien Miquel
  0 siblings, 1 reply; 13+ messages in thread
From: Bastien @ 2020-09-04 10:31 UTC (permalink / raw)
  To: Sébastien Miquel; +Cc: emacs-orgmode

Hi Sébastien,

Sébastien Miquel <sebastien.miquel@posteo.eu> writes:

> Thank you again for looking into this. I guess you couldn't find an
> easy fix.

... nope.  I'm confirming this bug so that someone else can find it 
later on.

-- 
 Bastien


^ permalink raw reply	[flat|nested] 13+ messages in thread

* 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-09-04 10:31             ` Bastien
@ 2020-10-04 21:08               ` Sébastien Miquel
  2020-10-23 21:06                 ` Sébastien Miquel
  0 siblings, 1 reply; 13+ messages in thread
From: Sébastien Miquel @ 2020-10-04 21:08 UTC (permalink / raw)
  To: emacs-orgmode; +Cc: Bastien

[-- Attachment #1: Type: text/plain, Size: 225 bytes --]

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



[-- Attachment #2: 0001-org-edit-src-Use-replace-buffer-contents.patch --]
[-- Type: text/x-patch, Size: 5016 bytes --]

From 36f01b8e9d8dc335564f8dc932caf21c43c374eb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Miquel?= <sebastien.miquel@posteo.eu>
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 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.28.0


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* 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 21:08               ` [Patch] " Sébastien Miquel
@ 2020-10-23 21:06                 ` Sébastien Miquel
  2021-05-16 12:12                   ` Bastien
  0 siblings, 1 reply; 13+ messages in thread
From: Sébastien Miquel @ 2020-10-23 21:06 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1219 bytes --]

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


[-- Attachment #2: 0001-org-edit-src-Use-replace-buffer-contents.patch --]
[-- Type: text/x-patch, Size: 5216 bytes --]

From 8788ec81130130b538ca1d2f91853c9640040506 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Miquel?= <sebastien.miquel@posteo.eu>
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 (current-buffer))
 	  (source-buffer (marker-buffer beg)))
-      (unless source-buffer (error "Source buffer disappeared.  Aborting"))
+      (unless source-buffer
+	(when write-back-buf (kill-buffer write-back-buf))
+	(error "Source buffer disappeared.  Aborting"))
       (org-src-switch-to-buffer source-buffer 'exit)
       (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)
+	 (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")))))
+    (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.29.0



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* 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 21:06                 ` Sébastien Miquel
@ 2021-05-16 12:12                   ` Bastien
  2021-05-16 17:28                     ` Sébastien Miquel
  0 siblings, 1 reply; 13+ messages in thread
From: Bastien @ 2021-05-16 12:12 UTC (permalink / raw)
  To: Sébastien Miquel; +Cc: emacs-orgmode

Hi Sébastien,

Sébastien Miquel <sebastien.miquel@posteo.eu> 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


^ permalink raw reply	[flat|nested] 13+ messages in thread

* 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 12:12                   ` Bastien
@ 2021-05-16 17:28                     ` Sébastien Miquel
  2021-05-16 19:53                       ` Bastien
  0 siblings, 1 reply; 13+ messages in thread
From: Sébastien Miquel @ 2021-05-16 17:28 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 352 bytes --]

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


[-- Attachment #2: 0001-org-src.el-org-edit-src-exit-Fix-write-back-buf-not-.patch --]
[-- Type: text/x-patch, Size: 1166 bytes --]

From f293a9d5808c413ce785646ebf5f480df3a00a2f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Miquel?= <sebastien.miquel@posteo.eu>
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


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* 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 17:28                     ` Sébastien Miquel
@ 2021-05-16 19:53                       ` Bastien
  0 siblings, 0 replies; 13+ messages in thread
From: Bastien @ 2021-05-16 19:53 UTC (permalink / raw)
  To: Sébastien Miquel; +Cc: emacs-orgmode

Hi Sébastien,

Sébastien Miquel <sebastien.miquel@posteo.eu> 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


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2021-05-16 19:54 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-02 10:22 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/)] Sébastien Miquel
2020-02-03 18:47 ` Bastien
2020-02-04  6:47   ` Sébastien Miquel
2020-02-04 16:07     ` Bastien
2020-02-09 15:26       ` Sébastien Miquel
2020-02-10  6:25         ` Bastien
2020-02-29 20:24           ` Sébastien Miquel
2020-09-04 10:31             ` Bastien
2020-10-04 21:08               ` [Patch] " Sébastien Miquel
2020-10-23 21:06                 ` Sébastien Miquel
2021-05-16 12:12                   ` Bastien
2021-05-16 17:28                     ` Sébastien Miquel
2021-05-16 19:53                       ` Bastien

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).