tmalsburg/helm-bibtex

Ivy-bibtex cannot insert citation

Closed this issue · 4 comments

First of all, thanks for the great tools. I tried to use ivy-bibtex to insert a citation, however everytime I found the reference from the bib file and tried to insert it it the text, it gave me the the error of wrong type argument listp for the publication year. Helm-bibtex works fine, just tried to get ivy version works right too. The following is the message using toggle-debug-on-error:

Debugger entered--Lisp error: (wrong-type-argument listp "1983")
  mapconcat(identity ("1983 On the Convergence Properties of the \\EM\\ Algorithm Wu, C F J article Wu83" ("year" . "1983") ("title" . "{On the Convergence Properties of the {\\{}EM{\\}} Algorithm}") ("author" . "Wu, C F J") ("=type=" . "article") ("=key=" . "Wu83")) ", ")
  s-join(", " ("1983 On the Convergence Properties of the \\EM\\ Algorithm Wu, C F J article Wu83" ("year" . "1983") ("title" . "{On the Convergence Properties of the {\\{}EM{\\}} Algorithm}") ("author" . "Wu, C F J") ("=type=" . "article") ("=key=" . "Wu83")))
  (format "\\%s{%s}" cite-command (s-join ", " keys))
  (if (and (string= "" prenote) (string= "" postnote)) (format "\\%s{%s}" cite-command (s-join ", " keys)) (format "\\%s[%s][%s]{%s}" cite-command prenote postnote (s-join ", " keys)))
  (let ((prenote (if bibtex-completion-cite-prompt-for-optional-arguments (read-from-minibuffer "Prenote: ") "")) (postnote (if bibtex-completion-cite-prompt-for-optional-arguments (read-from-minibuffer "Postnote: ") ""))) (if (and (string= "" prenote) (string= "" postnote)) (format "\\%s{%s}" cite-command (s-join ", " keys)) (format "\\%s[%s][%s]{%s}" cite-command prenote postnote (s-join ", " keys))))
  (if (member cite-command '("nocite" "supercite")) (format "\\%s{%s}" cite-command (s-join ", " keys)) (let ((prenote (if bibtex-completion-cite-prompt-for-optional-arguments (read-from-minibuffer "Prenote: ") "")) (postnote (if bibtex-completion-cite-prompt-for-optional-arguments (read-from-minibuffer "Postnote: ") ""))) (if (and (string= "" prenote) (string= "" postnote)) (format "\\%s{%s}" cite-command (s-join ", " keys)) (format "\\%s[%s][%s]{%s}" cite-command prenote postnote (s-join ", " keys)))))
  (let* ((initial (if bibtex-completion-cite-default-as-initial-input (progn bibtex-completion-cite-default-command))) (default (if bibtex-completion-cite-default-as-initial-input nil bibtex-completion-cite-default-command)) (default-info (if default (format " (default \"%s\")" default) "")) (cite-command (completing-read (format "Cite command%s: " default-info) bibtex-completion-cite-commands nil nil initial 'bibtex-completion-cite-command-history default nil))) (if (member cite-command '("nocite" "supercite")) (format "\\%s{%s}" cite-command (s-join ", " keys)) (let ((prenote (if bibtex-completion-cite-prompt-for-optional-arguments (read-from-minibuffer "Prenote: ") "")) (postnote (if bibtex-completion-cite-prompt-for-optional-arguments (read-from-minibuffer "Postnote: ") ""))) (if (and (string= "" prenote) (string= "" postnote)) (format "\\%s{%s}" cite-command (s-join ", " keys)) (format "\\%s[%s][%s]{%s}" cite-command prenote postnote (s-join ", " keys))))))
  (cond ((and (require 'reftex-parse nil t) (setq macro (reftex-what-macro 1)) (stringp (car macro)) (string-match "\\`\\\\cite\\|cite\\'" (car macro))) (delete-horizontal-space) (concat (let* ((val (preceding-char))) (cond ((eq val 123) "") ((eq val 44) " ") (t ", "))) (s-join ", " keys) (if (member (following-char) '(125 44)) "" ", "))) ((and (equal (preceding-char) 125) (require 'reftex-parse nil t) (save-excursion (forward-char -1) (setq macro (reftex-what-macro 1))) (stringp (car macro)) (string-match "\\`\\\\cite\\|cite\\'" (car macro))) (delete-char -1) (delete-horizontal-space t) (concat (let* ((val (preceding-char))) (cond ((eq val 123) "") ((eq val 44) " ") (t ", "))) (s-join ", " keys) "}")) (t (let* ((initial (if bibtex-completion-cite-default-as-initial-input (progn bibtex-completion-cite-default-command))) (default (if bibtex-completion-cite-default-as-initial-input nil bibtex-completion-cite-default-command)) (default-info (if default (format " (default \"%s\")" default) "")) (cite-command (completing-read (format "Cite command%s: " default-info) bibtex-completion-cite-commands nil nil initial 'bibtex-completion-cite-command-history default nil))) (if (member cite-command '("nocite" "supercite")) (format "\\%s{%s}" cite-command (s-join ", " keys)) (let ((prenote (if bibtex-completion-cite-prompt-for-optional-arguments (read-from-minibuffer "Prenote: ") "")) (postnote (if bibtex-completion-cite-prompt-for-optional-arguments (read-from-minibuffer "Postnote: ") ""))) (if (and (string= "" prenote) (string= "" postnote)) (format "\\%s{%s}" cite-command (s-join ", " keys)) (format "\\%s[%s][%s]{%s}" cite-command prenote postnote (s-join ", " keys))))))))
  (let (macro) (cond ((and (require 'reftex-parse nil t) (setq macro (reftex-what-macro 1)) (stringp (car macro)) (string-match "\\`\\\\cite\\|cite\\'" (car macro))) (delete-horizontal-space) (concat (let* ((val (preceding-char))) (cond ((eq val 123) "") ((eq val 44) " ") (t ", "))) (s-join ", " keys) (if (member (following-char) '(125 44)) "" ", "))) ((and (equal (preceding-char) 125) (require 'reftex-parse nil t) (save-excursion (forward-char -1) (setq macro (reftex-what-macro 1))) (stringp (car macro)) (string-match "\\`\\\\cite\\|cite\\'" (car macro))) (delete-char -1) (delete-horizontal-space t) (concat (let* ((val (preceding-char))) (cond ((eq val 123) "") ((eq val 44) " ") (t ", "))) (s-join ", " keys) "}")) (t (let* ((initial (if bibtex-completion-cite-default-as-initial-input (progn bibtex-completion-cite-default-command))) (default (if bibtex-completion-cite-default-as-initial-input nil bibtex-completion-cite-default-command)) (default-info (if default (format " (default \"%s\")" default) "")) (cite-command (completing-read (format "Cite command%s: " default-info) bibtex-completion-cite-commands nil nil initial 'bibtex-completion-cite-command-history default nil))) (if (member cite-command '("nocite" "supercite")) (format "\\%s{%s}" cite-command (s-join ", " keys)) (let ((prenote (if bibtex-completion-cite-prompt-for-optional-arguments (read-from-minibuffer "Prenote: ") "")) (postnote (if bibtex-completion-cite-prompt-for-optional-arguments (read-from-minibuffer "Postnote: ") ""))) (if (and (string= "" prenote) (string= "" postnote)) (format "\\%s{%s}" cite-command (s-join ", " keys)) (format "\\%s[%s][%s]{%s}" cite-command prenote postnote (s-join ", " keys)))))))))
  bibtex-completion-format-citation-cite(("1983 On the Convergence Properties of the \\EM\\ Algorithm Wu, C F J article Wu83" ("year" . "1983") ("title" . "{On the Convergence Properties of the {\\{}EM{\\}} Algorithm}") ("author" . "Wu, C F J") ("=type=" . "article") ("=key=" . "Wu83")))
  funcall(bibtex-completion-format-citation-cite ("1983 On the Convergence Properties of the \\EM\\ Algorithm Wu, C F J article Wu83" ("year" . "1983") ("title" . "{On the Convergence Properties of the {\\{}EM{\\}} Algorithm}") ("author" . "Wu, C F J") ("=type=" . "article") ("=key=" . "Wu83")))
  (insert (funcall format-function keys))
  (let ((format-function (cdr (or (assoc major-mode bibtex-completion-format-citation-functions) (assoc 'default bibtex-completion-format-citation-functions))))) (insert (funcall format-function keys)))
  bibtex-completion-insert-citation(("1983 On the Convergence Properties of the \\EM\\ Algorithm Wu, C F J article Wu83" ("year" . "1983") ("title" . "{On the Convergence Properties of the {\\{}EM{\\}} Algorithm}") ("author" . "Wu, C F J") ("=type=" . "article") ("=key=" . "Wu83")))
  ivy-call()
  ivy-read("BibTeX entries: " (("1998 Integration of Data Mining and Case-based Reasoning Dingsoyr, T inproceedings Ding98" ("year" . "1998") ("title" . "{Integration of Data Mining and Case-based Reasoning}") ("author" . "Dingsoyr, T") ("=type=" . "inproceedings") ("=key=" . "Ding98")) ("1983 On the Convergence Properties of the \\EM\\ Algorithm Wu, C F J article Wu83" ("year" . "1983") ("title" . "{On the Convergence Properties of the {\\{}EM{\\}} Algorithm}") ("author" . "Wu, C F J") ("=type=" . "article") ("=key=" . "Wu83"))) :preselect nil :caller ivy-bibtex :action bibtex-completion-insert-citation)
  (let* ((candidates (bibtex-completion-candidates)) (key (bibtex-completion-key-at-point)) (preselect (and key (cl-position-if (function (lambda (cand) (member (cons "=key=" key) (cdr cand)))) candidates)))) (ivy-read (format "BibTeX entries%s: " (if local-bib " (local)" "")) candidates :preselect preselect :caller 'ivy-bibtex :action ivy-bibtex-default-action))
  ivy-bibtex(nil)
  funcall-interactively(ivy-bibtex nil)
  call-interactively(ivy-bibtex nil nil)
  command-execute(ivy-bibtex)

Hi, I'm not using ivy-bibtex myself but I just tried it and couldn't reproduce the problem you describe. Does it happen with all entries or just with specific entries? It's a bit strange that you don't get the same problem with helm-bibtex because they're using the same code for inserting citations. A minimal reproducible example (with emacs -q) would be very helpful.

Hi, sorry for the late response. It happens with all the entries. The following is my configuration for ivy-bibtex and a sample bib file:

(use-package ivy-bibtex
  :ensure t
  :bind ("C-c b b" . ivy-bibtex)
  :config
  ;(setq bibtex-completion-bibliography 
  ;      '("~/Dropbox/bib/library.bib"))
  (setq bibtex-completion-bibliography 
        '("~/library.bib"))
  (setq bibtex-completion-library-path 
        '("~/Dropbox/bib/pdfs"))

  ;; using bibtex path reference to pdf file
  (setq bibtex-completion-pdf-field "file")

  ;;open pdf with external viwefoxit
  (setq helm-bibtex-pdf-open-function
        (lambda (fpath)
          (start-process "open" "*open*" "open" fpath)))

  (setq ivy-bibtex-default-action 'bibtex-completion-insert-citation))

Here is a sample bib files that leads to the problem:

@article{Paciorek2003,
author = {Paciorek, Christopher Joseph},
doi = {10.1371/journal.pone.0019736},
journal = {Carnegie Mellon University},
number = {May},
pages = {258},
title = {{Nonstationary Gaussian processes for regression and spatial modelling}},
volume = {6},
year = {2003}
}
@article{Di2017,
author = {Di, Qian and Wang, Yan and Zanobetti, Antonella and Wang, Yun and Koutrakis, Petros and Choirat, Christine and Dominici, Francesca and Schwartz, Joel D.},
doi = {10.1056/NEJMoa1702747},
issn = {0028-4793},
journal = {New England Journal of Medicine},
month = {jun},
number = {26},
pages = {2513--2522},
publisher = {Massachusetts Medical Society},
title = {{Air Pollution and Mortality in the Medicare Population}},
volume = {376},
year = {2017}
}

I think it's your configuration. Please trying replacing this:

(setq ivy-bibtex-default-action 'bibtex-completion-insert-citation)

with this:

(setq ivy-bibtex-default-action 'ivy-bibtex-insert-citation)

I'm closing this issue. Feel free to reopen if the problem persists.

Great, yes, the problem is gone. Thanks!