diff options
author | Carsten Dominik <carsten.dominik@gmail.com> | 2013-09-01 14:04:48 +0200 |
---|---|---|
committer | Carsten Dominik <carsten.dominik@gmail.com> | 2013-09-01 14:04:48 +0200 |
commit | b8c85a13bbfaa369874ee618bfaf218fb21f56f6 (patch) | |
tree | 4dc4b9b279b70743c69f85d86f2b76309ece9d76 | |
parent | 2c38c3acc0b5d1cb3bb26f45829d711bb6f4009c (diff) | |
download | org-mode-b8c85a13bbfaa369874ee618bfaf218fb21f56f6.tar.gz |
Improve the empty line insertion behavior of `org-insert-heading'.
* lisp/org.el (org-insert-heading): Improve the empty line insertion behavior.
Basically, never remove empty lines, only add them.
This was a request by Nicolas Goaziou.
-rw-r--r-- | lisp/org.el | 29 |
1 files changed, 19 insertions, 10 deletions
diff --git a/lisp/org.el b/lisp/org.el index 97b5365..a3e9f3e 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -7564,7 +7564,7 @@ This is important for non-interactive uses of the command." (or arg (not itemp)))) ;; At beginning of buffer or so hight up that only a heading makes sense. (insert - (if (org-previous-line-empty-p) "" "\n") + (if (or (bobp) (org-previous-line-empty-p)) "" "\n") (if (org-in-src-block-p) ",* " "* ")) (run-hooks 'org-insert-heading-hook)) @@ -7576,13 +7576,17 @@ This is important for non-interactive uses of the command." ;; Insert a heading (save-restriction (widen) - (let* ((empty-line-p nil) - (level nil) + (let* ((level nil) (on-heading (org-at-heading-p)) - ;; Get a level to fall back on + (empty-line-p (if on-heading + (org-previous-line-empty-p) + ;; We will decide later + nil)) + ;; Get a level string to fall back on (fix-level (save-excursion (org-back-to-heading t) + (if (org-previous-line-empty-p) (setq empty-line-p t)) (looking-at org-outline-regexp) (make-string (1- (length (match-string 0))) ?*))) (stars @@ -7603,10 +7607,12 @@ This is important for non-interactive uses of the command." (error "This should not happen"))) (unless (and (save-excursion (save-match-data - (org-backward-heading-same-level 1 invisible-ok)) + (org-backward-heading-same-level + 1 invisible-ok)) (= (point) (match-beginning 0))) (not (org-previous-line-empty-p t))) - (setq empty-line-p (org-previous-line-empty-p))) + (setq empty-line-p (or empty-line-p + (org-previous-line-empty-p)))) (match-string 0)) (error (or fix-level "* "))))) (blank-a (cdr (assq 'heading org-blank-before-new-entry))) @@ -7640,9 +7646,10 @@ This is important for non-interactive uses of the command." (setq initial-content (org-trim initial-content))) (goto-char pos)) ;; a normal line - (setq initial-content (buffer-substring (point) (point-at-eol))) - (delete-region (point) (point-at-eol)) - (setq initial-content (org-trim initial-content)))) + (unless (bolp) + (setq initial-content (buffer-substring (point) (point-at-eol))) + (delete-region (point) (point-at-eol)) + (setq initial-content (org-trim initial-content))))) ;; If we are at the beginning of the line, insert before it. Else after (cond @@ -7657,7 +7664,9 @@ This is important for non-interactive uses of the command." (insert stars) (just-one-space) (insert initial-content) - (if adjust-empty-lines (org-N-empty-lines-before-current (if empty-line-p 1 0))) + (if adjust-empty-lines + (if (and blank (not (org-previous-line-empty-p))) + (org-N-empty-lines-before-current (if blank 1 0)))) (run-hooks 'org-insert-heading-hook))))))) (defun org-N-empty-lines-before-current (N) |