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

Add OpenExternal, a function for opening files in the OS #4295

Merged
merged 1 commit into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions doc/ref/streams.xml
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,19 @@ separation character (usually a comma).
<#Include Label="PrintCSV">

</Section>

<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<Section Label="Opening files in the Operating System">
<Heading>Opening files in the Operating System</Heading>

In some situations it can be desirable to open a file outside of &GAP;,
for example HTML files, PDFs, or pictures.

<#Include Label="OpenExternal">

</Section>


</Chapter>

<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
Expand Down
18 changes: 18 additions & 0 deletions lib/streams.gd
Original file line number Diff line number Diff line change
Expand Up @@ -1232,3 +1232,21 @@ DeclareGlobalFunction( "UnInstallCharReadHookFunc" );
## <#/GAPDoc>
##
DeclareGlobalFunction( "InputFromUser" );

#############################################################################
##
#F OpenExternal( <filename> )
##
## <#GAPDoc Label="OpenExternal">
## <ManSection>
## <Func Name="OpenExternal" Arg='filename'/>
##
## <Description>
## Open the file <A>filename</A> using the default application for this file
## in the operating system. This can be used to open files like HTML and PDF
## files in the GUI.
## </Description>
## </ManSection>
## <#/GAPDoc>
##
DeclareGlobalFunction( "OpenExternal" );
23 changes: 23 additions & 0 deletions lib/streams.gi
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,29 @@ InstallGlobalFunction( InputFromUser,
end );


#############################################################################
##
#M OpenExternal(filename) . . . . . . . . . . . . open file in external GUI
##
InstallGlobalFunction( OpenExternal, function(filename)
local file;
if ARCH_IS_MAC_OS_X() then
Exec(Concatenation("open \"",filename,"\""));
Copy link
Member

Choose a reason for hiding this comment

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

OK, I don't want to whine, but I have to repeat one of my pet mantras, namely that "Exec is a really bad function, we should never have added it like that". Besides not even allowing to check the return value, it also forces us to do silly quoting tricks like the above; and this is not even complete, a filename with a " in it will break it. We really should think about adding an alternative to Exec which is a bit better (doesn't go through a shell and hence has no quoting issues, and gives access to return value, and optionally allows for suppressing or redirecting output.

Copy link
Member

Choose a reason for hiding this comment

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

(But of course none of this is required for this PR!)

elif ARCH_IS_WINDOWS() then
Exec(Concatenation("cmd /c start \"",filename,"\""));
elif ARCH_IS_WSL() then
# If users pass a URL, make sure if does not get mangled.
if ForAny(["https://", "http://"], {pre} -> StartsWith(filename, pre)) then
file := filename;
else
file := Concatenation("$(wslpath -a -w \"",filename,"\")");
fi;
Exec(Concatenation("explorer.exe \"", file, "\""));
else
Exec(Concatenation("xdg-open \"",filename,"\""));
fi;
end );


#############################################################################
##
Expand Down