From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id +gpeLfVFk1/zGAAA0tVLHw (envelope-from ) for ; Fri, 23 Oct 2020 21:07:01 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id qOKwKPVFk1/OMgAAB5/wlQ (envelope-from ) for ; Fri, 23 Oct 2020 21:07:01 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id C6F689403EE for ; Fri, 23 Oct 2020 21:07:00 +0000 (UTC) Received: from localhost ([::1]:35418 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kW4H4-0000FQ-IO for larch@yhetil.org; Fri, 23 Oct 2020 17:06:58 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49336) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kW4GV-0000FI-Bn for emacs-orgmode@gnu.org; Fri, 23 Oct 2020 17:06:23 -0400 Received: from mout02.posteo.de ([185.67.36.66]:54705) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kW4GS-0002Aw-R3 for emacs-orgmode@gnu.org; Fri, 23 Oct 2020 17:06:23 -0400 Received: from submission (posteo.de [89.146.220.130]) by mout02.posteo.de (Postfix) with ESMTPS id EF2032400FC for ; Fri, 23 Oct 2020 23:06:16 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.eu; s=2017; t=1603487177; bh=46NkKNgtULWOXniBfaSg1aSa4YXXZ+59xXw5uxHKEas=; h=From:Subject:To:Date:From; b=GCWs2hFKhnojnwRwcf8HNq1bLh68ssM/+x18RnVhr0YLZEeCz6GKPief3cvZ1fhHp 4Vs2pLanOEDPt12Gdl3lUl9QrMODohOtgmRqb7X8oFIH7/fEZ1/OQgznPpPJLfBaGO +Bd7E7THfyOYVkExg3VX6aaKI9u3Ke2neMfA2h6IedM4A3NzonhWL1YLQuRNSsWV+n j0f6hzd5C86g0JJQdwgKbL78E/NOtnWRYDCj1z8qhYD4SFl1m3T2xqWycCeEWn9oIn qJEiXO0HLERZ+5JZ7PddKAY9g1RdP4oJLQnBIz+E8RX5bnOTSbxfoj0V1t6tnFRlYJ l8eNxwRNZpbLA== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4CHxbr2Gvlz9rxR for ; Fri, 23 Oct 2020 23:06:16 +0200 (CEST) From: =?UTF-8?Q?S=c3=a9bastien_Miquel?= Subject: 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/)] To: emacs-orgmode@gnu.org References: <603f9c4e-c621-7592-5b20-a7354c11d5d2@posteo.eu> <87zhdz1par.fsf@gnu.org> <878slijpzg.fsf@gnu.org> <4545c239-c8bf-fa16-2591-b0ee541ea732@posteo.eu> <87d0andkmi.fsf@gnu.org> <0b735431-d1e3-090d-da9f-904bd538b57f@posteo.eu> <875z8tomtx.fsf@gnu.org> <5afb1142-5167-fc44-783d-937ad2bd5584@posteo.eu> Message-ID: <3072e244-4615-aaad-4019-621bb3d1d4f8@posteo.eu> Date: Fri, 23 Oct 2020 23:06:15 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 MIME-Version: 1.0 In-Reply-To: <5afb1142-5167-fc44-783d-937ad2bd5584@posteo.eu> Content-Type: multipart/mixed; boundary="------------39B77A113C6B2A454CE8DEEA" Content-Language: fr Received-SPF: pass client-ip=185.67.36.66; envelope-from=sebastien.miquel@posteo.eu; helo=mout02.posteo.de X-detected-operating-system: by eggs.gnu.org: First seen = 2020/10/23 11:10:06 X-ACL-Warn: Detected OS = Linux 3.11 and newer [fuzzy] X-Spam_score_int: -44 X-Spam_score: -4.5 X-Spam_bar: ---- X-Spam_report: (-4.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, NICE_REPLY_A=-0.108, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: sebastien.miquel@posteo.eu Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Scanner: scn0 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=posteo.eu header.s=2017 header.b=GCWs2hFK; dmarc=pass (policy=none) header.from=posteo.eu; spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Spam-Score: -0.21 X-TUID: ZhhFYHT6XbRl This is a multi-part message in MIME format. --------------39B77A113C6B2A454CE8DEEA Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit 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 --------------39B77A113C6B2A454CE8DEEA Content-Type: text/x-patch; charset=UTF-8; name="0001-org-edit-src-Use-replace-buffer-contents.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0001-org-edit-src-Use-replace-buffer-contents.patch" >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 (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 --------------39B77A113C6B2A454CE8DEEA--