emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* How to visit every source block and modify it's NAME property with org-element?
@ 2015-08-09  3:06 Grant Rettke
  2015-08-09  8:12 ` Nicolas Goaziou
  0 siblings, 1 reply; 4+ messages in thread
From: Grant Rettke @ 2015-08-09  3:06 UTC (permalink / raw)
  To: emacs-orgmode@gnu.org

Good evening,

Emacs 24.4 and Org from Git.

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'?

This is very similar to a post I just made about headlines and the ID
property but they are different enough to post separately.


[provides]
http://orgmode.org/w/?p=org-mode.git;a=blob_plain;f=lisp/org-id.el;hb=HEAD

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: How to visit every source block and modify it's NAME property with org-element?
  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
  2015-08-10  1:28   ` Grant Rettke
  0 siblings, 1 reply; 4+ messages in thread
From: Nicolas Goaziou @ 2015-08-09  8:12 UTC (permalink / raw)
  To: Grant Rettke; +Cc: emacs-orgmode@gnu.org

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: How to visit every source block and modify it's NAME property with org-element?
  2015-08-09  8:12 ` Nicolas Goaziou
@ 2015-08-10  1:28   ` Grant Rettke
  2015-08-10 20:59     ` Grant Rettke
  0 siblings, 1 reply; 4+ messages in thread
From: Grant Rettke @ 2015-08-10  1:28 UTC (permalink / raw)
  To: Grant Rettke, emacs-orgmode@gnu.org

On Sun, Aug 9, 2015 at 3:12 AM, Nicolas Goaziou <mail@nicolasgoaziou.fr> wrote:
> Probably something like this (untested)
>
>   (let ((case-fold-search t))

That is a valuable example for us to learn from. Right off the bat I
wouldn't have
though to indent that way, but would have noticed quickly enough when I didn't
account for the case! There were 276 blocks corrected.

Here is the final version with a tweaked regex and removed and removal
of my original replacement desire.

Thank you Nicolas.

┌────
│ (let ((case-fold-search t))
│   (while (re-search-forward "^\s*#[+]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: " (org-id-new) "\n"))
│               (indent-to i)
│               (forward-line 2)))))))
└────

if you are unfamiliar with that indent-to bit, here is ax example of
what it handled correctly:

 #+NAME: FD94197F-0A18-480E-8DEA-F5943B860105
 #+BEGIN_SRC emacs-lisp
 (add-to-list 'load-path "~/src/org-mode/lisp")
 #+END_SRC

  #+NAME: F4D65E13-0E55-4443-A7A7-13DD64A13051
  #+BEGIN_SRC emacs-lisp
  (add-to-list 'load-path "~/src/org-mode/lisp")
  #+END_SRC

   #+NAME: 705F4F89-0431-42A3-A10D-1C528945CAC7
   #+BEGIN_SRC emacs-lisp
   (add-to-list 'load-path "~/src/org-mode/lisp")
   #+END_SRC

    #+NAME: 233C2AFF-A257-4091-875C-945E3189AFEB
    #+BEGIN_SRC emacs-lisp
    (add-to-list 'load-path "~/src/org-mode/lisp")
    #+END_SRC

     #+NAME: FC730EEE-A32B-449D-94EA-EBB750D89204
     #+BEGIN_SRC emacs-lisp
     (add-to-list 'load-path "~/src/org-mode/lisp")
     #+END_SRC

- Foo
  #+NAME: FD94197F-0A18-480E-8DEA-F5943B860105
  #+BEGIN_SRC emacs-lisp
  (add-to-list 'load-path "~/src/org-mode/lisp")
  #+END_SRC
  - Foo
    #+NAME: FD94197F-0A18-480E-8DEA-F5943B860105
    #+BEGIN_SRC emacs-lisp
    (add-to-list 'load-path "~/src/org-mode/lisp")
    #+END_SRC
    - Foo
      #+NAME: FD94197F-0A18-480E-8DEA-F5943B860105
      #+BEGIN_SRC emacs-lisp
      (add-to-list 'load-path "~/src/org-mode/lisp")
      #+END_SRC
      - Foo
       #+NAME: FD94197F-0A18-480E-8DEA-F5943B860105
       #+BEGIN_SRC emacs-lisp
       (add-to-list 'load-path "~/src/org-mode/lisp")
       #+END_SRC

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: How to visit every source block and modify it's NAME property with org-element?
  2015-08-10  1:28   ` Grant Rettke
@ 2015-08-10 20:59     ` Grant Rettke
  0 siblings, 0 replies; 4+ messages in thread
From: Grant Rettke @ 2015-08-10 20:59 UTC (permalink / raw)
  To: Grant Rettke, emacs-orgmode@gnu.org

Here is perhaps the final version:
- Go to point-min first
- Do before tangling
- Save buffer

┌────
│ (defun help/org-prp-hdln ()
│   "Visit every Headline. If it doesn't have an ID property then add one and
│   assign it a UUID. Attribution: URL
`http://article.gmane.org/gmane.emacs.orgmode/99738'"
│   (interactive)
│   (goto-char (point-min))
│   (dolist (p (nreverse
│               (org-element-map (org-element-parse-buffer 'headline) 'headline
│                 (lambda (headline) (org-element-property :begin headline)))))
│     (goto-char p)
│     (org-id-get-create))
│   (save-buffer))
│
│ (defun help/org-prp-src-blk ()
│   "Visit every Source-Block. If it doesn't have a NAME property then
add one and
│    assign it a UUID. Attribution: URL
`http://article.gmane.org/gmane.emacs.orgmode/99740'"
│   (interactive)
│   (goto-char (point-min))
│   (let ((case-fold-search t))
│     (while (re-search-forward "^\s*#[+]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: " (org-id-new) "\n"))
│                 (indent-to i)
│                 (forward-line 2)))))))
│   (save-buffer))
│ (add-hook #'org-babel-pre-tangle-hook #'help/org-prp-hdln)
│ (add-hook #'org-babel-pre-tangle-hook #'help/org-prp-src-blk)
└────

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-08-10 21:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2015-08-10  1:28   ` Grant Rettke
2015-08-10 20:59     ` Grant Rettke

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).