Mercurial > hg > octave-nkf
annotate scripts/miscellaneous/what.m @ 20770:c1a6c31ac29a
eliminate more simple uses of error_state
* ov-classdef.cc: Eliminate simple uses of error_state.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 06 Oct 2015 00:20:02 -0400 |
parents | df437a52bcaf |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19794
diff
changeset
|
1 ## Copyright (C) 2007-2015 David Bateman |
7050 | 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 -*- | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
20 ## @deftypefn {Command} {} what |
7050 | 21 ## @deftypefnx {Command} {} what @var{dir} |
22 ## @deftypefnx {Function File} {w =} what (@var{dir}) | |
19375 | 23 ## List the Octave specific files in directory @var{dir}. |
24 ## | |
25 ## If @var{dir} is not specified then the current directory is used. | |
26 ## | |
27 ## If a return argument is requested, the files found are returned in the | |
28 ## structure @var{w}. The structure contains the following fields: | |
29 ## | |
30 ## @table @asis | |
31 ## @item path | |
32 ## Full path to directory @var{dir} | |
19602
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19375
diff
changeset
|
33 ## |
19375 | 34 ## @item m |
35 ## Cell array of m-files | |
19602
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19375
diff
changeset
|
36 ## |
19375 | 37 ## @item mat |
38 ## Cell array of mat files | |
19602
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19375
diff
changeset
|
39 ## |
19375 | 40 ## @item mex |
41 ## Cell array of mex files | |
19602
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19375
diff
changeset
|
42 ## |
19375 | 43 ## @item oct |
44 ## Cell array of oct files | |
19602
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19375
diff
changeset
|
45 ## |
19375 | 46 ## @item mdl |
47 ## Cell array of mdl files | |
19602
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19375
diff
changeset
|
48 ## |
19375 | 49 ## @item slx |
50 ## Cell array of slx files | |
19602
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19375
diff
changeset
|
51 ## |
19375 | 52 ## @item p |
53 ## Cell array of p-files | |
19602
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19375
diff
changeset
|
54 ## |
19375 | 55 ## @item classes |
56 ## Cell array of class directories (@file{@@@var{classname}/}) | |
19602
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19375
diff
changeset
|
57 ## |
19375 | 58 ## @item packages |
59 ## Cell array of package directories (@file{+@var{pkgname}/}) | |
60 ## @end table | |
61 ## | |
20374
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
62 ## Compatibility Note: Octave does not support mdl, slx, and p files; nor does |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
63 ## it support package directories. @code{what} will always return an empty |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
64 ## list for these categories. |
19375 | 65 ## @seealso{which, ls, exist} |
7050 | 66 ## @end deftypefn |
67 | |
19375 | 68 function retval = what (dir) |
69 | |
70 if (nargin > 1) | |
71 print_usage (); | |
72 endif | |
7050 | 73 |
74 if (nargin == 0) | |
19375 | 75 dir = pwd (); |
7050 | 76 else |
19375 | 77 dtmp = canonicalize_file_name (dir); |
18060
427412d40f1a
what.m: Workaround file separator issues in code (bug #40726).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
78 if (isempty (dtmp)) |
427412d40f1a
what.m: Workaround file separator issues in code (bug #40726).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
79 ## Search for directory name in path |
19375 | 80 if (dir(end) == '/' || dir(end) == '\') |
81 dir(end) = []; | |
18060
427412d40f1a
what.m: Workaround file separator issues in code (bug #40726).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
82 endif |
19375 | 83 dtmp = dir_in_loadpath (dir); |
18060
427412d40f1a
what.m: Workaround file separator issues in code (bug #40726).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
84 if (isempty (dtmp)) |
19375 | 85 error ("what: could not find the directory %s", dir); |
18060
427412d40f1a
what.m: Workaround file separator issues in code (bug #40726).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
86 endif |
7050 | 87 endif |
19375 | 88 dir = dtmp; |
7050 | 89 endif |
90 | |
19375 | 91 files = readdir (dir); |
92 w.path = dir; | |
7050 | 93 w.m = cell (0, 1); |
19375 | 94 w.mat = cell (0, 1); |
7050 | 95 w.mex = cell (0, 1); |
96 w.oct = cell (0, 1); | |
97 w.mdl = cell (0, 1); | |
19375 | 98 w.slx = cell (0, 1); |
7050 | 99 w.p = cell (0, 1); |
100 w.classes = cell (0, 1); | |
19375 | 101 w.packages = cell (0, 1); |
7050 | 102 |
103 for i = 1 : length (files) | |
18309
1456702a8b24
what.m: 3X performance boost. Don't display @fname as class unless it is a directory.
Rik <rik@octave.org>
parents:
18109
diff
changeset
|
104 n = files{i}; |
7050 | 105 ## Ignore . and .. |
106 if (strcmp (n, ".") || strcmp (n, "..")) | |
107 continue; | |
108 else | |
19794
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19602
diff
changeset
|
109 ## Ignore mdl, slx, p, and packages since they are not |
18060
427412d40f1a
what.m: Workaround file separator issues in code (bug #40726).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
110 [~, f, e] = fileparts (n); |
7050 | 111 if (strcmp (e, ".m")) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
112 w.m{end+1} = n; |
19375 | 113 elseif (strcmp (e, ".mat")) |
114 w.mat{end+1} = n; | |
7050 | 115 elseif (strcmp (e, ".oct")) |
10549 | 116 w.oct{end+1} = n; |
17386
6dbc866379e2
Replace cellfun() occurrences with faster code where possible.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
117 elseif (strcmp (e, mexext ())) |
6dbc866379e2
Replace cellfun() occurrences with faster code where possible.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
118 w.mex{end+1} = n; |
19375 | 119 elseif (n(1) == "@" && isdir (n)) |
10549 | 120 w.classes{end+1} = n; |
7050 | 121 endif |
122 endif | |
123 endfor | |
124 | |
125 if (nargout == 0) | |
126 __display_filenames__ ("M-files in directory", w.path, w.m); | |
19375 | 127 __display_filenames__ ("\nMAT-files in directory", w.path, w.mat); |
7050 | 128 __display_filenames__ ("\nMEX-files in directory", w.path, w.mex); |
129 __display_filenames__ ("\nOCT-files in directory", w.path, w.oct); | |
130 __display_filenames__ ("\nClasses in directory", w.path, w.classes); | |
131 else | |
19375 | 132 retval = w; |
7050 | 133 endif |
19375 | 134 |
7050 | 135 endfunction |
136 | |
137 function __display_filenames__ (msg, p, f) | |
19375 | 138 |
7050 | 139 if (length (f) > 0) |
11589
b0084095098e
missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
140 printf ("%s %s:\n\n", msg, p); |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
141 |
12931
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
12668
diff
changeset
|
142 maxlen = max (cellfun ("length", f)); |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
143 ncols = max (1, floor (terminal_size ()(2) / (maxlen + 3))); |
19375 | 144 fmt = sprintf (" %%-%ds", maxlen); |
145 fmt = repmat (fmt, [1, ncols]); | |
146 fmt = [fmt "\n"]; | |
7050 | 147 |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
148 nrows = ceil (length (f) / ncols); |
7050 | 149 for i = 1 : nrows |
150 args = f(i:nrows:end); | |
151 if (length (args) < ncols) | |
19375 | 152 args(end+1 : ncols) = {""}; |
7050 | 153 endif |
154 printf (fmt, args{:}); | |
155 endfor | |
156 endif | |
19375 | 157 |
7050 | 158 endfunction |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
16724
diff
changeset
|
159 |
19375 | 160 |
161 %!test | |
162 %! w = what (); | |
163 %! assert (w.path, pwd); | |
164 %! assert (fieldnames (w), {"path"; "m"; "mat"; "mex"; "oct"; "mdl"; "slx"; | |
165 %! "p"; "classes"; "packages"}); | |
166 | |
167 %!error what (1, 2) | |
168 |