Ihor Radchenko <yanta...@posteo.net> writes:

>
> Hmm. :after-element keyword logic is broken. It does not account for the
> case when :after-element is past the START point.
>
> It is the time to refactor this function yet again.
> (a tricky endeavour considering all the edge cases we can encounter when
> there are changes in buffer while `org-element-cache-map' is mapping
> over it).

See attached for a way to break :from-pos as well.  I would like to help
refactor but studying this function is a little dizzying for me.


>From eb37560b9c94bed6e91d6834462173d0a6d7d44b Mon Sep 17 00:00:00 2001
From: Morgan Smith <morgan.j.sm...@outlook.com>
Date: Fri, 19 Apr 2024 10:46:28 -0400
Subject: [PATCH] test-org-element/cache-map: Add test for :from-pos

* testing/lisp/test-org-element.el (test-org-element/cache-map): Add
test for :from-pos.
---
 testing/lisp/test-org-element.el | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/testing/lisp/test-org-element.el b/testing/lisp/test-org-element.el
index ad4ff1ce9..a0dbc726e 100644
--- a/testing/lisp/test-org-element.el
+++ b/testing/lisp/test-org-element.el
@@ -4994,7 +4994,21 @@ Text
    (equal
     '(org-data headline section drawer paragraph)
     (org-test-with-temp-text "* headline\n:DRAWER:\nparagraph\n:END:"
-      (org-element-cache-map #'car :granularity 'element)))))
+      (org-element-cache-map #'car :granularity 'element))))
+  (should
+   (equal
+    ;; This fails returning '(paragraph paragraph) because it returns
+    ;; the paragraph under "headline".  This is because at
+    ;; org-element.el:8028 we travel up the parents of drawer trying
+    ;; to find a paragraph.
+    '(paragraph)
+    (org-test-with-temp-text "* headline\nparagraph\n<point>** subheading\n:DRAWER:\nparagraph\n:END:\n"
+      (let ((subheading (org-element-at-point)))
+        (org-element-cache-map
+         #'car
+         :granularity 'element
+         :restrict-elements '(paragraph)
+         :from-pos (org-element-contents-begin subheading)))))))
 
 (ert-deftest test-org-element/cache ()
   "Test basic expectations and common pitfalls for cache."
-- 
2.41.0

Reply via email to