diff --git a/doc/ref/mloop.xml b/doc/ref/mloop.xml
index a0f844dd4d..5292ad5a52 100644
--- a/doc/ref/mloop.xml
+++ b/doc/ref/mloop.xml
@@ -743,12 +743,21 @@ is an example in the ⪆ code where the idea is actually used.
Backtrace
Stack trace
shows the last nr commands on the execution stack during whose execution
-the error occurred. If not given, nr defaults to 5. (Assume, for the
+the error occurred. If not given, nr defaults to the value of the
+WhereDepth user preference (initially 5).
+If nr or the WhereDepth preference is not a non-negative integer,
+it is treated as 5.
+(Assume, for the
following example, that after the last example
has been set back to its default value.). acts the
same as while also showing the arguments and local
variables of each function.
+ acts the same as while also
+showing the arguments and local variables of each function.
+
+The following example assumes that is set to its default
+value.
StabChain(SymmetricGroup(100)); # After this we typed ^C
user interrupt at
diff --git a/lib/error.g b/lib/error.g
index fbaceed8a3..7b403f911d 100644
--- a/lib/error.g
+++ b/lib/error.g
@@ -169,6 +169,9 @@ end);
BIND_GLOBAL("WHERE_INTERNAL", function(depth, showlocals)
local activecontext;
+ if not IsInt(depth) or depth < 0 then
+ depth := 5;
+ fi;
if ErrorLVars = fail or ErrorLVars = GetBottomLVars() then
PrintTo(ERROR_OUTPUT, "not in any function ");
else
@@ -181,7 +184,7 @@ end);
BIND_GLOBAL("WhereWithVars", function(arg)
local depth;
if LEN_LIST(arg) = 0 then
- depth := 5;
+ depth := UserPreference("WhereDepth");
else
depth := arg[1];
fi;
@@ -192,7 +195,7 @@ end);
BIND_GLOBAL("Where", function(arg)
local depth;
if LEN_LIST(arg) = 0 then
- depth := 5;
+ depth := UserPreference("WhereDepth");
else
depth := arg[1];
fi;
diff --git a/lib/init.g b/lib/init.g
index 810305951f..03601c810a 100644
--- a/lib/init.g
+++ b/lib/init.g
@@ -582,6 +582,16 @@ DeclareUserPreference( rec(
default:= 3,
check:= val -> IsInt( val ) and 0 <= val,
) );
+DeclareUserPreference( rec(
+ name:= "WhereDepth",
+ description:= [
+ "The number of stack frames shown by Where and WhereWithVars \
+when called without an explicit depth argument, e.g. in the default OnBreak \
+handler."
+ ],
+ default:= 5,
+ check:= val -> IsInt( val ) and 0 <= val,
+ ) );
DeclareUserPreference( rec(
name:= "ReproducibleBehaviour",
description:= [
diff --git a/tst/testinstall/error.tst b/tst/testinstall/error.tst
index 9cb0d353f7..e75a4a80ba 100644
--- a/tst/testinstall/error.tst
+++ b/tst/testinstall/error.tst
@@ -34,3 +34,18 @@ gap> if false then Stabilizer; fi;
Syntax error: found an expression when a statement was expected in stream:1
if false then Stabilizer; fi;
^
+
+#
+# WhereDepth user preference
+#
+gap> UserPreference("WhereDepth");
+5
+gap> SetUserPreference("WhereDepth", 10);
+gap> UserPreference("WhereDepth");
+10
+gap> SetUserPreference("WhereDepth", 0);
+gap> UserPreference("WhereDepth");
+0
+gap> SetUserPreference("WhereDepth", 5);
+gap> UserPreference("WhereDepth");
+5