# HG changeset patch # User address@hidden # Date 1232730509 -3600 # Node ID 8dd39dca00e8f9c90fd1463bd6ac1e46af2ea891 # Parent 6adcafc70c321cc2299a98535d86840099be79e8 Make 'type X' work when X is a variable diff -r 6adcafc70c32 -r 8dd39dca00e8 scripts/ChangeLog --- a/scripts/ChangeLog Fri Jan 23 11:28:25 2009 -0500 +++ b/scripts/ChangeLog Fri Jan 23 18:08:29 2009 +0100 @@ -1,3 +1,7 @@ +2009-01-23 Soren Hauberg + + * help/type.m: Make 'type X' work, when X is the name of a variable. + 2009-01-22 John W. Eaton * help/which.m: New function. diff -r 6adcafc70c32 -r 8dd39dca00e8 scripts/help/type.m --- a/scripts/help/type.m Fri Jan 23 11:28:25 2009 -0500 +++ b/scripts/help/type.m Fri Jan 23 18:08:29 2009 +0100 @@ -45,57 +45,68 @@ varargin (idx) = []; endif - text = cell (size (varargin)); + if (nargout > 0) + retval = cell (size (varargin)); + endif + for n = 1:length (varargin) - text {n} = do_type (varargin {n}, quiet); + name = varargin {n}; + + ## Find function and get its code + text = ""; + cmd = sprintf ("exist ('%s')", name); + e = evalin ("caller", cmd); + if (e == 1) + ## Variable + cmd = sprintf ("disp (%s);", name); + desc = evalin ("caller", cmd); + if (quiet) + text = desc; + else + text = sprintf ("%s is a variable\n%s", name, desc); + endif + elseif (e == 2) + ## m-file or ordinary file + file = which (name); + if (isempty (file)) + ## 'name' is an ordinary file, and not a function name. + ## FIXME: Should we just print it anyway? + error ("type: `%s' undefined\n", name); + endif + + ## Read the file + fid = fopen (file, "r"); + if (fid < 0) + error ("type: couldn't open `%s' for reading", file); + endif + contents = char (fread (fid).'); + fclose (fid); + + if (quiet) + text = contents; + else + text = sprintf ("%s is the user-defined function defined from: %s\n\n%s", + name, file, contents); + endif + elseif (e == 3) + text = sprintf ("%s is a dynamically-linked function", name); + elseif (e == 5) + text = sprintf ("%s is a built-in function", name); + elseif (any (strcmp (__operators__ (), name))) + text = sprintf ("%s is an operator", name); + elseif (any (strcmp (__keywords__ (), name))) + text = sprintf ("%s is a keyword", name); + else + error ("type: `%s' undefined\n", name); + endif + + ## Should we return the text or print if if (nargout == 0) - disp (text {n}); + disp (text); + else + retval {n} = text; endif endfor - - ## Should we return the text or print if - if (nargout > 0) - retval = text; - endif endfunction -function text = do_type (name, quiet) - ## Find function and get its code - text = ""; - e = exist (name); - if (e == 2) - ## m-file or ordinary file - file = which (name); - if (isempty (file)) - ## 'name' is an ordinary file, and not a function name. - ## FIXME: Should we just print it anyway? - error ("type: `%s' undefined\n", name); - endif - - ## Read the file - fid = fopen (file, "r"); - if (fid < 0) - error ("type: couldn't open `%s' for reading", file); - endif - contents = char (fread (fid).'); - fclose (fid); - - if (quiet) - text = contents; - else - text = sprintf ("%s is the user-defined function defined from: %s\n\n%s", - name, file, contents); - endif - elseif (e == 3) - text = sprintf ("%s is a dynamically-linked function", name); - elseif (e == 5) - text = sprintf ("%s is a built-in function", name); - elseif (any (strcmp (__operators__ (), name))) - text = sprintf ("%s is an operator", name); - elseif (any (strcmp (__keywords__ (), name))) - text = sprintf ("%s is a keyword", name); - else - error ("type: `%s' undefined\n", name); - endif -endfunction