fix(vertico): escape perl syntax in selection

+ Escape any special regexp characters in the active selection.
+ Change the async split character (#) to the first of %, @, !, &, or /
  that is absent in the active selection (falling back to %
  otherwise) (since consult doesn't recognize escaping for these
  characters).
This commit is contained in:
Henrik Lissner 2021-07-26 22:44:51 -04:00
parent ffcfb6d8b3
commit fd844ea18d

View file

@ -30,30 +30,40 @@ orderless."
(setq deactivate-mark t) (setq deactivate-mark t)
(let* ((project-root (or (doom-project-root) default-directory)) (let* ((project-root (or (doom-project-root) default-directory))
(directory (or in project-root)) (directory (or in project-root))
(args (split-string (args
(string-trim (split-string
(concat (if all-files "-uu") (string-trim
(unless recursive "--maxdepth 1") (concat (if all-files "-uu")
"--null --line-buffered --color=always --max-columns=500 --no-heading --line-number" (unless recursive "--maxdepth 1")
" --hidden -g !.git " "--null --line-buffered --color=always --max-columns=500 --no-heading --line-number"
(mapconcat #'shell-quote-argument args " "))) " --hidden -g !.git "
" ")) (mapconcat #'shell-quote-argument args " ")))
(prompt (or prompt " "))
(format "rg [%s]: " (prompt (if (stringp prompt) (string-trim prompt) "Search"))
(cond ((equal directory default-directory)
"./")
((equal directory project-root)
(projectile-project-name))
((file-relative-name directory project-root))))))
(query (or query (query (or query
(when (doom-region-active-p) (when (doom-region-active-p)
(replace-regexp-in-string (rxt-quote-pcre (doom-thing-at-point-or-region)))))
"[! |]" (lambda (substr) (ripgrep-command (string-join `("rg" ,@args "." "-e ARG OPTS" ) " "))
(cond ((string= substr " ") " ") (consult-async-split-style consult-async-split-style)
((string= substr "|") "\\\\\\\\|") (consult-async-split-styles-alist consult-async-split-styles-alist))
((concat "\\\\" substr)))) ;; Change the split style if the initial query contains the separator.
(rxt-quote-pcre (doom-thing-at-point-or-region)))))) (when query
(ripgrep-command (mapconcat #'identity `("rg" ,@args "." "-e ARG OPTS" ) " "))) (cl-destructuring-bind (&key type separator initial)
(consult--async-split-style)
(pcase type
(`separator
(replace-regexp-in-string (regexp-quote (char-to-string separator))
(concat "\\" separator)
query t t))
(`perl
(when (string-match-p initial query)
(setf (alist-get 'perlalt consult-async-split-styles-alist)
`(:initial ,(or (cl-loop for char in (list "%" "@" "!" "&" "/" ";")
unless (string-match-p char query)
return char)
"%")
:type perl)
consult-async-split-style 'perlalt))))))
(consult--grep prompt ripgrep-command directory query))) (consult--grep prompt ripgrep-command directory query)))
;;;###autoload ;;;###autoload