From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nicolas Goaziou 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 Message-ID: <87oaiganis.fsf@nicolasgoaziou.fr> References: Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:47006) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZOLh5-0006nO-3F for emacs-orgmode@gnu.org; Sun, 09 Aug 2015 04:10:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZOLgz-0001Gb-M1 for emacs-orgmode@gnu.org; Sun, 09 Aug 2015 04:10:59 -0400 Received: from relay5-d.mail.gandi.net ([2001:4b98:c:538::197]:50157) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZOLgz-0001GV-Ev for emacs-orgmode@gnu.org; Sun, 09 Aug 2015 04:10:53 -0400 In-Reply-To: (Grant Rettke's message of "Sat, 8 Aug 2015 22:06:13 -0500") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Grant Rettke Cc: "emacs-orgmode@gnu.org" Hello, Grant Rettke 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