From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id IAtPDXnn1mBbHQEAgWs5BA (envelope-from ) for ; Sat, 26 Jun 2021 10:38:17 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id yDbzCHnn1mCfOAAAbx9fmQ (envelope-from ) for ; Sat, 26 Jun 2021 08:38:17 +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 41224AA3E for ; Sat, 26 Jun 2021 10:38:16 +0200 (CEST) Received: from localhost ([::1]:54394 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lx3pO-0003X6-U0 for larch@yhetil.org; Sat, 26 Jun 2021 04:38:14 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33746) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lx3oq-0003Wx-SN for emacs-orgmode@gnu.org; Sat, 26 Jun 2021 04:37:40 -0400 Received: from mout02.posteo.de ([185.67.36.66]:56821) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lx3ok-00037Z-V1 for emacs-orgmode@gnu.org; Sat, 26 Jun 2021 04:37:40 -0400 Received: from submission (posteo.de [89.146.220.130]) by mout02.posteo.de (Postfix) with ESMTPS id E43082400FC for ; Sat, 26 Jun 2021 10:37:29 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.eu; s=2017; t=1624696649; bh=TzCTXCc3qQBCq0UVGGPXLzEyHb8ItMMoz9higgKqdDo=; h=Subject:To:Cc:From:Date:From; b=H9ziJ1SI5KzTPGDKwGOMG9XlX3I9Njswg1MIYJdjX8lp4JaT5BlpDZcmnmG+yG9AN kzU/TL76yNqHsP7WpVBBYzMJJ7/cs1+KSIJIsaEc39PkFmT8bCBCb6AA4YseWcI8YU 6m08JgIB3zJgd2sVtmFKWzu3ELdEJK092OQ7NaFT0DBjA8cFQP8vkEIz0wWkvc8WeU 0lNX3qVq5/VD23Is6XbjipdNvb625wqw0CjsyCFI8DJZpfDEyNpDx+9k+i4BGHt/pi qE111irZ4Yn6tKR+wROc3eE6fFKeJd0r5gbGADtd9ykI8jnUTEOMqKYAewRS1V1MYL 5xN0qgSElJMjQ== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4GBnLK2Fg8z6tmL; Sat, 26 Jun 2021 10:37:29 +0200 (CEST) Subject: Re: [PATCH] extra space at the end of lines in source To: Greg Minshall References: <2913163.1624524931@apollo2.minshall.org> From: =?UTF-8?Q?S=c3=a9bastien_Miquel?= Message-ID: <7fee566f-4f9e-82dd-4f1b-a5ff9dca89ab@posteo.eu> Date: Sat, 26 Jun 2021 08:37:28 +0000 MIME-Version: 1.0 In-Reply-To: <2913163.1624524931@apollo2.minshall.org> Content-Type: multipart/mixed; boundary="------------C550E5DB2C18EE466FDFF7EE" Content-Language: fr Received-SPF: pass client-ip=185.67.36.66; envelope-from=sebastien.miquel@posteo.eu; helo=mout02.posteo.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 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, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H4=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 Cc: org-mode-email Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1624696696; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=kitomWXkdhOSXVoCPSctEr+EyP2yPuw4JuqPKw0qa20=; b=o20XoGnRyitptZOiGiIVQlEJ/OvH/7/AZgtHSwZLdMF9wZD3Hvf+yShUa9Cv18EVOCPbUU gmz8sKzH1r6N1T3ryyXqNXw6H5bEjhN8cRmwi+NjyxN3/aRrKgaz+tXn2CQvrTxDY7+HuY b53oh5+Iid45uCamiw68ZRHAHjAfQv5mZlTylcg9N+Rha2NMfDFOPKWjMRCIpmCZlRUh0M sF/R24saTIN6PJ2C7dBVFwrqe2xjtNxd4EGAsDvzeixgPQMq6Yguqe83oqkdfdjJu7T7i3 LMEj2PG0Qcm4CtillxEMdhxPPASjhPAv8/Rr1EPvuzF2SB1RRD3SgbGTPfMKww== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1624696696; a=rsa-sha256; cv=none; b=PzxgYfuNZIB5Pyi70n4XXPwOiVYXDSsUt6hkXABjeE6lPGOE5DkiiTsYq2NsFi/C77Q3ox mv54TDvUS1F+/Rw8+DO7KDmPpninlLMOYr310iLmC+qwmgERJBQUzvL8gTPi71FecOqFvi AIARBFqZJ+kY2dVJHPbh083ICxGuMTdmP7Vgeds2p6sN6mNV5I1unCyeIUe0Hnmok7vEua aZ8tlQWZmN9vjGKnHOlUCzvGwO+9o9kCpPEcx8NjmIB/Ox0463jNcTgkOrYvUQbIumV+// tAeW9igz167V4xrdiroLRiprr1iko9Xn2sy29JKF7fHrDV4Vy3d5MA95XY0GMQ== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=posteo.eu header.s=2017 header.b=H9ziJ1SI; 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-Migadu-Spam-Score: -3.13 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=posteo.eu header.s=2017 header.b=H9ziJ1SI; 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-Migadu-Queue-Id: 41224AA3E X-Spam-Score: -3.13 X-Migadu-Scanner: scn1.migadu.com X-TUID: ER2hqX/urMir This is a multi-part message in MIME format. --------------C550E5DB2C18EE466FDFF7EE Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Greg Minshall writes: > thanks. my trivial test shows this works*except* in the particular > case where, when closing the Org Src buffer, `point` is on an empty > line. in this case, that one empty line is given extra spaces. Yes, I was aware of this, but didn't think we could do better in this case. I've thought about it some more, and came up with a solution. Here's a new patch that's smarter about indenting the current line, and resolves this remaining issue. Regards, -- Sébastien Miquel --------------C550E5DB2C18EE466FDFF7EE Content-Type: text/x-patch; charset=UTF-8; name="0001-org-src.el-Do-not-indent-blank-lines-except-current-.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-org-src.el-Do-not-indent-blank-lines-except-current-.pa"; filename*1="tch" >From 8c31e6b732a45c0ddbbf0a0db7a2cb4ef3af0414 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Miquel?= Date: Wed, 23 Jun 2021 15:25:58 +0200 Subject: [PATCH] org-src.el: Do not indent blank lines, except current one * lisp/org-src.el (org-src--contents-for-write-back): Do not indent blank lines, except for the current line maybe. (org-src--preserve-blank-line): New variable, whether to preserve indentation of the current blank line. (org-src--edit-element): Set `org-src--preserve-blank-line'. * lisp/org.el (org-indent-line): When tab acts natively, do some preindentation, which signals `org-src--edit-element' to preserve the indentation of current blank line. Removing all the whitespace was the original behaviour for all blank lines, before `857ae366b3`. --- lisp/org-src.el | 34 +++++++++++++++++++++++++++------- lisp/org.el | 15 +++++++++++++++ 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/lisp/org-src.el b/lisp/org-src.el index 79f002e56..c6311adbb 100644 --- a/lisp/org-src.el +++ b/lisp/org-src.el @@ -299,6 +299,9 @@ is 0.") "File name associated to Org source buffer, or nil.") (put 'org-src-source-file-name 'permanent-local t) +(defvar-local org-src--preserve-blank-line nil) +(put 'org-src--preserve-blank-line 'permanent-local t) + (defun org-src--construct-edit-buffer-name (org-buffer-name lang) "Construct the buffer name for a source editing buffer." (concat "*Org Src " org-buffer-name "[ " lang " ]*")) @@ -443,14 +446,21 @@ Assume point is in the corresponding edit buffer." 0)))) (use-tabs? (and (> org-src--tab-width 0) t)) (source-tab-width org-src--tab-width) - (contents (org-with-wide-buffer (buffer-string))) - (write-back org-src--allow-write-back)) + (contents (org-with-wide-buffer + (let ((eol (line-end-position))) + (list (buffer-substring (point-min) eol) + (buffer-substring eol (point-max)))))) + (write-back org-src--allow-write-back) + (preserve-blank-line org-src--preserve-blank-line) + marker) (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)) ;; Apply WRITE-BACK function on edit buffer contents. - (insert (org-no-properties contents)) + (insert (org-no-properties (car contents))) + (setq marker (point-marker)) + (insert (org-no-properties (car (cdr contents)))) (goto-char (point-min)) (when (functionp write-back) (save-excursion (funcall write-back))) ;; Add INDENTATION-OFFSET to every line in buffer, @@ -458,10 +468,14 @@ Assume point is in the corresponding edit buffer." (when (> indentation-offset 0) (while (not (eobp)) (skip-chars-forward " \t") - (let ((i (current-column))) - (delete-region (line-beginning-position) (point)) - (indent-to (+ i indentation-offset))) - (forward-line)))))) + (when (or (not (eolp)) ; not a blank line + (and (eq (point) (marker-position marker)) ; current line + preserve-blank-line)) + (let ((i (current-column))) + (delete-region (line-beginning-position) (point)) + (indent-to (+ i indentation-offset)))) + (forward-line))) + (set-marker marker nil)))) (defun org-src--edit-element (datum name &optional initialize write-back contents remote) @@ -506,6 +520,11 @@ Leave point in edit buffer." (block-ind (org-with-point-at (org-element-property :begin datum) (current-indentation))) (content-ind org-edit-src-content-indentation) + (blank-line (save-excursion (beginning-of-line) + (looking-at-p "^[[:space:]]*$"))) + (empty-line (and blank-line (looking-at-p "^$"))) + (preserve-blank-line (or (and blank-line (not empty-line)) + (and empty-line (= (+ block-ind content-ind) 0)))) (preserve-ind (and (memq type '(example-block src-block)) (or (org-element-property :preserve-indent datum) @@ -554,6 +573,7 @@ Leave point in edit buffer." (setq org-src--overlay overlay) (setq org-src--allow-write-back write-back) (setq org-src-source-file-name source-file-name) + (setq org-src--preserve-blank-line preserve-blank-line) ;; Start minor mode. (org-src-mode) ;; Clear undo information so we cannot undo back to the diff --git a/lisp/org.el b/lisp/org.el index 1bd9e02eb..3bc4d0383 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -19164,6 +19164,21 @@ Also align node properties according to `org-property-format'." (org-with-point-at (org-element-property :end element) (skip-chars-backward " \t\n") (line-beginning-position)))) + ;; At the beginning of a blank line, do some preindentation. This + ;; signals org-src--edit-element to preserve the indentation on exit + (when (and (looking-at-p "^[[:space:]]*$") + (not org-src-preserve-indentation)) + (let ((element (org-element-at-point)) + block-content-ind some-ind) + (org-with-point-at (org-element-property :begin element) + (setq block-content-ind (+ (current-indentation) + org-edit-src-content-indentation)) + (forward-line) + (save-match-data (re-search-forward "^[ \t]*\\S-" nil t)) + (backward-char) + (setq some-ind (if (looking-at-p "#\\+end_src") + block-content-ind (current-indentation)))) + (indent-line-to (min block-content-ind some-ind)))) (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB"))) (t (let ((column (org--get-expected-indentation element nil))) -- 2.32.0 --------------C550E5DB2C18EE466FDFF7EE--