emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Nicolas Goaziou <mail@nicolasgoaziou.fr>
To: Grant Rettke <gcr@wisdomandwonder.com>
Cc: "emacs-orgmode@gnu.org" <emacs-orgmode@gnu.org>
Subject: Re: How to visit every source block and modify it's NAME property with org-element?
Date: Sun, 09 Aug 2015 10:12:27 +0200	[thread overview]
Message-ID: <87oaiganis.fsf@nicolasgoaziou.fr> (raw)
In-Reply-To: <CAAjq1mc=u=hTpNLA2TuMCuCt6OwhzCM2ipkSFQme4gD21LnC+Q@mail.gmail.com> (Grant Rettke's message of "Sat, 8 Aug 2015 22:06:13 -0500")

Hello,

Grant Rettke <gcr@wisdomandwonder.com> 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

  reply	other threads:[~2015-08-09  8:10 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-09  3:06 How to visit every source block and modify it's NAME property with org-element? Grant Rettke
2015-08-09  8:12 ` Nicolas Goaziou [this message]
2015-08-10  1:28   ` Grant Rettke
2015-08-10 20:59     ` Grant Rettke

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87oaiganis.fsf@nicolasgoaziou.fr \
    --to=mail@nicolasgoaziou.fr \
    --cc=emacs-orgmode@gnu.org \
    --cc=gcr@wisdomandwonder.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).