Mercurial > hg > octave-nkf
changeset 19463:030d56f67363
implement the open function
* open.m: New function.
* system.txi: Document it.
* __unimplemented__.m (missing_functions): Remove open from the list.
* scripts/miscellaneous/module.mk (miscellaneous_FCN_FILES):
Include open.m in the list.
* sysdep.cc (F__w32_shell_execute__): New function.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 15 Oct 2014 22:35:59 -0400 |
parents | efccb2a65b9a |
children | 26d1c3b73174 |
files | doc/interpreter/system.txi libinterp/corefcn/sysdep.cc scripts/help/__unimplemented__.m scripts/miscellaneous/module.mk scripts/miscellaneous/open.m |
diffstat | 5 files changed, 121 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/doc/interpreter/system.txi +++ b/doc/interpreter/system.txi @@ -330,6 +330,8 @@ @DOCSTRING(dos) +@DOCSTRING(open) + @DOCSTRING(perl) @DOCSTRING(python)
--- a/libinterp/corefcn/sysdep.cc +++ b/libinterp/corefcn/sysdep.cc @@ -106,6 +106,7 @@ #define WIN32_LEAN_AND_MEAN #include <tlhelp32.h> +#include <windows.h> static void w32_set_octave_home (void) @@ -179,6 +180,36 @@ } #endif +DEFUN (__w32_shell_execute__, args, , + "-*- texinfo -*-\n\ +@deftypefn {Loadable Function} {} __w32_shell_execute__ (@var{file})\n\ +Undocumented internal function.\n\ +@end deftypefn") +{ + bool retval = false; + +#if defined (__WIN32__) && ! defined (_POSIX_VERSION) + if (args.length () == 1) + { + std::string file = args(0).string_value (); + + if (! error_state) + { + HINSTANCE status = ShellExecute (0, 0, file.c_str (), 0, 0, SW_SHOWNORMAL); + + // ShellExecute returns a value greater than 32 if successful. + retval = (reinterpret_cast<ptrdiff_t> (status) > 32); + } + else + error ("__w32_shell_execute__: expecting argument to be a file name"); + } + else + print_usage (); +#endif + + return octave_value (retval); +} + #if defined (__MINGW32__) static void MINGW_init (void)
--- a/scripts/help/__unimplemented__.m +++ b/scripts/help/__unimplemented__.m @@ -745,7 +745,6 @@ "odeget", "odeset", "odextend", - "open", "openfig", "opengl", "openvar",
--- a/scripts/miscellaneous/module.mk +++ b/scripts/miscellaneous/module.mk @@ -50,6 +50,7 @@ miscellaneous/movefile.m \ miscellaneous/namelengthmax.m \ miscellaneous/news.m \ + miscellaneous/open.m \ miscellaneous/orderfields.m \ miscellaneous/pack.m \ miscellaneous/paren.m \
new file mode 100644 --- /dev/null +++ b/scripts/miscellaneous/open.m @@ -0,0 +1,87 @@ +## Copyright (C) 2014 John W. Eaton +## +## This file is part of Octave. +## +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or (at +## your option) any later version. +## +## Octave is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, see +## <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{output} =} open @var{file} +## @deftypefnx {Function File} {@var{output} =} open (@var{file}) +## Open the file @var{file} in Octave or in an external application +## based on the file type as determined by the file name extension. +## +## Recognized file types are +## +## @table @code +## @item .m +## Open file in the editor. +## @item .mat +## Load the file in the base workspace. +## @item .exe +## Execute the program (on Windows systems only). +## @end table +## +## Other file types are opened in the appropriate external application. +## @end deftypefn + +function output = open (file) + + if (nargin != 1) + print_usage (); + endif + + if (! ischar (file)) + error ("expecting argument to be a file name"); + endif + + [~, ~, ext] = fileparts (file); + + if (strcmpi (ext, ".m")) + edit (file); + elseif (strcmpi (ext, ".mat")) + if (nargout > 0) + output = load (file); + else + evalin ("base", sprintf ("load ('%s');", file)); + endif + elseif (any (strcmpi (ext, {".fig", ".mdl", ".slx", ".prj"}))) + error ("opening file type '%s' is not supported", ext); + elseif (strcmpi (ext, ".exe")) + if (ispc ()) + dos (file); + else + error ("executing .exe files is only supported on Windows systems"); + endif + else + open_with_system_app (file); + endif + +endfunction + +%% Test input validation +%!error open +%!error open (1) +%!error output = open (1) + +function open_with_system_app (file) + + if (ispc ()) + __w32_shell_execute__ (file); + else + ## FIXME: might not be xdg-open... + system (sprintf ("xdg-open %s 2> /dev/null", file), false, "async"); + endif + +endfunction