From mboxrd@z Thu Jan 1 00:00:00 1970 From: Leo Vivier Subject: [PATCH 2/2] Prevent deletion of newline added by narrowing Date: Mon, 18 Feb 2019 01:25:47 +0100 Message-ID: <20190218002547.30325-2-leo.vivier@gmail.com> References: <20190218002547.30325-1-leo.vivier@gmail.com> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Return-path: Received: from eggs.gnu.org ([209.51.188.92]:53398) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gvWl1-00021t-7x for emacs-orgmode@gnu.org; Sun, 17 Feb 2019 19:26:04 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gvWkz-0006s7-Li for emacs-orgmode@gnu.org; Sun, 17 Feb 2019 19:26:02 -0500 Received: from mail-wr1-x42d.google.com ([2a00:1450:4864:20::42d]:33319) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gvWkz-0006rF-CH for emacs-orgmode@gnu.org; Sun, 17 Feb 2019 19:26:01 -0500 Received: by mail-wr1-x42d.google.com with SMTP id i12so16490194wrw.0 for ; Sun, 17 Feb 2019 16:26:01 -0800 (PST) In-Reply-To: <20190218002547.30325-1-leo.vivier@gmail.com> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: emacs-orgmode@gnu.org Cc: Leo Vivier * lisp/org.el (org-delete-backward-char): Prevent deletion of newline added by narrowing (org-delete-char): Prevent deletion of newline added by narrowing (org-kill-line): Prevent deletion of newline added by narrowing (org-kill-region): Create wrapper for `kill-region' to prevent deletion of newline added by narrowing * lisp/org-keys.el (org-remap): Remap `kill-region' to `org-kill-region' This ensures that the newline added by the narrowing commands cannot be deleted by the user. It does so by having every interactive deletion command check whether it would delete the last newline of a narrowed buffer. If it would, the new command deletes whatever the original command normally would but keep the last newline. If the original command would have resulted in a movement, e.g. `org-delete-backward-char', the new command also moves the point as if the last newline had been deleted. --- lisp/org-keys.el | 1 + lisp/org.el | 28 ++++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/lisp/org-keys.el b/lisp/org-keys.el index 90e8139b0..26a3852b3 100644 --- a/lisp/org-keys.el +++ b/lisp/org-keys.el @@ -532,6 +532,7 @@ COMMANDS is a list of alternating OLDDEF NEWDEF command names." 'delete-char 'org-delete-char 'delete-backward-char 'org-delete-backward-char 'kill-line 'org-kill-line + 'kill-region 'org-kill-region 'widen 'org-widen 'open-line 'org-open-line 'yank 'org-yank diff --git a/lisp/org.el b/lisp/org.el index 3110f14ba..02130ab6a 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -18851,7 +18851,11 @@ because, in this case the deletion might narrow the column." (looking-at-p ".*?|") (org-at-table-p)) (progn (forward-char -1) (org-delete-char 1)) - (backward-delete-char N) + (if (and (eobp) + (save-excursion (forward-char -1) + (looking-at "\n"))) + (forward-char -1) + (backward-delete-char N)) (org-fix-tags-on-the-fly)))) (defun org-delete-char (N) @@ -18868,7 +18872,9 @@ because, in this case the deletion might narrow the column." (eq (char-after) ?|) (save-excursion (skip-chars-backward " \t") (bolp)) (not (org-at-table-p))) - (delete-char N) + (unless (and (save-excursion (forward-char) (eobp)) + (looking-at "\n")) + (delete-char N)) (org-fix-tags-on-the-fly)) ((looking-at ".\\(.*?\\)|") (let* ((update? org-table-may-need-update) @@ -22301,8 +22307,12 @@ depending on context." (user-error (substitute-command-keys "`\\[org-kill-line]' aborted as it would kill a hidden subtree"))) - (call-interactively - (if (bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line))) + (unless (and (looking-at-p "\n") + (save-excursion + (forward-char 1) + (eobp))) + (call-interactively + (if (bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))) ((org-match-line org-tag-line-re) (let ((end (save-excursion (goto-char (match-beginning 1)) @@ -22314,6 +22324,16 @@ depending on context." (org-align-tags)) (t (kill-region (point) (line-end-position))))) +(defun org-kill-region (beg end &optional region) + (interactive (list (mark) (point) 'region)) + (kill-region + beg + end + region) + (save-excursion + (when (eobp) + (insert "\n")))) + (defun org-yank (&optional arg) "Yank. If the kill is a subtree, treat it specially. This command will look at the current kill and check if is a single -- 2.20.1