Function: org-element-swap-A-B
org-element-swap-A-B is a byte-compiled function defined in
org-element.el.gz.
Signature
(org-element-swap-A-B ELEM-A ELEM-B)
Documentation
Swap elements ELEM-A and ELEM-B.
Assume ELEM-B is after ELEM-A in the buffer. Leave point at the end of ELEM-A.
Source Code
;; Defined in /usr/src/emacs/lisp/org/org-element.el.gz
(defun org-element-swap-A-B (elem-A elem-B)
"Swap elements ELEM-A and ELEM-B.
Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
end of ELEM-A."
(goto-char (org-element-begin elem-A))
;; There are two special cases when an element doesn't start at bol:
;; the first paragraph in an item or in a footnote definition.
(let ((specialp (not (bolp))))
;; Only a paragraph without any affiliated keyword can be moved at
;; ELEM-A position in such a situation. Note that the case of
;; a footnote definition is impossible: it cannot contain two
;; paragraphs in a row because it cannot contain a blank line.
(when (and specialp
(or (not (org-element-type-p elem-B 'paragraph))
(/= (org-element-begin elem-B)
(org-element-contents-begin elem-B))))
(error "Cannot swap elements"))
;; Preserve folding state when `org-fold-core-style' is set to
;; `text-properties'.
(org-fold-core-ignore-modifications
;; In a special situation, ELEM-A will have no indentation. We'll
;; give it ELEM-B's (which will in, in turn, have no indentation).
(let* ((ind-B (when specialp
(goto-char (org-element-begin elem-B))
(current-indentation)))
(beg-A (org-element-begin elem-A))
(end-A (save-excursion
(goto-char (org-element-end elem-A))
(skip-chars-backward " \r\t\n")
(line-end-position)))
(beg-B (org-element-begin elem-B))
(end-B (save-excursion
(goto-char (org-element-end elem-B))
(skip-chars-backward " \r\t\n")
(line-end-position)))
;; Store inner folds responsible for visibility status.
(folds
(cons
(org-fold-core-get-regions :from beg-A :to end-A :relative t)
(org-fold-core-get-regions :from beg-B :to end-B :relative t)))
;; Get contents.
(body-A (buffer-substring beg-A end-A))
(body-B (buffer-substring beg-B end-B)))
;; Clear up the folds.
(org-fold-region beg-A end-A nil)
(org-fold-region beg-B end-B nil)
(delete-region beg-B end-B)
(goto-char beg-B)
(when specialp
(setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
(indent-to-column ind-B))
(insert body-A)
;; Restore ex ELEM-A folds.
(org-fold-core-regions (car folds) :relative beg-B)
(goto-char beg-A)
(delete-region beg-A end-A)
(insert body-B)
;; Restore ex ELEM-A folds.
(org-fold-core-regions (cdr folds) :relative beg-A)
(goto-char (org-element-end elem-B))))))