]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
[gdb/tui] Add tui_try_activate
authorTom de Vries <tdevries@suse.de>
Thu, 19 Mar 2026 09:55:46 +0000 (10:55 +0100)
committerTom de Vries <tdevries@suse.de>
Thu, 19 Mar 2026 09:55:46 +0000 (10:55 +0100)
In tui/tui.c, there this type of pattern related to activating TUI:
...
static int
tui_rl_delete_other_windows (int notused1, int notused2)
{
  if (!tui_active)
    tui_rl_switch_mode ();

  if (tui_active)
    tui_remove_some_windows ();

  return 0;
}
...

Add a new function tui_try_activate that allows us to write the shorter:
...
static int
tui_rl_delete_other_windows (int notused1, int notused2)
{
  if (tui_try_activate ())
    tui_remove_some_windows ();

  return 0;
}
...

Tested on x86_64-linux.

Reviewed-By: Keith Seitz <keiths@redhat.com>
gdb/tui/tui.c

index 5b322db5bdf5d2cc05ce17b205a6ba48bf1e6083..677a8c04b535b677962fc03633886e99aa43f113 100644 (file)
@@ -178,6 +178,18 @@ tui_rl_switch_mode (int notused1 = 0, int notused2 = 0)
   return 0;
 }
 
+/* Try to switch into TUI mode, if not already active.  Return if TUI mode
+   is active.  */
+
+static bool
+tui_try_activate ()
+{
+  if (!tui_active)
+    tui_rl_switch_mode ();
+
+  return tui_active;
+}
+
 /* TUI readline command.
    Change the TUI layout to show a next layout.
    This function is bound to CTRL-X 2.  It is intended to provide
@@ -185,10 +197,7 @@ tui_rl_switch_mode (int notused1 = 0, int notused2 = 0)
 static int
 tui_rl_change_windows (int notused1, int notused2)
 {
-  if (!tui_active)
-    tui_rl_switch_mode ();
-
-  if (tui_active)
+  if (tui_try_activate ())
     tui_next_layout ();
 
   return 0;
@@ -199,10 +208,7 @@ tui_rl_change_windows (int notused1, int notused2)
 static int
 tui_rl_delete_other_windows (int notused1, int notused2)
 {
-  if (!tui_active)
-    tui_rl_switch_mode ();
-
-  if (tui_active)
+  if (tui_try_activate ())
     tui_remove_some_windows ();
 
   return 0;
@@ -213,8 +219,7 @@ tui_rl_delete_other_windows (int notused1, int notused2)
 static int
 tui_rl_other_window (int count, int key)
 {
-  if (!tui_active)
-    tui_rl_switch_mode ();
+  tui_try_activate ();
 
   tui_set_win_focus_to (tui_next_win (tui_win_with_focus ()));
 
@@ -266,8 +271,7 @@ tui_rl_command_mode (int count, int key)
 static int
 tui_rl_next_keymap (int notused1, int notused2)
 {
-  if (!tui_active)
-    tui_rl_switch_mode ();
+  tui_try_activate ();
 
   if (rl_end)
     {