I'm writing a function and I would like to operate on the current org heading and its content. For example:
* Head 1
Text
** Head 2
- Item 1
- Item 2[p]
- Item 3
If we look at the above text, with the location of point at [p]
, I would like to get the location right before "Head 2" and right after "Item 3".
Extra brownie points if I could pass in an optional parameter that would indicate the number of levels to include. So, 0
would do what I describe above, 1
would include "Head 1", etc.
Here's the code I have thus far:
(defun my-eval-org (&optional levels)
"Evaluate the current Org mode heading and its content"
(interactive "p")
(if (eq major-mode 'org-mode)
(save-excursion
(while (and (> levels 0) (org-up-heading-safe)) ; Go up by 'levels' number of headings
(setq levels (1- levels)))
(let* ((element (org-element-at-point)) ; Get the org element at the current point
(start (org-element-property :begin element)) ; The start point is the beginning of the element
(end (org-element-property :end element))) ; The end point is the end of the element
(my-eval-region start end)))
(message "Not in org-mode")))
(defun my-eval-region (start end)
"Evaluate the region from START to END and does something with it"
(interactive "r")
;; Do something cool with the region contents
)
The problem with this code is that if it runs within a list element, it will only evaluate the list element itself and not the enclosing heading.
Any help would be appreciated.