2016-09-29 21 views
0

我不確定這個問題的最佳表達方式,但希望我的示例能夠清楚地說明發生了什麼。Emacs bibtex模式無法解析未訪問文件

我有一些代碼,我想在一個臨時緩衝區中插入bibtex文件的內容,並一次移動一個條目,使用bibtex-parse-entry獲取條目供以後使用。但是,每當我在emacs會話期間沒有訪問過的bibtex文件上運行代碼時,bibtex-parse-entry將返回(wrong-type-argument stringp nil)錯誤。

一旦我訪問該文件,即使我然後關閉緩衝區,代碼運行沒有任何問題。如果我刪除bibtex-parse-entry呼叫,bibtex-kill-entry也有同樣的問題。

下面是我用的elisp代碼:

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

和虛擬文件名爲.bib:

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

有了這些,你應該能夠重現我的錯誤。

我不知道發生了什麼,所以我非常感謝任何幫助!

回答

1

我不是這方面的專家。我只是調試了一下情況(在這種情況下嘗試M-x toggle-debug-on-error),並發現looking-at的值爲nil。堆棧跟蹤告訴我們問題出在bibtex函數bibtex-valid-entry中。在那裏,我發現變量bibtex-entry-maybe-empty-head - 根據其文檔字符串 - 由bibtex-set-dialect設置。

因此,在調用bibtex-mode後將函數添加到bibtex-set-dialect似乎解決了該問題。因爲我不知道,最終你想達到什麼目的,我不確定它是否能夠解決你的問題。至少該函數確實會引發一個錯誤。

希望,這是有道理的,並幫助。

(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))) 
+0

工作正常!謝謝你的幫助。我試圖進行調試,但是我還沒有弄清楚調試elisp代碼的情況(我的大部分經驗都是在Python和Matlab中,它們的工作方式非常不同)。 –