Also, what naming convention should we use? f.el seems to prefer
shorter names like "f-no-ext" rather than "f-no-extension". I guess we
don't need the "f-" prefix since users can import the module with their
preferred prefix anyway.
Yeah, we don't need the any kind of implicit prefix like "f-".
Most of the Scheme code I read was quite verbose. Take, for example, this
function name from Guix: "specifications->manifest" — it could have been
"specs->manifest" or something like that, but it seems Scheme people
prefer to be clear about stuff. So I think having full words in the name
is the way to go. It also flows better when reading the code, it sounds
like English without any extra in-brain processing needed.
I ended up not using `extension` in the definition of
`replace-extension` so it gets a separate commit.
Hidden files should now work as expected and `extension` will
return an empty string if there is no extension to be found.
Xinglu Chen (2):
Add replace-extension.
Add extension.
f.scm | 30 +++++++++++++++++++++++++++++-
1 file changed, 29 insertions(+), 1 deletion(-)
base-commit: da553c5fcb9c3865a5e3a7506a02bb9314bb572
Oops, `no-ext` should have been `file-no-extension`, I was playing
around in the REPL earlier so `no-ext` must have been some function I
renamed. My bad.
Nice idea, and thanks for looking into this, 'cause I even forgot I had
this project!
I tried running this and it seems to behave perfectly when it's used on
a file that does indeed have an extension:
--8<---------------cut here---------------start------------->8---
(replace-extension "asdf.txt" "defg")
=> "asdf.defg"
--8<---------------cut here---------------end--------------->8---
But it seems to throw an ugly exception when FILE has no dot in it, or
when the dot is the last character:
--8<---------------cut here---------------start------------->8---
(replace-extension "asdf." "defg")
ice-9/boot-9.scm:1669:16: In procedure raise-exception:
In procedure vector-ref: Wrong type argument in position 1 (expecting vector): #f
(replace-extension "asdf" "defg")
ice-9/boot-9.scm:1669:16: In procedure raise-exception:
In procedure vector-ref: Wrong type argument in position 1 (expecting vector): #f
--8<---------------cut here---------------end--------------->8---
I looked into the equivalent function from f.el [1] and I think its
behaviour is a bit more user friendly.
--8<---------------cut here---------------start------------->8---
(f-swap-ext "asdf.txt" "defg")
=> "asdf.defg"
(f-swap-ext "asdf." "defg")
=> "asdf.defg"
(f-swap-ext "asdf" "defg")
=> "asdf.defg"
--8<---------------cut here---------------end--------------->8---
The only thing that I don't really find that intuitive about it is that
when the extension is the empty string, instead of returning the file
name without an extension (as I thought it would), it gives an error:
--8<---------------cut here---------------start------------->8---
(f-swap-ext "asdf.txt" "")
=> Lisp error: (error "Extension cannot be empty or nil")
--8<---------------cut here---------------end--------------->8---
Let me know what you think about this.
I agree that it should probably just return the filename if the
extension is emtpy. This behaviour would be more liberal and consistent
with the other cases IMO.
Thanks for the feedback!
You named the replacement function "replace-extension", but the others
start with "file-". All of them are related to file extensions. What do
you say about just removing the "file-" prefix on the first two? It
would still be clear what they do IMO, because the only thing that comes
to my mind when using a filesystem API and it has an "extension"
function is that it's related to file extensions.
(f:file-extension "file")
=> "file"
This seems to return FILE if there is no dot in it. I would expect it to
return "", because there is no extension in that case.
Also, it seems to think that hidden files have their whole name as an
extension, and that isn't right.
(f:file-extension ".hidden-file")
=> "hidden-file"
This should return "", as ".hidden-file" is a file with no extension. It
works correctly on hidden files *with* extensions:
(f:file-extension ".hidden-file.ext")
=> "ext"
This seems to have the same problems with hidden files. It thinks the
whole file name is an extension, so here's what happens:
(f:replace-extension ".file" "asdf")
=> ".asdf"
If you still have the time, energy and motivation, I'd greatly
appreciate a V3 addressing these issues. Feel free to discuss anything
if you need to.
I ended up not using `extension` in the definition of
`replace-extension` so it gets a separate commit.
Hidden files should now work as expected and `extension` will
return an empty string if there is no extension to be found.
Xinglu Chen (2):
Add replace-extension.
Add extension.
f.scm | 30 +++++++++++++++++++++++++++++-
1 file changed, 29 insertions(+), 1 deletion(-)
base-commit: da553c5fcb9c3865a5e3a7506a02bb9314bb572