;;; -*- lexical-binding: t -*-

;;; This file defines the most common and most general keymaps which are
;;; frequently extended by other modules.  Examples include the leader keymap,
;;; local-leader keymap, and the respective keymaps for window, file, and buffer
;;; operations.

(require 'syd/general)
(require 'syd/leader)

(general-def
  :prefix-map 'syd-leader-buffer-map
  "k" `("Kill buffer" . ,#'kill-current-buffer)
  "i" `("IBuffer" . ,#'ibuffer)
  "C" `("Clone (indirect) buffer O/W" . ,#'clone-indirect-buffer-other-window)
  "c" `("Clone (indirect) buffer" . ,#'clone-indirect-buffer)
  "r" `("Revert buffer" . ,#'revert-buffer))

(general-def
  :prefix-map 'syd-leader-search-map
  "i" `("IMenu" . ,#'consult-imenu))

(general-def
  :prefix-map 'syd-leader-file-map
  "r" `("Browse recent file" . ,#'consult-recent-file))

(general-def
  :prefix-map 'syd-leader-window-map
  "h" `("Select window left" . ,#'evil-window-left)
  "j" `("Select window below" . ,#'evil-window-down)
  "k" `("Select window above" . ,#'evil-window-up)
  "l" `("Select window right" . ,#'evil-window-right)
  "T" `("Tear off window" . ,#'tear-off-window)
  "=" `("Balance windows" . ,#'balance-windows)
  "v" `("Vertical split" . ,#'evil-window-vsplit)
  "s" `("Horizontal split" . ,#'evil-window-split)
  "F" `("Fit window to contents" . ,#'fit-window-to-buffer)
  "r" `("Rotate window downwards" . ,#'evil-window-rotate-downwards)
  "R" `("Rotate window upwards" . ,#'evil-window-rotate-upwards))

(general-def
  :prefix-map 'syd-leader-open-map)

(general-def
    :prefix-map 'syd-leader-project-map
    "C" `("Compile project" . ,#'project-compile)
    "&" `("Async cmd in project root" . ,#'project-async-shell-command)
    "p" `("Switch project" . ,#'project-switch-project)
    "." `("Browse project from root" . ,#'syd-project-root-find-file))

(general-def
  :prefix-map 'syd-leader-help-package-map
  "u" `("Temporarily install package" . ,#'straight-use-package)
  "v" `("Browse package repo" . ,#'straight-visit-package))

(general-def
  :prefix-map 'help-map
  "F" #'describe-face
  "'" #'describe-char
  "T" #'consult-theme
  "p" `("Packages" . ,syd-leader-help-package-map))

(general-def
  :prefix-map 'syd-leader-notes-map)

(general-def
  :prefix-map 'syd-leader-code-map)

(general-def
  :prefix-map 'syd-leader-insert-map
  "u" #'insert-char
  "e" #'emoji-insert
  "p" #'syd-insert-file-name)

;; This is necessary to properly rebind `universal-argument'.
;; `universal-argument-more' is a command that provides additional prefixes
;; after the first.  Without it, the first 'C-u' will be interpreted as a
;; prefix argument for the second 'C-u'.
(general-def
  :keymaps 'universal-argument-map
  "SPC u" #'universal-argument-more
  "u" #'universal-argument-more)

(general-def
  :keymaps 'syd-leader-map
  "." #'find-file
  "SPC" `("Find file in project" . ,#'project-find-file)
  "x" `("Open scratch buffer" . ,#'scratch-buffer)
  "u" `("Universal argument" . ,#'universal-argument)
  "b" `("Buffer" . ,syd-leader-buffer-map)
  "o" `("Open" . ,syd-leader-open-map)
  "p" `("Project" . ,syd-leader-project-map)
  "w" `("Window" . ,syd-leader-window-map)
  "f" `("File" . ,syd-leader-file-map)
  "s" `("Search" . ,syd-leader-search-map)
  "h" `("Help" . ,help-map)
  "n" `("Notes" . ,syd-leader-notes-map)
  "i" `("Insert" . ,syd-leader-insert-map)
  "c" `("Code" . ,syd-leader-code-map)
  "," `("Switch buffer in project" . ,#'consult-project-buffer)
  "<" `("Switch buffer" . ,#'consult-buffer))

(provide 'syd/keymaps)
