Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support adding files in directories to context #438

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

benthamite
Copy link
Contributor

Currently, attempting to add directories to the context in Dired will throw an error. This PR provides support for adding files in directories to the context: if there is a directory at point, or the marked files include one or more directories, their files will recursively be added to the context.

@karthink
Copy link
Owner

Looks good! I'll test it when I can and merge.

@benthamite
Copy link
Contributor Author

I just noticed this in the docstring of this function (emphasis added):

If a region is selected, add the selected region to the context. If there is already a gptel context at point, remove it instead.

I may not be understanding what this means, but as far as I can tell the function does not behave in the manner described: if I select a region, add it to the context, then re-select it and call gptel-add again, the region is not removed from the context. (The docstring talks about the "context at point" rather than the region, but I am not sure what that means in this, er, context. 😛)

@karthink
Copy link
Owner

karthink commented Nov 5, 2024

I may not be understanding what this means, but as far as I can tell the function does not behave in the manner described: if I select a region, add it to the context, then re-select it and call gptel-add again, the region is not removed from the context.

It could probably be worded better. Here is an updated description, let me know if it makes more sense:

gptel-add is an alias for ‘gptel-context-add’ in ‘gptel-context.el’.

(gptel-add &optional ARG)

Add context to gptel in a DWIM fashion.

- If a region is selected, add the selected region to the
  context.  If point is in a context region already, it
  is removed first -- overlapping context regions are not
  supported.

- If there is already a gptel context at point, remove it
  instead.

- If in Dired, add marked files or file at point to the context.
  With negative prefix ARG, remove them from the context instead.

- Otherwise add the current buffer to the context.  With positive
  prefix ARG, prompt for a buffer name and add it to the context.

- With negative prefix ARG, remove all gptel contexts from the
  current buffer.

@karthink
Copy link
Owner

karthink commented Nov 6, 2024

We should also add the ability to add directories to the context when running gptel-add-file, shouldn't we?

@benthamite
Copy link
Contributor Author

benthamite commented Nov 9, 2024

We should also add the ability to add directories to the context when running
gptel-add-file, shouldn't we?

I wasn’t familiar with this command, but yeah, it seems it should mirror the behavior of gptel-add when the latter is used with a file or directory. Would you like me to make the changes?

@benthamite
Copy link
Contributor Author

Perhaps we should change gptel-context-add-file so that this is the function called by gptel-add in Dired mode. Currently, the former doesn’t take a prefix argument.

@karthink
Copy link
Owner

karthink commented Nov 26, 2024

Would you like me to make the changes?

Please go ahead.

Perhaps we should change gptel-context-add-file so that this is the function called by gptel-add in Dired mode

gptel-context-add-file is the function called by gptel-add in Dired mode. There is no other way to add files to the context list.

@benthamite benthamite force-pushed the add-files-in-dirs-to-context branch from 8e0419c to dcedf1c Compare December 5, 2024 13:49
@benthamite
Copy link
Contributor Author

@karthink, I undid the previous attempt and pushed a new version, which (1) works with both gptel-add-file and gptel-add and (2) provides the functionality to both add and remove files in directories. Let me know what you think.

@benthamite benthamite force-pushed the add-files-in-dirs-to-context branch from dcedf1c to 147fe6e Compare December 5, 2024 14:09
gptel-context.el Outdated
(unless (file-directory-p file)
(pcase action
('add (gptel--file-binary-p file)
(gptel-context--handle-binary file)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I missing something here? Seems like this should be (if (gptel--file-binary-p file) (gptel-context--handle-binary file) ....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, not sure what happened. Will make these and the other changes shortly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed: 2a40dae

gptel-context.el Outdated
(let ((files (directory-files-recursively path "." t)))
(mapc (lambda (file)
(unless (file-directory-p file)
(pcase action
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use pcase-exhaustive to future-proof uses.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed: 6c66982

gptel-context.el Outdated
(cl-pushnew (list path) gptel-context--alist :test #'equal)
(message "File \"%s\" added to context." path)
path))
(if (file-directory-p path)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Convert to a cond clause, it's easier to read here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed: bc9138a

gptel-context.el Outdated
@@ -91,8 +91,10 @@ context chunk. This is accessible as, for example:
context. If there is already a gptel context at point, remove it
instead.

- If in Dired, add marked files or file at point to the context.
With negative prefix ARG, remove them from the context instead.
- If in Dired, add marked files or file at point to the context. If
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should ask for confirmation (y-or-n-p) when adding a directory with gptel-add?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I weakly lean towards asking for confirmation. The prompt could include the number of files to be added.

Copy link
Contributor Author

@benthamite benthamite Dec 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here’s an attempt: 8e5e3c8

Perhaps we should add a user option to allow the user to decide if they want to be asked for confirmation.

Also, note that the user will be asked once for each directory. This seems like the correct behavior, though it can become tedious if the user is adding several dirs. (Perhaps this provides an additional reason for adding the user option.)

gptel-context.el Outdated
- If in Dired, add marked files or file at point to the context.
With negative prefix ARG, remove them from the context instead.
- If in Dired, add marked files or file at point to the context. If
the file at point is a directory, or directories are marked,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is difficult to read. How about

- If in Dired, add marked files or file at point to the context.  Directories
  will have all their files added recursively.  With negative prefix ARG, remove
  them from the context instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

Copy link
Contributor Author

@benthamite benthamite Dec 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed: 8bee48a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants