[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [O] finding a parent node
From: |
Thorsten Jolitz |
Subject: |
Re: [O] finding a parent node |
Date: |
Tue, 02 Jul 2013 11:40:35 +0200 |
User-agent: |
Gnus/5.130002 (Ma Gnus v0.2) Emacs/24.3 (gnu/linux) |
address@hidden (Łukasz Stelmach) writes:
Hello,
> What is the best way to iterate over ((great)*grand)?parent headlines of
> the current one (at point), that meets some criteria.
not an answer to your question, but rather a related question I wanted
to post anyway, so I do it in this thread:
simple-test.org:
#+begin_src org
* header 1
:PROPERTIES:
:CUSTOM_ID: XYZ22
:END:
* header 2
[2013-06-28 Fr 11:01]
** subheader 1
Some text
** subheader 2
More text and a table
| label | col1 | col2 |
|--------+------+------|
| string | 3 | 4 |
Text and a src-block
#+begin_src emacs-lisp
(+ 3 4)
#+end_src
#+end_src
parse buffer:
#+begin_src emacs-lisp
(progn
(goto-char (point-max))
(newline 2)
(insert
(format "%s"
(with-current-buffer
(find-file-noselect
"/path/to/simple-test.org")
(setq tree (org-element-parse-buffer))))) )
#+end_src
excerpts form parse-tree:
#+begin_src emacs-lisp
(org-data
nil
;; first headline
(headline
(:raw-value header 1 :begin 1 :end55 :pre-blank0 :hiddenp
outline :contents-begin 12 :contents-end 55 :level 1 :priority
nil :tags nil :todo-keyword nil :todo-type nil :post-blank
1 :footnote-section-p nil :archivedp nil :commentedp nil :quotedp
nil :CUSTOM_ID XYZ22 :CATEGORY simple-test :title (header
1) :parent #0)
(section (:begin 12 :end 55 :contents-begin 12 :contents-end
55 :post-blank 0 :parent #1)
(property-drawer (:begin 12 :end 55 :hiddenp
outline :contents-begin 27 :contents-end 47 :post-blank
0 :post-affiliated 12 :parent #2) (node-property (:key
CUSTOM_ID :value XYZ22 :begin 27 :end 47 :post-blank 0 :parent
#3)))))
;; second headline
(headline (:raw-value header 2 :begin 55 :end 295 :pre-blank
0 :hiddenp outline :contents-begin 66 :contents-end 295 :level
1 :priority nil :tags nil :todo-keyword nil :todo-type
nil :post-blank 0 :footnote-section-p nil :archivedp
nil :commentedp nil :quotedp nil :CATEGORY
simple-test :title (header 2) :parent #0)
(section (:begin 66 :end 90 :contents-begin 66 :contents-end
90 :post-blank 0 :parent #1) (paragraph (:begin 66 :end
90 :contents-begin 66 :contents-end 90 :post-blank
0 :post-affiliated 66 :parent #2)
(timestamp (:type inactive :raw-value [2013-06-28 Fr
11:01] :year-start 2013 :month-start 6 :day-start 28 :hour-start
11 :minute-start 1 :year-end 2013 :month-end 6 :day-end
28 :hour-end 11 :minute-end 1 :begin 68 :end 89 :post-blank
0 :parent #3)) )) ...) )
#+end_src
now I can use `org-element-map' to access the elements and their attributes:
#+begin_src emacs-lisp
(insert
(format "\n\n%s"
(org-element-map
tree
'timestamp
'(lambda (X) (org-element-property :raw-value X)))))
;; => ([2013-06-28 Fr 11:01])
#+end_src
this gives me the first timestamp element and its property list:
#+begin_src emacs-lisp
(setq stamp1
(org-element-map
tree
'timestamp
'identity nil t))
#+end_src
but for further processing I always struggle with the :parent attribute and
the circularities in the list.
I often would like to use just a sublist like this, with :parent being a Link
or ID and not the parent object that refers to its parent object and so on ...
#+begin_src emacs-lisp
(timestamp (:type inactive :raw-value [2013-06-28 Fr
11:01] :year-start 2013 :month-start 6 :day-start 28 :hour-start
11 :minute-start 1 :year-end 2013 :month-end 6 :day-end
28 :hour-end 11 :minute-end 1 :begin 68 :end 89 :post-blank
0 :parent <<ID of or LINK to #3>>))
#+end_src
I tried to use the NO-RECURSION arg of `org-element-map' for this, but often
the parent is a headline or section, and then 'org-element-map' doesn't enter
headlines or sections and returns nil, i.e. the timestamp is never found.
Is there a way - using the parser/export framework toolbox - to avoid these
circularities, i.e. to either ignore the :parent attribute or better replace
the list object in it by something else that nevertheless identifies the
parent unmistakenly?
I know that with special bookkeeping while walking the tree one can deal with
these circularities, maybe I'm just new to this stuff, but I feel they make
the usual processing of such a nested list a bit complicated.
--
cheers,
Thorsten