Mercurial > hg > octave-lyh
annotate scripts/miscellaneous/what.m @ 8746:5dd06f19e9be
handle commands in the lexer
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sun, 15 Feb 2009 23:49:15 -0500 |
parents | cadc73247d65 |
children | 2c8b2399247b |
rev | line source |
---|---|
7050 | 1 ## Copyright (C) 2007 David Bateman |
2 ## | |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
16 ## along with Octave; see the file COPYING. If not, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 | |
19 ## -*- texinfo -*- | |
20 ## @deftypefn {Command} {} what | |
21 ## @deftypefnx {Command} {} what @var{dir} | |
22 ## @deftypefnx {Function File} {w =} what (@var{dir}) | |
23 ## List the Octave specific files in a directory. If the variable @var{dir} | |
24 ## is given then check that directory rather than the current directory. If | |
25 ## a return argument is requested, the files found are returned in the | |
26 ## structure @var{w}. | |
27 ## @seealso{which} | |
28 ## @end deftypefn | |
29 | |
30 function ret = what (d) | |
31 | |
32 if (nargin == 0) | |
33 d = pwd (); | |
34 elseif (isempty (strfind (d, filesep ()))) | |
8507 | 35 ## Find the appropriate directory on the path. |
7050 | 36 p = split (path (), pathsep()); |
37 p = cellfun (@(x) deblank (x), mat2cell (p, ones (1, size (p, 1)), ... | |
38 size (p, 2)), "UniformOutput", false); | |
39 d = p{find (cellfun (@(x) ! isempty (strfind (x, d)), p))(end)}; | |
40 else | |
41 [status, msg, msgid] = fileattrib (d); | |
42 if (status != 1) | |
43 error ("could not find the file or path %s", d); | |
44 else | |
45 d = msg.Name; | |
46 endif | |
47 endif | |
48 | |
49 files = dir (d); | |
50 w.path = d; | |
51 w.m = cell (0, 1); | |
52 w.mex = cell (0, 1); | |
53 w.oct = cell (0, 1); | |
54 w.mat = cell (0, 1); | |
55 w.mdl = cell (0, 1); | |
56 w.p = cell (0, 1); | |
57 w.classes = cell (0, 1); | |
58 | |
59 for i = 1 : length (files) | |
60 n = files(i).name; | |
61 ## Ignore . and .. | |
62 if (strcmp (n, ".") || strcmp (n, "..")) | |
63 continue; | |
64 else | |
65 ## Ignore mdl and p files | |
66 [dummy, f, e] = fileparts (n); | |
67 if (strcmp (e, ".m")) | |
7208 | 68 w.m{end+1} = n; |
7050 | 69 elseif (strcmp (e, mexext ())) |
7208 | 70 w.mex{end+1} = n; |
7050 | 71 elseif (strcmp (e, ".oct")) |
7208 | 72 w.oct{end+1} = n; |
7050 | 73 elseif (strcmp (e, ".mat")) |
7208 | 74 w.mat{end+1} = n; |
7050 | 75 elseif(strcmp (n(1), "@")) |
7208 | 76 w.classes{end+1} = n; |
7050 | 77 endif |
78 endif | |
79 endfor | |
80 | |
81 if (nargout == 0) | |
82 __display_filenames__ ("M-files in directory", w.path, w.m); | |
83 __display_filenames__ ("\nMEX-files in directory", w.path, w.mex); | |
84 __display_filenames__ ("\nOCT-files in directory", w.path, w.oct); | |
85 __display_filenames__ ("\nMAT-files in directory", w.path, w.mat); | |
86 __display_filenames__ ("\nClasses in directory", w.path, w.classes); | |
87 else | |
88 ret = w; | |
89 endif | |
90 endfunction | |
91 | |
92 function __display_filenames__ (msg, p, f) | |
93 if (length (f) > 0) | |
94 printf ("%s %s:\n\n", msg, p) | |
95 | |
96 maxlen = max (cellfun (@(x) length (x), f)); | |
97 ncols = max (1, floor (terminal_size()(2) / (maxlen + 3))); | |
98 fmt = ""; | |
99 for i = 1: ncols | |
100 fmt = sprintf ("%s %%-%ds", fmt, maxlen); | |
101 endfor | |
102 fmt = [fmt, "\n"]; | |
103 | |
104 nrows = ceil (length (f) / ncols); | |
105 for i = 1 : nrows | |
106 args = f(i:nrows:end); | |
107 if (length (args) < ncols) | |
8455
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
7208
diff
changeset
|
108 args(end : ncols) = {""}; |
7050 | 109 endif |
110 printf (fmt, args{:}); | |
111 endfor | |
112 endif | |
113 endfunction |