Hi,

On 19/04/2021 23:08, Nicolas Goaziou wrote:
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/"
Fair enough. Here comes another, more involved, attempt.
Maybe first variant deserves to be committed while discussion of a 
better option is in progress.
--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
Sorry, I can not help you with polishing code of this function, I am not 
familiar with functions working on org element tree yet.
I can not even determine what type of structure is returned when 
`org-sort-remove-invisible' is called from ert or from scratch buffer:
(org-sort-remove-invisible "A")
#("A" 0 1 (:parent (#("A" 0 1 ...))))

A couple of obvious problems:

1. Link handling

#+begin_src elisp
(org-sort-remove-invisible
 "- [[https://orgmode.org/submit?a=bc%20d&e=t+r#1234][a link]]")
#+end_src

#+RESULTS:
: - [[https://orgmode.org/submit?a=bc%20d&e=t+r#1234][a link]]

2. Missed spaces

#+begin_src elisp
(org-sort-remove-invisible "A *b* /i/ t.")
#+end_src

#+RESULTS:
: A bit.

    (let ((tree (org-element-parse-secondary-string
                 s (org-element-restriction 'paragraph))))
      (org-element-map tree '(bold code italic 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.
            (_
             (let ((contents (org-element-contents 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---

It is not perfect, but it does a better job.

WDYT?

Reply via email to