diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 8c4177c1901..4ae84ec5a63 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -30311,10 +30311,9 @@ assembly windows.
 @item set tui compact-source @r{[}on@r{|}off@r{]}
 @kindex set tui compact-source
 Set whether the TUI source window is displayed in ``compact'' form.
-The default display uses more space for line numbers and starts the
-source text at the next tab stop; the compact display uses only as
-much space as is needed for the line numbers in the current file, and
-only a single space to separate the line numbers from the source.
+The default display uses more space for line numbers; the compact
+display uses only as much space as is needed for the line numbers in
+the current file.
 
 @kindex set debug tui
 @item set debug tui @r{[}on|off@r{]}
diff --git a/gdb/testsuite/gdb.tui/compact-source.exp b/gdb/testsuite/gdb.tui/compact-source.exp
new file mode 100644
index 00000000000..71e6b7b0b0a
--- /dev/null
+++ b/gdb/testsuite/gdb.tui/compact-source.exp
@@ -0,0 +1,61 @@
+# Copyright 2023 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Check that "set tui compact-source on" has the intended effect.
+
+require allow_tui_tests
+
+tuiterm_env
+
+standard_testfile
+
+# Let's generate the source file.  We want a short file, with less than 10
+# lines, and the copyright notice by itself is already more that that.
+set src_txt \
+    [join \
+	 [list \
+	      "int" \
+	      "main (void)" \
+	      "{" \
+	      "  return 0;" \
+	      "}"] "\n"]
+set srcfile [standard_output_file $srcfile]
+set fd [open $srcfile w]
+puts $fd $src_txt
+close $fd
+
+if {[build_executable "failed to prepare" ${testfile} ${srcfile}] == -1} {
+    return -1
+}
+
+Term::clean_restart 17 80 $binfile
+
+gdb_test_no_output "maint set tui-left-margin-verbose on"
+gdb_test_no_output "set tui compact-source on"
+
+if {![Term::enter_tui]} {
+    unsupported "TUI not supported"
+    return
+}
+
+set re_border "\\|"
+Term::check_contents "compact source format" \
+    "${re_border}___04_  return 0; *$re_border"
+
+with_test_prefix window-resize=1 {
+    Term::command "wh src -1"
+    Term::check_contents "compact source" \
+	"${re_border}___4_  return 0; *$re_border"
+}
diff --git a/gdb/tui/tui-source.c b/gdb/tui/tui-source.c
index 3d08ea8b7cd..1233e945cab 100644
--- a/gdb/tui/tui-source.c
+++ b/gdb/tui/tui-source.c
@@ -79,8 +79,12 @@ tui_source_window::set_contents (struct gdbarch *arch,
     {
       /* Solaris 11+gcc 5.5 has ambiguous overloads of log10, so we
 	 cast to double to get the right one.  */
-      double l = log10 ((double) offsets->size ());
-      m_digits = 1 + (int) l;
+      int lines_in_file = offsets->size ();
+      int last_line_nr_in_window = line_no + nlines - 1;
+      int max_line_nr = std::max (lines_in_file, last_line_nr_in_window);
+      int digits_needed = 1 + (int)log10 ((double) max_line_nr);
+      int trailing_space = 1;
+      m_digits = digits_needed + trailing_space;
     }
 
   m_max_length = -1;
diff --git a/gdb/tui/tui-win.c b/gdb/tui/tui-win.c
index 7eac03f47a1..6710b3e17e5 100644
--- a/gdb/tui/tui-win.c
+++ b/gdb/tui/tui-win.c
@@ -1274,8 +1274,7 @@ When enabled GDB will print a message when the terminal is resized."),
 Set whether the TUI source window is compact."), _("\
 Show whether the TUI source window is compact."), _("\
 This variable controls whether the TUI source window is shown\n\
-in a compact form.  The compact form puts the source closer to\n\
-the line numbers and uses less horizontal space."),
+in a compact form.  The compact form uses less horizontal space."),
 			   tui_set_compact_source, tui_show_compact_source,
 			   &tui_setlist, &tui_showlist);
 
