org-metaup, org-metadown: Move subtrees in active region

* lisp/org.el (org-metaup):
(org-metadown): When active region contains headings, move the
containing subtrees according to the selection.  Do not deactive
region.
* testing/lisp/test-org.el (test-org/move-subtree): Add test.
* etc/ORG-NEWS (~org-metaup~ and ~org-metadown~ now act on headings in
region): Announce the new features.
This commit is contained in:
Ihor Radchenko 2023-01-15 12:31:31 +00:00
parent 581df107b0
commit b665f8de31
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
3 changed files with 105 additions and 0 deletions

View File

@ -81,6 +81,11 @@ a date range in the agenda. It inherits from the default face in
order to remain backward-compatible. order to remain backward-compatible.
** New features ** New features
*** ~org-metaup~ and ~org-metadown~ now act on headings in region
When region is active and starts at a heading, ~org-metaup~ and
~org-metadown~ will move all the selected subtrees.
*** Datetree structure headlines can now be complex *** Datetree structure headlines can now be complex
TODO state, priority, tags, statistics cookies, and COMMENT keywords TODO state, priority, tags, statistics cookies, and COMMENT keywords

View File

@ -16899,6 +16899,30 @@ for more information."
(interactive "P") (interactive "P")
(cond (cond
((run-hook-with-args-until-success 'org-metaup-hook)) ((run-hook-with-args-until-success 'org-metaup-hook))
((and (org-region-active-p)
(org-with-limited-levels
(save-excursion
(goto-char (region-beginning))
(org-at-heading-p))))
(when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
(let ((beg (region-beginning))
(end (region-end)))
(save-excursion
(goto-char end)
(setq end (point-marker))
(goto-char beg)
(let ((level (org-current-level)))
(when (or (and (> level 1) (re-search-forward (format "^\\*\\{1,%s\\} " (1- level)) end t))
;; Search previous subtree.
(progn
(goto-char beg)
(beginning-of-line)
(not (re-search-backward (format "^\\*\\{%s\\} " level) nil t))))
(user-error "Cannot move past superior level or buffer limit"))
;; Drag first subtree above below the selected.
(while (< (point) end)
(let ((deactivate-mark nil))
(call-interactively 'org-move-subtree-down)))))))
((org-region-active-p) ((org-region-active-p)
(let* ((a (save-excursion (let* ((a (save-excursion
(goto-char (region-beginning)) (goto-char (region-beginning))
@ -16932,6 +16956,28 @@ commands for more information."
(interactive "P") (interactive "P")
(cond (cond
((run-hook-with-args-until-success 'org-metadown-hook)) ((run-hook-with-args-until-success 'org-metadown-hook))
((and (org-region-active-p)
(org-with-limited-levels
(save-excursion
(goto-char (region-beginning))
(org-at-heading-p))))
(when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
(let ((beg (region-beginning))
(end (region-end)))
(save-excursion
(goto-char beg)
(setq beg (point-marker))
(let ((level (org-current-level)))
(when (or (and (> level 1) (re-search-forward (format "^\\*\\{1,%s\\} " (1- level)) end t))
;; Search next subtree.
(progn
(goto-char end)
(not (re-search-forward (format "^\\*\\{%s\\} " level) nil t))))
(user-error "Cannot move past superior level or buffer limit"))
;; Drag first subtree below above the selected.
(while (> (point) beg)
(let ((deactivate-mark nil))
(call-interactively 'org-move-subtree-up)))))))
((org-region-active-p) ((org-region-active-p)
(let* ((a (save-excursion (let* ((a (save-excursion
(goto-char (region-beginning)) (goto-char (region-beginning))

View File

@ -5125,6 +5125,60 @@ Text.
;;; Outline structure ;;; Outline structure
(ert-deftest test-org/move-subtree ()
"Test `org-metaup' and `org-metadown' on headings."
(should
(equal "* H2\n* H1\n"
(org-test-with-temp-text "* H1<point>\n* H2\n"
(org-metadown)
(buffer-string))))
(should
(equal "* H2\n* H1\n"
(org-test-with-temp-text "* H1\n* H2<point>\n"
(org-metaup)
(buffer-string))))
(should-error
(org-test-with-temp-text "* H1\n* H2<point>\n"
(org-metadown)
(buffer-string)))
(should-error
(org-test-with-temp-text "* H1<point>\n* H2\n"
(org-metaup)
(buffer-string)))
(should-error
(org-test-with-temp-text "* H1\n** H1.2<point>\n* H2"
(org-metadown)
(buffer-string)))
(should-error
(org-test-with-temp-text "* H1\n** H1.2<point>\n"
(org-metaup)
(buffer-string)))
;; With selection
(should
(equal "* T\n** H3\n** H1\n** H2\n"
(org-test-with-temp-text "* T\n** <point>H1\n** H2\n** H3\n"
(set-mark (point))
(search-forward "H2")
(org-metadown)
(buffer-string))))
(should
(equal "* T\n** H1\n** H2\n** H0\n** H3\n"
(org-test-with-temp-text "* T\n** H0\n** <point>H1\n** H2\n** H3\n"
(set-mark (point))
(search-forward "H2")
(org-metaup)
(buffer-string))))
(should-error
(org-test-with-temp-text "* T\n** <point>H1\n** H2\n* T2\n"
(set-mark (point))
(search-forward "H2")
(org-metadown)))
(should-error
(org-test-with-temp-text "* T\n** <point>H1\n** H2\n* T2\n"
(set-mark (point))
(search-forward "H2")
(org-metaup))))
(ert-deftest test-org/demote () (ert-deftest test-org/demote ()
"Test `org-demote' specifications." "Test `org-demote' specifications."
;; Add correct number of stars according to `org-odd-levels-only'. ;; Add correct number of stars according to `org-odd-levels-only'.