all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Leo Vivier <leo.vivier@gmail.com>
To: emacs-orgmode@gnu.org
Cc: Leo Vivier <leo.vivier@gmail.com>
Subject: [PATCH 2/2] Prevent deletion of newline added by narrowing
Date: Mon, 18 Feb 2019 01:25:47 +0100	[thread overview]
Message-ID: <20190218002547.30325-2-leo.vivier@gmail.com> (raw)
In-Reply-To: <20190218002547.30325-1-leo.vivier@gmail.com>

* 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

  reply	other threads:[~2019-02-18  0:26 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-18  0:25 [PATCH 1/2] Fix narrowing for 1-line subtrees Leo Vivier
2019-02-18  0:25 ` Leo Vivier [this message]
2019-02-18  1:02   ` [PATCH] Fix other commands for exiting narrowing Leo Vivier
2019-02-18  1:21     ` [PATCH] Fix other commands for exiting narrowing (2) Leo Vivier
2019-02-18 17:18       ` [PATCH] Fix problems Leo Vivier
2019-02-19 10:01 ` [PATCH 1/2] Fix narrowing for 1-line subtrees Nicolas Goaziou
2019-02-19 10:24   ` Leo Vivier
2019-02-19 10:35     ` [PATCH] Fix narrowing for 1-line subtrees (squashed) Leo Vivier
2019-02-19 12:05     ` [PATCH 1/2] Fix narrowing for 1-line subtrees Nicolas Goaziou
2019-02-19 13:37       ` Leo Vivier
2019-02-19 15:28         ` Leo Vivier
2019-02-19 15:40           ` Leo Vivier
2019-02-20 13:25             ` Nicolas Goaziou
2019-02-20 13:36               ` Leo Vivier
2019-02-21 15:38                 ` [PATCH] Fix narrowed " Leo Vivier
2019-02-21 15:41                   ` Leo Vivier
2019-02-21 15:43                     ` [PATCH] Fix spaces with `org-remove-timestamp-with-keyword' Leo Vivier
2019-02-22 20:23                     ` [PATCH] Fix narrowed 1-line subtrees Leo Vivier
2019-02-22 20:54                       ` Leo Vivier
2019-02-22 21:53                         ` Samuel Wales
2019-02-22 22:04                           ` Leo Vivier
2019-02-22 23:58                             ` Samuel Wales
2019-02-23 10:43                               ` Leo Vivier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190218002547.30325-2-leo.vivier@gmail.com \
    --to=leo.vivier@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.