[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] Add contents-begin and contents-end to src-block in org-element.

From: Somelauw .
Subject: Re: [O] Add contents-begin and contents-end to src-block in org-element.el
Date: Wed, 27 Dec 2017 21:50:15 +0100

2017-12-25 23:42 GMT+01:00 Nicolas Goaziou <address@hidden>:
> 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
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))
                  (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)))

reply via email to

[Prev in Thread] Current Thread [Next in Thread]