1########################################################################
2##
3## Copyright (C) 2012-2021 The Octave Project Developers
4##
5## See the file COPYRIGHT.md in the top-level directory of this
6## distribution or <https://octave.org/copyright/>.
7##
8## This file is part of Octave.
9##
10## Octave is free software: you can redistribute it and/or modify it
11## under the terms of the GNU General Public License as published by
12## the Free Software Foundation, either version 3 of the License, or
13## (at your option) any later version.
14##
15## Octave is distributed in the hope that it will be useful, but
16## WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with Octave; see the file COPYING.  If not, see
22## <https://www.gnu.org/licenses/>.
23##
24########################################################################
25
26## -*- texinfo -*-
27## @deftypefn  {} {} rmpref ("@var{group}", "@var{pref}")
28## @deftypefnx {} {} rmpref ("@var{group}", @{"@var{pref1}", "@var{pref2}", @dots{}@})
29## @deftypefnx {} {} rmpref ("@var{group}")
30## Remove the named preference @var{pref} from the preference group
31## @var{group}.
32##
33## The named preference group must be a string.
34##
35## The preference @var{pref} may be a string or cell array of strings.
36##
37## If @var{pref} is not specified, remove the preference group @var{group}.
38##
39## It is an error to remove a nonexistent preference or group.
40## @seealso{addpref, ispref, setpref, getpref}
41## @end deftypefn
42
43function rmpref (group, pref)
44
45  if (nargin < 1 || nargin > 2)
46    print_usage ();
47  elseif (! ischar (group))
48    error ("rmpref: GROUP must be a string");
49  elseif (nargin == 2 && ! (ischar (pref) || iscellstr (pref)))
50    error ("rmpref: PREF must be a string or cell array of strings");
51  endif
52
53  if (nargin == 1)
54    if (! ispref (group))
55      error ("rmpref: GROUP %s does not exist", group);
56    endif
57    prefs = loadprefs ();
58    prefs = rmfield (prefs, group);
59    saveprefs (prefs);
60  else
61    valid = ispref (group, pref);
62    if (all (valid))
63      prefs = loadprefs ();
64      prefs.(group) = rmfield (prefs.(group), pref);
65      saveprefs (prefs);
66    else
67      if (! ispref (group))
68        error ("rmpref: GROUP %s does not exist", group);
69      elseif (ischar (pref))
70        error ("rmpref: preference %s does not exist", pref);
71      else
72        idx = find (! valid, 1);
73        error ("rmpref: preference %s does not exist", pref{idx});
74      endif
75    endif
76  endif
77
78endfunction
79
80
81%!test
82%! HOME = getenv ("HOME");
83%! save_default_options ("-binary", "local");
84%! unwind_protect
85%!   setenv ("HOME", P_tmpdir ());
86%!   addpref ("group1", "pref1", [1 2 3]);
87%!   addpref ("group2", {"prefA", "prefB", "prefC"}, {"strA", "strB", "strC"});
88%!
89%!   assert (ispref ("group1"));
90%!   rmpref ("group1");
91%!   assert (! ispref ("group1"));
92%!
93%!   assert (ispref ("group2", "prefB"));
94%!   rmpref ("group2", "prefB");
95%!   assert (! ispref ("group2", "prefB"));
96%!
97%!   fail ('rmpref ("group3")', ...
98%!         "GROUP group3 does not exist");
99%!   fail ('rmpref ("group3", "prefA")', ...
100%!         "GROUP group3 does not exist");
101%!   fail ('rmpref ("group2", "prefB")',
102%!         "preference prefB does not exist");
103%!   fail ('rmpref ("group2", {"prefA", "prefB"})',
104%!         "preference prefB does not exist");
105%!
106%! unwind_protect_cleanup
107%!   unlink (fullfile (P_tmpdir (), ".octave_prefs"));
108%!   if (isempty (HOME))
109%!     unsetenv ("HOME");
110%!   else
111%!     setenv ("HOME", HOME);
112%!   endif
113%! end_unwind_protect
114
115## Test input validation
116%!error rmpref ()
117%!error rmpref (1,2,3)
118%!error <GROUP must be a string> rmpref (1)
119%!error <PREF must be a string> rmpref ("group1", 1)
120