aboutsummaryrefslogtreecommitdiff
path: root/emacs/.emacs.d/init/ycp-complete.el
blob: bd3b3caa5d5259e8216ab19df42bad2146cd5433 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
;;; ycp-complete.el -- My config for minibuffer and completions -*- lexical-binding: t -*-

;; Copyright (C) 2023 Free Software Foundation.

;; Author: Yuchen Pei <id@ypei.org>
;; Package-Requires: ((emacs "28.2"))

;; This file is part of dotfiles.

;; dotfiles is free software: you can redistribute it and/or modify it under
;; the terms of the GNU Affero General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; dotfiles is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
;; Public License for more details.

;; You should have received a copy of the GNU Affero General Public
;; License along with dotfiles.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; My config for minibuffer and completions.

;;; Code:



;; completion, minibuffer, corfu, tempel
;; part adapted from prot-dotfiles
(my-package minibuffer
  (setq completion-styles '(basic partial-completion emacs22 orderless))
  (setq completion-category-overrides
        ;; NOTE 2021-10-25: I am adding `basic' because it works better as a
        ;; default for some contexts.  Read:
        ;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=50387>.
        ;;
        ;; `partial-completion' is a killer app for files, because it
        ;; can expand ~/.l/s/fo to ~/.local/share/fonts.
        ;;
        ;; If `basic' cannot match my current input, Emacs tries the
        ;; next completion style in the given order.  In other words,
        ;; `orderless' kicks in as soon as I input a space or one of its
        ;; style dispatcher characters.
        '((file (styles . (basic partial-completion orderless)))
          (project-file (styles . (basic substring partial-completion orderless)))
          (imenu (styles . (emacs22 substring orderless)))
          (kill-ring (styles . (emacs22 substring orderless)))
          (consult-location (styles . (emacs22 substring orderless)))
          (eglot (styles . (emacs22 substring orderless)))))
  (setq completion-ignore-case t)
  (setq read-buffer-completion-ignore-case t)
  (setq read-file-name-completion-ignore-case t)
  ;; disable space to run minibuffer-complete-word
  (my-keybind minibuffer-mode-map
    "SPC" nil
    "?" nil)
  (my-keybind minibuffer-local-completion-map
    "SPC" nil
    "?" nil)
  (setq enable-recursive-minibuffers t)
  (minibuffer-depth-indicate-mode 1)
  (setq resize-mini-windows t)
  (setq read-answer-short t)
  (setq echo-keystrokes 0.25)
  ;; Do not allow the cursor to move inside the minibuffer prompt.  I
  ;; got this from the documentation of Daniel Mendler's Vertico
  ;; package: <https://github.com/minad/vertico>.
  (setq minibuffer-prompt-properties
        '(read-only t cursor-intangible t face minibuffer-prompt))
  (add-hook 'minibuffer-setup-hook #'cursor-intangible-mode)
  (minibuffer-electric-default-mode 1))

;;;; `savehist' (minibuffer and related histories)
(my-package savehist
  (setq savehist-file (locate-user-emacs-file "savehist"))
  (setq history-length 500)
  (setq history-delete-duplicates t)
  (setq savehist-save-minibuffer-history t)
  (setq savehist-additional-variables '(register-alist kill-ring))
  (savehist-mode 1))

;;;; `dabbrev' (dynamic word completion (dynamic abbreviations))
(my-package dabbrev
  (setq dabbrev-abbrev-char-regexp "\\sw\\|\\s_")
  (setq dabbrev-abbrev-skip-leading-regexp "[$*/=~']")
  (setq dabbrev-backward-only nil)
  (setq dabbrev-case-distinction 'case-replace)
  (setq dabbrev-case-fold-search nil)
  (setq dabbrev-case-replace 'case-replace)
  (setq dabbrev-check-other-buffers t)
  (setq dabbrev-eliminate-newlines t)
  (setq dabbrev-upcase-means-case-search t))

;;; icomplete
(my-package icomplete
  (icomplete-vertical-mode t)
  (setq icomplete-show-matches-on-no-input t)
  (setq icomplete-prospects-height 4)
  (setq icomplete-scroll t)
  (setq icomplete-matches-format "[%s/%s] ")
  (require 'my-complete)
  (my-keybind icomplete-minibuffer-map
    "<tab>" #'icomplete-force-complete
    "M-<tab>" #'minibuffer-complete
    "C-M-i" #'minibuffer-complete
    "C-s"  #'icomplete-forward-completions
    "C-r"  #'icomplete-backward-completions
    "C-v" #'my-icomplete-vertical-forward-page
    "M-v" #'my-icomplete-vertical-backward-page
    "<ret>" nil))

(my-package recentf
  (setq recentf-max-saved-items 1000)
  (setq
   recentf-exclude
   '("~\\'" "\\`out\\'" "\\.log\\'" "^/[^/]*:" "\\.el\\.gz\\'" "~$" "/mnt/"
     "^/tmp/"))
  (recentf-mode 1)
  ;; disable recentf-save-list on quit on non-emacs-client so that it does not
  ;; overwrite the recentf file
  (require 'my-utils)
  (unless (my-server-p)
	  (setq kill-emacs-hook (delete 'recentf-save-list kill-emacs-hook)))
  (require 'my-complete)
  (my-server-timer recentf-timer nil 300 'my-recentf-save-list-silently)
  )

;;; corfu
(my-package corfu
  (:install t)
  (:delay 5)
  (global-corfu-mode 1)
  (corfu-popupinfo-mode 1)
  (setq corfu-auto t
        corfu-cycle t
        corfu-separator ?\s)
  (define-key corfu-map [remap next-line] nil)
  (define-key corfu-map [remap previous-line] nil)
  (define-key corfu-map [remap beginning-of-buffer] nil)
  (define-key corfu-map [remap end-of-buffer] nil)
  (define-key corfu-map [remap move-beginning-of-line] nil)
  (define-key corfu-map [remap move-end-of-line] nil)
  (my-keybind corfu-map
    "C-j" #'corfu-insert
    "<RET>" nil
    "C-s" #'corfu-next
    "C-r" #'corfu-previous
    "C-a" nil
    )
  (require 'my-corfu)
  (add-hook 'minibuffer-setup-hook
            #'my-corfu-enable-always-in-minibuffer 1)
;;; corfu does not work well in gud as it "flushes" completion
;;; suggestions to the buffer
  (setq corfu-exclude-modes '(gud-mode))
  )

;;; We still need company mode because corfu does not work well in
;;; certain modes yet
(my-package company
  (:install t)
  (:delay 5)
  (add-hook 'message-mode-hook #'company-mode)
  (setq company-idle-delay .1
        company-minimum-prefix-length 3
        company-selection-wrap-around t
        company-show-numbers t
        company-require-match 'never)
  (my-keybind company-active-map
    "\t" #'company-complete-selection
    "<tab>" #'company-complete-selection
    "<RET>" nil
    "<return>" nil
    "C-j" #'company-complete-selection
    "C-s" #'company-select-next
    "C-r" #'company-select-previous
    ))

;;; cape
(my-package cape
  (:install t)
  (:delay 15)
  (setq cape-dabbrev-min-length 3)
  (setq cape-symbol-wrapper
        '((org-mode ?~ ?~)
          (markdown-mode ?` ?`)
          (log-edit-mode ?' ?')
          (message-mode ?' ?')))
  (dolist (backend '(cape-elisp-symbol cape-keyword cape-file cape-history
                                       cape-dabbrev))
    (add-to-list 'completion-at-point-functions backend)))

(my-package imenu
  (:delay 5)
  (my-keybind global-map "M-g i" #'imenu)
  )

;;; consult
(my-package consult
  (:install t)
  (:delay 10)
  (setq consult-line-numbers-widen t)
  ;; (setq completion-in-region-function #'consult-completion-in-region)
  (setq consult-async-min-input 3)
  (setq consult-async-input-debounce 0.5)
  (setq consult-async-input-throttle 0.8)
  (setq consult-narrow-key ">")
  (setq register-preview-delay 0.8
        register-preview-function #'consult-register-format)
  (setq consult-find-args "find . -not ( -path */.git* -prune )")
  (setq consult-preview-key 'any)
  (add-to-list 'consult-mode-histories '(vc-git-log-edit-mode . log-edit-comment-ring))
  (add-hook 'completion-list-mode-hook #'consult-preview-at-point-mode)
  (require 'consult-imenu) ; the `imenu' extension is in its own file
  (require 'my-consult)
  (my-keybind global-map
    "C-x b" #'consult-buffer
    "C-z" #'consult-buffer
    "C-x l" #'consult-locate
    "M-K" #'consult-keep-lines            ; M-S-k is similar to M-S-5 (M-%)
    "M-F" #'consult-focus-lines           ; same principle
    "M-g g" #'consult-goto-line
    "M-s M-b" #'consult-buffer
    "M-s M-f" #'consult-find
    "M-s M-G" #'my-consult-grep
    "M-s M-g" #'my-consult-grep-default
    "M-s M-h" #'consult-history
    "M-s M-i" #'consult-imenu
    "M-s M-l" #'consult-line
    "M-s M-m" #'consult-mark
    "M-y" #'consult-yank-pop
    "M-s M-s" #'consult-outline)
  (my-keybind consult-narrow-map "?" #'consult-narrow-help)
  (my-keybind minibuffer-local-map "C-s" #'consult-history)
  )

;;; marginalia
(my-package marginalia
  (:install t)
  (:delay 10)
  (setq marginalia-max-relative-age 0)
  (my-keybind minibuffer-local-map
    "M-A" #'marginalia-cycle)
  (marginalia-mode 1))

(setq tempel-path
      (locate-user-emacs-file "*tempel-templates"))
(my-package tempel
  (:install t)
  (:delay 15)
  (require 'my-tempel)
  (my-keybind global-map
    "M-=" #'tempel-complete             ; Alternative: `tempel-expand'
    "M-*" #'tempel-insert)
  (my-keybind tempel-map
    "RET" #'tempel-done
    "C-p" #'tempel-previous
    "C-n" #'tempel-next
    "<tab>" #'tempel-next
    "<backtab>" #'tempel-previous
    "C-S-<iso-lefttab>" #'tempel-previous)
  (require 'my-tempel)
  (my-add-hooks #'my-tempel-setup-capf '(prog-mode-hook text-mode-hook))
  (add-hook 'eglot-managed-mode-hook #'my-tempel-setup-capf)
  )

;; consult-recoll
(my-package consult-recoll
  (:delay 30)
  (:install t)
  )

(my-package hmm
  (:delay 60)
  (my-read-local-config)
  (my-setq-from-local hmm-web-search-engines hmm-transformers hmm-handlers)
  (require 'my-net)
  (setq hmm-web-browsers
        '((:name eww :command eww)
          (:name luwak :command luwak-open)
          (:name firefox :command browse-url-firefox)
          (:name firefox-private :command my-browse-url-firefox-private)
          (:name tor-browser :command my-browse-url-tor-browser)
          (:name mullvad-browser :command my-browse-url-mullvad)
          (:name qutebrowser :command my-browse-url-qutebrowser)
          (:name download-and-open :command my-fetch-url)))
  (setq hmm-external-handlers
        '((:name mpv
                 :external-command "mpv %U"
                 :display-name "mpv player"
                 :description "Play url with mpv"
                 :schemes
                 ("ftp" "http" "https" "mms" "rtmp" "rtsp" "sftp" "smb" "srt")
                 :handling :url)
          (:name wget
                 :external-command "wget %U"
                 :display-name "GNU Wget"
                 :description "The non-interactive network downloader"
                 :schemes
                 ("ftp" "http" "https")
                 :handling :url)
          (:name qutebrowser
                 :external-command "qutebrowser %U"
                 :display-name "qutebrowser"
                 :description "A keyboard-driven, vim-like browser based on PyQt5"
                 :schemes
                 ("http" "https")
                 :handling :url)
          (:name torsocks-mpv
                 :external-command "torsocks mpv %U"
                 :display-name "mpv player torsocks"
                 :description "Play url with mpv over torsocks"
                 :schemes
                 ("ftp" "http" "https" "mms" "rtmp" "rtsp" "sftp" "smb" "srt")
                 :handling :url)
          (:name clean-elc-compile
                 :external-command "~/bin/clean-elc-compile %f"
                 :description "Clean compile a directory of elisp files"
                 :display-buffer t
                 :mimetypes ("inode/directory")
                 :handling :file)
          (:name mogrify-strip
                 :external-command "mogrify -strip %F"
                 :description "Strip images of all profiles and comments"
                 :display-buffer t
                 :handling :file)
          (:name pacfind
                 :external-command "pacfind %f"
                 :description "Find the pacman package containing a command"
                 :display-buffer t
                 :handling :query)))
  (setq hmm-matchers
        '(((thing-at-point-url-at-point) . hmm-url)
          ((thing-at-point-file-at-point) . hmm-file)
          ((and (derived-mode-p 'dired-mode) (dired-get-filename nil t))
           . hmm-file)
          ((and (derived-mode-p 'dired-mode) (expand-file-name default-directory))
           . hmm-file)
          ((and (derived-mode-p 'org-mode) (my-org-link-at-point)) . hmm-url)
          ((get-text-property (point) 'shr-url) . hmm-url)
          ((and (derived-mode-p 'luwak-mode)
                (get-text-property (point) 'url))
           . hmm-url)
          ((and (derived-mode-p 'luwak-mode)
                (plist-get luwak-data :url))
           . hmm-url)
          ((thing-at-point 'symbol) . hmm-query)
          ((buffer-file-name) . hmm-file)
          ((expand-file-name default-directory) . hmm-file)))
  (hmm-update))

(provide 'ycp-complete)