annotate scripts/help/type.m @ 9752:51c21837686f

Period spellcheck of Octave documentation to check and fix newly added DOCSTRINGS
author Rik <rdrider0-list@yahoo.com>
date Thu, 22 Oct 2009 20:25:08 -0700
parents f22bbc5d56e9
children be55736a0783
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
1 ## Copyright (C) 2009 S�ren Hauberg
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
2 ##
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
3 ## This program is free software; you can redistribute it and/or modify it
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
4 ## 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
5 ## 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
6 ## your option) any later version.
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
7 ##
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
8 ## This program is distributed in the hope that it will be useful, but
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
10 ## 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
11 ## General Public License for more details.
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
12 ##
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
13 ## You should have received a copy of the GNU General Public License
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
14 ## along with this program; see the file COPYING. If not, see
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
15 ## <http://www.gnu.org/licenses/>.
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
16
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
17 ## -*- texinfo -*-
9724
f22bbc5d56e9 Fix various incorrect usages of TeXinfo deffn and deftypefn macros
Rik <rdrider0-list@yahoo.com>
parents: 9037
diff changeset
18 ## @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
19 ## @deftypefnx {Command} {} type -q @var{name} @dots{}
9752
51c21837686f Period spellcheck of Octave documentation to check and fix newly added DOCSTRINGS
Rik <rdrider0-list@yahoo.com>
parents: 9724
diff changeset
20 ## @deftypefnx {Function File} {dfns =} type ("@var{name}", @dots{})
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
21 ## Display the definition of each @var{name} that refers to a function.
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
22 ##
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
23 ## Normally also displays whether each @var{name} is user-defined or built-in;
9037
4cb9f994dcec Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents: 8746
diff changeset
24 ## the @code{-q} option suppresses this behavior.
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
25 ##
9037
4cb9f994dcec Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents: 8746
diff changeset
26 ## If an output argument is requested nothing is displayed. Instead, a cell
4cb9f994dcec Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents: 8746
diff changeset
27 ## array of strings is returned, where each element corresponds to the
4cb9f994dcec Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents: 8746
diff changeset
28 ## definition of each requested function.
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
29 ## @end deftypefn
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
30
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
31 function retval = type (varargin)
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
32 ## Parse input
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
33 if (nargin == 0)
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
34 error ("type: not enough input arguments");
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
35 endif
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
36
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
37 if (!iscellstr (varargin))
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
38 error ("type: input arguments must be strings");
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
39 endif
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
40
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
41 quiet = false;
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
42 idx = strcmpi (varargin, "-q") | strcmpi (varargin, "-quiet");
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
43 if (any (idx))
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
44 quiet = true;
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
45 varargin (idx) = [];
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
46 endif
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
47
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
48 if (nargout > 0)
8582
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
49 retval = cell (size (varargin));
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
50 endif
8582
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
51
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
52 for n = 1:length (varargin)
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
53 name = varargin {n};
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
54
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
55 ## Find function and get its code
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
56 text = "";
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
57 cmd = sprintf ("exist ('%s')", name);
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
58 e = evalin ("caller", cmd);
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
59 if (e == 1)
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
60 ## Variable
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
61 cmd = sprintf ("disp (%s);", name);
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
62 desc = evalin ("caller", cmd);
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
63 if (quiet)
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
64 text = desc;
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
65 else
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
66 text = sprintf ("%s is a variable\n%s", name, desc);
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
67 endif
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
68 elseif (e == 2)
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
69 ## m-file or ordinary file
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
70 file = which (name);
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
71 if (isempty (file))
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
72 ## 'name' is an ordinary file, and not a function name.
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
73 ## FIXME: Should we just print it anyway?
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
74 error ("type: `%s' undefined\n", name);
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
75 endif
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
76
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
77 ## Read the file
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
78 fid = fopen (file, "r");
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
79 if (fid < 0)
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
80 error ("type: couldn't open `%s' for reading", file);
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
81 endif
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
82 contents = char (fread (fid).');
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
83 fclose (fid);
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
84
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
85 if (quiet)
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
86 text = contents;
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
87 else
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
88 text = sprintf ("%s is the user-defined function defined from: %s\n\n%s",
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
89 name, file, contents);
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
90 endif
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
91 elseif (e == 3)
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
92 text = sprintf ("%s is a dynamically-linked function", name);
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
93 elseif (e == 5)
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
94 text = sprintf ("%s is a built-in function", name);
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
95 elseif (any (strcmp (__operators__ (), name)))
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
96 text = sprintf ("%s is an operator", name);
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
97 elseif (any (strcmp (__keywords__ (), name)))
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
98 text = sprintf ("%s is a keyword", name);
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
99 else
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
100 error ("type: `%s' undefined\n", name);
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
101 endif
8582
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
102
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
103 ## Should we return the text or print if
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
104 if (nargout == 0)
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
105 disp (text);
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
106 else
8582
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
107 retval {n} = text;
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
108 endif
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
109 endfor
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
110 endfunction
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
111
8582
92d66bbd74af Make 'type X' work when X is a variable
sh@sh-t400
parents: 8575
diff changeset
112