summaryrefslogtreecommitdiff
path: root/completion-conf.el
blob: 7885943c26cb68475476741ea9775ce646c6963e (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
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
;;; completion-conf.el --- My configurations for completions inside Emacs  -*- lexical-binding: t; -*-

;; Author: JSDurand <mmemmew@gmail.com>
;; Created: 2021-01-22 21:41:30
;; Version: 0.0.1
;; Keywords: matching

;; This file is NOT part of GNU/Emacs.

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

;; This program 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 General Public License for more details.

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

;;; Commentary:

;; Thsi file is my configurations of the built in completion framework
;; in Emacs.  I intend to configure it to use my yet-to-come
;; completion style using suffix trees.

;;; Code:

(require 'minibuffer)
(require 'simple)

;;; Basic settings

(setq completion-styles '(initials substring partial-completion regex flex))
(setq completion-category-defaults nil)
(setq completion-flex-nospace nil)
(setq completion-pcm-complete-word-inserts-delimiters t)
(setq completion-show-help nil)
(setq completion-ignore-case t)
(setq read-file-name-completion-ignore-case t)
(setq read-buffer-completion-ignore-case t)
(setq resize-mini-windows t)

(set 'completions-format 'one-column)

(define-key minibuffer-local-completion-map (vector 'down) #'durand-embark-scroll-down-or-go-to-completions)
(define-key minibuffer-local-completion-map (vector 'up) #'durand-embark-scroll-up-or-go-to-completions)
(define-key minibuffer-local-completion-map (vector ?\C-n) #'durand-embark-scroll-down-or-go-to-completions)
(define-key minibuffer-local-completion-map (vector ?\C-p) #'durand-embark-scroll-up-or-go-to-completions)
(define-key minibuffer-local-completion-map (vector ?\s-v) #'durand-completion-toggle-display)
(define-key minibuffer-local-completion-map (vector 32) nil)
(define-key minibuffer-local-completion-map (vector ?\M-o) #'durand-completion-goto-minibuffer)
(define-key minibuffer-local-must-match-map (vector 32) nil)
(define-key completion-list-mode-map (vector 'down) #'durand-completion-scroll-down-or-go-to-minibuffer)
(define-key completion-list-mode-map (vector 'up) #'durand-completion-scroll-up-or-go-to-minibuffer)
(define-key completion-list-mode-map (vector ?\C-n) #'durand-completion-scroll-down-or-go-to-minibuffer)
(define-key completion-list-mode-map (vector ?\C-p) #'durand-completion-scroll-up-or-go-to-minibuffer)
(define-key completion-list-mode-map (vector ?\M-o) #'durand-completion-goto-minibuffer)

;;; New behaviours of emacs 29 make the default completion list more useful now

(setq completion-auto-select 'second-tab)
(setq completion-auto-wrap t)

;;; Headlong stuff

;;;###autoload
(defvar durand-headlong-entered-minibuffer-p nil
  "Whether or not we have entered minibuffer.

This is used for determining if we shall exit the minibuffer when
there is only one candidate left.")

;;;###autoload
(defun durand-headlong-minibuffer-setup-hook ()
  "The entry for the completion to be headlong.
Simply add this function to `minibuffer-setup-hook'."
  ;; NOTE: When we run this function we first enter minibuffer, so we
  ;; set a variable to its appropriate value.
  (set 'durand-headlong-entered-minibuffer-p nil)
  (add-hook 'post-command-hook 'durand-headlong-post-command-hook t)
  (add-hook 'minibuffer-exit-hook 'durand-headlong-minibuffer-exit-hook))

;;;###autoload
(defun durand-headlong-post-command-hook ()
  "Exit the minibuffer if there is only one candidate left.

In practice, when we first enter minibuffer, there is some
abnormal behaviour, so we only check when we have entered the
minibuffer as usual."
  (let ((comps (completion-all-completions
                (minibuffer-contents)
                minibuffer-completion-table minibuffer-completion-predicate
                (- (point) (minibuffer-prompt-end)))))
    (cond
     ((and durand-headlong-entered-minibuffer-p
           comps
           (not (consp (cdr comps))))
      (minibuffer-force-complete-and-exit))
     (t (set 'durand-headlong-entered-minibuffer-p t)))))

;;;###autoload
(defun durand-headlong-minibuffer-exit-hook ()
  "Remove the hooks we added."
  (remove-hook 'post-command-hook 'durand-headlong-post-command-hook)
  (remove-hook 'minibuffer-setup-hook 'durand-headlong-minibuffer-setup-hook)
  (remove-hook 'minibuffer-exit-hook 'durand-headlong-minibuffer-exit-hook))

;; Make sure `minibuffer-message' respects `inhibit-message'.

(defun durand-minibuffer-respect-inhibit-advice (fn &rest args)
  (cond (inhibit-message)
        (t (apply fn args))))

(advice-add 'minibuffer-message :around 'durand-minibuffer-respect-inhibit-advice)

;;; Display completion candidates

;;;###autoload
(defun durand-completion-display-after-change (&rest args)
  "Call `minibuffer-completion-help' with no input.

\(fn &rest ARGS)"
  (cond
   ((minibufferp)
    (let ((while-no-input-ignore-events '(selection-request)))
      (while-no-input
        (condition-case nil
            (save-excursion
              (goto-char (point-max))
              (save-match-data
                (let ((inhibit-message t))
                  (minibuffer-completion-help))))
          (quit (abort-recursive-edit))))))))

;; (fset
;;  'durand-completion-display-after-change
;;  (make-byte-code
;;   #x080
;;   (unibyte-string 192 32 135)
;;   (vector #'minibuffer-completion-help)
;;   2
;;   "Call `minibuffer-completion-help' with no input.

;; \(fn &rest ARGS)"
;;   nil))

;;;###autoload
(defalias
  'durand-completion-toggle-display
  (make-byte-code
   0
   (unibyte-string 192 9 62              ; determine if it is a member
                   131 14 0
                   ;; memq
                   194 193 192 196 35 130 20 0
                   ;; not memq
                   195 193 192 197 196 36
                   135)
   (vector #'durand-completion-display-after-change
           'after-change-functions
           'remove-hook 'add-hook t nil)
   6
   "Toggle to display the list of completions after each input.

\(fn)"
   nil))

;; (defun durand-completion-toggle-display ()
;;   "Toggle to display the list of completions after each input."
;;   (interactive)
;;   (cond
;;    ((memq #'durand-completion-display-after-change 'post-command-hook)
;;     (remove-hook 'post-command-hook #'durand-completion-display-after-change t))
;;    ((add-hook 'post-command-hook #'durand-completion-display-after-change nil t))))

;;; Live previews

;; TODO: Add the functionality to "preview" the completions in some
;; sense.

(defun durand-preview-in-completion-list ()
  "Preview the current candidate in the completion list buffer."
  (cond
   ((derived-mode-p 'completion-list-mode)
    (let ((mes "Nothing to preview here")
          prop)
      (cond
       ((setq prop (get-text-property
                    (point) 'durand-preview-property))
        (cond
         ((eq (car prop) 'buffer)
          (display-buffer-use-least-recent-window
           (cadr prop)
           '((".*" (display-buffer-use-some-window
                    display-buffer-use-least-recent-window))))
          (cond ((integerp (caddr prop)) (goto-line (caddr prop)))))))
       ((active-minibuffer-window) (minibuffer-message mes))
       ((message mes)))))))

(defun durand-propertize-buffer-for-completion-list (ls)
  "Propertize the buffer candidates so as to preview buffers.
LS has two argument: STR and GROUP-FUN.

STR is the string to insert, and GROUP-FUN is used to group
candidates."
  (let ((str (car ls))
        (group-fun (cadr ls)))
    (cond
     ((not (consp str))
      ;; if it is a cons, it will be processed and called again.
      (list
       (propertize
        str
        'durand-preview-property
        (list 'buffer (get-buffer str)))
       group-fun))
     ((list str grou-fun)))))

(defun durand-preview-exit-hook ()
  "Clear things up."
  (let (bf)
    (cond
     ((setq bf (get-buffer "*Completions*"))
      (with-current-buffer bf
        (remove-hook 'post-command-hook
                     #'durand-preview-in-completion-list))))))

(defun durand-open-completion-list-with-buffer-preview ()
  "Open the completion list buffer and propertize the candidates."
  (interactive)
  (advice-add #'completion--insert :filter-args
              #'durand-propertize-buffer-for-completion-list)
  (add-hook 'minibuffer-exit-hook #'durand-preview-exit-hook)
  (let ((minibuffer-allow-text-properties t))
    (minibuffer-completion-help))
  (advice-remove #'completion--insert
                 #'durand-propertize-buffer-for-completion-list)
  (goto-char (point-max))
  (durand-embark-scroll-down-or-go-to-completions 1)
  (cond
   ((derived-mode-p 'completion-list-mode)
    (add-hook 'post-command-hook
              #'durand-preview-in-completion-list nil t))
   ((user-error "Something is wrong!"))))

;; (defun durand-quit-completion-list-with-buffer-preview ()
;;   "Restore the buffer and quit the completion list."
;;   (interactive)
;;   (quit-window)
;;   (set-window-configuration durand-completion-list-window-conf))

(defvar durand-preview-keymap (make-sparse-keymap "Buffer:")
  "A keymap used to read buffers with previews.")

(define-key durand-preview-keymap (vector 'tab)
  #'durand-open-completion-list-with-buffer-preview)

(defun durand-read-buffer-with-preview-function
    (prompt &optional def require-match predicate)
  "Use a custom keymap when reading buffers.
The arguments PROMPT, DEF, REQUIRE-MATCH and PREDICATE have the
same meaning as in `read-buffer'."
  (let* ((base-keymap
          (cond (require-match minibuffer-local-must-match-map)
                (minibuffer-local-completion-map)))
         (keymap (make-composed-keymap
                  (list base-keymap durand-preview-keymap)))
         (buffer (current-buffer))
         (result
          (minibuffer-with-setup-hook
              (lambda ()
                (setq-local minibuffer-completion-table #'internal-complete-buffer)
                (setq-local minibuffer-completion-predicate predicate)
                ;; FIXME: Remove/rename this var, see the next one.
                (setq-local minibuffer-completion-confirm
                            (unless (eq require-match t) require-match))
                (setq-local minibuffer--require-match require-match)
                (setq-local minibuffer--original-buffer buffer))
            (read-from-minibuffer
             prompt nil keymap nil 'buffer-name-history def))))
    (when (and (equal result "") def)
      (setq result (if (consp def) (car def) def)))
    result))

(defun durand-switch-to-buffer-preview ()
  "Preview switching buffers."
  (interactive)
  (let ((read-buffer-function
         #'durand-read-buffer-with-preview-function))
    (switch-to-buffer
     (let ((default (other-buffer)))
       (read-buffer-to-switch
        (format
         "Switch to buffer%s"
         (cond
          ((and (bufferp default)
                (buffer-live-p default))
           (format " (default %s): " (buffer-name default)))
          (": "))))))))

;;; Regular expression completion style

;;;###autoload
(defun regex-try-completion (string table pred point &optional metadata)
  "The function that tries to complete STRING using completion table \
TABLE for the `regex' style.
Only the elements that satisfy the predicate PRED are considered.

POINT is the position of point within STRING.

The return value is nil if there is no completion, and is t if
STRING is the only possible completion. It is a pair

\(NEWSTRING . NEWPOINT)

otherwise, where NEWSTRING is the completed result and NEWPOINT
is the new position for point."
  (let* ((completion-regexp-list (cons string completion-regexp-list))
         (result (try-completion "" table pred)))
    (cond
     ;; For nil values we don't have to return nil explicitly.
     ((null result) nil)
     ((eq result t))
     ((stringp result) (cons result (length result))))))

;;;###autoload
(defun regex-all-completion (string table pred point &optional metadata)
  "List the possible completions of STRING in completion table \
TABLE for the style `regex'.
Only the elements of TABLE that satisfy the predicate PRED are
considered. POINT is the position of point within STRING.

The return value is a list of completions and may contain the
base-size in the last `cdr'."
  (let ((completion-regexp-list (cons string completion-regexp-list)))
    (all-completions "" table pred)))

(let ((style-elements
       (list #'regex-try-completion
             #'regex-all-completion
             "Simple regular expression completion.
This considers the input as the regular expression itself." ))
      (assoc-result (assoc 'regex completion-styles-alist)))
  (cond
   (assoc-result (setcdr assoc-result style-elements))
   ((setq completion-styles-alist
          (cons (cons 'regex style-elements) completion-styles-alist)))))

(provide 'completion-conf)
;;; completion-conf.el ends here