.emacs.d

the most bloated Emacs config on this planet
git clone https://git.trogloxene.org/.emacs.d.git
Log | Files | Refs

init-completion.el (8974B)


      1 ;;; init-completion.el --- completion config -*- lexical-binding: t -*-
      2 
      3 ;;; Commentary:
      4 
      5 ;;; Code:
      6 
      7 (use-package emacs
      8   :custom
      9   (tab-always-indent 'complete)
     10   (text-mode-ispell-word-completion nil)
     11   (read-extended-command-predicate #'command-completion-default-include-p))
     12 
     13 ;; minibuffer completion
     14 (use-package vertico
     15   :hook
     16   (after-init . vertico-mode))
     17 
     18 ;; orderless completion
     19 (use-package orderless
     20   :custom
     21   (completion-styles '(orderless basic)))
     22 
     23 ;; marginalia in minibuffer
     24 (use-package marginalia
     25   :hook
     26   (after-init . marginalia-mode))
     27 
     28 ;; search and navigation
     29 (use-package consult
     30   :config
     31   (defun my-consult-git-grep-project ()
     32     "Run `consult-git-grep' in current project's root."
     33     (interactive)
     34     (when-let* ((proj (project-current t)))
     35       (consult-git-grep (project-root proj))))
     36 
     37   (defun consult-colors-emacs (color)
     38     "Show a list of all supported colors for a particular frame.
     39 
     40 You can insert the name (default), or insert or kill the hexadecimal or RGB
     41 value of the selected COLOR."
     42     (interactive
     43      (list (consult--read (list-colors-duplicates (defined-colors))
     44                           :prompt "Emacs color: "
     45                           :require-match t
     46                           :category 'color
     47                           :history '(:input consult-colors-history))))
     48     (insert color))
     49 
     50   (defun consult-directory-externally (file)
     51     "Open FILE externally using the default application of the system."
     52     (interactive "fOpen externally: ")
     53     (if (and (eq system-type 'windows-nt)
     54              (fboundp 'w32-shell-execute))
     55         (shell-command-to-string
     56          (encode-coding-string
     57           (replace-regexp-in-string "/" "\\\\"
     58                                     (format "explorer.exe %s"
     59                                             (file-name-directory
     60                                              (expand-file-name file))))
     61           'gbk))
     62       (call-process (pcase system-type
     63                       ('darwin "open")
     64                       ('cygwin "cygstart")
     65                       (_ "xdg-open"))
     66                     nil 0 nil
     67                     (file-name-directory (expand-file-name file)))))
     68 
     69   (defun my-open-current-directory ()
     70     "Open current directory in default file explorer."
     71     (interactive)
     72     (consult-directory-externally default-directory))
     73 
     74   (defun consult-fd-home ()
     75     "Search for all files in home folder use fd."
     76     (interactive)
     77     (let ((consult-fd-args "fd -H -I -i -c never --full-path")
     78           (default-directory "~/"))
     79       (consult-fd)))
     80 
     81   ;; set locate arguments
     82   (cond
     83    (*is-windows*
     84     (setq consult-locate-args (encode-coding-string "es.exe -i -p -r" 'gbk)
     85           consult-ripgrep-args (encode-coding-string
     86                                 "rg --null --line-buffered --color=never --max-columns=1000 --path-separator / --smart-case --no-heading --line-number"
     87                                 'gbk))
     88     (add-to-list 'process-coding-system-alist '("es" gbk . gbk)))
     89    (*is-mac*
     90     (setq consult-locate-args "mdfind -name"))
     91    (*is-linux*
     92     (setq consult-locate-args "plocate --basename --ignore-case")))
     93 
     94   ;; help
     95   (setq consult-narrow-key "<")
     96   (define-key consult-narrow-map (vconcat consult-narrow-key "?") #'consult-narrow-help)
     97 
     98   ;; xref
     99   (setq xref-show-definitions-function #'consult-xref)
    100   (setq xref-show-xrefs-function #'consult-xref)
    101 
    102   ;; set some buffer filter
    103   (setq consult-buffer-filter
    104         (cl-union consult-buffer-filter
    105                   '("^\\*dirvish"
    106                     "^PREVIEW"
    107                     "^\\*helpful"
    108                     "^\\*Help"
    109                     "^\\*Diff"
    110                     "^\\*straight"
    111                     "\\*Messages\\*"
    112                     "\\*Chinese-word-segmentation\\*"
    113                     "\\*gptel-reasoning\\*"
    114                     "\\*gptel-log\\*"
    115                     "\\*pi-coding-agent-input:"
    116                     "\\*pi-coding-agent-sessions:"
    117                     "\\*pi-coding-agent-tree:"
    118                     "\\*Synonyms List\\*"
    119                     "\\*Wordnet\\*")
    120                   :test 'equal)))
    121 
    122 ;; right-click
    123 (use-package embark
    124   :commands
    125   embark-prefix-help-command
    126   :init
    127   (setq prefix-help-command 'embark-prefix-help-command)
    128   :config
    129 
    130   ;; from centaur emacs
    131   (defun my-embark-preview ()
    132     "Previews candidate in vertico buffer, unless it's a consult command."
    133     (interactive)
    134     (unless (bound-and-true-p consult--preview-function)
    135       (save-selected-window
    136         (let ((embark-quit-after-action nil))
    137           (embark-dwim)))))
    138 
    139   (add-to-list 'display-buffer-alist
    140                '("\\`\\*Embark Collect \\(Live\\|Completions\\)\\*"
    141                  nil
    142                  (window-parameters (mode-line-format . none))))
    143 
    144   (with-no-warnings
    145     (with-eval-after-load 'which-key
    146       (defun embark-which-key-indicator ()
    147         "An embark indicator that displays keymaps using which-key.
    148 The which-key help message will show the type and value of the
    149 current target followed by an ellipsis if there are further
    150 targets."
    151         (lambda (&optional keymap targets prefix)
    152           (if (null keymap)
    153               (which-key--hide-popup-ignore-command)
    154             (which-key--show-keymap
    155              (if (eq (plist-get (car targets) :type) 'embark-become)
    156                  "Become"
    157                (format "Act on %s '%s'%s"
    158                        (plist-get (car targets) :type)
    159                        (embark--truncate-target (plist-get (car targets) :target))
    160                        (if (cdr targets) "…" "")))
    161              (if prefix
    162                  (pcase (lookup-key keymap prefix 'accept-default)
    163                    ((and (pred keymapp) km) km)
    164                    (_ (key-binding prefix 'accept-default)))
    165                keymap)
    166              nil nil t (lambda (binding)
    167                          (not (string-suffix-p "-argument" (cdr binding))))))))
    168 
    169       (setq embark-indicators
    170             '(embark-which-key-indicator
    171               embark-highlight-indicator
    172               embark-isearch-highlight-indicator))
    173 
    174       (defun embark-hide-which-key-indicator (fn &rest args)
    175         "Hide the which-key indicator immediately when using the completing-read prompter."
    176         (which-key--hide-popup-ignore-command)
    177         (let ((embark-indicators
    178                (remq #'embark-which-key-indicator embark-indicators)))
    179           (apply fn args)))
    180 
    181       (advice-add #'embark-completing-read-prompter
    182                   :around #'embark-hide-which-key-indicator))))
    183 
    184 ;; embark-consult
    185 (use-package embark-consult)
    186 
    187 ;; eglot
    188 (use-package eglot
    189   :config
    190   (setq eglot-send-changes-idle-time 0.25))
    191 
    192 ;; corfu
    193 (use-package corfu
    194   :autoload
    195   (corfu-quit consult-completion-in-region)
    196   :bind
    197   (:map corfu-map
    198         ("M-d" . corfu-popupinfo-documentation)
    199         ("RET" . nil))
    200   :custom
    201   (corfu-auto t)
    202   (corfu-auto-prefix 2)
    203   (corfu-count 12)
    204   (corfu-preview-current nil)
    205   (corfu-on-exact-match nil)
    206   (corfu-auto-delay 0.2)
    207   (corfu-popupinfo-delay '(0.4 . 0.2))
    208   (global-corfu-modes '((not erc-mode
    209                              circe-mode
    210                              help-mode
    211                              gud-mode
    212                              vterm-mode)
    213                         t))
    214   :hook
    215   (after-init . global-corfu-mode)
    216   (after-init . corfu-popupinfo-mode)
    217   (after-init . corfu-history-mode)
    218   :config
    219   (add-hook 'before-save-hook #'corfu-quit)
    220   (add-hook 'eshell-mode-hook
    221             (lambda ()
    222               (setq-local corfu-auto nil)
    223               (local-set-key (kbd "TAB") #'completion-at-point)))
    224   (add-hook 'org-mode-hook
    225             (lambda () (setq-local corfu-auto nil))))
    226 
    227 ;; cape
    228 (use-package cape
    229   :commands (cape-file cape-elisp-block cape-keyword)
    230   :autoload (cape-wrap-nonexclusive cape-wrap-buster)
    231   :autoload (cape-wrap-silent)
    232   :init
    233   (add-to-list 'completion-at-point-functions #'cape-dabbrev)
    234   (add-to-list 'completion-at-point-functions #'cape-file)
    235   (add-to-list 'completion-at-point-functions #'cape-elisp-block)
    236   (add-to-list 'completion-at-point-functions #'cape-keyword)
    237   (add-to-list 'completion-at-point-functions #'cape-abbrev)
    238 
    239   (advice-add 'comint-completion-at-point :around #'cape-wrap-nonexclusive)
    240   (advice-add 'eglot-completion-at-point :around #'cape-wrap-buster)
    241   (advice-add 'eglot-completion-at-point :around #'cape-wrap-nonexclusive)
    242   (advice-add 'pcomplete-completions-at-point :around #'cape-wrap-nonexclusive)
    243 
    244   (defun my-org-completion-setup ()
    245     (add-hook 'completion-at-point-functions #'cape-file nil t)
    246     (add-hook 'completion-at-point-functions #'cape-elisp-symbol nil t))
    247   (add-hook 'org-mode-hook #'my-org-completion-setup)
    248 
    249   (defun my-minibuffer-completion-setup ()
    250     (setq-local completion-at-point-functions nil))
    251   (add-hook 'minibuffer-mode-hook 'my-minibuffer-completion-setup)
    252 
    253   :config
    254   (setf (alist-get 'org-mode cape-elisp-symbol-wrapper) '(?= ?=)))
    255 
    256 (provide 'init-completion)
    257 ;;; init-completion.el ends here