diff --git a/lisp/org.el b/lisp/org.el index e948d8f44..3c25c464a 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -20679,29 +20679,15 @@ which make use of the date at the cursor." (defun org-mark-subtree (&optional up) "Mark the current subtree. This puts point at the start of the current subtree, and mark at -the end. If point is in an inline task, mark that task instead. -If a numeric prefix UP is given, move up into the hierarchy of -headlines by UP levels before marking the subtree." +the end. If a numeric prefix UP is given, move up into the +hierarchy of headlines by UP levels before marking the subtree." (interactive "P") - (let ((inline-task-p - (and (featurep 'org-inlinetask) - (org-inlinetask-in-task-p))) - (beg)) - ;; Get beginning of subtree - (cond - (inline-task-p (org-inlinetask-goto-beginning)) - ((org-at-heading-p) (beginning-of-line)) - (t (org-with-limited-levels (outline-previous-visible-heading 1)))) - ;; Move up - (when up (dotimes (c (abs up)) (ignore-errors (org-element-up)))) - (setq beg (point)) - ;; Get end of it - (if inline-task-p - (org-inlinetask-goto-end) - (org-end-of-subtree)) - ;; Mark zone - (push-mark (point) nil t) - (goto-char beg))) + (when (org-with-limited-levels (org-before-first-heading-p)) + (error "Not currently in a subtree")) + (if (org-at-heading-p) (beginning-of-line) + (org-with-limited-levels (outline-previous-visible-heading 1))) + (when up (dotimes (c (abs up)) (ignore-errors (org-element-up)))) + (org-element-mark-element)) ;;; Indentation diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el index 489da2430..cb4852426 100644 --- a/testing/lisp/test-org.el +++ b/testing/lisp/test-org.el @@ -327,6 +327,36 @@ http://article.gmane.org/gmane.emacs.orgmode/21459/" (buffer-string)))))) + +;;; Mark region + +(ert-deftest test-org/mark-subtree () + "Test `org-mark-subtree' specifications." + ;; Error when point is before first headline. + (should-error + (org-test-with-temp-text "Paragraph\n* Headline\nBody" + (progn (transient-mark-mode 1) + (org-mark-subtree)))) + ;; Without argument, mark current subtree. + (should + (equal + '(12 32) + (org-test-with-temp-text "* Headline\n** Sub-headline\nBody" + (progn (transient-mark-mode 1) + (forward-line 2) + (org-mark-subtree) + (list (region-beginning) (region-end)))))) + ;; With an argument, move ARG up. + (should + (equal + '(1 32) + (org-test-with-temp-text "* Headline\n** Sub-headline\nBody" + (progn (transient-mark-mode 1) + (forward-line 2) + (org-mark-subtree 1) + (list (region-beginning) (region-end))))))) + + (provide 'test-org) ;;; test-org.el ends here