Simplify file pattern query for grep

The original version of `grep-read-files` includes file names in its default
values, giving an irritating completion candidate list when used with ivy.
Changed this to just let `completing-read` do the completion itself.
This commit is contained in:
Daniel - 2022-12-15 16:29:36 +01:00
parent 7c9f795a38
commit 461f212529
No known key found for this signature in database
GPG Key ID: 784AA8DF0CCDF625
2 changed files with 65 additions and 3 deletions

11
init.el
View File

@ -476,7 +476,13 @@
:commands (rgrep zrgrep)
:bind (:map grep-mode-map
("C-x C-q" . wgrep-change-to-wgrep-mode)
("C-c C-c" . wgrep-finish-edit)))
("C-c C-c" . wgrep-finish-edit))
:config (progn
;; I am not quite sure why `grep-read-files' is prompting for file
;; names when asking for a file pattern, so let's just hook it up
;; and replace it with something more straightforward.
(advice-add 'grep-read-files :around #'db/grep-read-files)))
(use-package winner
:commands (winner-mode winner-undo winner-redo))
@ -638,7 +644,8 @@
db/convert-crlf-to-lf-in-buffer
db/sync-magit-repos-from-projectile
db/replace-variables-in-string
db/dired-ediff-files))
db/dired-ediff-files
db/grep-read-files))
(use-package db-hydras
:commands (hydra-toggle/body

View File

@ -500,6 +500,61 @@ From: https://oremacs.com/2017/03/18/dired-ediff/."
(set-window-configuration wnd))))
(error "No more than 2 files should be marked"))))
(defun db/grep-read-files (_ regexp)
"As for file pattern similar to `grep-read-files' but more direct.
This function is meant as a replacement for `grep-read-files',
replacing it by not calling calling `read-file-name-internal'.
REGEXP is only used for display at the completion prompt, the
same way `grep-read-files' does.
Also add the default as initial input instead of as default
proper. The latter does not play well with my current completion
framework, as it always tries to match my input with default
entries, even if I want to use the input directly."
(let* ((bn (funcall grep-read-files-function))
(fn (and bn
(stringp bn)
(file-name-nondirectory bn)))
(default-alias
(and fn
(let ((aliases (remove (assoc "all" grep-files-aliases)
grep-files-aliases))
alias)
(while aliases
(setq alias (car aliases)
aliases (cdr aliases))
(if (string-match (mapconcat
#'wildcard-to-regexp
(split-string (cdr alias) nil t)
"\\|")
fn)
(setq aliases nil)
(setq alias nil)))
(cdr alias))))
(default-extension
(and fn
(let ((ext (file-name-extension fn)))
(and ext (concat "*." ext)))))
(default
(or default-alias
default-extension
(car grep-files-history)
(car (car grep-files-aliases))))
(files (completing-read
(format "Search for \"%s\" in files matching wildcard: "
regexp)
nil nil nil
(delete-dups
(delq nil
(append (list default default-alias default-extension)
(mapcar #'car grep-files-aliases))))
'grep-files-history)))
(and files
(or (cdr (assoc files grep-files-aliases))
files))))
;;; Base45 Decoding
@ -511,7 +566,7 @@ From: https://oremacs.com/2017/03/18/dired-ediff/."
(-each-indexed (string-to-list base45-alphabet)
(-lambda (index char)
(puthash char index decode-hash-table)
(puthash char index decode-hash-table)
;; Add an encode-hash-table here in case base45-encode-string will ever be
;; written, like so: (puthash index char encode-hash-table)
))