annotate scripts/help/type.m @ 17744:d63878346099

maint: Update copyright notices for release.
author John W. Eaton <jwe@octave.org>
date Wed, 23 Oct 2013 22:09:27 -0400
parents fedcd3717ebc
children 446c46af4b42
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17744
d63878346099 maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents: 17513
diff changeset
1 ## Copyright (C) 2009-2013 Søren Hauberg
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
2 ##
11104
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 10793
diff changeset
3 ## This file is part of Octave.
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 10793
diff changeset
4 ##
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 10793
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
8 ## your option) any later version.
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
9 ##
11104
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 10793
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
13 ## General Public License for more details.
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
14 ##
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
11104
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 10793
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
18
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
19 ## -*- texinfo -*-
9724
f22bbc5d56e9 Fix various incorrect usages of TeXinfo deffn and deftypefn macros
Rik <rdrider0-list@yahoo.com>
parents: 9037
diff changeset
20 ## @deftypefn {Command} {} type @var{name} @dots{}
f22bbc5d56e9 Fix various incorrect usages of TeXinfo deffn and deftypefn macros
Rik <rdrider0-list@yahoo.com>
parents: 9037
diff changeset
21 ## @deftypefnx {Command} {} type -q @var{name} @dots{}
17399
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
22 ## @deftypefnx {Function File} {text =} type ("@var{name}", @dots{})
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
23 ## Display the contents of @var{name} which may be a file, function (m-file),
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
24 ## variable, operator, or keyword.
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
25 ##
17399
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
26 ## @code{type} normally prepends a header line describing the category
17513
fedcd3717ebc doc: grammarcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents: 17399
diff changeset
27 ## of @var{name} such as function or variable; The @option{-q} option
17399
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
28 ## suppresses this behavior.
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
29 ##
17399
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
30 ## If no output variable is used the contents are displayed on screen.
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
31 ## Otherwise, a cell array of strings is returned, where each element
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
32 ## corresponds to the contents of each requested function.
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
33 ## @end deftypefn
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
34
17399
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
35 function text = type (varargin)
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
36
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
37 if (nargin == 0)
17399
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
38 print_usage ();
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
39 elseif (! iscellstr (varargin))
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
40 error ("type: input arguments must be strings");
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
41 endif
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
42
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
43 quiet = false;
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
44 idx = strcmpi (varargin, "-q") | strcmpi (varargin, "-quiet");
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
45 if (any (idx))
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
46 quiet = true;
17399
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
47 varargin(idx) = [];
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
48 endif
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
49
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
50 if (nargout > 0)
17399
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
51 text = cell (size (varargin));
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
52 endif
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
53
8582
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
54 for n = 1:length (varargin)
17399
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
55 name = varargin{n};
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
56
8582
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
57 ## Find function and get its code
17399
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
58 txt = "";
8582
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
59 cmd = sprintf ("exist ('%s')", name);
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
60 e = evalin ("caller", cmd);
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
61 if (e == 1)
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
62 ## Variable
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
63 cmd = sprintf ("disp (%s);", name);
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
64 desc = evalin ("caller", cmd);
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
65 if (quiet)
17399
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
66 txt = desc;
8582
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
67 else
17399
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
68 txt = sprintf ("%s is a variable\n%s", name, desc);
8582
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
69 endif
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
70 elseif (e == 2)
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
71 ## m-file or ordinary file
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
72 file = which (name);
17399
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
73 if (length (file) > 2)
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
74 ext = file(end-1:end);
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
75 endif
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
76 if (isempty (file) || ! strcmpi (ext, ".m"))
8582
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
77 ## 'name' is an ordinary file, and not a function name.
17399
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
78 file = file_in_loadpath (name);
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
79 quiet = true;
8582
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
80 endif
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
81
8582
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
82 ## Read the file
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
83 fid = fopen (file, "r");
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
84 if (fid < 0)
15466
d174210ce1ec use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
85 error ("type: couldn't open '%s' for reading", file);
8582
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
86 endif
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
87 contents = char (fread (fid).');
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
88 fclose (fid);
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
89
8582
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
90 if (quiet)
17399
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
91 txt = contents;
8582
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
92 else
17399
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
93 txt = sprintf ("%s is the user-defined function defined from: %s\n\n%s",
8582
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
94 name, file, contents);
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
95 endif
8582
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
96 elseif (e == 3)
17399
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
97 txt = sprintf ("%s is a dynamically-linked function", name);
8582
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
98 elseif (e == 5)
17399
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
99 txt = sprintf ("%s is a built-in function", name);
8582
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
100 elseif (any (strcmp (__operators__ (), name)))
17399
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
101 txt = sprintf ("%s is an operator", name);
8582
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
102 elseif (any (strcmp (__keywords__ (), name)))
17399
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
103 txt = sprintf ("%s is a keyword", name);
8582
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
104 else
15466
d174210ce1ec use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
105 error ("type: '%s' undefined\n", name);
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
106 endif
8582
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
107
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
108 if (nargout == 0)
17399
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
109 disp (txt);
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
110 else
17399
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
111 text{n} = txt;
8582
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
112 endif
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
113 endfor
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
114 endfunction
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
115
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
116
12838
b584c07cd818 codesprint: Add tests for type()
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
117 %!test
b584c07cd818 codesprint: Add tests for type()
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
118 %! var = 1;
17399
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
119 %! text = type ("var");
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
120 %! typestr = text{1}(1:17);
12838
b584c07cd818 codesprint: Add tests for type()
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
121 %! assert (typestr, "var is a variable");
8582
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
122
17399
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
123 %!test
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
124 %! text = type ("ls");
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
125 %! typestr = text{1}(1:31);
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
126 %! assert (typestr, "ls is the user-defined function");
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
127
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
128 %!test
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
129 %! text = type ("ls", "-q");
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
130 %! typestr = text{1}(1:21);
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
131 %! assert (typestr, "## Copyright (C) 2006");
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
132
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14363
diff changeset
133 %!assert (type ("amd"){1}, "amd is a dynamically-linked function")
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
134 %!assert (type ("cat"){1}, "cat is a built-in function")
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
135 %!assert (type ("+"){1}, "+ is an operator")
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
136 %!assert (type ("end"){1}, "end is a keyword")
17399
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
137
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
138 %!error type ()
a1fa416c7c4a type.m: Fix function for filenames without extension (bug #39922).
Rik <rik@octave.org>
parents: 17338
diff changeset
139 %!error <'__NO_NAME__' undefined> type ('__NO_NAME__')
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
140