all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Ihor Radchenko <yantar92@posteo.net>
To: emacs-orgmode@gnu.org
Cc: Ihor Radchenko <yantar92@posteo.net>
Subject: [PATCH 3/4] org-metaup, org-metadown: Move subtrees in active region
Date: Sun, 15 Jan 2023 12:31:31 +0000	[thread overview]
Message-ID: <8f6fcd70966c1d5434d9058affb6a03498c95c89.1673785107.git.yantar92@posteo.net> (raw)
In-Reply-To: <cover.1673785107.git.yantar92@posteo.net>

* 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.
---
 lisp/org.el              | 46 ++++++++++++++++++++++++++++++++++
 testing/lisp/test-org.el | 54 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 100 insertions(+)

diff --git a/lisp/org.el b/lisp/org.el
index 9fd8189a7..0c782769d 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -16888,6 +16888,30 @@ (defun org-metaup (&optional _arg)
   (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))
@@ -16925,6 +16949,28 @@ (defun org-metadown (&optional _arg)
   (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 7ed4ffd19..4c66fa038 100644
--- a/testing/lisp/test-org.el
+++ b/testing/lisp/test-org.el
@@ -5091,6 +5091,60 @@ (ert-deftest test-org/previous-block ()
 \f
 ;;; 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 ()
   "Test `org-demote' specifications."
   ;; Add correct number of stars according to `org-odd-levels-only'.
-- 
2.39.0



  parent reply	other threads:[~2023-01-15 12:32 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-15 12:31 [PATCH 0/4] Structure editing when region is active Ihor Radchenko
2023-01-15 12:31 ` [PATCH 1/4] Preserve active region after structure edits Ihor Radchenko
2023-01-15 12:31 ` [PATCH 2/4] Preserve active region when toggling heading state Ihor Radchenko
2023-01-15 12:31 ` Ihor Radchenko [this message]
2023-01-15 12:31 ` [PATCH 4/4] org-manual.org: Document changes in 8f6fcd709, 5719a8163, and eda9909a9 Ihor Radchenko
2023-01-16  0:24 ` [PATCH 0/4] Structure editing when region is active Samuel Wales
2023-01-16 11:17   ` Ihor Radchenko
2023-01-17  1:45     ` Samuel Wales
2023-01-17  9:46       ` Ihor Radchenko
2023-01-18  1:11         ` Samuel Wales
2023-01-18 10:14           ` Ihor Radchenko
2023-02-04 22:45             ` Samuel Wales
2023-02-05 11:11               ` Ihor Radchenko
2023-02-04 13:54 ` Ihor Radchenko
2023-02-06 21:00   ` Rudolf Adamkovič
2023-02-07 10:56     ` Ihor Radchenko
2023-02-15 18:36       ` Rudolf Adamkovič
2023-02-15 20:35         ` Ihor Radchenko
2023-02-18 21:18           ` Philipp Kiefer
2023-04-02 14:19             ` Ihor Radchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8f6fcd70966c1d5434d9058affb6a03498c95c89.1673785107.git.yantar92@posteo.net \
    --to=yantar92@posteo.net \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.