0

I'm not sure the best way to phrase this question, but hopefully my examples will make clear what's going on.

I have some code where I want to insert the contents of a bibtex file in a temporary buffer and move through the entries one at a time, grabbing the entry using bibtex-parse-entry for later use. However, whenever I run the code on a bibtex file that I haven't visited during this emacs session, bibtex-parse-entry returns a (wrong-type-argument stringp nil) error.

Once I visit the file, even if I then close the buffer, the code runs without any issues. And if I remove the bibtex-parse-entry call, bibtex-kill-entry has the same issue.

Here's the elisp code I'm using:

(with-temp-buffer
  (insert-file-contents "~/test.bib")
  (goto-char (point-min))
  (bibtex-mode)
  (while (not (eobp))
    (let* ((entry (bibtex-parse-entry t)))
      (message "i'm here"))
    (bibtex-kill-entry)
    (bibtex-beginning-of-entry)
    )
  )

and a dummy .bib file:

@Article{test,
  author =   {joe shmo},
  title =    {lorem ipsum},
  journal =      {something},
  year =     {1990},
}

With these you should be able to reproduce my error.

I have no idea what's going on, so I'd greatly appreciate any help!

Bill Broderick
  • 303
  • 1
  • 2
  • 10

1 Answers1

1

I am not really an expert at this. I just debugged the situation a bit (try M-x toggle-debug-on-error in cases like this) and found a call to looking-at with a nil value. The stack-trace tells us that the problem is in the bibtex function bibtex-valid-entry. There, I found the variable bibtex-entry-maybe-empty-head which --according to its docstring-- is set by bibtex-set-dialect.

Thus, adding a call to bibtex-set-dialect to your function after calling bibtex-mode seems to fix the issue. As I do not really know, what you want to achieve in the end, I am not sure it actually fixes your problem. At least the function does raise an error anymore.

Hope, that makes sense and helps.

(with-temp-buffer
  (insert-file-contents "~/test.bib")
  (goto-char (point-min))
  (bibtex-mode)
  (bibtex-set-dialect) ;; <-- add this
  (while (not (eobp))
    (let* ((entry (bibtex-parse-entry t)))
    (message "i'm here"))
    (bibtex-kill-entry)
    (bibtex-beginning-of-entry)))
Stefan Kamphausen
  • 1,615
  • 15
  • 20
  • That worked! Thanks for your help. I was trying to debug but I haven't gotten the hang of debugging elisp code yet (most of my experience is with Python and Matlab, which work very differently). – Bill Broderick Sep 30 '16 at 21:57