[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [O] How to visit every source block and modify it's NAME property wi
From: |
Nicolas Goaziou |
Subject: |
Re: [O] How to visit every source block and modify it's NAME property with org-element? |
Date: |
Sun, 09 Aug 2015 10:12:27 +0200 |
Hello,
Grant Rettke <address@hidden> writes:
> Goal:
>
> 1) Visit every source block
> 2) If there is a NAME property
> 1) Then replace it's value with a UUID
> 3) If there is not an NAME property
> 1) Add an NAME property and give it a UUID.
>
> `org-uuid' [provides] the functionality to create a UUID. It seems like
> this could be re-used for this NAME property. Would that be a good idea?
> The code would be nearly the same (choose a UUID program, get a value,
> insert it).
>
> What is the best way to do it with `org-element'?
Probably something like this (untested)
(let ((case-fold-search t))
(while (re-search-forward "^[ \t]*#+BEGIN_SRC" nil t)
(let ((element (org-element-at-point)))
(when (eq (org-element-type element) 'src-block)
(if (not (org-element-property :name element))
(let ((i (org-get-indentation)))
(beginning-of-line)
(save-excursion (insert "#+NAME: whatever\n"))
(indent-to i)
(forward-line 2))
(re-search-backward "^[ \t]*#\\+NAME: +\\(.*\\)" nil t)
(replace-match "whatever" ni nil nil 1))))))
> This is very similar to a post I just made about headlines and the ID
> property but they are different enough to post separately.
Unlike to the previous post, using `org-element' is mandatory in order
to avoid false positive when matching a source block.
Regards,
--
Nicolas Goaziou