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,7 +30,8 @@ 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
(split-string
(string-trim (string-trim
(concat (if all-files "-uu") (concat (if all-files "-uu")
(unless recursive "--maxdepth 1") (unless recursive "--maxdepth 1")
@ -38,22 +39,31 @@ orderless."
" --hidden -g !.git " " --hidden -g !.git "
(mapconcat #'shell-quote-argument args " "))) (mapconcat #'shell-quote-argument args " ")))
" ")) " "))
(prompt (or prompt (prompt (if (stringp prompt) (string-trim prompt) "Search"))
(format "rg [%s]: "
(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