On 20/04/2021 20:57, Nicolas Goaziou wrote:
Maxim Nikulin writes:
(org-sort-remove-invisible "A")
#("A" 0 1 (:parent (#("A" 0 1 ...))))
This is a string.
Thank you, from second attempt I have managed to strip text properties.
Since the intended usage of return value is sorting key, would not it
benefit from passing result through the following expression?
(set-text-properties 0 (length s) nil s)
An alternative is to clean up keys in `org-sort-list' function.
Ah! I forgot the link part! Hopefully done here.
Surprisingly there are still cases when the old approach works better:
(let ((s (org-sort-remove-invisible
"A /wrapping [[https://orgmode.org/?a=b&c=d#e][link]] emphasis/")))
(set-text-properties 0 (length s) nil s)
s)
"A wrapping [[https://orgmode.org?a=b&c=d#e][link]] emphasis/"
I expect "A wrapping link emphasis".
In the meanwhile I have tried
(benchmark-run 1 (org-sort-list t ?a))
in a file (1100 lines) obtained using
grep '^- ' doc/org-manual.org >/tmp/list.org
It seems, performance is still acceptable (single run hardly could be
considered as an accurate test):
(1.115571472 18 0.5986466069999999) ; new variant
(0.260384514 1 0.09805475199999947) ; original code
--8<---------------cut here---------------start------------->8---
(defun org-sort-remove-invisible (s)
"Remove emphasis markers and any invisible property from string S.
Assume S may contain only objects."
;; org-element-interpret-data clears any text property, including
;; invisible part.
(org-element-interpret-data
(let ((tree (org-element-parse-secondary-string
s (org-element-restriction 'paragraph))))
(org-element-map tree '(bold code italic link strike-through underline
verbatim)
(lambda (o)
(pcase (org-element-type o)
;; Terminal object. Replace it with its value.
((or `code `verbatim)
(let ((new (org-element-property :value o)))
(org-element-insert-before new o)
(org-element-put-property
new :post-blank (org-element-property :post-blank o))))
;; Non-terminal objects. Splice contents.
(type
(let ((contents
(or (org-element-contents o)
(and (eq type 'link)
(list (org-element-property :raw-link o)))))
(c nil))
(while contents
(setq c (pop contents))
(org-element-insert-before c o))
(org-element-put-property
c :post-blank (org-element-property :post-blank o)))))
(org-element-extract-element o)))
;; Return modified tree.
tree)))
--8<---------------cut here---------------end--------------->8---