Browse Source

Fix spacing and cursor position bug caused by `org-insert-property-drawer'

* lisp/org.el (org-insert-property-drawer): Insert only after space in
current line.

This fixed a bug reported by Mathieu Boespflug, who writes:

> Hi,
>
> consider the following very simple org file:
>
> * TODO Thing1[]
>
> where "[]" marks where the point is. Now if I C-S-RET to insert a new
> TODO heading, I get
>
> * TODO Thing1
> * TODO []
>
> Note that there is a trailing space at the end of the last line, as
> expected. The problem is that if I now press C-c C-x p to set
> a property (any property), then I end up with the following situation:
>
> * TODO Thing1
> * TODO
>  :PROPERTIES:
>  :PROP:     val
>  :END: []
>
> The point is now at the end of the property drawer, rather than where it
> was before. Notice how the trailing whitespace in the heading has
> moreover been moved to after the property drawer.
>
> Expected behaviour:
>
> After C-c C-x p, I expect to get:
>
> * TODO Thing1
> * TODO []
>  :PROPERTIES:
>  :PROP:     val
>  :END:
>
> This is exactly what I get if there is *no trailing whitespace* in the
> TODO heading before hitting C-c C-x p. So it seems that the bug is that
> org-set-property does not deal well with trailing whitespace.
>
> This is a problem because it messes up setting properties by advising
> org-insert-todo-heading, say to set a CREATED property automatically for
> new TODO items.
Carsten Dominik 4 years ago
parent
commit
0d2c9eecd5
1 changed files with 3 additions and 1 deletions
  1. 3 1
      lisp/org.el

+ 3 - 1
lisp/org.el

@@ -15606,7 +15606,9 @@ formats in the current buffer."
 	(beginning-of-line 1)))
     (org-skip-over-state-notes)
     (skip-chars-backward " \t\n\r")
-    (if (eq (char-before) ?*) (forward-char 1))
+    (if (and (eq (char-before) ?*) (not (eq (char-after) ?\n)))
+	(forward-char 1))
+    (goto-char (point-at-eol))
     (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
     (beginning-of-line 0)
     (org-indent-to-column indent)