@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" () };
  bind "<ctrl>x" { "set-mode" ("emacs-x", transient) };
  bind "<ctrl>underscore" { "undo" () };
  bind "<alt>x" { "action" ("win", "show-command-bar", "") };
  bind "<ctrl>r" { "action" ("frame", "find", "2") };
  bind "<ctrl>s" { "action" ("frame", "find", "3") };
  bind "<alt>period" { "goto-definition" () };
  bind "<alt>n" { "move-error" (down) };
  bind "<alt>p" { "move-error" (up) };
  bind "<ctrl>j" { "action" ("view-grid", "focus-neighbor", "3") };
  bind "<shift><ctrl>j" { "action" ("view-stack", "split-down", "") };
  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" ("view", "find-other-file", "") };

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

  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) };
}

@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" ("view", "close", "") };
  bind "k" { "action" ("view", "close", "") };
  bind "<ctrl>f" { "action" ("win", "open-with-dialog", "") };
  bind "<ctrl>s" { "action" ("view", "save", "") };
  bind "s" { "action" ("win", "save-all", "") };
  bind "<ctrl>b" { "action" ("view-stack", "show-list", "") };
  bind "<ctrl>w" { "action" ("view", "save-as", "") };
  bind "u" { "redo" () };
  bind "2" { "action" ("view-stack", "split-down", "") };
  bind "3" { "action" ("view-stack", "split-right", "") };
  bind "o" { "action" ("view-grid", "focus-neighbor", "0") };
  bind "grave" { "move-error" (down) };
  bind "h" { "select-all" (1) };
}

/*
 * 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;
}

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,
                     builder-gb-project-tree;
}
