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