19463
|
1 ## Copyright (C) 2014 John W. Eaton |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {@var{output} =} open @var{file} |
|
21 ## @deftypefnx {Function File} {@var{output} =} open (@var{file}) |
|
22 ## Open the file @var{file} in Octave or in an external application |
|
23 ## based on the file type as determined by the file name extension. |
|
24 ## |
|
25 ## Recognized file types are |
|
26 ## |
|
27 ## @table @code |
|
28 ## @item .m |
|
29 ## Open file in the editor. |
|
30 ## @item .mat |
|
31 ## Load the file in the base workspace. |
|
32 ## @item .exe |
|
33 ## Execute the program (on Windows systems only). |
|
34 ## @end table |
|
35 ## |
|
36 ## Other file types are opened in the appropriate external application. |
|
37 ## @end deftypefn |
|
38 |
|
39 function output = open (file) |
|
40 |
|
41 if (nargin != 1) |
|
42 print_usage (); |
|
43 endif |
|
44 |
|
45 if (! ischar (file)) |
|
46 error ("expecting argument to be a file name"); |
|
47 endif |
|
48 |
|
49 [~, ~, ext] = fileparts (file); |
|
50 |
|
51 if (strcmpi (ext, ".m")) |
|
52 edit (file); |
|
53 elseif (strcmpi (ext, ".mat")) |
|
54 if (nargout > 0) |
|
55 output = load (file); |
|
56 else |
|
57 evalin ("base", sprintf ("load ('%s');", file)); |
|
58 endif |
|
59 elseif (any (strcmpi (ext, {".fig", ".mdl", ".slx", ".prj"}))) |
|
60 error ("opening file type '%s' is not supported", ext); |
|
61 elseif (strcmpi (ext, ".exe")) |
|
62 if (ispc ()) |
|
63 dos (file); |
|
64 else |
|
65 error ("executing .exe files is only supported on Windows systems"); |
|
66 endif |
|
67 else |
|
68 open_with_system_app (file); |
|
69 endif |
|
70 |
|
71 endfunction |
|
72 |
|
73 %% Test input validation |
|
74 %!error open |
|
75 %!error open (1) |
|
76 %!error output = open (1) |
|
77 |
|
78 function open_with_system_app (file) |
|
79 |
|
80 if (ispc ()) |
|
81 __w32_shell_execute__ (file); |
|
82 else |
|
83 ## FIXME: might not be xdg-open... |
|
84 system (sprintf ("xdg-open %s 2> /dev/null", file), false, "async"); |
|
85 endif |
|
86 |
|
87 endfunction |