emacs-citar / citar

Emacs package to quickly find and act on bibliographic references, and edit org, markdown, and latex academic documents.
GNU General Public License v3.0
479 stars 53 forks source link

Add options to customize citar-add-file-to-library #806

Closed mpedramfar closed 8 months ago

mpedramfar commented 8 months ago

PR for issue #804

bdarcus commented 8 months ago

Thanks!

The CI has flagged a few things you'll want to fix; seems mostly documentation related.

Also, note: we use conventional commit guidelines. If you want, you can rewrite to match them, or I can squash merge when done.

Related: you can just write "Close #840" and github will close the issue when the PR is merged.

mpedramfar commented 8 months ago

Thanks for the tips! It's probably easiest if you squash merge them when done.

bdarcus commented 8 months ago

This is really weird, and I guess a bug in GH. But @roshanshariff left a comment that is not now showing in the web UI.

I'll reproduce it below just to be sure:

Instead of checking citar-add-file-sources here, add a clause to the citar--check-configuration function and just call (citar--check-configuration 'citar-add-file-source). We'd like to centralize all such validation in one place.

If it shows up, I'll delete this.

roshanshariff commented 8 months ago

@bdarcus Sorry, I was doing a code review and accidentally submitted a single comment instead of submitting as a batch.

I like the general direction of this change. I just thought the UI for selecting a file source could be simplified using the read-multiple-choice function instead of reimplementing similar functionality. For example, suppose citar-add-file-sources was defined as follows:

(setq citar-add-file-sources
  '((?b "buffer" "current buffer" citar-add-file--from-buffer)
    (?f "file" "existing file" citar-add-file--from-file)
    (?u "url" "download from URL" citar-add-file--from-url)))

Then the code to select a file source would just be

(nth 3 (read-multiple-choice "Add file from" citar-add-file-sources))

This handles highlighting the shortcut character, shows extra help if requested, etc.

mpedramfar commented 8 months ago

I added the check for citar-save-file-function to citar--check-configuration. We can add the check for citar-add-file-sources after we're certain about its type.

When it comes to citar--check-configuration, there are some things in the code seem inconsistent.

  1. error vs user-error: Currently citar--check-configuration checks if the type of citar-library-paths is correct (a list of strings) but it doesn't check if it's non-nil. If the type is incorrect, it raises an error, with backtrace and everythin. Inside citar-add-file-to-library it's checked if this variable is non-nil and if it is nil, it raises a user-error.

It's not clear to me why error is being used at all. Wouldn't user-error be better? And wouldn't it be better if we check if citar-library-paths is non-nil inside citar--check-configuration?

  1. There seems to be a lot of customization options that do not appear in citar--check-configuration. Here are the the source/resource variables:
    • citar-open-resources: not checked
    • citar-file-sources: not checked
    • citar-notes-sources: not checked in citar--check-configuration. Instead, there is a separate function that checks a single source (i.e. citar--check-notes-source) and this is only called from citar-register-notes-source. If the user changes citar-notes-sources using setq or the customize interface, it won't be checked at all.

Wouldn't it be better to move all the checks to citar--check-configuration?

roshanshariff commented 8 months ago

Another small suggestion: instead of having the file sources return a (function . plist), just have them return a plist with the function included under a :write-file key. In the future, we could have other kinds of file sources that don't fit that abstraction (e.g. open and return a new Emacs buffer) and expose actions other than :write-file.

roshanshariff commented 8 months ago

@mpedramfar, yeah the usage of citar--check-configuration is somewhat inconsistent at the moment. Ideally, it would check the types of customization variables and raise user-error. Any functions that have additional requirements (like citar-library-paths being non-empty) would do that check on their own, after the call to citar--check-configuration.

New code should follow this pattern, and over time the old code can be changed as well.

EDIT: The idea is that citar--check-configuration could eventually be replaced (as far as possible) with an automated function that checks its value against the defcustom :type. Any functions that have additional preconditions will need to do their own checking.

mpedramfar commented 8 months ago

I see, there is a use of citar-library-paths that doesn't need it being non-nil, so it makes sense to check it only when we need it to be non-nil. There is no use case of citar-add-file-sources when it is nil. Is there a way to specify this in defcustom :type? Should we check for citar-add-file-sources being non-nil in citar--check-configuration or citar-add-file-to-library?

roshanshariff commented 8 months ago

Now that I look at it, it doesn't seem like there's a good built-in type for non-nil lists. I think it's fine to go back to what you had, just checking the list is non-nil before using it.

bdarcus commented 8 months ago

So what's the status here, given the back-and-forth?

mpedramfar commented 8 months ago

I applied the comments. Let me know if there are other issues to fix.

roshanshariff commented 8 months ago

I pushed a couple of commits with some changes. I also tested the code a little, but @bdarcus if you have time to give it a whirl, that couldn't hurt :)

If we don't discover any issues, I'm okay with merging now!

Thanks @mpedramfar for suggesting this change! I'm looking forward to writing more functions to do things like saving to Zotero libraries (which is what I use). Then we can probably rename citar-add-file-to-library to just citar-add-file. When I have time, I'd like to collect some of the integration code that's floating around into a citar-zotero package.

mpedramfar commented 8 months ago

Thanks for the tips!

Using write-region makes sense. I think the only exception is the one you mentioned, when the buffer is visiting the destfile that we want to save into. In that case, calling write-region saves the buffer to its file, but calling buffer-modified-p returns t. So it makes sense to simply call save-buffer instead. I implemented that in the last commit.

As a side-note, you might find the Zotra package useful. It basically gives Emacs one of the main features of Zotero, i.e. capturing bib entries and attachments from the web. After this PR is merged, I'll add the integration code to Zotra, so that it can be added as one of the sources in citar-add-file-sources.

bdarcus commented 8 months ago

CI is complaining :-)

mpedramfar commented 8 months ago

Should be ok now :)

bdarcus commented 8 months ago

Merged; thanks!

I did test the basics, and didn't see any issues. But I don't tend to use this command anyway!