From b665f8de31369a0344048bab9692dc2f6b936a74 Mon Sep 17 00:00:00 2001 From: Ihor Radchenko Date: Sun, 15 Jan 2023 12:31:31 +0000 Subject: [PATCH] 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. --- etc/ORG-NEWS | 5 ++++ lisp/org.el | 46 ++++++++++++++++++++++++++++++++++ testing/lisp/test-org.el | 54 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index 40477e73f..4752af373 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -81,6 +81,11 @@ a date range in the agenda. It inherits from the default face in order to remain backward-compatible. ** 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 TODO state, priority, tags, statistics cookies, and COMMENT keywords diff --git a/lisp/org.el b/lisp/org.el index b55bb608c..e68be8f81 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -16899,6 +16899,30 @@ for more information." (interactive "P") (cond ((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) (let* ((a (save-excursion (goto-char (region-beginning)) @@ -16932,6 +16956,28 @@ commands for more information." (interactive "P") (cond ((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) (let* ((a (save-excursion (goto-char (region-beginning)) diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el index 36c5a878e..d7c801fe5 100644 --- a/testing/lisp/test-org.el +++ b/testing/lisp/test-org.el @@ -5125,6 +5125,60 @@ Text. ;;; 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\n* H2\n" + (org-metadown) + (buffer-string)))) + (should + (equal "* H2\n* H1\n" + (org-test-with-temp-text "* H1\n* H2\n" + (org-metaup) + (buffer-string)))) + (should-error + (org-test-with-temp-text "* H1\n* H2\n" + (org-metadown) + (buffer-string))) + (should-error + (org-test-with-temp-text "* H1\n* H2\n" + (org-metaup) + (buffer-string))) + (should-error + (org-test-with-temp-text "* H1\n** H1.2\n* H2" + (org-metadown) + (buffer-string))) + (should-error + (org-test-with-temp-text "* H1\n** H1.2\n" + (org-metaup) + (buffer-string))) + ;; With selection + (should + (equal "* T\n** H3\n** H1\n** H2\n" + (org-test-with-temp-text "* T\n** 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** 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** H1\n** H2\n* T2\n" + (set-mark (point)) + (search-forward "H2") + (org-metadown))) + (should-error + (org-test-with-temp-text "* T\n** H1\n** H2\n* T2\n" + (set-mark (point)) + (search-forward "H2") + (org-metaup)))) + (ert-deftest test-org/demote () "Test `org-demote' specifications." ;; Add correct number of stars according to `org-odd-levels-only'.