Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs org-mode insert text after heading properties

Tags:

emacs

org-mode

I want to insert some text after all the headings in an org file.

For example, suppose I have:

* Header foo
  :PROPERTIES:
  :EXPORT_FILE_NAME: ./tmp/Test
  :END:
* Header bar

After running (insert-after-heading "NEW TEXT"), I should have:

* Header foo
  :PROPERTIES:
  :EXPORT_FILE_NAME: ./tmp/Test
  :END:
NEW TEXT
* Header bar
NEW TEXT

The best I managed so far was doing the following:

  (goto-char (point-min))
  (goto-char (re-search-forward "^*"))
  (set-mark (line-beginning-position))
  (goto-char (point-max))
  (org-map-entries
   (lambda () (progn (forward-line)(new-line)(previous-line) (insert "NEW TEXT") )

However, this insert the text before the properties.


Edit:

(defun goto-header()
  (interactive)
  (org-back-to-heading)
  (let ((beg-end (org-get-property-block))):
       (when beg-end
         (let ((end (cdr beg-end)))
           (goto-char end))))
  (forward-line)
  (newline)
  (previous-line))

works as a way of moving the point to the correct location, so that insert can properly insert the text. Is there a better way?

like image 274
Ivan Tadeu Ferreira Antunes Fi Avatar asked Aug 31 '18 19:08

Ivan Tadeu Ferreira Antunes Fi


Video Answer


2 Answers

Here's a heavily commented function that does what you want.

(defun my/insert-text-after-heading (text)
  "Insert TEXT after every heading in the file, skipping property drawers."
  (interactive "sText to insert: ")

  ;; The Org Element API provides functions that allow you to map over all
  ;; elements of a particular type and perform modifications. However, as
  ;; as soon as the buffer is modified the parsed data becomes out of date.
  ;;
  ;; Instead, we treat the buffer as text and use other org-element-*
  ;; functions to parse out important data.

  ;; Use save-excursion so the user's point is not disturbed when this code
  ;; moves it around.
  (save-excursion
    ;; Go to the beginning of the buffer.
    (goto-char (point-min))

    ;; Use save-match-data as the following code uses re-search-forward,
    ;; will disturb any regexp match data the user already has.
    (save-match-data

      ;; Search through the buffer looking for headings. The variable
      ;; org-heading-regexp is defined by org-mode to match anything
      ;; that looks like a valid Org heading.
      (while (re-search-forward org-heading-regexp nil t)

        ;; org-element-at-point returns a list of information about
        ;; the element the point is on. This includes a :contents-begin
        ;; property which is the buffer location of the first character
        ;; of the contents after this headline.
        ;;
        ;; Jump to that point.
        (goto-char (org-element-property :contents-begin (org-element-at-point)))

        ;; Point is now on the first character after the headline. Find out
        ;; what type of element is here using org-element-at-point.
        (let ((first-element (org-element-at-point)))

          ;; The first item in the list returned by org-element-at-point
          ;; says what type of element this is.  See
          ;; https://orgmode.org/worg/dev/org-element-api.html for details of
          ;; the different types.
          ;;
          ;; If this is a property drawer we need to skip over it. It will
          ;; an :end property containing the buffer location of the first
          ;; character after the property drawer. Go there if necessary.
          (when (eq 'property-drawer (car first-element))
            (goto-char (org-element-property :end first-element))))

      ;; Point is now after the heading, and if there was a property
      ;; drawer then it's after that too. Insert the requested text.
      (insert text "\n\n")))))
like image 79
Nik Avatar answered Oct 10 '22 18:10

Nik


To go to the end of property drawer you can use (org-end-of-meta-data t). So one shorter function would be

(defun goto-header()
  (interactive)
  (org-back-to-heading)
  (org-end-of-meta-data t)
  (forward-line)
  (newline)
  (previous-line))
like image 37
George Moutsopoulos Avatar answered Oct 10 '22 19:10

George Moutsopoulos