1

Executing the following code should create, for each top level tree in your file ~/tmp/file.org, an other file in ~/tmp named after the tree heading and containing its content.

Problem: it does not work.

The content is not the one expected...

Is it me or the code that is wrong? Both?

(let ()
  (with-temp-buffer
    (org-mode)
    (insert-file-contents "~/tmp/file.org")
    (set-mark (point-min))
    (goto-char (point-max))

    (org-map-entries
     (lambda ()
       (let ((entry_id (replace-regexp-in-string " " "_" (downcase (nth 4 (org-heading-components))))))
         (org-set-property
          "EXPORT_FILE_NAME"
          (expand-file-name entry_id "~/tmp")))
       (org-org-export-to-org nil t)
       )
     nil
     'region-start-level)))

Content of file.org:

* Headline 1                                                          :ready:

  Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Praesent libero orci,
  auctor sed, faucibus vestibulum, gravida vitae, arcu. Nunc posuere. Suspendisse
  potenti. Praesent in arcu ac nisl ultricies ultricies. Fusce eros. Sed pulvinar
  vehicula ante. Maecenas urna dolor, egestas vel, tristique et, porta eu,
  leo. Curabitur vitae sem eget arcu laoreet vulputate. Cras orci neque, faucibus
  et, rhoncus ac, venenatis ac, magna. Aenean eu lacus. Aliquam luctus facilisis
  augue. Nullam fringilla consectetuer sapien. Aenean neque augue, bibendum a,
  feugiat id, lobortis vel, nunc. Suspendisse in nibh quis erat condimentum
  pretium. Vestibulum tempor odio et leo. Sed sodales vestibulum justo. Cras
  convallis pellentesque augue. In eu magna. In pede turpis, feugiat pulvinar,
  sodales eget, bibendum consectetuer, magna. Pellentesque vitae augue.

  [[file:data/fool.jpg][Foo]]



* Headline 2                                                          :ready:

  Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Praesent libero orci,
  auctor sed, faucibus vestibulum, gravida vitae, arcu. Nunc posuere. Suspendisse
  potenti. Praesent in arcu ac nisl ultricies ultricies. Fusce eros. Sed pulvinar
  vehicula ante. Maecenas urna dolor, egestas vel, tristique et, porta eu,
  leo. Curabitur vitae sem eget arcu laoreet vulputate. Cras orci neque, faucibus
  et, rhoncus ac, venenatis ac, magna. Aenean eu lacus. Aliquam luctus facilisis
  augue. Nullam fringilla consectetuer sapien. Aenean neque augue, bibendum a,
  feugiat id, lobortis vel, nunc. Suspendisse in nibh quis erat condimentum
  pretium. Vestibulum tempor odio et leo. Sed sodales vestibulum justo. Cras
  convallis pellentesque augue. In eu magna. In pede turpis, feugiat pulvinar,
  sodales eget, bibendum consectetuer, magna. Pellentesque vitae augue.

  [[file:data/fool-2.jpg][Foo2]]

Expected result:

heading_1.org:

* Headline 1                                                          :ready:

  Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Praesent libero orci,
  auctor sed, faucibus vestibulum, gravida vitae, arcu. Nunc posuere. Suspendisse
  potenti. Praesent in arcu ac nisl ultricies ultricies. Fusce eros. Sed pulvinar
  vehicula ante. Maecenas urna dolor, egestas vel, tristique et, porta eu,
  leo. Curabitur vitae sem eget arcu laoreet vulputate. Cras orci neque, faucibus
  et, rhoncus ac, venenatis ac, magna. Aenean eu lacus. Aliquam luctus facilisis
  augue. Nullam fringilla consectetuer sapien. Aenean neque augue, bibendum a,
  feugiat id, lobortis vel, nunc. Suspendisse in nibh quis erat condimentum
  pretium. Vestibulum tempor odio et leo. Sed sodales vestibulum justo. Cras
  convallis pellentesque augue. In eu magna. In pede turpis, feugiat pulvinar,
  sodales eget, bibendum consectetuer, magna. Pellentesque vitae augue.

  [[file:data/fool.jpg][Foo]]

heading_2.org:

* Headline 2                                                          :ready:

  Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Praesent libero orci,
  auctor sed, faucibus vestibulum, gravida vitae, arcu. Nunc posuere. Suspendisse
  potenti. Praesent in arcu ac nisl ultricies ultricies. Fusce eros. Sed pulvinar
  vehicula ante. Maecenas urna dolor, egestas vel, tristique et, porta eu,
  leo. Curabitur vitae sem eget arcu laoreet vulputate. Cras orci neque, faucibus
  et, rhoncus ac, venenatis ac, magna. Aenean eu lacus. Aliquam luctus facilisis
  augue. Nullam fringilla consectetuer sapien. Aenean neque augue, bibendum a,
  feugiat id, lobortis vel, nunc. Suspendisse in nibh quis erat condimentum
  pretium. Vestibulum tempor odio et leo. Sed sodales vestibulum justo. Cras
  convallis pellentesque augue. In eu magna. In pede turpis, feugiat pulvinar,
  sodales eget, bibendum consectetuer, magna. Pellentesque vitae augue.

  [[file:data/fool-2.jpg][Foo]]

Maybe a duplicate of : this question but did not help...

GNU Emacs 25.1.1 (x86_64-apple-darwin13.4.0, NS appkit-1265.21 Version 10.9.5 (Build 13F1911)) of 2016-09-17

Org-mode version 8.3.6

0

ANSWER

Well, ended up implementing my function. So far, it is working:

(defun me/org-map-entries (org-file in-tags func)
  (let ((tags (if (stringp in-tags)
                   (list in-tags)
                 in-tags)))

    (with-temp-buffer
      (org-mode)
      (insert-file-contents org-file-main)

      ;; Execute func at each heading that matches tags.
      (while (< (point) (point-max))

        ;; If find a heading...
        (and (search-forward-regexp "^\* " nil "end")

             ;; ...that matches the given tags...
             (seq-reduce
              (lambda(a b) (and a b))
              (mapcar
               (lambda (tag)
                 (beginning-of-line)
                 (search-forward-regexp
                  (concat ":" tag ":") (line-end-position) "end"))
               tags)
              t)

             ;; ... then execute given function with cursor at beginning of
             ;; heading.
             (progn
               (beginning-of-line)
               (save-excursion
                 (funcall func))
               (end-of-line)))))))
  • Note that (seq-reduce (lambda (a b) (and a b)) SEQUENCE t) can be simplified to (seq-every-p #'identity SEQUENCE). – Basil May 21 '17 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.