annotate scripts/prefs/setpref.m @ 27919:1891570abac8

update Octave Project Developers copyright for the new year In files that have the "Octave Project Developers" copyright notice, update for 2020.
author John W. Eaton <jwe@octave.org>
date Mon, 06 Jan 2020 22:29:51 -0500
parents b442ec6dda5c
children bd51beb6205e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27919
1891570abac8 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27918
diff changeset
1 ## Copyright (C) 2012-2020 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27898
diff changeset
2 ##
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27898
diff changeset
3 ## See the file COPYRIGHT.md in the top-level directory of this distribution
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27898
diff changeset
4 ## or <https://octave.org/COPYRIGHT.html/>.
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27898
diff changeset
5 ##
13261
9134ca9d4ec8 new functions for Matlab compatibility
John W. Eaton <jwe@octave.org>
parents:
diff changeset
6 ##
9134ca9d4ec8 new functions for Matlab compatibility
John W. Eaton <jwe@octave.org>
parents:
diff changeset
7 ## This file is part of Octave.
9134ca9d4ec8 new functions for Matlab compatibility
John W. Eaton <jwe@octave.org>
parents:
diff changeset
8 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
9 ## Octave is free software: you can redistribute it and/or modify it
13261
9134ca9d4ec8 new functions for Matlab compatibility
John W. Eaton <jwe@octave.org>
parents:
diff changeset
10 ## 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: 23220
diff changeset
11 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
12 ## (at your option) any later version.
13261
9134ca9d4ec8 new functions for Matlab compatibility
John W. Eaton <jwe@octave.org>
parents:
diff changeset
13 ##
9134ca9d4ec8 new functions for Matlab compatibility
John W. Eaton <jwe@octave.org>
parents:
diff changeset
14 ## Octave is distributed in the hope that it will be useful, but
9134ca9d4ec8 new functions for Matlab compatibility
John W. Eaton <jwe@octave.org>
parents:
diff changeset
15 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## GNU General Public License for more details.
13261
9134ca9d4ec8 new functions for Matlab compatibility
John W. Eaton <jwe@octave.org>
parents:
diff changeset
18 ##
9134ca9d4ec8 new functions for Matlab compatibility
John W. Eaton <jwe@octave.org>
parents:
diff changeset
19 ## You should have received a copy of the GNU General Public License
9134ca9d4ec8 new functions for Matlab compatibility
John W. Eaton <jwe@octave.org>
parents:
diff changeset
20 ## 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: 23220
diff changeset
21 ## <https://www.gnu.org/licenses/>.
13261
9134ca9d4ec8 new functions for Matlab compatibility
John W. Eaton <jwe@octave.org>
parents:
diff changeset
22
9134ca9d4ec8 new functions for Matlab compatibility
John W. Eaton <jwe@octave.org>
parents:
diff changeset
23 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20834
diff changeset
24 ## @deftypefn {} {} setpref ("@var{group}", "@var{pref}", @var{val})
22845
e23f7f47d8a3 doc: Small fixes to docstrings (bug #49733).
Rik <rik@octave.org>
parents: 22323
diff changeset
25 ## @deftypefnx {} {} setpref ("@var{group}", @{"@var{pref1}", "@var{pref2}", @dots{}@}, @{@var{val1}, @var{val2}, @dots{}@})
20834
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
26 ## Set the preference @var{pref} to the given @var{val} in the named preference
20160
03b9d17a2d95 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
27 ## group @var{group}.
13261
9134ca9d4ec8 new functions for Matlab compatibility
John W. Eaton <jwe@octave.org>
parents:
diff changeset
28 ##
20834
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
29 ## The named preference group must be a string.
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
30 ##
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
31 ## The preference @var{pref} may be a string or a cell array of strings.
13261
9134ca9d4ec8 new functions for Matlab compatibility
John W. Eaton <jwe@octave.org>
parents:
diff changeset
32 ##
20834
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
33 ## The corresponding value @var{val} may be any Octave value, .e.g., double,
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
34 ## struct, cell array, object, etc. Or, if @var{pref} is a cell array of
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
35 ## strings then @var{val} must be a cell array of values with the same size as
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
36 ## @var{pref}.
13261
9134ca9d4ec8 new functions for Matlab compatibility
John W. Eaton <jwe@octave.org>
parents:
diff changeset
37 ##
9134ca9d4ec8 new functions for Matlab compatibility
John W. Eaton <jwe@octave.org>
parents:
diff changeset
38 ## If the named preference or group does not exist, it is added.
13943
5820f8ce683e doc: Add undocumented functions to manual for 3.6.0 release
Rik <octave@nomad.inbox5.com>
parents: 13929
diff changeset
39 ## @seealso{addpref, getpref, ispref, rmpref}
13261
9134ca9d4ec8 new functions for Matlab compatibility
John W. Eaton <jwe@octave.org>
parents:
diff changeset
40 ## @end deftypefn
9134ca9d4ec8 new functions for Matlab compatibility
John W. Eaton <jwe@octave.org>
parents:
diff changeset
41
9134ca9d4ec8 new functions for Matlab compatibility
John W. Eaton <jwe@octave.org>
parents:
diff changeset
42 function setpref (group, pref, val)
9134ca9d4ec8 new functions for Matlab compatibility
John W. Eaton <jwe@octave.org>
parents:
diff changeset
43
20834
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
44 if (nargin != 3)
13261
9134ca9d4ec8 new functions for Matlab compatibility
John W. Eaton <jwe@octave.org>
parents:
diff changeset
45 print_usage ();
9134ca9d4ec8 new functions for Matlab compatibility
John W. Eaton <jwe@octave.org>
parents:
diff changeset
46 endif
9134ca9d4ec8 new functions for Matlab compatibility
John W. Eaton <jwe@octave.org>
parents:
diff changeset
47
20834
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
48 if (! ischar (group))
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
49 error ("setpref: GROUP must be a string");
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
50 endif
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
51 if (! (ischar (pref) || iscellstr (pref)))
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
52 error ("setpref: PREF must be a string or cellstr");
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
53 endif
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
54
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
55 prefs = loadprefs ();
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
56
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
57 if (ischar (pref))
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
58 prefs.(group).(pref) = val;
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
59 else
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
60 if (! size_equal (pref, val))
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
61 error ("setpref: size mismatch for PREF and VAL");
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
62 endif
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
63
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
64 for i = 1:numel (pref)
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
65 prefs.(group).(pref{i}) = val{i};
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
66 endfor
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
67 endif
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
68
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
69 saveprefs (prefs);
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
70
13261
9134ca9d4ec8 new functions for Matlab compatibility
John W. Eaton <jwe@octave.org>
parents:
diff changeset
71 endfunction
9134ca9d4ec8 new functions for Matlab compatibility
John W. Eaton <jwe@octave.org>
parents:
diff changeset
72
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 14868
diff changeset
73
20834
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
74 %!test
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
75 %! HOME = getenv ("HOME");
24835
c644c53c8d37 use unique directory names in getpref and setpref tests
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
76 %! tmp_home = tempname ();
20834
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
77 %! unwind_protect
24835
c644c53c8d37 use unique directory names in getpref and setpref tests
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
78 %! mkdir (tmp_home);
c644c53c8d37 use unique directory names in getpref and setpref tests
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
79 %! setenv ("HOME", tmp_home);
20834
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
80 %!
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
81 %! setpref ("group1", "pref1", [1 2 3]);
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
82 %! assert (getpref ("group1", "pref1"), [1 2 3]);
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
83 %!
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
84 %! setpref ("group2", {"prefA", "prefB"}, {"StringA", {"StringB"}});
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
85 %! assert (getpref ("group2", "prefA"), "StringA");
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
86 %! assert (getpref ("group2", "prefB"), {"StringB"});
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
87 %!
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
88 %! setpref ("group1", {"pref1", "pref2"}, {1, 2});
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
89 %! assert (getpref ("group1", "pref1"), 1);
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
90 %! assert (getpref ("group1", "pref2"), 2);
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
91 %!
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
92 %! fail ('setpref ("group1", {"p1", "p2"}, 1)', ...
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
93 %! "size mismatch for PREF and VAL");
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
94 %! unwind_protect_cleanup
24835
c644c53c8d37 use unique directory names in getpref and setpref tests
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
95 %! unlink (fullfile (tmp_home, ".octave_prefs"));
25787
3293cd2651fb prefer 'isfolder' over 'exist (..., "dir")' in Octave functions
Mike Miller <mtmiller@octave.org>
parents: 25054
diff changeset
96 %! if (isfolder (tmp_home))
24835
c644c53c8d37 use unique directory names in getpref and setpref tests
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
97 %! rmdir (tmp_home);
c644c53c8d37 use unique directory names in getpref and setpref tests
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
98 %! endif
20834
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
99 %! if (isempty (HOME))
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
100 %! unsetenv ("HOME");
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
101 %! else
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
102 %! setenv ("HOME", HOME);
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
103 %! endif
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
104 %! end_unwind_protect
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 14868
diff changeset
105
20834
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
106 %!error setpref ()
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
107 %!error setpref (1)
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
108 %!error setpref (1,2)
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
109 %!error setpref (1,2,3,4)
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
110 %!error <GROUP must be a string> setpref (1, "pref1", 2)
27b333c88c8e Overhaul m-files in prefs directory.
Rik <rik@octave.org>
parents: 20711
diff changeset
111 %!error <PREF must be a string> setpref ("group1", 1, 2)