code: def newline_or_execute_outer(shell):
      def newline_or_execute(event):
          """When the user presses return, insert a newline or execute the code."""
          b = event.current_buffer
          d = b.document
  
          if b.complete_state:
              cc = b.complete_state.current_completion
              if cc:
                  b.apply_completion(cc)
              else:
                  b.cancel_completion()
              return
  
          # If there's only one line, treat it as if the cursor is at the end.
          # See https://github.com/ipython/ipython/issues/10425
          if d.line_count == 1:
              check_text = d.text
          else:
              check_text = d.text[: d.cursor_position]
          status, indent = shell.input_splitter.check_complete(
              check_text + "\n"
          )
  
          if not (
              d.on_last_line
              or d.cursor_position_row
              >= d.line_count - d.empty_line_count_at_the_end()
          ):
              b.insert_text("\n" + (" " * (indent or 0)))
              return
  
          if (status != "incomplete") and b.accept_action.is_returnable:
              b.accept_action.validate_and_handle(event.cli, b)
          else:
              b.insert_text("\n" + (" " * (indent or 0)))
  
      return newline_or_execute
  
imports: import signal
  import sys
  from prompt_toolkit.enums import DEFAULT_BUFFER, SEARCH_BUFFER
  from prompt_toolkit.filters import HasFocus, HasSelection, Condition, ViInsertMode, EmacsInsertMode, HasCompletions
  from prompt_toolkit.filters.cli import ViMode
  from prompt_toolkit.keys import Keys
  from prompt_toolkit.key_binding.bindings.completion import display_completions_like_readline
  from IPython.utils.decorators import undoc
label_desc: Use image node_id 3 for calling a global function with example usage: newline_or_execute_outer(shell) and returns: newline_or_execute