]> Creatis software - CreaPhase.git/blob - octave_packages/m/help/type.m
update packages
[CreaPhase.git] / octave_packages / m / help / type.m
1 ## Copyright (C) 2009-2012 S�ren Hauberg
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  {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.
24 ##
25 ## Normally also displays whether each @var{name} is user-defined or built-in;
26 ## the @option{-q} option suppresses this behavior.
27 ##
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.
31 ## @end deftypefn
32
33 function retval = type (varargin)
34   ## Parse input
35   if (nargin == 0)
36     error ("type: not enough input arguments");
37   endif
38
39   if (!iscellstr (varargin))
40     error ("type: input arguments must be strings");
41   endif
42
43   quiet = false;
44   idx = strcmpi (varargin, "-q") | strcmpi (varargin, "-quiet");
45   if (any (idx))
46     quiet = true;
47     varargin (idx) = [];
48   endif
49
50   if (nargout > 0)
51     retval = cell (size (varargin));
52   endif
53
54   for n = 1:length (varargin)
55     name = varargin {n};
56
57     ## Find function and get its code
58     text = "";
59     cmd = sprintf ("exist ('%s')", name);
60     e = evalin ("caller", cmd);
61     if (e == 1)
62       ## Variable
63       cmd = sprintf ("disp (%s);", name);
64       desc = evalin ("caller", cmd);
65       if (quiet)
66         text = desc;
67       else
68         text = sprintf ("%s is a variable\n%s", name, desc);
69       endif
70     elseif (e == 2)
71       ## m-file or ordinary file
72       file = which (name);
73       if (isempty (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);
77       endif
78
79       ## Read the file
80       fid = fopen (file, "r");
81       if (fid < 0)
82         error ("type: couldn't open `%s' for reading", file);
83       endif
84       contents = char (fread (fid).');
85       fclose (fid);
86
87       if (quiet)
88         text = contents;
89       else
90         text = sprintf ("%s is the user-defined function defined from: %s\n\n%s",
91                         name, file, contents);
92       endif
93     elseif (e == 3)
94       text = sprintf ("%s is a dynamically-linked function", name);
95     elseif (e == 5)
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);
101     else
102       error ("type: `%s' undefined\n", name);
103     endif
104
105     ## Should we return the text or print if
106     if (nargout == 0)
107       disp (text);
108     else
109       retval {n} = text;
110     endif
111   endfor
112 endfunction
113
114 %!test
115 %! var = 1;
116 %! typestr = type ("var");
117 %! typestr = typestr{1}(1:17);
118 %! assert (typestr, "var is a variable");
119
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'))