From a64c5ff86998fa3efbff02364376ffd165caba21 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Tue, 13 Apr 2021 17:45:11 -0500 Subject: `org-return-dwim': work in nested lists `org-return-dwim' now ends a list on an empty nested list item. See the comment for details. --- lisp/acdw-org.el | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/lisp/acdw-org.el b/lisp/acdw-org.el index 7e9fecd..dc85015 100644 --- a/lisp/acdw-org.el +++ b/lisp/acdw-org.el @@ -79,23 +79,33 @@ appropriate. In tables, insert a new row or end the table." (org-insert-todo-heading nil)) ((org-in-item-p) - ;; Plain list. Yes, this gets a little complicated... - (let ((context (org-element-context))) - (if (or (eq 'plain-list (car context)) ; First item in list - (and (eq 'item (car context)) - (not (eq (org-element-property - :contents-begin context) - (org-element-property - :contents-end context)))) - ;; Element in list item, e.g. a link - (unpackaged/org-element-descendant-of 'item context)) - ;; Non-empty item: Add new item. - (org-insert-item) - ;; Empty item: Close the list. - ;; TODO: Do this with org functions rather than operating - ;; on the text. Can't seem to find the right function. - (delete-region (line-beginning-position) (line-end-position)) - (insert "\n")))) + ;; Plain list + (let* ((context (org-element-context)) + (first-item-p (eq 'plain-list (car context))) + (itemp (eq 'item (car context))) + (emptyp (eq (org-element-property :contents-begin context) + (org-element-property :contents-end context))) + (item-child-p + (unpackaged/org-element-descendant-of 'item context))) + (message "(or %S (and %S %S) %S)" + first-item-p + itemp (not emptyp) + item-child-p) + ;; The original function from unpackaged just tested the (or ...) test + ;; in this cond, in an if. However, that doesn't auto-end nested + ;; lists. So I made this form a cond and added the (and...) test in + ;; the first position, which is clunky (the delete-region... stuff + ;; comes twice) and might not be needed. More testing, obviously, but + ;; for now, it works well enough. + (cond ((and itemp emptyp) + (delete-region (line-beginning-position) (line-end-position)) + (insert "\n\n")) + ((or first-item-p + (and itemp (not emptyp)) + item-child-p) + (org-insert-item)) + (t (delete-region (line-beginning-position) (line-end-position)) + (insert "\n"))))) ((when (fboundp 'org-inlinetask-in-task-p) (org-inlinetask-in-task-p)) -- cgit 1.4.1-21-gabe81