Search code examples
elisporg-mode

Wrong type argument: stringp, nil with org-map-entries


I've encountered an error which I can't resolve. I have a file test.org which simply contains a first level headline: * test

(with-temp-buffer
  (insert-file-contents "~/test.org")
  (goto-char (point-min))
  (org-map-entries
   (lambda ()
     (compare-strings
      "* test\n" nil nil
      (thing-at-point 'line t) nil nil
      t))
   "LEVEL=1"))

This returns Wrong type argument: stringp, nil. The org-map-entries function works normally, but there seems to be a problem when it is used with with-temp-buffer.


Solution

  • The temp buffer is in fundamental mode and nothing you do in your code changes that. OTOH, the org- functions assume that the buffer is in Org mode and (sometimes) barf if that is not the case.

    Try this:

    (with-temp-buffer
      (org-mode)
      (insert-file-contents "~/test.org")
      (goto-char (point-min))
      (org-map-entries
       (lambda ()
         (compare-strings
          "* test\n" nil nil
          (thing-at-point 'line t) nil nil
          t))
       "LEVEL=1"))