From 461f212529b4ffd49e7b180c0afc721a34f23c61 Mon Sep 17 00:00:00 2001 From: Daniel Borchmann Date: Thu, 15 Dec 2022 16:29:36 +0100 Subject: [PATCH] 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. --- init.el | 11 +++++++-- site-lisp/db-utils.el | 57 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/init.el b/init.el index e5908a7..89e7236 100644 --- a/init.el +++ b/init.el @@ -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 diff --git a/site-lisp/db-utils.el b/site-lisp/db-utils.el index 8d8fed7..d383f08 100644 --- a/site-lisp/db-utils.el +++ b/site-lisp/db-utils.el @@ -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) ))