Search code examples
emacsorg-mode

Emacs org-mode insert text after heading properties


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?


Solution

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