summaryrefslogtreecommitdiff
path: root/emacs/inits/11-completion.el
blob: 654ee5c90fe3d41fbb44aa23f36ca535e6b43122 (plain)
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
;;; -*- lexical-binding: t -*-

;; Time-stamp: <2026-08-25 15h27 BST (581d7bf5)>

(use-package savehist
  :ensure t
  :init (savehist-mode))

;; -----------------------------------------------------------------------------
;; General configuration

(use-package emacs
  :init
  ;; Add prompt indicator to `completing-read-multiple'.
  (defun crm-indicator (args)
    (cons (concat "[CRM] " (car args)) (cdr args)))
  (advice-add #'completing-read-multiple :filter-args #'crm-indicator)

  ;; Grow and shrink minibuffer
  ;;(setq resize-mini-windows t)

  ;; Do not allow the cursor in the minibuffer prompt
  (setq minibuffer-prompt-properties
        '(read-only t cursor-intangible t face minibuffer-prompt))
  (add-hook 'minibuffer-setup-hook #'cursor-intangible-mode)

  ;; Enable recursive minibuffers
  (setq enable-recursive-minibuffers t))

(use-package icomplete
  :hook  ((after-init
           . (lambda () (icomplete-vertical-mode 1)))
          (icomplete-minibuffer-setup
           . (lambda () (setq truncate-lines t))))

  :init
  (defun my-completing-read-in-region (start end collection &optional predicate)
    "Prompt for completion of region in the minibuffer."
    (barf-if-buffer-read-only)
    (let* ((initial (buffer-substring-no-properties start end))
           (source-buffer (current-buffer))
           (wrapped-table
            (if (functionp collection)
                `(lambda (str pred action)
                   (let ((result (with-current-buffer ,source-buffer
                                   (funcall ',collection str pred action))))
                     (if (eq action 'metadata)
                         (cons 'metadata
                               (mapcar
                                (lambda (x)
                                  (if (and (consp x)
                                           (symbolp (car x))
                                           (string-suffix-p
                                            "-function" (symbol-name (car x)))
                                           (functionp (cdr x)))
                                      (cons (car x)
                                            `(lambda (&rest args)
                                               (with-current-buffer ,',source-buffer
                                                 (apply ',(cdr x) args))))
                                    x))
                                (cdr result)))
                       result)))
              collection))
           (exit-fn (plist-get completion-extra-properties :exit-function))
           (ann-fun (plist-get completion-extra-properties :annotation-function))
           (aff-fun (plist-get completion-extra-properties :affixation-function))
           (completion-extra-properties
            `(,@(and ann-fun
                     (list :annotation-function
                           `(lambda (&rest args)
                              (with-current-buffer ,source-buffer
                                (apply ',ann-fun args)))))
              ,@(and aff-fun
                     (list :affixation-function
                           `(lambda (&rest args)
                              (with-current-buffer ,source-buffer
                                (apply ',aff-fun args)))))))
           (minibuffer-allow-text-properties t)
           (enable-recursive-minibuffers t)
           (completion (completing-read "Completion: " wrapped-table
                                        predicate t initial)))
      (cond
       ((and completion (not (string-empty-p completion)))
        (completion--replace start end completion)
        (when exit-fn
          (funcall exit-fn completion
                   (if (eq (try-completion completion collection predicate) t)
                       'finished 'exact)))
        t)
       (t ([message]I "No completion") nil))))

  (defun my-icomplete-backspace ()
    "Delete back to previous / in paths, or normal backspace otherwise."
    (interactive)
    (if (looking-back "[^/]/" (line-beginning-position))
        (let ((slash-pos (save-excursion
                           (backward-word 1)
                           (search-backward "/" nil t))))
          (if slash-pos
              (delete-region (1+ slash-pos) (point))
            (delete-backward-char 1)))
      (delete-backward-char 1)))

  (defun my-switch-to-buffer-with-recentf ()
    "Switch to buffer or recent file with visual separation."
    (interactive)
    (let* ((current-buffer (buffer-name (current-buffer)))
           (buffers (seq-remove
                     (lambda (b) (or (string-prefix-p " " b) (string= b current-buffer)))
                     (mapcar #'buffer-name (buffer-list))))
           (recent-files (seq-take recentf-list 20))
           (separator "─── Recent Files ───")
           (buffer-cands (mapcar (lambda (buf)
                                   (propertize buf 'multi-category (cons 'buffer buf)))
                                 buffers))
           (header-cand (propertize separator 'multi-category (cons 'separator t)))
           (file-cands (mapcar (lambda (file)
                                 (propertize file 'multi-category (cons 'file file)))
                               recent-files))
           (all-cands (append buffer-cands (list header-cand) file-cands))
           (my-table (lambda (string pred action)
                       (pcase action
                         ('metadata '(metadata (category . multi-category) (cycle-sort-function . identity)))
                         ('all all-cands)
                         ('lambda (or (try-completion string all-cands pred) string))
                         (_ (seq-filter (lambda (c) (string-prefix-p string (substring-no-properties c)))
                                        all-cands))))))

      (let ((result (completing-read "Switch to: " my-table nil t)))
        (unless (string-empty-p result)
          (cond
           ((string= result separator) nil)
           ((file-exists-p result) (find-file result))
           (t (switch-to-buffer result)))))))

  :bind (("C-x b" . my-switch-to-buffer-with-recentf)
         (:map icomplete-minibuffer-map
               ("TAB" . icomplete-force-complete)
               ("C-j" . icomplete-ret)
               ("RET" . icomplete-force-complete-and-exit)
               ("<backspace>" . my-icomplete-backspace)))
  :config
  (setq icomplete-compute-delay 0
        icomplete-show-matches-on-no-input t
        icomplete-hide-common-prefix nil
        icomplete-prospects-height 10
        icomplete-max-delay-chars 0
        icomplete-scroll t)

  (setq completion-styles '(flex basic partial-completion)
        completion-ignore-case t)

  (setq completion-in-region-function #'my-completing-read-in-region))

;; -----------------------------------------------------------------------------
;; Marginalia

(use-package marginalia
  :ensure t
  :bind (:map minibuffer-local-map ("M-A" . marginalia-cycle))
  :init (marginalia-mode))