@import url("resource:///org/gnome/builder/keybindings/shared.css");

@binding-set builder-emacs-text-entry
{
  bind "<ctrl>b" { "move-cursor" (logical-positions, -1, 0) };
  bind "<shift><ctrl>b" { "move-cursor" (logical-positions, -1, 1) };
  bind "<ctrl>f" { "move-cursor" (logical-positions, 1, 0) };
  bind "<shift><ctrl>f" { "move-cursor" (logical-positions, 1, 1) };

  bind "<alt>b" { "move-cursor" (words, -1, 0) };
  bind "<shift><alt>b" { "move-cursor" (words, -1, 1) };
  bind "<alt>f" { "move-cursor" (words, 1, 0) };
  bind "<shift><alt>f" { "move-cursor" (words, 1, 1) };

  bind "<ctrl>a" { "move-cursor" (paragraph-ends, -1, 0) };
  bind "<shift><ctrl>a" { "move-cursor" (paragraph-ends, -1, 1) };
  bind "<ctrl>e" { "move-cursor" (paragraph-ends, 1, 0) };
  bind "<shift><ctrl>e" { "move-cursor" (paragraph-ends, 1, 1) };

  bind "<ctrl>w" { "cut-clipboard" () };
  bind "<alt>w" { "copy-clipboard" () };
  bind "<ctrl>y" { "paste-clipboard" () };

  bind "<ctrl>d" { "delete-from-cursor" (chars, 1) };
  bind "<alt>d" { "delete-from-cursor" (word-ends, 1) };
  bind "<ctrl>k" { "delete-from-cursor" (paragraph-ends, 1) };
  bind "<alt>backslash" { "delete-from-cursor" (whitespace, 1) };

  bind "<alt>space" { "delete-from-cursor" (whitespace, 1)
                      "insert-at-cursor" (" ") };
  bind "<alt>KP_Space" { "delete-from-cursor" (whitespace, 1)
                         "insert-at-cursor" (" ")  };
}

/*
 * Bindings for GtkTextView
 */
@binding-set builder-emacs-text-view
{
  bind "<ctrl>p" { "move-cursor" (display-lines, -1, 0) };
  bind "<shift><ctrl>p" { "move-cursor" (display-lines, -1, 1) };
  bind "<ctrl>n" { "move-cursor" (display-lines, 1, 0) };
  bind "<shift><ctrl>n" { "move-cursor" (display-lines, 1, 1) };

  bind "<ctrl>space" { "set-anchor" () };
  bind "<ctrl>KP_Space" { "set-anchor" () };
}

@binding-set builder-emacs-source-view
{
  bind "Escape" { "clear-search" ()
                  "clear-modifier" ()
                  "clear-selection" ()
                  "clear-count" ()
                  "clear-snippets" ()
                  "hide-completion" ()
                  "remove-cursors" () };
  bind "<ctrl><shift>e" { "add-cursor" (column) };
  bind "<ctrl><shift>d" { "add-cursor" (match) };
  bind "<ctrl>x" { "set-mode" ("emacs-x", transient) };
  bind "<ctrl>c" { "set-mode" ("emacs-c", transient) };
  bind "<ctrl>underscore" { "clear-count" ()
                            "clear-selection" ()
                            "remove-cursors" ()
                            "undo" () };
  bind "<alt>x" { "action" ("win", "show-command-bar", "") };
  bind "<ctrl>r" { "action" ("editor-page", "find", "") };
  bind "<ctrl>s" { "action" ("editor-page", "find", "") };
  bind "<alt>dollar" { "action" ("spellcheck", "spellcheck", "") };
  bind "<alt>period" { "goto-definition" () };
  bind "<alt>n" { "move-error" (down) };
  bind "<alt>p" { "move-error" (up) };
  bind "<ctrl>j" { "action" ("grid", "focus-neighbor", "3") };
  bind "<shift><ctrl>j" { "action" ("frame", "split-page", "''") };
  bind "F2" { "clear-selection" ()
              "movement" (previous-word-end, 0, 1, 1)
              "movement" (next-word-start, 0, 1, 0)
              "movement" (next-word-end, 1, 0, 1)
              "request-documentation" ()
              "clear-count" ()
              "clear-selection" () };

  bind "<ctrl>minus" { "decrease-font-size" () };
  bind "<ctrl>plus" { "increase-font-size" () };
  bind "<ctrl>equal" { "increase-font-size" () };
  bind "<ctrl>0" { "reset-font-size" () };

  bind "<ctrl>Right" { "movement" (next-sub-word-start, 0, 0, 0) };
  bind "<ctrl>Left" { "movement" (previous-sub-word-start, 0, 0, 0) };
  bind "<ctrl><shift>Right" { "movement" (next-sub-word-start, 1, 0, 0) };
  bind "<ctrl><shift>Left" { "movement" (previous-sub-word-start, 1, 0, 0) };

  /* allow entering raw code */
  bind "<ctrl>q" { "capture-modifier" ()
                   "insert-modifier" (0)
                   "clear-modifier" () };

  /* swap between header/source */
  bind "<alt>o" { "action" ("win", "find-other-file", "") };

  /* cycle "tabs" */
  bind "<ctrl><alt>Page_Up" { "action" ("frame", "previous-page", "") };
  bind "<ctrl><alt>KP_Page_Up" { "action" ("frame", "previous-page", "") };
  bind "<ctrl><alt>Page_Down" { "action" ("frame", "next-page", "") };
  bind "<ctrl><alt>KP_Page_Down" { "action" ("frame", "next-page", "") };

  bind "<alt>0" { "append-to-count" (0) };
  bind "<alt>1" { "append-to-count" (1) };
  bind "<alt>2" { "append-to-count" (2) };
  bind "<alt>3" { "append-to-count" (3) };
  bind "<alt>4" { "append-to-count" (4) };
  bind "<alt>5" { "append-to-count" (5) };
  bind "<alt>6" { "append-to-count" (6) };
  bind "<alt>7" { "append-to-count" (7) };
  bind "<alt>8" { "append-to-count" (8) };
  bind "<alt>9" { "append-to-count" (9) };

  /* Add back emoji */
  bind "<ctrl>semicolon" { "insert-emoji" () };

  bind "<ctrl>m" { "insert-at-cursor" ("\n") };
}

@binding-set builder-emacs-source-view-has-indenter
{
  bind "Tab" { "reindent" () };
}

@binding-set builder-emacs-source-view-x
{
  bind "<ctrl>c" { "action" ("app", "quit", "") };
  bind "0" { "action" ("frame", "close-page", "") };
  bind "k" { "action" ("frame", "close-page", "") };
  bind "<ctrl>f" { "action" ("win", "open-with-dialog", "") };
  bind "<ctrl>s" { "action" ("editor-page", "save", "") };
  bind "s" { "action" ("win", "save-all", "") };
  bind "<ctrl>b" { "action" ("frame", "show-list", "") };
  bind "<ctrl>w" { "action" ("editor-page", "save-as", "") };
  bind "u" { "clear-count" ()
             "clear-selection" ()
             "remove-cursors" ()
             "redo" () };
  bind "2" { "action" ("frame", "split-page", "''") };
  bind "3" { "action" ("frame", "open-in-new-frame", "''") };
  bind "o" { "action" ("grid", "focus-neighbor", "0") };
  bind "grave" { "move-error" (down) };
  bind "h" { "select-all" (1) };
  bind "<ctrl>space" { "action" ("history", "move-previous-edit", "") };
}

@binding-set builder-emacs-source-view-c
{
  bind "<ctrl>f" { "format-selection" () };
}

/*
 * Bindings for GtkTreeView
 */
@binding-set builder-emacs-tree-view
{
  bind "<ctrl>s" { "start-interactive-search" () };
  bind "<ctrl>f" { "move-cursor" (logical-positions, 1) };
  bind "<ctrl>b" { "move-cursor" (logical-positions, -1) };
}

@binding-set builder-emacs-list-box
{
  bind "<ctrl>f" { "move-cursor" (display-lines, 1) };
  bind "<ctrl>b" { "move-cursor" (display-lines, -1) };
}

@binding-set builder-emacs-editor-search
{
  bind "<ctrl>r" { "action" ("frame", "previous-search-result", "") };
  bind "<ctrl>s" { "action" ("frame", "next-search-result", "") };
}

frame.gb-search-frame entry {
  -gtk-key-bindings: builder-emacs-editor-search,
                     builder-emacs-text-entry;
}

entry {
  -gtk-key-bindings: builder-emacs-text-entry;
}

textview {
  -gtk-key-bindings: builder-emacs-text-entry, builder-emacs-text-view;
}

.sourceview,
idesourceviewmode.default
{
  -IdeSourceViewMode-repeat-insert-with-count: true;

  -gtk-key-bindings: builder-emacs-text-entry, builder-emacs-source-view, builder-emacs-text-view;
}

.sourceview,
idesourceviewmode.default.has-indenter
{
  -IdeSourceViewMode-repeat-insert-with-count: true;

  -gtk-key-bindings: builder-emacs-text-entry,
                     builder-emacs-source-view-has-indenter,
                     builder-emacs-source-view,
                     builder-emacs-text-view;
}

idesourceviewmode.emacs-x {
  -IdeSourceViewMode-display-name: "C-x";

  -gtk-key-bindings: builder-emacs-source-view-x;
}

idesourceviewmode.emacs-c {
  -IdeSourceViewMode-display-name: "C-c";

  -gtk-key-bindings: builder-emacs-source-view-c;
}

treeview {
  -gtk-key-bindings: builder-emacs-tree-view;
}

listbox {
  -gtk-key-bindings: builder-emacs-list-box;
}

treeview.project-tree {
  -gtk-key-bindings: builder-emacs-tree-view;
}
