set-option -g command-alias[630] mighty_scroll_select_pane='\
  if-shell "$MIGHTY_SCROLL_SELECT_PANE" \
    "select-pane -t \"{mouse}\"" \
'

set-option -g command-alias[631] mighty_scroll_with_interval='\
  send-keys -t "{mouse}" -N $MIGHTY_SCROLL_INTERVAL \
'

set-option -g command-alias[632] mighty_scroll_exit_mode_if_bottom='\
  if-shell -F -t "{mouse}" "#{&&:#{pane_in_mode},#{==:#{scroll_position},0}}" \
    "send-keys -t \"{mouse}\" -X cancel" \
'

bind-key -T root WheelUpPane \
  mighty_scroll_select_pane \; \
  if-shell -t "{mouse}" \
    " \
      $PSCHECK #{pane_pid} $MIGHTY_SCROLL_BY_LINE || \
      ( \
        [ '$MIGHTY_SCROLL_FALLBACK_MODE' = 'by-line' ] && \
        #{?alternate_on,true,false} \
      ) \
    " \
    "mighty_scroll_with_interval Up" \
    "if-shell -t '{mouse}' \
      ' \
        $PSCHECK #{pane_pid} $MIGHTY_SCROLL_BY_PAGE || \
        ( \
          [ '$MIGHTY_SCROLL_FALLBACK_MODE' = 'by-page' ] && \
          #{?alternate_on,true,false} \
        ) \
      ' \
      'send-keys -t \"{mouse}\" PageUp' \
      'copy-mode -t \"{mouse}\" ; mighty_scroll_with_interval -X scroll-up' \
    "

bind-key -T root WheelDownPane \
  mighty_scroll_select_pane \; \
  if-shell -t "{mouse}" \
    " \
      $PSCHECK #{pane_pid} $MIGHTY_SCROLL_BY_LINE || \
      ( \
        [ '$MIGHTY_SCROLL_FALLBACK_MODE' = 'by-line' ] && \
        #{?alternate_on,true,false} \
      ) \
    " \
    "mighty_scroll_with_interval Down" \
    "if-shell -t \"{mouse}\" \
      ' \
        $PSCHECK #{pane_pid} $MIGHTY_SCROLL_BY_PAGE || \
        ( \
          [ '$MIGHTY_SCROLL_FALLBACK_MODE' = 'by-page' ] && \
          #{?alternate_on,true,false} \
        ) \
      ' \
      'send-keys -t \"{mouse}\" PageDown' \
    "

bind-key -T copy-mode WheelUpPane \
  mighty_scroll_select_pane \; \
  send-keys -X stop-selection \; \
  mighty_scroll_with_interval -X scroll-up

bind-key -T copy-mode WheelDownPane \
  mighty_scroll_select_pane \; \
  send-keys -X stop-selection \; \
  mighty_scroll_with_interval -X scroll-down \; \
  mighty_scroll_exit_mode_if_bottom

bind-key -T copy-mode-vi WheelUpPane \
  mighty_scroll_select_pane \; \
  send-keys -X stop-selection \; \
  mighty_scroll_with_interval -X scroll-up

bind-key -T copy-mode-vi WheelDownPane \
  mighty_scroll_select_pane \; \
  send-keys -X stop-selection \; \
  mighty_scroll_with_interval -X scroll-down \; \
  mighty_scroll_exit_mode_if_bottom
