summaryrefslogtreecommitdiff
path: root/completion-conf.el
blob: fdf0e100b380446ed8f79bb71497ce61c5521e21 (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
;;; 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)

(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)

(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 ?\s-v) #'durand-completion-toggle-display)
(define-key minibuffer-local-completion-map (vector 32) nil)
(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)

;;;###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))

;;;###autoload
(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
(fset
 '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))))

;;;###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