;;; init-minibuffer.el --- Ayuditas y autocompletado del minibufer  -*- lexical-binding: t -*-

;; Author: kj <webmaster@outcontrol.net>
;; URL: https://git.kj2.me/kj/confi-emacs-actual

;;; Commentary:

;; Aquí se encuentran todos los paquetes que tienen como
;; objetivo mejorar el minibuffer de Emacs.

;;; Code:

;; UI para completado
(use-package vertico
  :defer t
  :bind (:map vertico-map
         ("RET" . vertico-directory-enter)
         ("DEL" . vertico-directory-delete-char)
         ("TAB" . minibuffer-complete)
         ("M-DEL" . vertico-directory-delete-word))
  :hook ((elpaca-after-init . vertico-mode)
         (elpaca-after-init . savehist-mode) ;; savehist sirve para recordar el último comando de M-x
         (rfn-eshadow-update-overlay . vertico-directory-tidy))
  :config
  (setq read-file-name-completion-ignore-case t
        read-buffer-completion-ignore-case t
        completion-ignore-case t)
  )

;; Reemplazo para counsel
(use-package consult
  :defer t
  :bind (;; C-c bindings in `mode-specific-map'
         ("C-s"     . consult-line)
         ("C-r"     . consult-ripgrep)
         ("C-S-s"   . isearch-forward)
         ("C-S-r"   . isearch-backward)
         ("M-m"     . consult-mode-command)
         ("C-c h"   . consult-history)
         ("C-c k"   . consult-kmacro)

         ([remap Info-search]        . consult-info)
         ([remap imenu]              . consult-imenu)
         ([remap recentf-open-files] . consult-recent-file)


         ;; C-x bindings in `ctl-x-map'
         ("C-x M-." . consult-complex-command)     ;; orig. repeat-complex-command
         ("C-x b"   . consult-buffer)              ;; orig. switch-to-buffer
         ("C-x 4 b" . consult-buffer-other-window) ;; orig. switch-to-buffer-other-window
         ("C-x 5 b" . consult-buffer-other-frame)  ;; orig. switch-to-buffer-other-frame
         ("C-x r b" . consult-bookmark)            ;; orig. bookmark-jump
         ("C-c p b" . consult-project-buffer)      ;; orig. project-switch-to-buffer
         ("C-c p p" . project-switch-project)      ;; orig. project-switch-to-buffer
         ;; Consult search
         ("C-c p f" . consult-find)                  ;; Búsqueda de archivos en todo el projecto incluso en carpetas
         ("C-c p l" . consult-line-multi)            ;; Búsqueda en todos los buffers del projecto abiertos
         ;; Custom M-# bindings for fast register access
         ("C-'"   . consult-register-load)
         ("M-'"   . consult-register-store)        ;; orig. abbrev-prefix-mark (unrelated)
         ("M-¡"   . consult-register)
         ;; Other custom bindings
         ("M-y" . consult-yank-pop)                ;; orig. yank-pop
         ;; M-g bindings in `goto-map'
         ("M-g f" . consult-flymake)               ;; Alternative: consult-flycheck
         ("M-g g" . consult-goto-line)             ;; orig. goto-line
         ("M-g M-g" . consult-goto-line)           ;; orig. goto-line
         ("M-g o" . consult-outline)               ;; Alternative: consult-org-heading
         ("M-g m" . consult-mark)
         ("M-g k" . consult-global-mark)
         ("M-g i" . consult-imenu)
         ("M-g I" . consult-imenu-multi)

         ;; Minibuffer history
         :map minibuffer-local-map
         ("<escape>" . minibuffer-keyboard-quit) ;; Cacelar minibuffer con escape (más rápido que C-g)
         ("C-<return>" . newline) ;; Insertar nueva línea estando en el minibufer (Mas rápido que C-q C-j)
         ("C-s" . (lambda ()
                    "Insert the current symbol."
                    (interactive)
                    (insert (save-excursion
		                      (set-buffer (window-buffer (minibuffer-selected-window)))
		                      (or (thing-at-point 'symbol t) ""))))) ;; Al presionar por segunda ves C-s busca el símbolo actual.
         ("M-s" . consult-history)                 ;; orig. historial de búsqueda
         ("M-r" . consult-history))                ;; orig. historual de búsqueda

  ;; Enable automatic preview at point in the *Completions* buffer. This is
  ;; relevant when you use the default completion UI.
  :hook (completion-list-mode . consult-preview-at-point-mode)

  ;; The :init configuration is always executed (Not lazy)
  :init
  ;; Optionally configure the register formatting. This improves the register
  ;; preview for `consult-register', `consult-register-load',
  ;; `consult-register-store' and the Emacs built-ins.
  (setq register-preview-delay 0.5
        register-preview-function #'consult-register-format)

  ;; Optionally tweak the register preview window.
  ;; This adds thin lines, sorting and hides the mode line of the window.
  (advice-add #'register-preview :override #'consult-register-window)

  ;; Use Consult to select xref locations with preview
  (with-eval-after-load 'xref
    (setq xref-show-xrefs-function #'consult-xref
          xref-show-definitions-function #'consult-xref))

  :config
  ;; Optionally configure preview. The default value
  ;; is 'any, such that any key triggers the preview.
  ;; (setq consult-preview-key 'any)
  (setq consult-preview-key "M-.")
  ;; (setq consult-preview-key '("S-<down>" "S-<up>"))
  ;; For some commands and buffer sources it is useful to configure the
  ;; :preview-key on a per-command basis using the `consult-customize' macro.
  (consult-customize
   consult-goto-line
   consult-theme :preview-key '(:debounce 0.4 any))

  ;; Optionally configure the narrowing key.
  ;; Both < and C-+ work reasonably well.
  (setq consult-narrow-key "<") ;; "C-+"

  ;; Optionally make narrowing help available in the minibuffer.
  ;; You may want to use `embark-prefix-help-command' or which-key instead.
  (define-key consult-narrow-map (vconcat consult-narrow-key "?") #'consult-narrow-help))

;; nerd-icons en ibuffer
(use-package nerd-icons-ibuffer
  :defer t
  :hook (ibuffer-mode . nerd-icons-ibuffer-mode))

;; Integración entre consult y project
(use-package consult-project-extra
  :defer t
  :ensure t
  :bind
  (("C-x p" . consult-project-extra-find)
   ("C-c p o" . consult-project-extra-find-other-window)))

;; Descripciones en el minibufer
(use-package marginalia
  :defer t
  :hook (elpaca-after-init . marginalia-mode))

;; Nerd icons para vertico
(use-package nerd-icons-completion
  :after nerd-icons
  :config (nerd-icons-completion-mode))

;; Todolist de todo el projecto desde consult.
(use-package consult-todo
  :demand t
  :bind ("C-c t" . consult-todo)
  :defer t)

(provide 'init-minibuffer)
;;; init-minibuffer.el ends here