emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Somelauw ." <somelauw@gmail.com>
To: Nicolas Goaziou <mail@nicolasgoaziou.fr>
Cc: emacs-orgmode@gnu.org
Subject: Re: Add contents-begin and contents-end to src-block in org-element.el
Date: Wed, 27 Dec 2017 21:50:15 +0100	[thread overview]
Message-ID: <CA+gt_a_F_-L3yaP9y4a5OxoDHsb-DywA2OtPv0Ewn15Ycrn=bQ@mail.gmail.com> (raw)
In-Reply-To: <87po721lb5.fsf@nicolasgoaziou.fr>

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

      reply	other threads:[~2017-12-27 20:50 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 . [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CA+gt_a_F_-L3yaP9y4a5OxoDHsb-DywA2OtPv0Ewn15Ycrn=bQ@mail.gmail.com' \
    --to=somelauw@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=mail@nicolasgoaziou.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).