emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Add contents-begin and contents-end to src-block in org-element.el
@ 2017-12-24 14:56 Somelauw .
  2017-12-24 15:05 ` Nicolas Goaziou
  0 siblings, 1 reply; 7+ messages in thread
From: Somelauw . @ 2017-12-24 14:56 UTC (permalink / raw)
  To: emacs-orgmode

Here is a simple patch that makes it possible to find the inner
contents of a src-code block.

In the function called "defun org-element-src-block-parser (limit affiliated)":

- First find contents-begin and contents-end using the following:
#+BEGIN_SRC emacs-lisp
           (contents-begin (save-excursion
                             (goto-char begin)
                             (forward-line 1)
                             (line-beginning-position)))
           (contents-end (save-excursion
                           (goto-char end)
                           (forward-line
                            (- -1 (count-lines pos-before-blank end)))
                           (line-beginning-position)))
#+END_SRC

- Then make sure both are included in the list below that:
#+BEGIN_SRC emacs-lisp
             :begin begin
             :end end
             :contents-begin contents-begin
             :contents-end contents-end
#+END_SRC

With kind regards,
Somelauw

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

* Re: Add contents-begin and contents-end to src-block in org-element.el
  2017-12-24 14:56 Add contents-begin and contents-end to src-block in org-element.el Somelauw .
@ 2017-12-24 15:05 ` Nicolas Goaziou
  2017-12-24 23:55   ` Somelauw .
  0 siblings, 1 reply; 7+ messages in thread
From: Nicolas Goaziou @ 2017-12-24 15:05 UTC (permalink / raw)
  To: Somelauw .; +Cc: emacs-orgmode

Hello,

"Somelauw ." <somelauw@gmail.com> writes:

> Here is a simple patch that makes it possible to find the inner
> contents of a src-code block.

Contents mean "data in Org syntax". There are no such contents in source
blocks. Therefore, the parser does not provide :contents-begin
and :contents-end for them.

HTH,

Regards,

-- 
Nicolas Goaziou

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

* Re: Add contents-begin and contents-end to src-block in org-element.el
  2017-12-24 15:05 ` Nicolas Goaziou
@ 2017-12-24 23:55   ` Somelauw .
  2017-12-25  8:49     ` Nicolas Goaziou
  0 siblings, 1 reply; 7+ messages in thread
From: Somelauw . @ 2017-12-24 23:55 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: emacs-orgmode

2017-12-24 16:05 GMT+01:00 Nicolas Goaziou <mail@nicolasgoaziou.fr>:
> Contents mean "data in Org syntax". There are no such contents in source
> blocks. Therefore, the parser does not provide :contents-begin
> and :contents-end for them.

Thanks for your quick response As I understand from your reply,
:contents-begin/end should be reserved for elements that can contain
other org-elements or org-objects.

I'm writing a plugin that allows one to operate on the inner body of
an org-element and so far I'm relying on :contents-begin and
:contents-end. This has worked pretty well so far on most org
elements, but org-src-blocks seem to be an exception. I may want to
special-case these.

By the way, do you know of any other org-elements that have an inner
body that doesn't classify as "data in Org syntax"?

Kind regards,
Somelauw

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

* Re: Add contents-begin and contents-end to src-block in org-element.el
  2017-12-24 23:55   ` Somelauw .
@ 2017-12-25  8:49     ` Nicolas Goaziou
  2017-12-25 13:09       ` Somelauw .
  0 siblings, 1 reply; 7+ messages in thread
From: Nicolas Goaziou @ 2017-12-25  8:49 UTC (permalink / raw)
  To: Somelauw .; +Cc: emacs-orgmode

Hello,

"Somelauw ." <somelauw@gmail.com> writes:

> 2017-12-24 16:05 GMT+01:00 Nicolas Goaziou <mail@nicolasgoaziou.fr>:
>> Contents mean "data in Org syntax". There are no such contents in source
>> blocks. Therefore, the parser does not provide :contents-begin
>> and :contents-end for them.
>
> Thanks for your quick response As I understand from your reply,
> :contents-begin/end should be reserved for elements that can contain
> other org-elements or org-objects.
>
> I'm writing a plugin that allows one to operate on the inner body of
> an org-element and so far I'm relying on :contents-begin and
> :contents-end. This has worked pretty well so far on most org
> elements, but org-src-blocks seem to be an exception. I may want to
> special-case these.
>
> By the way, do you know of any other org-elements that have an inner
> body that doesn't classify as "data in Org syntax"?

comment, comment blocks, example blocks, export blocks, fixed-width,
keyword, latex-environment, node property.

These elements usually set :value property instead.

Regards,

-- 
Nicolas Goaziou

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

* Re: Add contents-begin and contents-end to src-block in org-element.el
  2017-12-25  8:49     ` Nicolas Goaziou
@ 2017-12-25 13:09       ` Somelauw .
  2017-12-25 22:42         ` Nicolas Goaziou
  0 siblings, 1 reply; 7+ messages in thread
From: Somelauw . @ 2017-12-25 13:09 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: emacs-orgmode

2017-12-25 9:49 GMT+01:00 Nicolas Goaziou <mail@nicolasgoaziou.fr>
> "Somelauw ." <somelauw@gmail.com> writes:
>
>> By the way, do you know of any other org-elements that have an inner
>> body that doesn't classify as "data in Org syntax"?
>
> comment, comment blocks, example blocks, export blocks, fixed-width,
> keyword, latex-environment, node property.

Thanks I'll consider these

>
> These elements usually set :value property instead.

Thank you. What I need is the beginning and ending position of :value
so I can yank/kill/delete/select it (or create a text-object in Evil).
I think a general solution to find the inner body of these elements is
using (forward-search (org-get-property :value element)) and reading
match-data afterwards.

Merry Christmas,
Somelauw

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

* Re: Add contents-begin and contents-end to src-block in org-element.el
  2017-12-25 13:09       ` Somelauw .
@ 2017-12-25 22:42         ` Nicolas Goaziou
  2017-12-27 20:50           ` Somelauw .
  0 siblings, 1 reply; 7+ messages in thread
From: Nicolas Goaziou @ 2017-12-25 22:42 UTC (permalink / raw)
  To: Somelauw .; +Cc: emacs-orgmode

"Somelauw ." <somelauw@gmail.com> writes:


I suggest the following.

For contents begin:

    (save-excursion
      (goto-char (org-element-property :post-affiliated element))
      (line-beginning-position 2))

For contents end:

    (save-excursion
      (goto-char (org-element-property :end element))
      (skip-chars-backward " \t\n")
      (line-beginning-position))

Regards,

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

* Re: Add contents-begin and contents-end to src-block in org-element.el
  2017-12-25 22:42         ` Nicolas Goaziou
@ 2017-12-27 20:50           ` Somelauw .
  0 siblings, 0 replies; 7+ messages in thread
From: Somelauw . @ 2017-12-27 20:50 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: emacs-orgmode

2017-12-25 23:42 GMT+01:00 Nicolas Goaziou <mail@nicolasgoaziou.fr>:
> I suggest the following.
>
> For contents begin:
>
>     (save-excursion
>       (goto-char (org-element-property :post-affiliated element))
>       (line-beginning-position 2))
>
> For contents end:
>
>     (save-excursion
>       (goto-char (org-element-property :end element))
>       (skip-chars-backward " \t\n")
>       (line-beginning-position))
>

Thanks, your code works as long as the element at point is some kind
of block element.
I have identified the following block elements:
- all elements that end with "-block
- latex-export/environment
- diary
- drawer
- export
- fixed_width

However, I'm trying to write something that works on all elements and
I want my code to continue working even if new elements are added to
org-mode.
It would be ideal if elements had the following additional properties:

- :value-begin
- :value-end

Unfortunately, they don't.

I have come to the following hackish solution:

#+BEGIN_SRC emacs-lisp
(defun org-select-inner-element ()
  "Select inner-element.
Return begin and end of inner contents of org-element at point."

  (let* ((element (org-element-context)))

    ;; Element has a value
    (if-let ((value (org-element-property :value element)))

        ;; Try to return begin and end of :value property
        (let ((lines (remove "" (split-string value "[\n\r]"))))
          (list (save-excursion
                  (goto-char (org-element-property :post-affiliated element))
                  (search-forward (first lines))
                  (match-beginning 0))
                (save-excursion
                  (goto-char (org-element-property :end element))
                  (search-backward (car (last lines)))
                  (match-end 0))))

      ;; Check if element has :contents-begin and contents-end
      (if (org-element-property :contents-begin element)
          (list (org-element-property :contents-begin element)
                (org-element-property :contents-end element))

        ;; Otherwise select the whole element
        (list (org-element-property :begin element)
              (org-element-property :end element)))
      )))
#+END_SRC

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

end of thread, other threads:[~2017-12-27 20:50 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-12-24 14:56 Add contents-begin and contents-end to src-block in org-element.el Somelauw .
2017-12-24 15:05 ` Nicolas Goaziou
2017-12-24 23:55   ` Somelauw .
2017-12-25  8:49     ` Nicolas Goaziou
2017-12-25 13:09       ` Somelauw .
2017-12-25 22:42         ` Nicolas Goaziou
2017-12-27 20:50           ` Somelauw .

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