From 20d888e6c9a7b8cb28ba081bfa127b67c7ca07a7 Mon Sep 17 00:00:00 2001 From: jenchieh Date: Wed, 24 Jun 2020 17:36:32 +0800 Subject: [PATCH] Add initial input. --- ivy-searcher.el | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/ivy-searcher.el b/ivy-searcher.el index 374a9b5..bf9636a 100644 --- a/ivy-searcher.el +++ b/ivy-searcher.el @@ -54,9 +54,17 @@ :type 'string :group 'ivy-searcher) +(defcustom ivy-searcher-default-input "" + "Default initial input for searcher." + :type 'string + :group 'ivy-searcher) + (defconst ivy-searcher--prompt-format "[Searcher] %s: " "Prompt string when using `ivy-searcher'.") +(defvar ivy-searcher--initial-input nil + "Current initial input for searcher.") + (defvar ivy-searcher--target-buffer nil "Record down the current target buffer.") @@ -111,6 +119,12 @@ (setq ln-str (nth 3 data)))) (list :file file :string ln-str :position pos :line-number ln :column col))) +(defun ivy-searcher--initial-input-or-region () + "Return the default initiali input depend if region is active or not." + (if (use-region-p) + (buffer-substring-no-properties (region-beginning) (region-end)) + ivy-searcher-default-input)) + ;;; Search (defun ivy-searcher--do-search-complete-action (cand) @@ -186,19 +200,23 @@ (defun ivy-searcher-search-project () "Search through the project." (interactive) - (ivy-read (format ivy-searcher--prompt-format "Search") - #'ivy-searcher--do-search-project - :dynamic-collection t - :require-match t - :action #'ivy-searcher--do-search-complete-action)) + (let ((ivy-searcher--initial-input (ivy-searcher--initial-input-or-region))) + (ivy-read (format ivy-searcher--prompt-format "Search") + #'ivy-searcher--do-search-project + :initial-input ivy-searcher--initial-input + :dynamic-collection t + :require-match t + :action #'ivy-searcher--do-search-complete-action))) ;;;###autoload (defun ivy-searcher-search-file () "Search through current file." (interactive) - (let ((ivy-searcher--target-buffer (or (buffer-file-name) (buffer-name)))) + (let ((ivy-searcher--initial-input (ivy-searcher--initial-input-or-region)) + (ivy-searcher--target-buffer (or (buffer-file-name) (buffer-name)))) (ivy-read (format ivy-searcher--prompt-format "Search") #'ivy-searcher--do-search-file + :initial-input ivy-searcher--initial-input :dynamic-collection t :require-match t :action #'ivy-searcher--do-search-complete-action))) @@ -246,19 +264,23 @@ (defun ivy-searcher-replace-project () "Search and replace string in project." (interactive) - (ivy-read (format ivy-searcher--prompt-format "Replace") - #'ivy-searcher--do-search-project - :dynamic-collection t - :require-match t - :action #'ivy-searcher--do-replace-matched-action)) + (let ((ivy-searcher--initial-input (ivy-searcher--initial-input-or-region))) + (ivy-read (format ivy-searcher--prompt-format "Replace") + #'ivy-searcher--do-search-project + :initial-input ivy-searcher--initial-input + :dynamic-collection t + :require-match t + :action #'ivy-searcher--do-replace-matched-action))) ;;;###autoload (defun ivy-searcher-replace-file () "Search and replace string in file." (interactive) - (let ((ivy-searcher--target-buffer (or (buffer-file-name) (buffer-name)))) + (let ((ivy-searcher--initial-input (ivy-searcher--initial-input-or-region)) + (ivy-searcher--target-buffer (or (buffer-file-name) (buffer-name)))) (ivy-read (format ivy-searcher--prompt-format "Replace") #'ivy-searcher--do-search-file + :initial-input ivy-searcher--initial-input :dynamic-collection t :require-match t :action #'ivy-searcher--do-replace-matched-action)))