.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 (9060B)


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