changeset 8582:92d66bbd74af

Make 'type X' work when X is a variable
author sh@sh-t400
date Fri, 23 Jan 2009 15:03:16 -0500
parents 6adcafc70c32
children 27b2db6ff0d7
files scripts/ChangeLog scripts/help/type.m
diffstat 2 files changed, 62 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Fri Jan 23 11:28:25 2009 -0500
+++ b/scripts/ChangeLog	Fri Jan 23 15:03:16 2009 -0500
@@ -1,3 +1,7 @@
+2009-01-23  Søren Hauberg  <hauberg@gmail.com>
+
+	* help/type.m: Make 'type X' work, when X is the name of a variable.
+
 2009-01-22  John W. Eaton  <jwe@octave.org>
 
 	* help/which.m: New function.
--- a/scripts/help/type.m	Fri Jan 23 11:28:25 2009 -0500
+++ b/scripts/help/type.m	Fri Jan 23 15:03:16 2009 -0500
@@ -45,57 +45,68 @@
     varargin (idx) = [];
   endif
 
-  text = cell (size (varargin));
-  for n = 1:length (varargin)
-    text {n} = do_type (varargin {n}, quiet);
-    if (nargout == 0)
-      disp (text {n});
-    endif
-  endfor
-  
-  ## Should we return the text or print if
   if (nargout > 0)
-    retval = text;
+    retval = cell (size (varargin));
   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?
+  
+  for n = 1:length (varargin)
+    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
-    
-    ## 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;
+
+    ## Should we return the text or print if
+    if (nargout == 0)
+      disp (text);
     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
+      retval {n} = text;
+    endif
+  endfor
 endfunction
 
+