;;------------ Languages -------------
;; 2 issues with project.el : straight.el use the local version 
;; - Symbol’s value as variable is void: project-switch-commands (magit)
;; - Symbol’s value as variable is void: project-root (eglot)
;; So we have to install it by hand with `package-install` -> `project`
(use-package eglot
  :config
  (add-hook 'haskell-mode-hook 'eglot-ensure))

;;---------- Haskell 
;; With egplot, we have haskell-server too
(use-package haskell-mode
  :bind ("C-c l" . haskell-process-load-file))

;; Project management with project.el (builtin)


;; ------------------- Completion ---------------
;; Selectrum replaces ivy (uses Emacs API)
;; Minimalist alternative to selectrum, fully compatible with Emacs api
;; We could use icomplete (vertical mode built-in in emacs 28) but it's less fluid
(use-package vertico
  :init
  (vertico-mode)
  )

;; Use orderless to improve filtering (faster that prescient it seems)
(use-package orderless
  :init
  (setq completion-styles '(orderless))
  ;; Persist history over Emacs restarts
  (savehist-mode)
  )

;; ---  Show more details in the minibuffer
(use-package marginalia
  :init
  ;; Enabled right away. Note that this forces loading the package.
  (marginalia-mode))

;; Counsel alternative (Super uselful). Default mapping suggested from the authors
(use-package consult
   :bind (;; C-c bindings (mode-specific-map)
         ("C-c h" . consult-history)
         ("C-c b" . consult-bookmark)
         ("C-c k" . consult-kmacro)
	 ("C-c r" . consult-recent-file)
         ;; C-x bindings (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
         ;; Custom M-# bindings for fast register access
         ("M-#" . consult-register-load)
         ("M-'" . consult-register-store) ;; orig. abbrev-prefix-mark (unrelated)
         ("C-M-#" . consult-register)
         ;; Other custom bindings
         ("M-y" . consult-yank-pop)	;; orig. yank-pop
         ("<help> a" . consult-apropos)	;; orig. apropos-command
         ;; M-g bindings (goto-map)
         ("M-g e" . consult-compile-error)
         ("M-g f" . consult-flymake) ;; Alternative: consult-flycheck
         ("M-g g" . consult-goto-line) ;; orig. goto-line
         ("M-g o" . consult-outline)
         ("M-g m" . consult-mark)
         ("M-g k" . consult-global-mark)
         ("M-g i" . consult-imenu)
         ("M-g I" . consult-project-imenu)
         ;; M-s bindings (search-map)
         ("M-s f" . consult-find)
         ("M-s g" . consult-grep)
         ("M-s G" . consult-git-grep)
         ("M-s r" . consult-ripgrep)
         ("M-s l" . consult-line)
         ("M-s m" . consult-multi-occur)
         ("M-s k" . consult-keep-lines)
         ("M-s u" . consult-focus-lines)
         ;; Isearch integration
         ("M-s e" . consult-isearch)
         :map isearch-mode-map
         ("M-e" . consult-isearch)   ;; orig. isearch-edit-string
         ("M-s e" . consult-isearch) ;; orig. isearch-edit-string
         ("M-s l" . consult-line)) ;; required by consult-line to detect isearch

  ;; Enable automatic preview at point in the *Completions* buffer.
  :hook (completion-list-mode consult-preview-at-point-mode)

  :config
  ;; Ripgrep-all (cannot jump to pdf page though)
  (defcustom consult-ripgrep-all-command
    "rga --null --line-buffered --color=ansi --max-columns=1000\
   --no-heading --line-number . -e ARG OPTS"
    "Command line string for ripgrep-all, see `consult-ripgrep-all'.
The command string must have a specific format, including ARG and OPTS
substrings. ARG is replaced by the filter string and OPTS by the auxillary
command options."
    :type 'string)
  (defun consult-ripgrep-all (&optional dir initial)
    (interactive "P")
    (consult--grep "Ripgrep-all" consult-ripgrep-all-command dir initial))
  )

;; Lighter alternative to company, without backend to programming languages
;; With eglot, we should be fine
(use-package corfu
  :init
  (corfu-global-mode) ; Enable globally (recommended)
  ;; Optionally use TAB for cycling, default is `corfu-complete'.
  :bind (:map corfu-map
	      ("TAB" . corfu-next)
	      ("S-TAB" . corfu-previous))
  )

(use-package emacs
  :init
  ;; TAB cycle if there are only few candidates
  (setq completion-cycle-threshold 3)

  ;; Enable indentation+completion using the TAB
  ;; Won't work properly with TAB without this !
  (setq tab-always-indent 'complete))

;; ------------- Others --------------
(use-package magit)
;; Do not use lispy, but built-in sexp
;; Do not use yasnippet but skeleton (builti)n
(use-package wgrep) ; Editable grep results <3
(show-paren-mode 1) ; Show matching parenthesis

;; Yank from mouse selection. By default, only available with mouse-2....
(defun my/yank-primary ()
  "Yank from primary selection."
  (interactive)
  (push-mark)
  (insert-for-yank (gui-get-primary-selection)))

(use-package fish-mode) ; Fish shell