joostkremers / ebib

A BibTeX database manager for Emacs.
https://joostkremers.github.io/ebib/
BSD 3-Clause "New" or "Revised" License
275 stars 37 forks source link

`ebib-import-file` file rename #294

Open rLry opened 6 months ago

rLry commented 6 months ago

Hello, I just started switching from zotero to ebib When I use ebib-import-file, the pdf is imported into the database and renamed to entry key.pdf, which is great. But I'm confused, is there a function how to customize the pdf name to date-titile.pdf? Thanks.

joostkremers commented 6 months ago

Hi, the user option ebib-name-transform-function holds a function that takes a key and converts it to a file name (without the .pdf extension, which is added automatically). To convert the key to the format date-title.pdf, you'd need to write a function that takes the key, retrieves the title and returns a string of the desired format (without the extension.)

You can retrieve the title from the database with the function ebib-get-field-value, something like this:

(ebib-get-field-value "Title" "<key>" ebib--cur-db t t)

Check the doc string for ebib-get-field-value for details, you may want to pass slightly different arguments.

rLry commented 5 months ago

I know very little about Elisp. According to your opinion, the following code is written by ChatGPT. It is right for me. Thanks.

(defun my/ebib-name-transform-function (key)
"Generate a filename based on the year, title, and publisher of the entry."
(let* ((year (ebib-get-field-value "year" key ebib--cur-db t t))
  (title (ebib-get-field-value "title" key ebib--cur-db t t))
  (publisher (ebib-get-field-value "publisher" key ebib--cur-db t t)))
 (if (and year title publisher)
 (format "%s-%s-%s" year title publisher)
 (progn
 (message "Year, title or publisher missing in entry %s" key)
 nil))))

(setq ebib-name-transform-function 'my/ebib-name-transform-function)
joostkremers commented 5 months ago

Well, let me just say that ChatGPT isn't much of an Elisp programmer... :laughing:

I'll take a look later today or tomorrow and come up with something better. :slightly_smiling_face:

joostkremers commented 4 months ago

If you want to create a file name based on year, title and publisher, you can use something like this:

(defun my/ebib-name-transform-function (key)
  "Generate a filename based on the year, title, and publisher of the entry."
  (let ((year (ebib-get-field-value "year" key ebib--cur-db "XXXX" t))
        (title (ebib-get-field-value "title" key ebib--cur-db "No_Title" t))
        (publisher (ebib-get-field-value "publisher" key ebib--cur-db "No_Publisher" t)))
    (format "%s-%s-%s" year title publisher)))

This way you'll get default strings if year, title or publisher are not available.

If, on the other hand, you want to get an error if one of these is missing, you can use the following:

(defun my/ebib-name-transform-function (key)
  "Generate a filename based on the year, title, and publisher of the entry."
  (let ((year (ebib-get-field-value "year" key ebib--cur-db nil t))
        (title (ebib-get-field-value "title" key ebib--cur-db nil t))
        (publisher (ebib-get-field-value "publisher" key ebib--cur-db nil t)))
    (format "%s-%s-%s" year title publisher)))
joostkremers commented 4 months ago

Other variations are possible, of course, depending on your exact preferences.

rLry commented 4 months ago

thanks~It's good