* Buffer positions from `org-element-map'?
@ 2015-01-28 1:48 Charles C. Berry
2015-01-28 2:24 ` Rasmus
2015-01-28 7:32 ` Nicolas Goaziou
0 siblings, 2 replies; 3+ messages in thread
From: Charles C. Berry @ 2015-01-28 1:48 UTC (permalink / raw)
To: Org-Mode mailing list
I'd like to be able to determine the buffer position of (say) inline
src blocks as in the following and operate on them using
`org-element-map'. This might enable modification and replacement of a
few objects in a large buffer.
What I would like is to have the same values of :begin as would be
obtained by placing the cursor on each inline src block and evaluating
(org-element-context). As is evident below, :begin is sometimes a
relative position.
Suggestions on how to get the buffer position or some other way to do
in situ buffer modifications from `org-element-map' without
interpreting a complete parse tree (which I expect requires more
computation that doing selected `delete then insert' operations) are
welcome.
Chuck
--8<---------------cut here---------------start------------->8---
#+BEGIN_SRC emacs-lisp
(org-element-map (org-element-parse-buffer)
'inline-src-block
(lambda (x) (list
(org-element-property :begin x)
(org-element-property :value x))))
#+END_SRC
#+RESULTS:
| 326 | "not in headline" |
| 1 | "in head line" |
| 395 | "not in headline" |
src_emacs-lisp{"not in headline"}
* src_emacs-lisp{"in head line"}
src_emacs-lisp{"not in headline"}
--8<---------------cut here---------------end--------------->8---
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Buffer positions from `org-element-map'?
2015-01-28 1:48 Buffer positions from `org-element-map'? Charles C. Berry
@ 2015-01-28 2:24 ` Rasmus
2015-01-28 7:32 ` Nicolas Goaziou
1 sibling, 0 replies; 3+ messages in thread
From: Rasmus @ 2015-01-28 2:24 UTC (permalink / raw)
To: emacs-orgmode
Hi Charles,
"Charles C. Berry" <ccberry@ucsd.edu> writes:
> What I would like is to have the same values of :begin as would be
> obtained by placing the cursor on each inline src block and evaluating
> (org-element-context). As is evident below, :begin is sometimes a
> relative position.
OK
> Suggestions on how to get the buffer position or some other way to do
> in situ buffer modifications from `org-element-map' without
> interpreting a complete parse tree (which I expect requires more
> computation that doing selected `delete then insert' operations) are
> welcome.
Regexp and cl!
Note, if you want it do always do it on the whole file use
org-with-wide-buffer. You might also want to wrap it in a
save-match-data.
--8<---------------cut here---------------start------------->8---
#+BEGIN_SRC emacs-lisp :results code
(save-excursion
(goto-char (point-min))
(cl-loop while (search-forward-regexp org-babel-inline-src-block-regexp nil t)
collect (list :begin (match-beginning 1)
:at-heading (org-at-heading-p))))
#+END_SRC
#+RESULTS:
#+BEGIN_SRC emacs-lisp
((:begin 424 :at-heading nil)
(:begin 461 :at-heading t)
(:begin 493 :at-heading nil))
#+END_SRC
src_emacs-lisp{"not in headline"}
* src_emacs-lisp{"in head line"}
src_emacs-lisp{"not in headline"}
--8<---------------cut here---------------end--------------->8---
Hope it helps,
Rasmus
--
Governments should be afraid of their people
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Buffer positions from `org-element-map'?
2015-01-28 1:48 Buffer positions from `org-element-map'? Charles C. Berry
2015-01-28 2:24 ` Rasmus
@ 2015-01-28 7:32 ` Nicolas Goaziou
1 sibling, 0 replies; 3+ messages in thread
From: Nicolas Goaziou @ 2015-01-28 7:32 UTC (permalink / raw)
To: Charles C. Berry; +Cc: Org-Mode mailing list
Hello,
"Charles C. Berry" <ccberry@ucsd.edu> writes:
> What I would like is to have the same values of :begin as would be
> obtained by placing the cursor on each inline src block and evaluating
> (org-element-context). As is evident below, :begin is sometimes a
> relative position.
This was fixed recently.
> Suggestions on how to get the buffer position or some other way to do
> in situ buffer modifications from `org-element-map' without
> interpreting a complete parse tree (which I expect requires more
> computation that doing selected `delete then insert' operations) are
> welcome.
I think the most efficient is to do a fast search, e.g.,
(search-forward "src_" nil t)
then, with `org-element-context', double-check type of object found and
collect :begin property.
Regards,
--
Nicolas Goaziou
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2015-01-28 7:31 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-01-28 1:48 Buffer positions from `org-element-map'? Charles C. Berry
2015-01-28 2:24 ` Rasmus
2015-01-28 7:32 ` Nicolas Goaziou
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).