1 ## Copyright (C) 2009-2012 S�ren Hauberg
3 ## This file is part of Octave.
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.
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.
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/>.
20 ## @deftypefn {Command} {} type @var{name} @dots{}
21 ## @deftypefnx {Command} {} type -q @var{name} @dots{}
22 ## @deftypefnx {Function File} {dfns =} type ("@var{name}", @dots{})
23 ## Display the definition of each @var{name} that refers to a function.
25 ## Normally also displays whether each @var{name} is user-defined or built-in;
26 ## the @option{-q} option suppresses this behavior.
28 ## If an output argument is requested nothing is displayed. Instead, a cell
29 ## array of strings is returned, where each element corresponds to the
30 ## definition of each requested function.
33 function retval = type (varargin)
36 error ("type: not enough input arguments");
39 if (!iscellstr (varargin))
40 error ("type: input arguments must be strings");
44 idx = strcmpi (varargin, "-q") | strcmpi (varargin, "-quiet");
51 retval = cell (size (varargin));
54 for n = 1:length (varargin)
57 ## Find function and get its code
59 cmd = sprintf ("exist ('%s')", name);
60 e = evalin ("caller", cmd);
63 cmd = sprintf ("disp (%s);", name);
64 desc = evalin ("caller", cmd);
68 text = sprintf ("%s is a variable\n%s", name, desc);
71 ## m-file or ordinary file
74 ## 'name' is an ordinary file, and not a function name.
75 ## FIXME: Should we just print it anyway?
76 error ("type: `%s' undefined\n", name);
80 fid = fopen (file, "r");
82 error ("type: couldn't open `%s' for reading", file);
84 contents = char (fread (fid).');
90 text = sprintf ("%s is the user-defined function defined from: %s\n\n%s",
91 name, file, contents);
94 text = sprintf ("%s is a dynamically-linked function", name);
96 text = sprintf ("%s is a built-in function", name);
97 elseif (any (strcmp (__operators__ (), name)))
98 text = sprintf ("%s is an operator", name);
99 elseif (any (strcmp (__keywords__ (), name)))
100 text = sprintf ("%s is a keyword", name);
102 error ("type: `%s' undefined\n", name);
105 ## Should we return the text or print if
116 %! typestr = type ("var");
117 %! typestr = typestr{1}(1:17);
118 %! assert (typestr, "var is a variable");
120 %!assert (type ('dot'){1}, "dot is a dynamically-linked function")
121 %!assert (type ('cat'){1}, "cat is a built-in function")
122 %!assert (type ('+'){1}, "+ is an operator")
123 %!assert (type ('end'){1}, "end is a keyword")
124 %!error (type ('NO_NAME'))