Skip to content

Commit f6f2864

Browse files
committed
Added support for Zotero/Mendeley/JabRef-style file field for PDFs.
Bare paths to PDFs work, too.
1 parent c4e0201 commit f6f2864

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

helm-bibtex.el

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ bibliography files were last parsed.")
296296
"Checks that the files and directories specified by the user
297297
actually exist."
298298
(mapc (lambda (file)
299-
(unless (f-exists? file)
299+
(unless (f-file? file)
300300
(user-error "BibTeX file %s could not be found." file)))
301301
(-flatten (list helm-bibtex-bibliography))))
302302

@@ -402,19 +402,33 @@ file is specified, or if the specified file does not exist, or if
402402
(let* ((entry (if (stringp key-or-entry)
403403
(helm-bibtex-get-entry1 key-or-entry t)
404404
key-or-entry))
405-
(path (helm-bibtex-get-value helm-bibtex-pdf-field entry)))
406-
(when (f-exists? path) path))))
405+
(value (helm-bibtex-get-value helm-bibtex-pdf-field entry)))
406+
(cond
407+
((not value) nil) ; Field not defined.
408+
((f-file? value) value) ; A bare path was found.
409+
(t
410+
; Assuming Zotero/Mendeley/JabRef format:
411+
(cl-loop ; Looping over the files:
412+
for record in (s-split ";" value)
413+
for record = (s-split ":" record)
414+
for file-name = (nth 0 record)
415+
for path = (nth 1 record)
416+
if (f-file? path)
417+
collect (f-full path)
418+
else if (f-file? (f-full (f-join path file-name)))
419+
collect (f-full (f-join path file-name))))))))
407420

408421
(defun helm-bibtex-find-pdf-in-library (key-or-entry)
409422
"Searches the directories in `helm-bibtex-library-path' for a
410423
PDF whose names is composed of the BibTeX key plus \".pdf\". The
411424
path of the first matching PDF is returned."
412-
(let ((key (if (stringp key-or-entry)
413-
key-or-entry
414-
(helm-bibtex-get-value "=key=" key-or-entry))))
415-
(-first 'f-exists?
416-
(--map (f-join it (s-concat key ".pdf"))
417-
(-flatten (list helm-bibtex-library-path))))))
425+
(let* ((key (if (stringp key-or-entry)
426+
key-or-entry
427+
(helm-bibtex-get-value "=key=" key-or-entry)))
428+
(x (-first 'f-file?
429+
(--map (f-join it (s-concat key ".pdf"))
430+
(-flatten (list helm-bibtex-library-path))))))
431+
(when x (list x))))
418432

419433
(defun helm-bibtex-find-pdf (key-or-entry)
420434
"Returns the path of the PDF associated with the specified
@@ -440,17 +454,17 @@ find a PDF file."
440454
(entry (if (and (not do-not-find-pdf) (helm-bibtex-find-pdf entry))
441455
(setq entry (cons (cons "=has-pdf=" helm-bibtex-pdf-symbol) entry))
442456
entry))
457+
(entry-key (cdr (assoc "=key=" entry)))
443458
; Check for notes:
444459
(entry (if (and helm-bibtex-notes-path
445-
(f-exists? (f-join helm-bibtex-notes-path
446-
(s-concat entry-key helm-bibtex-notes-extension))))
460+
(f-file? (f-join helm-bibtex-notes-path
461+
(s-concat entry-key helm-bibtex-notes-extension))))
447462
(cons (cons "=has-note=" helm-bibtex-notes-symbol) entry)
448463
entry))
449464
; Remove unwanted fields:
450465
(entry (if fields
451466
(--filter (member-ignore-case (car it) fields) entry)
452-
entry))
453-
(entry-key (cdr (assoc "=key=" entry))))
467+
entry)))
454468
;; Normalize case of entry type:
455469
(setcdr (assoc "=type=" entry) (downcase (cdr (assoc "=type=" entry))))
456470
;; Remove duplicated fields:
@@ -522,7 +536,7 @@ function specified in `helm-bibtex-pdf-open-function'. All paths
522536
in `helm-bibtex-library-path' are searched. If there are several
523537
matching PDFs for an entry, the first is opened."
524538
(--if-let
525-
(-non-nil
539+
(-flatten
526540
(-map 'helm-bibtex-find-pdf (helm-marked-candidates :with-wildcard t)))
527541
(-each it helm-bibtex-pdf-open-function)
528542
(message "No PDF(s) found.")))
@@ -581,10 +595,10 @@ for arguments if the commands can take any."
581595
"Formatter for org-links to PDF. Uses first matching PDF if
582596
several are available. Entries for which no PDF is available are
583597
omitted."
584-
(s-join ", " (-non-nil
585-
(--map (when (cdr it)
586-
(format "[[%s][%s]]" (cdr it) (car it)))
587-
(--map (cons it (helm-bibtex-find-pdf it)) keys)))))
598+
(s-join ", " (cl-loop
599+
for key in keys
600+
for pdfs = (helm-bibtex-find-pdf key)
601+
append (--map (format "[[%s][%s]]" it key) pdfs))))
588602

589603
(defun helm-bibtex-insert-citation (_)
590604
"Insert citation at point. The format depends on
@@ -787,7 +801,7 @@ defined. Surrounding curly braces are stripped."
787801
(defun helm-bibtex-add-PDF-attachment (_)
788802
"Attach the PDFs of the selected entries where available."
789803
(--if-let
790-
(-non-nil
804+
(-flatten
791805
(-map 'helm-bibtex-find-pdf (helm-marked-candidates :with-wildcard t)))
792806
(-each it 'mml-attach-file)
793807
(message "No PDF(s) found.")))

0 commit comments

Comments
 (0)