2021-02-16 16:46:35 -06:00
|
|
|
;;; completion/selectrum/autoload/selectrum.el -*- lexical-binding: t; -*-
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defadvice! +selectrum--company-capf--candidates-a (fn &rest args)
|
2021-07-07 01:24:31 +03:00
|
|
|
"Highlight company matches correctly, and try default completion styles before
|
|
|
|
orderless."
|
2021-02-16 16:46:35 -06:00
|
|
|
:around 'company-capf--candidates
|
2021-07-07 01:24:31 +03:00
|
|
|
(let ((orderless-match-faces [completions-common-part])
|
|
|
|
(completion-styles '(basic partial-completion orderless)))
|
2021-02-16 16:46:35 -06:00
|
|
|
(apply fn args)))
|
|
|
|
|
2021-01-16 20:21:50 +08:00
|
|
|
;;;###autoload
|
|
|
|
(cl-defun +selectrum-file-search (&key query in all-files (recursive t) prompt args)
|
|
|
|
"Conduct a file search using ripgrep.
|
2021-02-22 15:47:06 -06:00
|
|
|
|
2021-01-16 20:21:50 +08:00
|
|
|
:query STRING
|
|
|
|
Determines the initial input to search for.
|
|
|
|
:in PATH
|
2021-02-16 20:42:41 -06:00
|
|
|
Sets what directory to base the search out of. Defaults to the current project's root.
|
2021-01-16 20:21:50 +08:00
|
|
|
:recursive BOOL
|
|
|
|
Whether or not to search files recursively from the base directory."
|
|
|
|
(declare (indent defun))
|
|
|
|
(unless (executable-find "rg")
|
|
|
|
(user-error "Couldn't find ripgrep in your PATH"))
|
|
|
|
(require 'consult)
|
2021-02-16 20:42:41 -06:00
|
|
|
(setq deactivate-mark t)
|
2021-05-17 13:20:02 +03:00
|
|
|
(let* ((project-root (or (doom-project-root) default-directory))
|
2021-02-23 22:12:10 -06:00
|
|
|
(directory (or in project-root))
|
|
|
|
(args (split-string
|
|
|
|
(string-trim
|
|
|
|
(concat (if all-files "-uu")
|
|
|
|
(unless recursive "--maxdepth 1")
|
|
|
|
"--null --line-buffered --color=always --max-columns=500 --no-heading --line-number"
|
2021-04-10 19:13:40 +03:00
|
|
|
" --hidden -g !.git "
|
2021-02-23 22:12:10 -06:00
|
|
|
(mapconcat #'shell-quote-argument args " ")))
|
|
|
|
" "))
|
|
|
|
(prompt (or prompt
|
|
|
|
(format "rg [%s]: "
|
|
|
|
(cond ((equal directory default-directory)
|
|
|
|
"./")
|
|
|
|
((equal directory project-root)
|
|
|
|
(projectile-project-name))
|
|
|
|
((file-relative-name directory project-root))))))
|
|
|
|
(query (or query
|
|
|
|
(when (doom-region-active-p)
|
|
|
|
(replace-regexp-in-string
|
|
|
|
"[! |]" (lambda (substr)
|
|
|
|
(cond ((and (string= substr " ")
|
|
|
|
(not (featurep! +fuzzy)))
|
|
|
|
" ")
|
|
|
|
((string= substr "|")
|
|
|
|
"\\\\\\\\|")
|
|
|
|
((concat "\\\\" substr))))
|
|
|
|
(rxt-quote-pcre (doom-thing-at-point-or-region))))))
|
2021-04-10 19:13:40 +03:00
|
|
|
(ripgrep-command (mapconcat #'identity `("rg" ,@args "." "-e ARG OPTS" ) " ")))
|
2021-02-23 22:12:10 -06:00
|
|
|
(consult--grep prompt ripgrep-command directory query)))
|
2021-01-16 20:21:50 +08:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +selectrum/project-search (&optional arg initial-query directory)
|
2021-02-16 20:42:41 -06:00
|
|
|
"Peforms a live project search from the project root using ripgrep.
|
2021-01-16 20:21:50 +08:00
|
|
|
If ARG (universal argument), include all files, even hidden or compressed ones,
|
|
|
|
in the search."
|
|
|
|
(interactive "P")
|
2021-02-16 20:42:41 -06:00
|
|
|
(+selectrum-file-search :query initial-query :in directory :all-files arg))
|
2021-01-16 20:21:50 +08:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +selectrum/project-search-from-cwd (&optional arg initial-query)
|
2021-02-16 20:42:41 -06:00
|
|
|
"Performs a live project search from the current directory.
|
2021-01-16 20:21:50 +08:00
|
|
|
If ARG (universal argument), include all files, even hidden or compressed ones."
|
|
|
|
(interactive "P")
|
|
|
|
(+selectrum/project-search arg initial-query default-directory))
|
2021-02-19 20:48:34 -06:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +selectrum/search-symbol-at-point ()
|
|
|
|
(interactive)
|
|
|
|
(consult-line (thing-at-point 'symbol)))
|
2021-05-07 21:27:14 +03:00
|
|
|
|
2021-06-30 19:24:27 +03:00
|
|
|
;;;###autoload
|
|
|
|
(defun +selectrum/backward-updir ()
|
|
|
|
"Delete char before or go up directory for file cagetory selectrum buffers."
|
|
|
|
(interactive)
|
|
|
|
(let ((metadata (completion-metadata (minibuffer-contents)
|
|
|
|
minibuffer-completion-table
|
|
|
|
minibuffer-completion-predicate)))
|
|
|
|
(if (and (eq (char-before) ?/)
|
|
|
|
(eq (completion-metadata-get metadata 'category) 'file))
|
|
|
|
(let ((new-path (minibuffer-contents)))
|
|
|
|
(delete-region (minibuffer-prompt-end) (point-max))
|
|
|
|
(insert (abbreviate-file-name
|
|
|
|
(file-name-directory
|
|
|
|
(directory-file-name
|
|
|
|
(expand-file-name new-path))))))
|
|
|
|
(call-interactively 'backward-delete-char))))
|
|
|
|
|
|
|
|
|
2021-05-07 21:27:14 +03:00
|
|
|
;;;###autoload
|
2021-05-21 13:19:52 +03:00
|
|
|
(defun +selectrum/embark-export-write ()
|
|
|
|
"Export the current selectrum results to a writable buffer if possible.
|
|
|
|
|
|
|
|
Supports exporting consult-grep to wgrep, file to wdeired, and consult-location to occur-edit"
|
2021-05-07 21:27:14 +03:00
|
|
|
(interactive)
|
|
|
|
(require 'wgrep)
|
|
|
|
(pcase-let ((`(,type . ,candidates)
|
|
|
|
(run-hook-with-args-until-success 'embark-candidate-collectors)))
|
2021-05-21 13:19:52 +03:00
|
|
|
(pcase type
|
|
|
|
('consult-grep (let ((embark-after-export-hook #'wgrep-change-to-wgrep-mode))
|
|
|
|
(embark-export)))
|
|
|
|
('file (let ((embark-after-export-hook #'wdired-change-to-wdired-mode))
|
|
|
|
(embark-export)))
|
|
|
|
('consult-location (let ((embark-after-export-hook #'occur-edit-mode))
|
|
|
|
(embark-export)))
|
|
|
|
(x (user-error "embark category %S doesn't support writable export" x)))))
|
2021-05-22 01:21:10 +03:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +selectrum/embark-preview ()
|
|
|
|
"Previews candidate in selectrum buffer, unless it's a consult command"
|
|
|
|
(interactive)
|
2021-05-22 23:22:17 +03:00
|
|
|
(unless (bound-and-true-p consult--preview-function)
|
|
|
|
(save-selected-window
|
|
|
|
(let ((embark-quit-after-action nil))
|
|
|
|
(embark-default-action)))))
|
2021-05-22 01:21:10 +03:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +selectrum/next-candidate-preview ()
|
|
|
|
"Move to next candidate and preivew it"
|
|
|
|
(interactive)
|
2021-06-30 17:36:08 +03:00
|
|
|
(if (featurep! :completion selectrum +vertico)
|
|
|
|
(vertico-next)
|
|
|
|
(selectrum-next-candidate))
|
2021-05-22 01:21:10 +03:00
|
|
|
(+selectrum/embark-preview))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +selectrum/previous-candidate-preview ()
|
|
|
|
"Move to previous candidate and preview it"
|
|
|
|
(interactive)
|
2021-06-30 17:36:08 +03:00
|
|
|
(if (featurep! :completion selectrum +vertico)
|
|
|
|
(vertico-previous)
|
|
|
|
(selectrum-previous-candidate))
|
2021-05-22 01:21:10 +03:00
|
|
|
(+selectrum/embark-preview))
|
2021-06-30 17:36:08 +03:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +selectrum/repeat ()
|
|
|
|
"Repeat the last selectrum/vertico command."
|
|
|
|
(interactive)
|
|
|
|
(if (featurep! :completion selectrum +vertico)
|
|
|
|
(user-error "The vertico flag doesn't support repeating commands (yet)")
|
|
|
|
(selectrum-repeat)))
|