Browse Source

Fix `org-insert-heading' before first headline

* lisp/org.el (org-insert-heading): Fix error when inserting
  a headline before first headline, with point not at bol.  Remove
  source block check for consistency with behavior after first
  headline.  Tiny fix to docstring.

* testing/lisp/test-org.el (test-org/meta-return): Remove unnecessary
  test (not testing specifications).
(test-org/insert-heading): New test.
Nicolas Goaziou 10 years ago
parent
commit
8cc4e09950
2 changed files with 42 additions and 15 deletions
  1. 11 7
      lisp/org.el
  2. 31 8
      testing/lisp/test-org.el

+ 11 - 7
lisp/org.el

@@ -7573,7 +7573,7 @@ on how to modify this behavior).
 
 With one universal prefix argument: If point is within a list,
 insert a heading instead of a list item.  Otherwise, set the
-value of `org-insert-heading-respect-content' to `t' for the
+value of `org-insert-heading-respect-content' to t for the
 duration of the command.
 
 With two universal prefix arguments, insert the heading at the
@@ -7602,12 +7602,16 @@ command."
 	       (or arg (not itemp))))
       ;; At beginning of buffer or so high up that only a heading
       ;; makes sense.
-      (when (and (org-before-first-heading-p) (not (bolp)))
-	(re-search-forward org-outline-regexp-bol)
-	(beginning-of-line 0))
-      (insert
-       (if (or (bobp) (org-previous-line-empty-p)) "" "\n")
-       (if (org-in-src-block-p) ",* " "* "))
+      (cond ((bolp) (insert "* "))
+	    ((not respect-content)
+	     (unless may-split (end-of-line))
+	     (insert "\n* "))
+	    ((re-search-forward org-outline-regexp-bol nil t)
+	     (beginning-of-line)
+	     (insert "* \n")
+	     (backward-char))
+	    (t (goto-char (point-max))
+	       (insert "\n* ")))
       (run-hooks 'org-insert-heading-hook))
 
      ((and itemp (not (member arg '((4) (16)))))

+ 31 - 8
testing/lisp/test-org.el

@@ -390,14 +390,6 @@
      (org-meta-return)
      (beginning-of-line)
      (looking-at "- $")))
-  ;; In a drawer and paragraph insert an empty line, in this case above.
-  (should
-   (let ((org-drawers '("MYDRAWER")))
-     (org-test-with-temp-text ":MYDRAWER:\na\n:END:"
-       (forward-line)
-       (org-meta-return)
-       (forward-line -1)
-       (looking-at "$"))))
   ;; In a drawer and item insert an item, in this case above.
   (should
    (let ((org-drawers '("MYDRAWER")))
@@ -407,6 +399,37 @@
        (beginning-of-line)
        (looking-at "- $")))))
 
+(ert-deftest test-org/insert-heading ()
+  "Test `org-insert-heading' specifications."
+  ;; FIXME: Test coverage is incomplete yet.
+  ;;
+  ;; In an empty buffer, insert a new headline.
+  (should
+   (equal "* "
+	  (org-test-with-temp-text ""
+	    (org-insert-heading)
+	    (buffer-string))))
+  ;; At the beginning of a line, turn it into a headline
+  (should
+   (equal "* P"
+	  (org-test-with-temp-text "<point>P"
+	    (org-insert-heading)
+	    (buffer-string))))
+  ;; In the middle of a line, split the line if allowed, otherwise,
+  ;; insert the headline at its end.
+  (should
+   (equal "Para\n* graph"
+	  (org-test-with-temp-text "Para<point>graph"
+	    (let ((org-M-RET-may-split-line '((default . t))))
+	      (org-insert-heading))
+	    (buffer-string))))
+  (should
+   (equal "Paragraph\n* "
+	  (org-test-with-temp-text "Para<point>graph"
+	    (let ((org-M-RET-may-split-line '((default . nil))))
+	      (org-insert-heading))
+	    (buffer-string)))))
+
 (ert-deftest test-org/insert-todo-heading-respect-content ()
   "Test `org-insert-todo-heading-respect-content' specifications."
   ;; Create a TODO heading.