Function: org-list-send-item
org-list-send-item is a byte-compiled function defined in
org-list.el.gz.
Signature
(org-list-send-item ITEM DEST STRUCT)
Documentation
Send ITEM to destination DEST.
STRUCT is the list structure.
DEST can have various values.
If DEST is a buffer position, the function will assume it points to another item in the same list as ITEM, and will move the latter just before the former.
If DEST is begin (respectively end), ITEM will be moved at
the beginning (respectively end) of the list it belongs to.
If DEST is a string like "N", where N is an integer, ITEM will be moved at the Nth position in the list.
If DEST is kill, ITEM will be deleted and its body will be
added to the kill-ring.
If DEST is delete, ITEM will be deleted.
Visibility of item is preserved.
This function returns, destructively, the new list structure.
Source Code
;; Defined in /usr/src/emacs/lisp/org/org-list.el.gz
(defun org-list-send-item (item dest struct)
"Send ITEM to destination DEST.
STRUCT is the list structure.
DEST can have various values.
If DEST is a buffer position, the function will assume it points
to another item in the same list as ITEM, and will move the
latter just before the former.
If DEST is `begin' (respectively `end'), ITEM will be moved at
the beginning (respectively end) of the list it belongs to.
If DEST is a string like \"N\", where N is an integer, ITEM will
be moved at the Nth position in the list.
If DEST is `kill', ITEM will be deleted and its body will be
added to the kill-ring.
If DEST is `delete', ITEM will be deleted.
Visibility of item is preserved.
This function returns, destructively, the new list structure."
(let* ((prevs (org-list-prevs-alist struct))
(item-end (org-list-get-item-end item struct))
;; Grab full item body minus its bullet.
(body (org-trim
(buffer-substring
(save-excursion
(goto-char item)
(looking-at
(concat "[ \t]*"
(regexp-quote (org-list-get-bullet item struct))))
(match-end 0))
item-end)))
;; Change DEST into a buffer position. A trick is needed
;; when ITEM is meant to be sent at the end of the list.
;; Indeed, by setting locally `org-M-RET-may-split-line' to
;; nil and insertion point (INS-POINT) to the first line's
;; end of the last item, we ensure the new item will be
;; inserted after the last item, and not after any of its
;; hypothetical sub-items.
(ins-point (cond
((or (eq dest 'kill) (eq dest 'delete)))
((eq dest 'begin)
(setq dest (org-list-get-list-begin item struct prevs)))
((eq dest 'end)
(setq dest (org-list-get-list-end item struct prevs))
(save-excursion
(goto-char (org-list-get-last-item item struct prevs))
(line-end-position)))
((and (stringp dest) (string-match-p "\\`[0-9]+\\'" dest))
(let* ((all (org-list-get-all-items item struct prevs))
(len (length all))
(index (mod (string-to-number dest) len)))
(if (not (zerop index))
(setq dest (nth (1- index) all))
;; Send ITEM at the end of the list.
(setq dest (org-list-get-list-end item struct prevs))
(save-excursion
(goto-char
(org-list-get-last-item item struct prevs))
(line-end-position)))))
(t dest)))
(org-M-RET-may-split-line nil)
;; Store inner overlays (to preserve visibility).
(overlays (cl-remove-if (lambda (o) (or (< (overlay-start o) item)
(> (overlay-end o) item)))
(overlays-in item item-end))))
(cond
((eq dest 'delete) (org-list-delete-item item struct))
((eq dest 'kill)
(kill-new body)
(org-list-delete-item item struct))
((and (integerp dest) (/= item ins-point))
(setq item (copy-marker item))
(setq struct (org-list-insert-item ins-point struct prevs nil body))
;; 1. Structure returned by `org-list-insert-item' may not be
;; accurate, as it cannot see sub-items included in BODY.
;; Thus, first compute the real structure so far.
(let ((moved-items
(cons (marker-position item)
(org-list-get-subtree (marker-position item) struct)))
(new-end (org-list-get-item-end (point) struct))
(old-end (org-list-get-item-end (marker-position item) struct))
(new-item (point))
(shift (- (point) item)))
;; 1.1. Remove the item just created in structure.
(setq struct (delete (assq new-item struct) struct))
;; 1.2. Copy ITEM and any of its sub-items at NEW-ITEM.
(setq struct (sort
(append
struct
(mapcar (lambda (e)
(let* ((cell (assq e struct))
(pos (car cell))
(end (nth 6 cell)))
(cons (+ pos shift)
(append (butlast (cdr cell))
(list (if (= end old-end)
new-end
(+ end shift)))))))
moved-items))
#'car-less-than-car)))
;; 2. Restore inner overlays.
(dolist (o overlays)
(move-overlay o
(+ (overlay-start o) (- (point) item))
(+ (overlay-end o) (- (point) item))))
;; 3. Eventually delete extra copy of the item and clean marker.
(prog1 (org-list-delete-item (marker-position item) struct)
(move-marker item nil)))
(t struct))))