/// <summary>
        /// Creates a new TextAreaDefaultInputHandler instance.
        /// </summary>
        public TextAreaDefaultInputHandler(TextArea textArea) : base(textArea)
        {
            this.NestedInputHandlers.Add(CaretNavigation = CaretNavigationCommandHandler.Create(textArea));
            this.NestedInputHandlers.Add(Editing         = EditingCommandHandler.Create(textArea));
            this.NestedInputHandlers.Add(MouseSelection  = new SelectionMouseHandler(textArea));

            this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Undo, ExecuteUndo, CanExecuteUndo));
            this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Redo, ExecuteRedo, CanExecuteRedo));
        }
 static ExecutedRoutedEventHandler OnDelete(CaretMovementType caretMovement)
 {
     return((target, args) => {
         TextArea textArea = GetTextArea(target);
         if (textArea != null && textArea.Document != null)
         {
             if (textArea.Selection.IsEmpty)
             {
                 TextViewPosition startPos = textArea.Caret.Position;
                 bool enableVirtualSpace = textArea.Options.EnableVirtualSpace;
                 // When pressing delete; don't move the caret further into virtual space - instead delete the newline
                 if (caretMovement == CaretMovementType.CharRight)
                 {
                     enableVirtualSpace = false;
                 }
                 double desiredXPos = textArea.Caret.DesiredXPos;
                 TextViewPosition endPos = CaretNavigationCommandHandler.GetNewCaretPosition(
                     textArea.TextView, startPos, caretMovement, enableVirtualSpace, ref desiredXPos);
                 // GetNewCaretPosition may return (0,0) as new position,
                 // thus we need to validate endPos before using it in the selection.
                 if (endPos.Line < 1 || endPos.Column < 1)
                 {
                     endPos = new TextViewPosition(Math.Max(endPos.Line, 1), Math.Max(endPos.Column, 1));
                 }
                 // Don't select the text to be deleted; just reuse the ReplaceSelectionWithText logic
                 var sel = new SimpleSelection(textArea, startPos, endPos);
                 sel.ReplaceSelectionWithText(string.Empty);
             }
             else
             {
                 textArea.RemoveSelectedText();
             }
             textArea.Caret.BringCaretToView();
             args.Handled = true;
         }
     });
 }