Hello, Florian Beck <f...@miszellen.de> writes:
> I hope the new version is an improvement. Thanks for the update. > But as long as I have a table cell ancestor, I should be fine. Am > I missing something? Yes. There are some place where `org-element-context' will return a `table-row' or `table' object even though you can technically move to the next (or maybe previous) cell. You may want to try `org-element-context' in the following places, where X denotes cursor: | a | b | X c | d | X a | b | | c | d | X | a | b | | c | d | | a | b | X | c | d | | a | b | X | c | d | > I added a function `org-element-get' to help with that. What do you > think? (If `cl-labels' is a no-go, we could split out a helper > function.) I don't think `org-element-get' should go into "org-element.el" in its current implementation. It is tied to a position, which implies to take care of boring stuff like buffer narrowing. It doesn't allow to re-use an already computed element or object either, which is sub-optimal. However, the feature is useful enough that some function could provide something similar. Maybe the following one: (defun org-element-parent (blob &optional types genealogy) "Return BLOB's parent, or nil. BLOB is an object or element. When optional argument TYPES is a list of symbols, return the first element or object between BLOB and its ancestors whose type belongs to that list. When optional argument GENEALOGY is non-nil, return a list of all ancestors, from closest to farthest. When BLOB is obtained through `org-element-context' or `org-element-at-point', only ancestors from its section can be found. There is no such limitation when BLOB belongs to a full parse tree." (if (not (or types genealogy)) (org-element-property :parent blob) (let ((up blob) ancestors) (while (and up (not (memq (org-element-type up) types))) (when genealogy (push up ancestors)) (setq up (org-element-property :parent up))) (if genealogy (nreverse ancestors) up)))) Its name is a bit confusing since it can return BLOB under some optional argument combination (i.e., if BLOB's type belongs to TYPES). However, it covers most, if not all, needs about ancestors. WDYT? > I added a couple of tests. Not really succinct, though. Thanks. > Also, I modified `org-element-table-cell-parser', because otherwise > :contents-begin and :contents-end point to the end of the cell. Not sure > if this breaks anything. Usually, when an object has no contents (e.g., a link without description), both :contents-begin and :contents-end are nil. It is less confusing than providing an arbitrary buffer position. > + (let ((cell (or (org-element-get 'table-cell) > + ;; Fuzzy matching when on a table border: > + (org-element-get 'table-cell (1+ (point))) > + (org-element-get 'table-cell (1- (point)))))) We need to be more careful here, as explained above. > + (should > + (org-test-with-temp-text > + "| Cell:1 | Cell2 |Cell3|\n| Cell4 | | Cell5|" > + (search-forward ":") Minor note: you can insert "<point>" in the string to avoid finding the correct position later. E.g., "| Cell:<point>1 | Cell2 |Cell3|\n| Cell4 | | Cell5|" Regards, -- Nicolas Goaziou