summaryrefslogtreecommitdiff
path: root/.config/emacs/modules/bd--minibuffer.el
blob: de9c37d10fb704e7fe690ce768fa508e7b41ed65 (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
;;; -*- lexical-binding: t; -*-
;;; Commentary:
;;; Code:


(require 'f)


(use-package icomplete
  :demand t
  :bind
  ((:map icomplete-minibuffer-map
         ("RET" . icomplete-force-complete-and-exit)))
  :config
  (setopt completing-read-function #'completing-read-default
          read-file-name-function #'read-file-name-default
          completion-styles '(basic
                              substring
                              initials
                              flex)
          icomplete-delay-completions-threshold 0
          icomplete-compute-delay 0.10
          icomplete-show-matches-on-no-input t
          icomplete-separator " | "
          completions-max-height '30
          completions-detailed t)
  (icomplete-vertical-mode t))

(use-package completion-preview
  :hook
  ((comint-mode . completion-preview-mode)
   (c-mode . completion-preview-mode)
   (c++-mode . completion-preview-mode)
   (emacs-lisp-mode . completion-preview-mode)))

(use-package selector
  :demand t
  :config
  (setopt selector-minibuffer-lines 15)
  (defvar bd/navigate-recent-display-number 6
    "The number of recent buffers that show up in bd/navigate.")
  (defvar bd/blacklisted-buffer-regexp-list
    '(
      "\\*Async Shell Command\\*"
      "\\*http"
      "\\magit-process"
      "\\*Minibuf"
      "\\*Echo Area"
      "\\*newsticker"
      "\\*Org Preview LaTeX Output\\*"
      "\\*Shell Command Output\\*"
      "\\*tramp"
      "\\*eldoc"
      "\\*server\\*"
      )
    "Buffers that should not show up in buffer-related
selection commands.")

  (defun bd/buffer-blacklisted-p (buf)
    "Return non-nil if BUF is blacklisted."
    (cl-reduce (lambda (x y) (or x y)) (mapcar (lambda (r) (string-match r buf))
                                          bd/blacklisted-buffer-regexp-list)))

  (defun bd/buffer-list ()
    "Return a list of non-blacklisted buffers."
    (cl-remove-if #'bd/buffer-blacklisted-p (mapcar 'buffer-name (buffer-list))))

  (defun bd/selector-recent-buffers ()
    (selector-source-create
     "Recent"
     :candidates
     (take bd/navigate-recent-display-number
           (cl-remove-if (lambda (b)
                           (get-buffer-window b 'visible))
                         (bd/buffer-list)))
     :actions
     selector-buffer-actions))

  (defun bd/selector-project-files ()
    (selector-source-create
     "Project Files"
     :candidates
     (let ((proj (project-current)))
       (when proj
         (project-files proj)))
     :actions
     selector-file-actions))

  (defmacro bd/selector-buffer-type (name c)
    `(selector-source-create
      ,name
      :candidates
      (cl-remove-if-not ,c (bd/buffer-list))
      :actions
      selector-buffer-actions))

  (defun bd/navigate ()
    (interactive)
    (selector
     (list
      (bd/selector-recent-buffers)
      (bd/selector-buffer-type "EWW" #'bd/buffer-eww-p)
      (bd/selector-buffer-type "EXWM" #'bd/buffer-exwm-p)
      (bd/selector-buffer-type "Shell" #'bd/buffer-shell-p)
      (bd/selector-buffer-type "IRC" #'bd/buffer-irc-p)
      (bd/selector-buffer-type "Text" #'bd/buffer-text-p)
      (bd/selector-buffer-type "Source" #'bd/buffer-prog-p)
      (bd/selector-buffer-type "Scratch" #'bd/buffer-scratch-p)
      (bd/selector-buffer-type "Directories" #'bd/buffer-dired-p)
      (bd/selector-buffer-type "Ordinary" #'bd/buffer-ordinary-p)
      (bd/selector-project-files)
      (selector-recentf-source))))
  (keymap-global-set "C-x b" 'bd/navigate)

  (defun bd/selector-rg ()
    "Sources for lines found via grep (or a clone)."
    (interactive)
    (let ((query (read-string "rg: ")))
      (defun conv (x)
        (cons (car x) (cons (- (string-to-number (cadr x)) 1) (caddr x))))
      (defun all-in-file (key list)
        (--map (to-candidate (cdr it)) (--filter (s-equals? key (car it)) list)))
      (defun to-candidate (x)
        (selector-candidate-create (cdr x) :value (car x)))
      (let* ((dir (expand-file-name (bd/get-directory-dwim)))
             (result (with-temp-buffer
                       (call-process "rg" nil t nil "-n" "-." query dir)
                       (buffer-string)))
             (lines (--map (conv (s-split-up-to ":" it 2)) (--filter (not (s-blank? it)) (s-split "\n" result))))
             (files (-uniq (-map #'car lines)))
             (sources (--map (selector-source-create
                              it
                              :candidates (all-in-file it lines)
                              :actions (selector-file-contents-actions it))
                             files)))
        (when (not (null sources))
          (selector sources)))))
  (keymap-global-set "C-z s" 'bd/selector-rg)
  (keymap-global-set "C-z d" 'bd/selector-occur))

(defun bd/selector-occur ()
  "Sources for lines in the current file."
  (interactive)
  (when-let ((file (buffer-file-name)))
    (selector (list (selector-file-contents-source file)))))

(defun bd/selector-rg ()
  "Sources for lines found via grep (or a clone)."
  (interactive)
  (let ((query (read-string "rg: ")))
    (defun conv (x)
      (cons (car x) (cons (- (string-to-number (cadr x)) 1) (caddr x))))
    (defun all-in-file (key list)
      (--map (to-candidate (cdr it)) (--filter (s-equals? key (car it)) list)))
    (defun to-candidate (x)
      (selector-candidate-create (cdr x) :value (car x)))
    (let* ((dir (expand-file-name (bd/get-directory-dwim)))
           (result (with-temp-buffer
                     (call-process "rg" nil t nil "-n" "-." query dir)
                     (buffer-string)))
           (lines (--map (conv (s-split-up-to ":" it 2)) (--filter (not (s-blank? it)) (s-split "\n" result))))
           (files (-uniq (-map #'car lines)))
           (sources (--map (selector-source-create
                            it
                            :candidates (all-in-file it lines)
                            :actions (selector-file-contents-actions it))
                           files)))
      (when (not (null sources))
        (selector sources)))))


(provide 'bd--minibuffer)
;;; bd--minibuffer.el ends here