Mercurial > hg > octave-nkf
comparison scripts/help/type.m @ 8575:f134925a1cfa
m-file implementation of help system
author | Soren Hauberg <soren@hauberg.org> |
---|---|
date | Thu, 22 Jan 2009 18:22:52 -0500 |
parents | |
children | 92d66bbd74af |
comparison
equal
deleted
inserted
replaced
8574:83b8c739d626 | 8575:f134925a1cfa |
---|---|
1 ## Copyright (C) 2009 Søren Hauberg | |
2 ## | |
3 ## This program is free software; you can redistribute it and/or modify it | |
4 ## under the terms of the GNU General Public License as published by | |
5 ## the Free Software Foundation; either version 3 of the License, or (at | |
6 ## your option) any later version. | |
7 ## | |
8 ## This program is distributed in the hope that it will be useful, but | |
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 ## General Public License for more details. | |
12 ## | |
13 ## You should have received a copy of the GNU General Public License | |
14 ## along with this program; see the file COPYING. If not, see | |
15 ## <http://www.gnu.org/licenses/>. | |
16 | |
17 ## -*- texinfo -*- | |
18 ## @deftypefn {Command} type options name @dots{} | |
19 ## Display the definition of each @var{name} that refers to a function. | |
20 ## | |
21 ## Normally also displays whether each @var{name} is user-defined or built-in; | |
22 ## the @code{-q} option suppresses this behaviour. | |
23 ## | |
24 ## If an output argument is requested nothing is displayed. Instead a cell array | |
25 ## of strings is returned, where each element corresponds to the definition of | |
26 ## each requested function. | |
27 ## @end deftypefn | |
28 | |
29 ## PKG_ADD: mark_as_command type | |
30 | |
31 function retval = type (varargin) | |
32 ## Parse input | |
33 if (nargin == 0) | |
34 error ("type: not enough input arguments"); | |
35 endif | |
36 | |
37 if (!iscellstr (varargin)) | |
38 error ("type: input arguments must be strings"); | |
39 endif | |
40 | |
41 quiet = false; | |
42 idx = strcmpi (varargin, "-q") | strcmpi (varargin, "-quiet"); | |
43 if (any (idx)) | |
44 quiet = true; | |
45 varargin (idx) = []; | |
46 endif | |
47 | |
48 text = cell (size (varargin)); | |
49 for n = 1:length (varargin) | |
50 text {n} = do_type (varargin {n}, quiet); | |
51 if (nargout == 0) | |
52 disp (text {n}); | |
53 endif | |
54 endfor | |
55 | |
56 ## Should we return the text or print if | |
57 if (nargout > 0) | |
58 retval = text; | |
59 endif | |
60 endfunction | |
61 | |
62 function text = do_type (name, quiet) | |
63 ## Find function and get its code | |
64 text = ""; | |
65 e = exist (name); | |
66 if (e == 2) | |
67 ## m-file or ordinary file | |
68 file = which (name); | |
69 if (isempty (file)) | |
70 ## 'name' is an ordinary file, and not a function name. | |
71 ## FIXME: Should we just print it anyway? | |
72 error ("type: `%s' undefined\n", name); | |
73 endif | |
74 | |
75 ## Read the file | |
76 fid = fopen (file, "r"); | |
77 if (fid < 0) | |
78 error ("type: couldn't open `%s' for reading", file); | |
79 endif | |
80 contents = char (fread (fid).'); | |
81 fclose (fid); | |
82 | |
83 if (quiet) | |
84 text = contents; | |
85 else | |
86 text = sprintf ("%s is the user-defined function defined from: %s\n\n%s", | |
87 name, file, contents); | |
88 endif | |
89 elseif (e == 3) | |
90 text = sprintf ("%s is a dynamically-linked function", name); | |
91 elseif (e == 5) | |
92 text = sprintf ("%s is a built-in function", name); | |
93 elseif (any (strcmp (__operators__ (), name))) | |
94 text = sprintf ("%s is an operator", name); | |
95 elseif (any (strcmp (__keywords__ (), name))) | |
96 text = sprintf ("%s is a keyword", name); | |
97 else | |
98 error ("type: `%s' undefined\n", name); | |
99 endif | |
100 endfunction | |
101 |