summaryrefslogtreecommitdiff
path: root/.config/emacs/modules/bd--minibuffer.el
blob: 4a928e32da12b4320fbd9389747e6943d6d60ef7 (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
;;; -*- 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)
  (icomplete-vertical-mode t))

(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