Skip to content

Commit

Permalink
Add OpenExternal, a function for opening files in the OS
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisJefferson committed Feb 27, 2021
1 parent 1454da4 commit d76d66c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
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 standard application for this file
## provided by the operating system. This can be used to open files like HTML and PDF
## files in the GUI.
## </Description>
## </ManSection>
## <#/GAPDoc>
##
DeclareGlobalFunction( "OpenExternal" );
16 changes: 16 additions & 0 deletions lib/streams.gi
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,22 @@ InstallGlobalFunction( InputFromUser,
end );


#############################################################################
##
#M OpenExternal(filename) . . . . . . . . . . . . open file in external GUI
##
InstallGlobalFunction( OpenExternal, function(filename)
if ARCH_IS_MAC_OS_X() then
Exec(Concatenation("open \"",filename,"\""));
elif ARCH_IS_WINDOWS() then
Exec(Concatenation("cmd /c start \"",filename,"\""));
elif ARCH_IS_WSL() then
Exec(Concatenation("explorer.exe \"$(wslpath -a -w \"",filename, "\")\""));
else
Exec(Concatenation("xdg-open \"",filename,"\""));
fi;
end );


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

0 comments on commit d76d66c

Please sign in to comment.