On 19/04/2021 15:33, Nicolas Goaziou wrote: > > Could you try the following instead? > > --8<---------------cut here---------------start------------->8--- > (defun org-sort-remove-invisible (s) > "Remove invisible part of links and emphasis markers from string S." > (let ((remove-markers > (lambda (m) Just a curiosity, what is style guide recommendation: let - lambda or cl-labels? > (concat (match-string 1 m) > (match-string 4 m) > (match-string 5 m))))) > (remove-text-properties 0 (length s) org-rm-props s) > (replace-regexp-in-string > org-verbatim-re remove-markers > (replace-regexp-in-string org-emph-re remove-markers (org-link-display-format s) t tt) Single "t" is at the end, isn't it? > t t))) I think, the patch is an improvement. There is still a minor shortcoming that ordering of emphasized and non-emphasized words is undefined - /a/ - *a* - a Let's leave it aside since it requires multilevel comparison similar to collation in locales and more strict definition which part of string is compared at each level: - /a/ :: A - /a/ :: Z - a :: A - a :: Z In my opinion, a more severe limitation comes from sequential regexp-based approach. Consider stripping markers from 1. "a =b *c* d= e" 2. "*b* /i/" First case could be solved by splitting the input string by verbatim regexp at first and by applying emphasis substitution only to non-verbatim parts. However I am rather shy to experiment with regexps definition to avoid including chars before and after emphasis markers. It would be great to get role of particular characters from more reliable parser (or at least from text properties affected by fontification). I have some tests for `org-sort-remove-invisible', see the attachment. Actually I do not like such style of tests, I strongly prefer to see all failures at once, but I have not found if such technique is used anywhere in org tests.