;;; -*- 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 "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