!*
 * Here we define some reasonable default function key macros.
 * Their corresponding command-line editing macros begin with .
 *!

!*
 * Edit command line to move <n> chars, updating existing move ops.
 * The command line at the time of call looks like: (123C)(M[FOO
 *!
@#c{{U.c
  <-A-("=-D1;' -D>
  .-4"< Oins ' -2A-C"N -2A-R"N Oins ' ' -A-)"N Oins '
    <R -A-("=1;'>
    \U.v .-1,Z-2D 0A-R"=-'Q.v%.c 2D
  !ins!
  I( Q.c"> Q.c\IC | -Q.c\IR ' I)
}}

!*
 * Similar to ESGETCOLUMN,4EJ but takes character representations and
 * tab draw mode into account
 *!
@[store-col]{
  U.t Q.tESLINEFROMPOSITIONESPOSITIONFROMLINE:U.f
  Q.t-Q.f< %.f-1-.AU.c
    Q.c-9"= ESGETTABDRAWMODE-2"N ESGETTABWIDTHU.w Q.w-(Q.x ^/ Q.w) %.x F> ' '
    ESGETREPRESENTATIONU.c U.r Q.r"NQ.r'%.x
  >
  Q.x,4EJ
}

!*
 * Similar to 4EJ,:-Q.pESFINDCOLUMN:-. but takes character representations and
 * tab draw mode into account
 *!
@[restore-col]{
  U.p Q.p+:-1ESPOSITIONFROMLINE:-.U.p
  < Q.x-(4EJ)+1"> 1; '
    Q.pAU.c
    Q.c"< 1; ' Q.c-10"= 1; ' Q.c-13"= 1; '
    %.p
    Q.c-9"= ESGETTABDRAWMODE-2"N ESGETTABWIDTHU.w Q.w-(Q.x ^/ Q.w) %.x F> ' '
    ESGETREPRESENTATIONU.c U.r Q.r"NQ.r'%.x
  >
  Q.p
}

!*
 * It would be tricky to work with arbitrary radixes when rewriting command lines
 * in c. This macro can be called as :M# to guard against non-10 radixes.
 * FIXME: Once we have the commands, throw a custom error message.
 *!
@#{
  -@0a"N(0/0)'
}

!*
 * Make DELETE an ESCAPE surrogate.
 * Macro is enabled everywhere.
 *!
@[DC]{}

!*
 * Make SHIFT+DELETE a rubout/re-insert key.
 * This reverses the ^G modifier for BACKSPACE.
 * The macro is enabled everywhere.
 *!
@[SDC]{}

!*
 * Command line editing macros.
 * They are enabled only in the start state (i.e. they
 * have no effect in string arguments, etc.).
 *!

@[HOME]{
  :M#
  0+.U.p
  Q.pU.l <Q.l-."U 1; ' Q.l-.AU.c Q.c- "N Q.c-9"N Q.lU.p 1; '' %.l>
  Q.pM[store-col]
  Q.p-.M#c
}
@[HOME]{(:M[HOME]}
1U[HOME]

@[END]{
  :M#
  :-1ESGETLINEENDPOSITION:U.p
  Q.pM[store-col]
  Q.p-.M#c
}
@[END]{(:M[END]}
1U[END]

@[NPAGE]{
  :M#
  0,4EJ
  :-1+ESLINESONSCREEN
  ESPOSITIONFROMLINEU.p
  Q.p"< Z | Q.p: '-.M#c
}
@[NPAGE]{(:M[NPAGE]}
1U[NPAGE]

@[PPAGE]{
  :M#
  0,4EJ
  :-1-ESLINESONSCREENU.l
  Q.l"< 0 | Q.lESPOSITIONFROMLINE: '-.M#c
}
@[PPAGE]{(:M[PPAGE]}
1U[PPAGE]

@[LEFT]{
  :M#
  ."=0|.-1'U.p
  Q.pM[store-col]
  Q.p-.M#c
}
@[LEFT]{(:M[LEFT]}
1U[LEFT]

@[SLEFT]{
  :M#
  1,0,ESWORDSTARTPOSITIONESWORDSTARTPOSITION:U.p
  Q.pM[store-col]
  Q.p-.M#c
}
@[SLEFT]{(:M[SLEFT]}
1U[SLEFT]

@[RIGHT]{
  :M#
  .-Z"=.|.+1'U.p
  Q.pM[store-col]
  Q.p-.M#c
}
@[RIGHT]{(:M[RIGHT]}
1U[RIGHT]

@[SRIGHT]{
  :M#
  0,1,ESWORDENDPOSITIONESWORDENDPOSITION:U.p
  Q.pM[store-col]
  Q.p-.M#c
}
@[SRIGHT]{(:M[SRIGHT]}
1U[SRIGHT]

@[UP]{
  :M#
  -1M[restore-col]M#c
}
@[UP]{(:M[UP]}
1U[UP]

@[DOWN]{
  :M#
  1M[restore-col]M#c
}
@[DOWN]{(:M[DOWN]}
1U[DOWN]

@[CLOSE]{(EX)}
1U[CLOSE]

!*
 * F1 toggles __all__ folds.
 *!
@[F1]{(2ESFOLDALL{-15D}}
1U[F1]

!*
 * Zoom with F9/F10 if function keys are enabled.
 * This is automatically rubbed out.
 *!
@[F9]{(ESZOOMOUT{-14D}}
1U[F9]
@[F10]{(ESZOOMIN{-13D}}
1U[F10]

!*
 * Mouse integration.
 * Might be disabled unless bit 7 (64) is enabled in ED.
 *
 * Left click: Edit command line to jump to position.
 * Ctrl+left click: Jump to beginning of line.
 * Right click: Insert position or position range (when dragging).
 * Double right click: insert range for word under cursor
 * Ctrl+right click: Insertion beginning of line
 * Scroll wheel: scrolls (faster with shift)
 * Ctrl+scroll wheel: zoom (GTK-only)
 *
 * Also, you can click on the folding margin to toggle folds.
 *!
@[MOUSE]{
  :M#

  -2EJESCHARPOSITIONFROMPOINTU.p

  -2EJU.x ESGETMARGINLEFTU.r
  ESGETMARGINS<
    Q.rU.l Q.iESGETMARGINWIDTHN%.r
    Q.x-Q.l+1"> Q.x-Q.r"< !* mouse within margin i *!
      Q.iESGETMARGINMASKN&(-33554432)"N !* folding margin *!
        -EJ-1"= !* mouse released *!
          Q.pESLINEFROMPOSITIONESTOGGLEFOLD
          {-10D}
        '
      '
      1; !* handle like click in text area *!
    ' '
  %.i>

  -4EJ&2"N Q.pESLINEFROMPOSITIONESPOSITIONFROMLINEU.p '
  1,Q.pESWORDSTARTPOSITION:U.#ws
  1,Q.pESWORDENDPOSITION:U.#we
  Q.p:U.p

  -EJOpressed,released,scrollup,scrolldown
  !pressed!
    !* left click *!
    -3EJ-1"=
      Q.pM[store-col]
      Q.p-.M#c !* not reached *!
    '
    { -10D
      !* right click *!
      -3EJ-3"= Q.p"U I(\.p ' '
      !* middle click *!
      -3EJ-2"= :Q~"U I(g~) ' '
    }
  !released!
    { -10D
      -3EJ-3"= Q.p"U
        !* right click *!
        <-A-("=1;'R> \U.o
        Q.o-Q.p"=
          .-1"> -2A-)"=
            R <-A-("=1;'R>
            \-Q.p"=
              !* double right-click *!
              .,ZD I\.#ws,\.#we
            '
          ' '
          ZJ I)
        |
          Q.o-Q.p"> Q.o,Q.p U.oU.p '
          !* right drag *!
          .,ZD I\.o,\.p)
        '
      ' '
    }
  !scrollup!
    -4EJ&2"=
      -4EJ&1"=-2|-4',0ESLINESCROLL
    |
      ESZOOMIN ESSCROLLCARET
    '
    {-10D}
  !scrolldown!
    -4EJ&2"=
      -4EJ&1"=2|4',0ESLINESCROLL
    |
      ESZOOMOUT ESSCROLLCARET
    '
    {-10D}
}
@[MOUSE]{(:M[MOUSE]}
1U[MOUSE]
