org.el (org-insert-heading): DTRT when in a narrowed region

* org.el (org-insert-heading): DTRT when in a narrowed region.

* org-compat.el (org-buffer-narrowed-p): New compatibility
function.

Thanks to Samuel Wales for reporting this problem.
This commit is contained in:
Bastien Guerry 2013-03-13 10:57:49 +01:00
parent d40c5804f5
commit bc7dbcab9f
2 changed files with 40 additions and 30 deletions

View File

@ -499,6 +499,13 @@ Implements `file-equal-p' for older emacsen and XEmacs."
(setq f2-attr (file-attributes (file-truename f2)))
(equal f1-attr f2-attr)))))
;; `buffer-narrowed-p' is available for Emacs >=24.3
(defun org-buffer-narrowed-p ()
"Compatibility function for `buffer-narrowed-p'."
(if (fboundp 'buffer-narrowed-p)
(buffer-narrowed-p)
(/= (- (point-max) (point-min)) (buffer-size))))
(defmacro org-with-silent-modifications (&rest body)
(if (fboundp 'with-silent-modifications)
`(with-silent-modifications ,@body)

View File

@ -7386,22 +7386,25 @@ and create a new headline with the text in the current line after point
When INVISIBLE-OK is set, stop at invisible headlines when going back.
This is important for non-interactive uses of the command."
(interactive "P")
(if (or (= (buffer-size) 0)
(cond
((or (= (buffer-size) 0)
(and (not (save-excursion
(and (ignore-errors (org-back-to-heading invisible-ok))
(org-at-heading-p))))
(or arg (not (org-in-item-p)))))
(progn
(insert
(if (org-previous-line-empty-p) "" "\n")
(if (org-in-src-block-p) ",* " "* "))
(run-hooks 'org-insert-heading-hook))
(when (or arg
(not (org-insert-item
((or arg (not (org-insert-item
(save-excursion
(beginning-of-line)
(looking-at org-list-full-item-re)
(match-string 3)))))
(let (begn endn)
(when (org-buffer-narrowed-p)
(setq begn (point-min) endn (point-max))
(widen))
(let* ((empty-line-p nil)
(eops (equal arg '(16))) ; insert at end of parent subtree
(org-insert-heading-respect-content
@ -7434,14 +7437,12 @@ This is important for non-interactive uses of the command."
(blank-a (cdr (assq 'heading org-blank-before-new-entry)))
(blank (if (eq blank-a 'auto) empty-line-p blank-a))
pos hide-previous previous-pos)
(cond
;; At the beginning of a heading, open a new line for insertiong
((and (bolp) (org-at-heading-p)
(if ;; At the beginning of a heading, open a new line for insertiong
(and (bolp) (org-at-heading-p)
(not eops)
(or (bobp)
(save-excursion (backward-char 1) (not (outline-invisible-p)))))
(open-line (if blank 2 1)))
(t
(open-line (if blank 2 1))
(save-excursion
(setq previous-pos (point-at-bol))
(end-of-line)
@ -7511,7 +7512,7 @@ This is important for non-interactive uses of the command."
(org-set-tags nil 'align))))
(t
(or split (end-of-line 1))
(newline (if blank 2 1)))))))
(newline (if blank 2 1))))))
(insert head) (just-one-space)
(setq pos (point))
(end-of-line 1)
@ -7520,7 +7521,9 @@ This is important for non-interactive uses of the command."
(save-excursion
(goto-char previous-pos)
(hide-subtree)))
(run-hooks 'org-insert-heading-hook)))))
(when (and begn endn)
(narrow-to-region (min (point) begn) (max (point) endn)))
(run-hooks 'org-insert-heading-hook))))))
(defun org-get-heading (&optional no-tags no-todo)
"Return the heading of the current entry, without the stars.