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

Better handling for non-file buffers #52

Closed
mohkale opened this issue Oct 17, 2021 · 6 comments · Fixed by #59
Closed

Better handling for non-file buffers #52

mohkale opened this issue Oct 17, 2021 · 6 comments · Fixed by #59

Comments

@mohkale
Copy link
Contributor

mohkale commented Oct 17, 2021

Atm apheleia has filepath and file command specifiers that're replaced with the value of the current buffers file. Quite a lot of the time I'm working with virtual files (such as by creating an org src-block for some c code) and apheliea will refuse to format that buffer (when called interactively through apheleia-format-buffer) because its been modified and it doesn't have a file-name. There should be a way to format buffers as well as files.

@raxod502
Copy link
Member

That seems reasonable. I think what we could do is, check if buffer-file-name is nil. If it is, then when processing file or filepath we can pretend like input was used instead. This won't work for all formatters (otherwise, we'd just always use input), but it'll do the best job that can be done.

@mohkale
Copy link
Contributor Author

mohkale commented Nov 16, 2021

I end up wanting this frequently enough to try and implement it now 😄.

I think what we could do is, check if buffer-file-name is nil.

Is there a reason we prefer the variable buffer-file-name over the function? Just curious. I didn't even know there was a variable until I worked on this code base.

If it is, then when processing file or filepath we can pretend like input was used instead.

I think a reasonable compromise would be to use the (buffer-name) in this case. That at least gives some level of configurability to users for multi-file-type formatters like clang-format which seems to take the file extension to decide how to format something (frankly kind of apalling there's no way to specify the format distinct from the filename).

mohkale added a commit to mohkale/apheleia that referenced this issue Nov 16, 2021
@mohkale
Copy link
Contributor Author

mohkale commented Nov 16, 2021

Thinking about this more it would be great if there was some way to hook apheleia-mode into indirect buffers like separedit (edit-indirect.el) or org source code blocks. Might be simple enough to do by parameterising apheleia--format-after-save and providing alternatives to apheleia--write-file-silently for these other buffer types.

@raxod502
Copy link
Member

Is there a reason we prefer the variable buffer-file-name over the function

No reason, I think they do exactly the same thing. I didn't know there was a function until just now!

I think a reasonable compromise would be to use the (buffer-name)

That could work, as long as we do a reasonable job at translating the buffer name (which could be something bizarre like, say, *scratch*) into an appropriate temp file name.

Might be simple enough to do by parameterising apheleia--format-after-save

Sure, we could maybe introduce a buffer-local user option that could be customized with a different way of performing the "save" operation instead of apheleia--write-file-silently.

@mohkale
Copy link
Contributor Author

mohkale commented Nov 17, 2021

That could work, as long as we do a reasonable job at translating the buffer name (which could be something bizarre like, say, scratch) into an appropriate temp file name.

We're only using the buffer-file-names extension when creating a temp file, so I don't think there's an issue there. On the other hand I suppose there's a chance someone could have a buffer like *foo.bar* so it might be worth removing weird characters (at least for windows).

Sure, we could maybe introduce a buffer-local user option that could be customized with a different way of performing the "save" operation instead of apheleia--write-file-silently.

Looked into it, might be a little too ambitious for this issue. The indirect-buffer might not exist by the time the formatter is done, instead will want to format a region in the original buffer but formatting regions is a separate issue to work on.

@raxod502
Copy link
Member

Fair point on both, sounds good!

mohkale added a commit to mohkale/apheleia that referenced this issue Nov 20, 2021
mohkale added a commit to mohkale/apheleia that referenced this issue Nov 20, 2021
mohkale added a commit to mohkale/apheleia that referenced this issue Nov 20, 2021
raxod502 added a commit that referenced this issue Nov 21, 2021
* Support non-file buffers (#52)

Closes #52

* Apply suggestions from code review

* Oops, my bad

Co-authored-by: Radon Rosborough <radon.neon@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants