org-element: Small refactoring

* contrib/lisp/org-element.el (org-element-swap-A-B,
  org-element-drag-backward, org-element-drag-forward): Small
  refactoring.
* testing/lisp/test-org-element.el: Add tests.
This commit is contained in:
Nicolas Goaziou 2012-03-22 19:32:09 +01:00
parent 5e950a9e56
commit 1581b16c78
2 changed files with 66 additions and 24 deletions

View File

@ -3637,27 +3637,25 @@ in-between, if any, are siblings of the element at point."
(defun org-element-swap-A-B (elem-A elem-B)
"Swap elements ELEM-A and ELEM-B.
Leave point at the end of ELEM-A.
Assume ELEM-A is before ELEM-B and that they are not nested."
Leave point at the end of ELEM-A."
(goto-char (org-element-property :begin elem-A))
(let* ((beg-B (org-element-property :begin elem-B))
(end-B-no-blank (save-excursion
(goto-char (org-element-property :end elem-B))
(skip-chars-backward " \r\t\n")
(forward-line)
(point)))
(beg-A (org-element-property :begin elem-A))
(end-A-no-blank (save-excursion
(goto-char (org-element-property :end elem-A))
(skip-chars-backward " \r\t\n")
(forward-line)
(point)))
(body-A (buffer-substring beg-A end-A-no-blank))
(body-B (buffer-substring beg-B end-B-no-blank))
(between-A-B (buffer-substring end-A-no-blank beg-B)))
(delete-region beg-A end-B-no-blank)
(insert body-B between-A-B body-A)
(let* ((beg-A (org-element-property :begin elem-A))
(end-A (save-excursion
(goto-char (org-element-property :end elem-A))
(skip-chars-backward " \r\t\n")
(point-at-eol)))
(beg-B (org-element-property :begin elem-B))
(end-B (save-excursion
(goto-char (org-element-property :end elem-B))
(skip-chars-backward " \r\t\n")
(point-at-eol)))
(body-A (buffer-substring beg-A end-A))
(body-B (delete-and-extract-region beg-B end-B)))
(goto-char beg-B)
(insert body-A)
(goto-char beg-A)
(delete-region beg-A end-A)
(insert body-B)
(goto-char (org-element-property :end elem-B))))
(defun org-element-backward ()
@ -3706,8 +3704,8 @@ Move to the previous element at the same level, when possible."
(org-element-backward)
(let ((prev-elem (org-element-at-point)))
(when (or (org-element-nested-p elem prev-elem)
(and (eq (car elem) 'headline)
(not (eq (car prev-elem) 'headline))))
(and (eq (org-element-type elem) 'headline)
(not (eq (org-element-type prev-elem) 'headline))))
(goto-char pos)
(error "Cannot drag element backward"))
;; Compute new position of point: it's shifted by PREV-ELEM
@ -3727,8 +3725,8 @@ Move to the previous element at the same level, when possible."
(goto-char (org-element-property :end elem))
(let ((next-elem (org-element-at-point)))
(when (or (org-element-nested-p elem next-elem)
(and (eq (car next-elem) 'headline)
(not (eq (car elem) 'headline))))
(and (eq (org-element-type next-elem) 'headline)
(not (eq (org-element-type elem) 'headline))))
(goto-char pos)
(error "Cannot drag element forward"))
;; Compute new position of point: it's shifted by NEXT-ELEM

View File

@ -431,6 +431,50 @@ Outside."
(org-element-down)
(should (looking-at "Paragraph"))))
(ert-deftest test-org-element/drag-backward ()
"Test `org-element-drag-backward' specifications."
;; 1. Error when trying to move first element of buffer.
(org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
(should-error (org-element-drag-backward)))
;; 2. Error when trying to swap nested elements.
(org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
(forward-line)
(should-error (org-element-drag-backward)))
;; 3. Error when trying to swap an headline element and
;; a non-headline element.
(org-test-with-temp-text "Test.\n* Head 1"
(forward-line)
(should-error (org-element-drag-backward)))
;; 4. Otherwise, swap elements, preserving column and blank lines
;; between elements.
(org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
(search-forward "graph")
(org-element-drag-backward)
(should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
(should (looking-at " 2"))))
(ert-deftest test-org-element/drag-forward ()
"Test `org-element-drag-forward' specifications."
;; 1. Error when trying to move first element of buffer.
(org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
(goto-line 3)
(should-error (org-element-drag-forward)))
;; 2. Error when trying to swap nested elements.
(org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
(forward-line)
(should-error (org-element-drag-forward)))
;; 3. Error when trying to swap a non-headline element and an
;; headline.
(org-test-with-temp-text "Test.\n* Head 1"
(should-error (org-element-drag-forward)))
;; 4. Otherwise, swap elements, preserving column and blank lines
;; between elements.
(org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
(search-forward "graph")
(org-element-drag-forward)
(should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
(should (looking-at " 1"))))
(provide 'test-org-element)
;;; test-org-element.el ends here