Subject: bug#39512: 28.0.50; Add command isearch-yank-region

From: Juri Linkov <[email-masked]>
Date: Sun, 09 Feb 2020 02:31:50 +0200

> I wish to have this command; it naturally completes other
> isearch-yank-... cases.
>
> This topic has been already discussed in the following links:
>
> https://lists.gnu.org/archive/html/emacs-devel/2019-04/msg01125.html
> https://lists.gnu.org/archive/html/emacs-devel/2019-05/msg00003.html
>
> Note that in those threads there were plenty of discussions; here I'd
> like to focus just in this proposed command.  If needed, I'd recommend
> to open further bugs to discuss about other commands.

Thanks for creating a new feature request that unlike these discussions
on emacs-devel won't fall into oblivion.

> I let this command to start the interactive search if we are
> not already there; from the above links I realized that
> such a functionality was also missing.  This is consistent with
> `isearch-yank-kill' (I think we should mention that in its docstring).

Please add this useful feature of `isearch-yank-kill' to the documentation.
Maybe it should be bound to a special key on the global `M-s' prefix map.
The most natural key would be `M-s C-y'.

>     Add command isearch-yank-region
>
>     During an incremental search, this command appends the region
>     to the search string.  Otherwise, start an incremental search
>     using the region as the search string.

What use cases do you think it could be used for?

I don't see any useful case for appending the region to the search string.

I see only 2 useful cases that don't append the region to the search string:

1. Before starting isearch, the user selects the region,
   then types a special command bound to a key on the global `M-s' prefix
   that yanks the region to the initially empty search string
   (i.e. it doesn't append, it replaces the empty search string)

I used such command to do this:

(defun isearch-forward-region ()
  "Do incremental search forward for text from the active region.
Like ordinary incremental search except that text from the region
is added to the search string initially if the region is active."
  (interactive)
  (isearch-forward nil 1)
  (cond
   ((use-region-p)
    (when (< (mark) (point))
      (exchange-point-and-mark))
    (isearch-yank-string
     (buffer-substring-no-properties (region-beginning) (region-end)))
    (deactivate-mark))
   (t
    (setq isearch-error "No active region")
    (isearch-push-state)
    (isearch-update))))

(define-key search-map "\M-." 'isearch-forward-region)

2. The second useful case is to activate the region, start isearch,
   use isearch to find the string at the region end, thus moving the region end
   to a new position, replace (don't append) the search string with region text -
   this is what isearch-yank-region could do.
   IOW, sync the region with the search string.

> +(defun isearch-yank-region ()
> +  "Pull region into search string.
> +If called out of an incremental search, then start an incremental
> +search with the region as the search string."
> +  (interactive)
> +  (cond ((use-region-p)
> +         (unless isearch-mode (isearch-mode t))
> +         (isearch-yank-string (funcall region-extract-function))

Here (funcall region-extract-function) signals the error

  (wrong-number-of-arguments (1 . 1) 0)

Have you tested your patch?