emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

option to let isearch select the search target (in transient mark mode)


From: Drew Adams
Subject: option to let isearch select the search target (in transient mark mode)
Date: Tue, 10 Jul 2007 12:58:48 -0700

How about adding this to Isearch: Depending on the value of an option,
`isearch-set-region-flag', Isearch leaves the region active around the
search target when you stop searching with `RET'.  This is only for
Transient Mark mode; if that mode is not on, then this does nothing.

This feature makes it easy to delete the found text, copy it, or type
to replace it in Delete Selection mode.  You can toggle the option at
any time during searching, with `C-SPC'.

I use `isearch-mode-end-hook' to implement this, but it could no doubt
be integrated with the Isearch code more directly.  Alternatively,
`transient-mark-mode' could perhaps add this to or remove it from
`isearch-mode-end-hook'.  In that case, the test for
`transient-mark-mode' would be removed.

(defcustom isearch-set-region-flag nil
  "Non-nil means set region around search target.
This is used only for Transient Mark mode.
You can toggle this with `isearch-toggle-set-region', bound to
`C-SPC' during isearch."
  :type 'boolean :group 'isearch)

(add-hook 'isearch-mode-end-hook 'isearch-set-region)

(defun isearch-set-region ()
  "Set the region around the search target, if `isearch-set-region-flag'.
This is used only for Transient Mark mode."
  (when (and isearch-set-region-flag transient-mark-mode)
    (push-mark isearch-other-end t 'activate)))

(defun isearch-toggle-set-region ()
  "Toggle `isearch-set-region-flag'."
  (interactive)
  (setq isearch-set-region-flag (not isearch-set-region-flag))
  (if isearch-set-region-flag
      (message "Setting region around search target is now ON")
    (message "Setting region around search target is now OFF")))

(define-key isearch-mode-map [(control ? )]
  'isearch-toggle-set-region)

And here is a standalone command, for users who want to do this only
upon demand and leave `isearch-set-region-flag' = nil:

(defun set-region-around-search-target ()
  "Set the region around the last search or query-replace target."
  (interactive)
  (case last-command
    ((isearch-forward isearch-backward isearch-forward-regexp
                      isearch-backward-regexp)
     (push-mark isearch-other-end t 'activate))
    (t (push-mark (match-beginning 0) t 'activate)))
  (setq deactivate-mark nil))





reply via email to

[Prev in Thread] Current Thread [Next in Thread]