nftable-migration
  1;;; config-completion.el --- -*- lexical-binding: t -*-
  2;;; Commentary:
  3;;; Setup completion framework
  4;;; Code
  5
  6(use-package which-key
  7  :custom
  8  (which-key-separator "" )
  9  :hook
 10  (after-init . which-key-mode)
 11  :config
 12  
 13  ;; Define custom, concise descriptions for `tab-bar` commands under "C-x t"
 14  (which-key-add-key-based-replacements
 15    "C-x t C-f" "Open file in new tab"
 16    "C-x t RET" "Switch tabs"
 17    "C-x t C-r" "Open file (read-only) in new tab"
 18    "C-x t 0"   "Close current tab"
 19    "C-x t 1"   "Close other tabs"
 20    "C-x t 2"   "New empty tab"
 21    "C-x t G"   "Group tabs"
 22    "C-x t M"   "Move tab to position"
 23    "C-x t N"   "New tab and switch to it"
 24    "C-x t O"   "Previous tab"
 25    "C-x t b"   "Switch buffer in new tab"
 26    "C-x t d"   "Dired in new tab"
 27    "C-x t f"   "Open file in new tab"
 28    "C-x t m"   "Move tab left/right"
 29    "C-x t n"   "Duplicate tab"
 30    "C-x t o"   "Next tab"
 31    "C-x t p"   "Project in new tab"
 32    "C-x t r"   "Rename tab"
 33    "C-x t t"   "Switch to other tab"
 34    "C-x t u"   "Undo tab close"
 35    "C-x t ^ f" "Detach tab window"))
 36
 37(use-package consult
 38  :bind
 39  ("M-g M-g" . consult-goto-line)
 40  ("M-K" . consult-keep-lines)
 41  ("M-s M-b" . consult-buffer)
 42  ("M-s M-f" . consult-find)
 43  ("M-s M-g" . consult-grep)
 44  ("M-s M-r" . consult-ripgrep)
 45  ("M-s M-h" . consult-history)
 46  ("M-s M-l" . consult-line)
 47  ("M-s M-m" . consult-mark)
 48  ("M-s M-y" . consult-yank-pop)
 49  ("M-s M-s" . consult-outline)
 50  :config
 51  ;; (general-leader
 52  ;;   "y"  #'(consult-yank-pop :which-key "Clipboard history")
 53  ;;   "b"  '(:ignore t :which-key "buffer")
 54  ;;   "bb" #'(consult-buffer :which-key "switch buffer")
 55  ;;   "bd" #'(kill-current-buffer :which-key "kill buffer")
 56  ;;   "bD" #'((lambda ()(interactive)(kill-current-buffer)(tab-close)) :wk "Kill buffer and tab")
 57  ;;   "bn" #'(next-buffer :which-key "next buffer")
 58  ;;   "bp" #'(previous-buffer :which-key "previous buffer")
 59  ;;   "s"  '(:ignore t :which-key "search")
 60  ;;   "sg" #'(consult-grep :which-key "Consult grep in current directory")
 61  ;;   "sR" #'(consult-ripgrep :which-key "Consult ripgrep in current directory")
 62  ;;   "sr" '(:ignore t :which-key "rg.el")'
 63  ;;   "srp" #'(rg-project :which-key "rg.el in current project")
 64  ;;   "srs" #'(rg-dwim :which-key "rg.el Do What I Mean")
 65  ;;   "sh"  #'(Info-goto-emacs-command-node :wk "Search help")
 66  ;;   "sc"  #'(consult-mode-command :wk "Mode Command")
 67  ;;   "s/"  #'(consult-isearch-history :wk "Consult Isearch history")
 68  ;;   "g"  '(:ignore t :which-key "go")
 69  ;;   "gu" #'(ffap-next-url :which-key "next url")
 70  ;;   "gd" #'(xref-find-definitions :which-key "find definition")
 71  ;;   "gD" #'(xref-find-definitions-other-window :which-key "find definition (other window)"))
 72  )
 73
 74(use-package consult-imenu
 75  :after (consult)
 76  :commands (consult-imenu)
 77  :bind
 78  ("M-s M-i" . consult-imenu))
 79
 80(use-package consult-xref
 81  :after (consult)
 82  :commands (consult-xref)
 83  :config
 84  (setq xref-show-xrefs-function #'consult-xref
 85        xref-show-definitions-function #'consult-xref)
 86  (defvar consult--xref-history nil
 87    "History for the `consult-recent-xref' results.")
 88
 89  (defun consult-recent-xref (&optional markers)
 90    "Jump to a marker in MARKERS list (defaults to `xref--history'.
 91
 92The command supports preview of the currently selected marker position.
 93The symbol at point is added to the future history."
 94    (interactive)
 95    (consult--read
 96     (consult--global-mark-candidates
 97      (or markers (flatten-list xref--history)))
 98     :prompt "Go to Xref: "
 99     :annotate (consult--line-prefix)
100     :category 'consult-location
101     :sort nil
102     :require-match t
103     :lookup #'consult--lookup-location
104     :history '(:input consult--xref-history)
105     :add-history (thing-at-point 'symbol)
106     :state (consult--jump-state))))
107
108;; https://github.com/oantolin/embark/blob/master/embark-consult.el
109(use-package embark
110  :unless noninteractive
111  :commands (emark-act embark-dwim embark-prefix-help-command)
112  :bind
113  ("C-." . embark-act)
114  ("M-." . embark-dwim)
115  ("C-h b" . embark-bindings)
116  ("C-h B" . embark-bindings-at-point)
117  ("C-h M" . embark-bindings-in-keymap)
118  ("C-h E" . embark-on-last-message)
119  (:map completion-list-mode-map
120        ("." . embark-act))
121  (:map embark-collect-mode-map
122        ("a") ; I don't like my own default :)
123        ("." . embark-act)
124        ("F" . consult-focus-lines))
125  (:map embark-package-map
126        ("t" . try))
127  (:map embark-identifier-map
128        ("(" . insert-parentheses)
129        ("[" . insert-pair-map))
130  (:map embark-expression-map
131        ("(" . insert-parentheses)
132        ("[" . insert-pair-map))
133  (:map embark-region-map
134        ("(" . insert-parentheses)
135        ("[" . insert-pair-map)
136        ("D" . dictionary-search))
137  (:map embark-email-map
138        ("+" . add-email-to-ecomplete)
139        ("\\" . remove-email-from-ecomplete))
140  (:map embark-encode-map
141        ("p" . topaz-paste-region))
142  (:map embark-url-map
143        ("x" . browse-url-generic)
144        ("p" . pocket-lib-add-urls))
145  (:map embark-identifier-map
146        ("D" . dictionary-lookup-definition))
147  :custom
148  (embark-quit-after-action t)
149  (prefix-help-command #'embark-prefix-help-command)
150  (embark-indicators '(embark-minimal-indicator
151                       embark-highlight-indicator
152                       embark-isearch-highlight-indicator))
153  (embark-cycle-key ".")
154  (embark-help-key "?")
155  (embark-confirm-act-all nil)
156  :config
157  (setq embark-candidate-collectors
158        (cl-substitute 'embark-sorted-minibuffer-candidates
159                       'embark-minibuffer-candidates
160                       embark-candidate-collectors))
161  (dolist (cmd '(comment-dwim
162                 insert-parentheses
163                 insert-pair
164                 markdown-insert-code
165                 markdown-insert-italic
166                 markdown-insert-bold
167                 org-emphasize
168                 cdlatex-math-modify
169                 TeX-font))
170    (push #'embark--mark-target (alist-get cmd embark-around-action-hooks)))
171  (push #'embark--xref-push-marker
172        (alist-get 'find-file embark-pre-action-hooks))
173  (defun embark-on-last-message (arg)
174    "Act on the last message displayed in the echo area."
175    (interactive "P")
176    (with-current-buffer "*Messages*"
177      (goto-char (1- (point-max)))
178      (embark-act arg)))
179
180  (defmacro ct/embark-display-in-side-window (side)
181    `(defun ,(intern (concat "display-in-side-window--" (symbol-name side))) (&optional buffer)
182       (interactive "b")
183       (when-let* ((buffer (or buffer (current-buffer)))
184                   (display-buffer-overriding-action '((display-buffer-in-side-window)
185                                                       (dedicated . t)
186                                                       (side . ,side)
187                                                       (window-parameters . ((no-delete-other-windows . t))))))
188	 (display-buffer buffer))))
189  (define-key embark-buffer-map (kbd "s b") (ct/embark-display-in-side-window bottom))
190  (define-key embark-buffer-map (kbd "s l") (ct/embark-display-in-side-window left))
191  (define-key embark-buffer-map (kbd "s r") (ct/embark-display-in-side-window right))
192  
193  (defun embark-which-key-indicator ()
194    "An embark indicator that displays keymaps using which-key.
195The which-key help message will show the type and value of the
196current target followed by an ellipsis if there are further
197targets."
198    (lambda (&optional keymap targets prefix)
199      (if (null keymap)
200          (which-key--hide-popup-ignore-command)
201	(which-key--show-keymap
202	 (if (eq (plist-get (car targets) :type) 'embark-become)
203             "Become"
204           (format "Act on %s '%s'%s"
205                   (plist-get (car targets) :type)
206                   (embark--truncate-target (plist-get (car targets) :target))
207                   (if (cdr targets) "" "")))
208	 (if prefix
209             (pcase (lookup-key keymap prefix 'accept-default)
210               ((and (pred keymapp) km) km)
211               (_ (key-binding prefix 'accept-default)))
212           keymap)
213	 nil nil t (lambda (binding)
214                     (not (string-suffix-p "-argument" (cdr binding))))))))
215
216  (setq embark-indicators
217	'(embark-which-key-indicator
218	  embark-highlight-indicator
219	  embark-isearch-highlight-indicator))
220
221  (defun embark-hide-which-key-indicator (fn &rest args)
222    "Hide the which-key indicator immediately when using the completing-read prompter."
223    (which-key--hide-popup-ignore-command)
224    (let ((embark-indicators
225           (remq #'embark-which-key-indicator embark-indicators)))
226      (apply fn args)))
227
228  (advice-add #'embark-completing-read-prompter
229              :around #'embark-hide-which-key-indicator))
230
231(use-package embark-consult
232  :after (embark consult)
233  :unless noninteractive
234  :hook
235  (embark-collect-mode . consult-preview-at-point-mode))
236
237(setq minibuffer-prompt-properties
238      '(read-only t cursor-intangible t face minibuffer-prompt))
239(add-hook 'minibuffer-setup-hook #'cursor-intangible-mode)
240
241(use-package emacs
242  :unless noninteractive
243  :custom
244  (completion-cycle-threshold 2)
245  (completion-ignore-case t)
246  (completion-show-inline-help nil)
247  (completions-detailed t)
248  (enable-recursive-minibuffers t)
249  (read-buffer-completion-ignore-case t)
250  (read-file-name-completion-ignore-case t)
251  (resize-mini-windows t)
252  (tab-always-indent 'complete)
253  :config
254  (minibuffer-depth-indicate-mode 1)
255  (minibuffer-electric-default-mode 1))
256
257(use-package vertico
258  :unless noninteractive
259  :hook (after-init . vertico-mode))
260
261(use-package marginalia
262  :unless noninteractive
263  :hook (after-init . marginalia-mode))
264
265(use-package corfu
266  :unless noninteractive
267  :bind (("C-<tab>" . corfu-candidate-overlay-complete-at-point))
268  :hook (after-init . global-corfu-mode)
269  :init
270  (require 'corfu-popupinfo)
271  (require 'corfu-history)
272  (require 'corfu-candidate-overlay)
273  :config
274  (setq corfu-popupinfo-delay '(1.25 . 0.5))
275  (corfu-popupinfo-mode 1)
276  (corfu-candidate-overlay-mode)
277  ;; Sort by input history (no need to modify `corfu-sort-function').
278  (with-eval-after-load 'savehist
279    (corfu-history-mode 1)
280    (add-to-list 'savehist-additional-variables 'corfu-history))
281
282  ;; Adapted from Corfu's manual.
283  (defun contrib/corfu-enable-always-in-minibuffer ()
284    "Enable Corfu in the minibuffer if MCT or Vertico is not active.
285Useful for prompts such as `eval-expression' and `shell-command'."
286    (unless (or (bound-and-true-p vertico--input)
287                (bound-and-true-p mct--active))
288      (corfu-mode 1)))
289
290  (add-hook 'minibuffer-setup-hook #'contrib/corfu-enable-always-in-minibuffer 1))
291
292;; Seems like it is a bit impacting the performance somehow.
293;; (use-package corfu-candidate-overlay
294;;   :after corfu
295;;   :bind (("C-<tab>" . corfu-candidate-overlay-complete-at-point))
296;;   :config
297;;   (corfu-candidate-overlay-mode +1))
298
299(use-package cape
300  :bind (("C-c p f" . cape-file)
301         ("C-c p /" . cape-dabbrev)
302         :map corfu-map
303         ("M-/" . cape-dabbrev)
304         ("C-x C-f" . cape-file))
305  :config
306  (add-to-list 'completion-at-point-functions #'cape-file))
307
308(use-package orderless
309  :unless noninteractive
310  :config
311  (setq completion-styles
312	'(orderless basic substring initials flex partial-completion))
313  (setq completion-category-defaults nil)
314  (setq completion-category-overrides nil)
315  )
316
317(use-package tempel
318  :bind (("M-+" . tempel-complete) ;; Alternative tempel-expand
319         ("M-*" . tempel-insert))
320  :init
321  ;; (defun tempel-setup-capf ()
322  ;;   ;; Add the Tempel Capf to `completion-at-point-functions'.
323  ;;   ;; `tempel-expand' only triggers on exact matches. Alternatively use
324  ;;   ;; `tempel-complete' if you want to see all matches, but then you
325  ;;   ;; should also configure `tempel-trigger-prefix', such that Tempel
326  ;;   ;; does not trigger too often when you don't expect it. NOTE: We add
327  ;;   ;; `tempel-expand' *before* the main programming mode Capf, such
328  ;;   ;; that it will be tried first.
329  ;;   (setq-local completion-at-point-functions
330  ;;               (cons #'tempel-expand
331  ;;                     completion-at-point-functions)))
332  (setq tempel-path (expand-file-name "templates" user-emacs-directory))
333  ;; (add-hook 'conf-mode-hook 'tempel-setup-capf)
334  ;; (add-hook 'prog-mode-hook 'tempel-setup-capf)
335  ;; (add-hook 'text-mode-hook 'tempel-setup-capf)
336  )
337
338(use-package tempel-collection
339  :after tempel)
340
341(provide 'config-completion)
342;;; config-completion.el ends here