changeset 27904:121d33ab44bc

what.m: Find all instances in loadpath for Matlab compatibility (bug #40904). * what.m: Use "all" option to dir_in_loadpath to find all occurrences of specified dir. Extract code for classifying files in a directory into a new internal function __what__. Use for loop over all directories found and call __what__ to get information and build a struct array. Use for loop over all results when printing to terminal.
author Rik <rik@octave.org>
date Fri, 03 Jan 2020 11:20:32 -0800
parents f7279dd915d8
children d1862db3bffb
files scripts/miscellaneous/what.m
diffstat 1 files changed, 47 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/miscellaneous/what.m	Fri Jan 03 09:43:43 2020 -0800
+++ b/scripts/miscellaneous/what.m	Fri Jan 03 11:20:32 2020 -0800
@@ -72,22 +72,58 @@
   endif
 
   if (nargin == 0)
-    dir = pwd ();
+    dir = { pwd() };
   else
     dtmp = canonicalize_file_name (dir);
     if (isempty (dtmp))
-      ## Search for directory name in path
-      if (dir(end) == '/' || dir(end) == '\')
-        dir(end) = [];
-      endif
-      dtmp = dir_in_loadpath (dir);
-      if (isempty (dtmp))
-        error ("what: could not find the directory %s", dir);
-      endif
+      dtmp = {};
+    else
+      dtmp = {dtmp};
+    endif
+    ## Search for directory name in path
+    if (dir(end) == '/' || dir(end) == '\')
+      dir(end) = [];
     endif
+    dtmp = unique ([dtmp; dir_in_loadpath(dir, "all")]);
+
+    if (isempty (dtmp) && nargout == 0)
+      printf ("%s not found\n", dir);
+      return;
+    endif
+
     dir = dtmp;
   endif
 
+   ## Lookup info for each directory
+   for i = 1 : numel (dir)
+     w(i) = __what__ (dir{i});
+   endfor
+
+   ## If none was found, return an empty struct
+   if (numel (dir) == 0)
+     w = __what__ ("");
+     w = resize (w, [0, 1]);  # Matlab compatibility, return 0x1 empty array
+   end
+
+  if (nargout == 0)
+    for i = 1 : numel (w)
+      __display_filenames__ ("M-files in directory", w(i).path, w(i).m);
+      __display_filenames__ ("\nMAT-files in directory", w(i).path, w(i).mat);
+      __display_filenames__ ("\nMEX-files in directory", w(i).path, w(i).mex);
+      __display_filenames__ ("\nOCT-files in directory", w(i).path, w(i).oct);
+      __display_filenames__ ("\nClasses in directory", w(i).path, w(i).classes);
+      __display_filenames__ ("\nPackages in directory", w(i).path, w(i).packages);
+    endfor
+  else
+    retval = w;
+  endif
+
+endfunction
+
+
+## what() functionality for a single directory
+function w = __what__ (dir)
+
   files = readdir (dir);
   w.path = dir;
   w.m = cell (0, 1);
@@ -137,19 +173,10 @@
 
   endfor
 
-  if (nargout == 0)
-    __display_filenames__ ("M-files in directory", w.path, w.m);
-    __display_filenames__ ("\nMAT-files in directory", w.path, w.mat);
-    __display_filenames__ ("\nMEX-files in directory", w.path, w.mex);
-    __display_filenames__ ("\nOCT-files in directory", w.path, w.oct);
-    __display_filenames__ ("\nClasses in directory", w.path, w.classes);
-    __display_filenames__ ("\nPackages in directory", w.path, w.packages);
-  else
-    retval = w;
-  endif
-
 endfunction
 
+
+## Pretty print filenames to terminal
 function __display_filenames__ (msg, p, f)
 
   if (length (f) > 0)