annotate scripts/help/which.m @ 30260:bfbe6e528af5

which: report correct info for classdef constructors (bug #49434) * help.cc (help_system::which): Handle octave_classdef_meta objects. * which.m: Fix test.
author John W. Eaton <jwe@octave.org>
date Sat, 30 Oct 2021 09:47:47 -0400
parents 7854d5752dd2
children 49c8538c64c4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
29358
0a5b15007766 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 28024
diff changeset
3 ## Copyright (C) 2009-2021 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27018
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
7 ##
11104
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 10549
diff changeset
8 ## This file is part of Octave.
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 10549
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23295
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23295
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22430
diff changeset
13 ## (at your option) any later version.
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
14 ##
11104
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 10549
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22430
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22430
diff changeset
18 ## GNU General Public License for more details.
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
19 ##
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
11104
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 10549
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23295
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
25
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20158
diff changeset
27 ## @deftypefn {} {} which name @dots{}
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
28 ## Display the type of each @var{name}.
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
29 ##
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
30 ## If @var{name} is defined from a function file, the full name of the file is
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
31 ## also displayed.
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
32 ## @seealso{help, lookfor}
11547
e1851653d59c Eliminate @deffn macros.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
33 ## @end deftypefn
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
34
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
35 function varargout = which (varargin)
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
36
20922
49081851fddc Eliminate duplicate input arg checking in which.m and __which__.
Rik <rik@octave.org>
parents: 20852
diff changeset
37 if (nargin == 0 || ! iscellstr (varargin))
49081851fddc Eliminate duplicate input arg checking in which.m and __which__.
Rik <rik@octave.org>
parents: 20852
diff changeset
38 print_usage ();
49081851fddc Eliminate duplicate input arg checking in which.m and __which__.
Rik <rik@octave.org>
parents: 20852
diff changeset
39 endif
49081851fddc Eliminate duplicate input arg checking in which.m and __which__.
Rik <rik@octave.org>
parents: 20852
diff changeset
40
49081851fddc Eliminate duplicate input arg checking in which.m and __which__.
Rik <rik@octave.org>
parents: 20852
diff changeset
41 m = __which__ (varargin{:});
49081851fddc Eliminate duplicate input arg checking in which.m and __which__.
Rik <rik@octave.org>
parents: 20852
diff changeset
42
49081851fddc Eliminate duplicate input arg checking in which.m and __which__.
Rik <rik@octave.org>
parents: 20852
diff changeset
43 ## Check whether each name is a variable, variables take precedence over
49081851fddc Eliminate duplicate input arg checking in which.m and __which__.
Rik <rik@octave.org>
parents: 20852
diff changeset
44 ## functions in name resolution.
49081851fddc Eliminate duplicate input arg checking in which.m and __which__.
Rik <rik@octave.org>
parents: 20852
diff changeset
45 for i = 1:nargin
49081851fddc Eliminate duplicate input arg checking in which.m and __which__.
Rik <rik@octave.org>
parents: 20852
diff changeset
46 m(i).is_variable = evalin ("caller",
27018
a20ead51515d move core of evalin function to evaluator
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
47 ['exist ("' undo_string_escapes(m(i).name) '", "var")'], "");
20922
49081851fddc Eliminate duplicate input arg checking in which.m and __which__.
Rik <rik@octave.org>
parents: 20852
diff changeset
48 if (m(i).is_variable)
49081851fddc Eliminate duplicate input arg checking in which.m and __which__.
Rik <rik@octave.org>
parents: 20852
diff changeset
49 m(i).file = "variable";
49081851fddc Eliminate duplicate input arg checking in which.m and __which__.
Rik <rik@octave.org>
parents: 20852
diff changeset
50 endif
49081851fddc Eliminate duplicate input arg checking in which.m and __which__.
Rik <rik@octave.org>
parents: 20852
diff changeset
51 endfor
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
52
20922
49081851fddc Eliminate duplicate input arg checking in which.m and __which__.
Rik <rik@octave.org>
parents: 20852
diff changeset
53 if (nargout == 0)
18451
2bd78ab75b23 which: Properly identify workspace variables (bug #37320)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
54 for i = 1:nargin
2bd78ab75b23 which: Properly identify workspace variables (bug #37320)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
55 if (m(i).is_variable)
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
56 printf ("'%s' is a variable\n", m(i).name);
20922
49081851fddc Eliminate duplicate input arg checking in which.m and __which__.
Rik <rik@octave.org>
parents: 20852
diff changeset
57 elseif (isempty (m(i).file))
49081851fddc Eliminate duplicate input arg checking in which.m and __which__.
Rik <rik@octave.org>
parents: 20852
diff changeset
58 if (! isempty (m(i).type))
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
59 printf ("'%s' is a %s\n",
20922
49081851fddc Eliminate duplicate input arg checking in which.m and __which__.
Rik <rik@octave.org>
parents: 20852
diff changeset
60 m(i).name, m(i).type);
49081851fddc Eliminate duplicate input arg checking in which.m and __which__.
Rik <rik@octave.org>
parents: 20852
diff changeset
61 endif
49081851fddc Eliminate duplicate input arg checking in which.m and __which__.
Rik <rik@octave.org>
parents: 20852
diff changeset
62 else
49081851fddc Eliminate duplicate input arg checking in which.m and __which__.
Rik <rik@octave.org>
parents: 20852
diff changeset
63 if (isempty (m(i).type))
25781
e04c56bbbace isdir.m: Make m-file a legacy function (bug #54489)
Rik <rik@octave.org>
parents: 25054
diff changeset
64 if (isfolder (m(i).file))
23046
168a18a57fc2 which.m: change output to distinguish between files and directories.
Rik <rik@octave.org>
parents: 22755
diff changeset
65 printf ("'%s' is the directory %s\n",
168a18a57fc2 which.m: change output to distinguish between files and directories.
Rik <rik@octave.org>
parents: 22755
diff changeset
66 m(i).name, m(i).file);
168a18a57fc2 which.m: change output to distinguish between files and directories.
Rik <rik@octave.org>
parents: 22755
diff changeset
67 else
168a18a57fc2 which.m: change output to distinguish between files and directories.
Rik <rik@octave.org>
parents: 22755
diff changeset
68 printf ("'%s' is the file %s\n",
168a18a57fc2 which.m: change output to distinguish between files and directories.
Rik <rik@octave.org>
parents: 22755
diff changeset
69 m(i).name, m(i).file);
168a18a57fc2 which.m: change output to distinguish between files and directories.
Rik <rik@octave.org>
parents: 22755
diff changeset
70 endif
20922
49081851fddc Eliminate duplicate input arg checking in which.m and __which__.
Rik <rik@octave.org>
parents: 20852
diff changeset
71 else
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
72 printf ("'%s' is a %s from the file %s\n",
20922
49081851fddc Eliminate duplicate input arg checking in which.m and __which__.
Rik <rik@octave.org>
parents: 20852
diff changeset
73 m(i).name, m(i).type, m(i).file);
49081851fddc Eliminate duplicate input arg checking in which.m and __which__.
Rik <rik@octave.org>
parents: 20852
diff changeset
74 endif
18451
2bd78ab75b23 which: Properly identify workspace variables (bug #37320)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
75 endif
2bd78ab75b23 which: Properly identify workspace variables (bug #37320)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
76 endfor
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
77 else
20922
49081851fddc Eliminate duplicate input arg checking in which.m and __which__.
Rik <rik@octave.org>
parents: 20852
diff changeset
78 varargout = {m.file};
23295
c1362a71fb0e which.m: Return a non-empty string for built-in classes and command line fcns (bug #50541);
Rik <rik@octave.org>
parents: 23220
diff changeset
79 ## Return type, instead of "", for built-in classes (bug #50541).
c1362a71fb0e which.m: Return a non-empty string for built-in classes and command line fcns (bug #50541);
Rik <rik@octave.org>
parents: 23220
diff changeset
80 ## FIXME: remove code if __which__ is updated to return path for classes
c1362a71fb0e which.m: Return a non-empty string for built-in classes and command line fcns (bug #50541);
Rik <rik@octave.org>
parents: 23220
diff changeset
81 idx = find (cellfun ("isempty", varargout));
c1362a71fb0e which.m: Return a non-empty string for built-in classes and command line fcns (bug #50541);
Rik <rik@octave.org>
parents: 23220
diff changeset
82 if (idx)
c1362a71fb0e which.m: Return a non-empty string for built-in classes and command line fcns (bug #50541);
Rik <rik@octave.org>
parents: 23220
diff changeset
83 varargout(idx) = m(idx).type;
c1362a71fb0e which.m: Return a non-empty string for built-in classes and command line fcns (bug #50541);
Rik <rik@octave.org>
parents: 23220
diff changeset
84 endif
8575
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
85 endif
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
86
f134925a1cfa m-file implementation of help system
Soren Hauberg <soren@hauberg.org>
parents:
diff changeset
87 endfunction
12843
3035c2e130de codesprint: Tests for which.m
Rik <octave@nomad.inbox5.com>
parents: 11547
diff changeset
88
3035c2e130de codesprint: Tests for which.m
Rik <octave@nomad.inbox5.com>
parents: 11547
diff changeset
89
3035c2e130de codesprint: Tests for which.m
Rik <octave@nomad.inbox5.com>
parents: 11547
diff changeset
90 %!test
3035c2e130de codesprint: Tests for which.m
Rik <octave@nomad.inbox5.com>
parents: 11547
diff changeset
91 %! str = which ("ls");
23295
c1362a71fb0e which.m: Return a non-empty string for built-in classes and command line fcns (bug #50541);
Rik <rik@octave.org>
parents: 23220
diff changeset
92 %! assert (str(end-17:end), fullfile ("miscellaneous", "ls.m"));
12843
3035c2e130de codesprint: Tests for which.m
Rik <octave@nomad.inbox5.com>
parents: 11547
diff changeset
93 %!test
28024
c28b8ba841fb move sparse functions from dldfcn to corefcn directory (bug #57459)
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
94 %! str = which ("fftw");
c28b8ba841fb move sparse functions from dldfcn to corefcn directory (bug #57459)
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
95 %! assert (str(end-7:end), "fftw.oct");
30260
bfbe6e528af5 which: report correct info for classdef constructors (bug #49434)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
96 %!assert <49434> (which ("inputParser"), file_in_loadpath ("inputParser.m"));
18451
2bd78ab75b23 which: Properly identify workspace variables (bug #37320)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
97 %!test
2bd78ab75b23 which: Properly identify workspace variables (bug #37320)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
98 %! x = 3;
2bd78ab75b23 which: Properly identify workspace variables (bug #37320)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
99 %! str = which ("x");
2bd78ab75b23 which: Properly identify workspace variables (bug #37320)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
100 %! assert (str, "variable");
2bd78ab75b23 which: Properly identify workspace variables (bug #37320)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
101
23295
c1362a71fb0e which.m: Return a non-empty string for built-in classes and command line fcns (bug #50541);
Rik <rik@octave.org>
parents: 23220
diff changeset
102 %!assert (which ("__NO_SUCH_NAME__"), "")
c1362a71fb0e which.m: Return a non-empty string for built-in classes and command line fcns (bug #50541);
Rik <rik@octave.org>
parents: 23220
diff changeset
103
18451
2bd78ab75b23 which: Properly identify workspace variables (bug #37320)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
104 %!test
28024
c28b8ba841fb move sparse functions from dldfcn to corefcn directory (bug #57459)
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
105 %! str = which ("fftw");
c28b8ba841fb move sparse functions from dldfcn to corefcn directory (bug #57459)
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
106 %! assert (str(end-7:end), "fftw.oct");
c28b8ba841fb move sparse functions from dldfcn to corefcn directory (bug #57459)
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
107 %! fftw = 12;
c28b8ba841fb move sparse functions from dldfcn to corefcn directory (bug #57459)
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
108 %! str = which ("fftw");
18451
2bd78ab75b23 which: Properly identify workspace variables (bug #37320)
Mike Miller <mtmiller@ieee.org>
parents: 17744
diff changeset
109 %! assert (str, "variable");
28024
c28b8ba841fb move sparse functions from dldfcn to corefcn directory (bug #57459)
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
110 %! clear fftw;
c28b8ba841fb move sparse functions from dldfcn to corefcn directory (bug #57459)
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
111 %! str = which ("fftw");
c28b8ba841fb move sparse functions from dldfcn to corefcn directory (bug #57459)
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
112 %! assert (str(end-7:end), "fftw.oct");
23295
c1362a71fb0e which.m: Return a non-empty string for built-in classes and command line fcns (bug #50541);
Rik <rik@octave.org>
parents: 23220
diff changeset
113
28896
90fea9cc9caa test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents: 28024
diff changeset
114 %!error <Invalid call> which ()
90fea9cc9caa test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents: 28024
diff changeset
115 %!error <Invalid call> which (1)
23295
c1362a71fb0e which.m: Return a non-empty string for built-in classes and command line fcns (bug #50541);
Rik <rik@octave.org>
parents: 23220
diff changeset
116