help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: automatic search/search and replace for a marked string


From: Kada Situ
Subject: Re: automatic search/search and replace for a marked string
Date: Sun, 23 Aug 2009 03:16:40 -0700

I have used UltraEdit before I switched to emacs, there is a searching feature in UltraEdit that I want it so bad in emacs.
Basically, in UltraEdit, you highlight some text, and press F3, it will automatically do the search of the lighlighted
text for you. Similarly, if you do Shift+F3 it will do the highlight search backward.
I did a little isearch hacking, looks like I am able to port this feature to emacs. Well, most of it, except that you can't
continuously press F3 to do the incremental search.
So, with region search, you highlight the text first, then press F3, the highlighted text will fill in the isearch input. From
this point on, you can C-s or C-r. Just don't do more than one F3 in a row, or you will end up with everything between the
first searching result and the second result filled in your isearch input field.
I am no Lisp expert, so if anyone likes to implement the missing part or improve the code, feel free to do so :)

--
___
K.S


;; save the following code as region-search.el
;; to install it, put the following code in your .emacs file
;;          (eval-after-load "isearch" '(require 'region-search))
;;          (global-set-key [f3] 'region-search-forward)
;;          (global-set-key [(shift f3)] 'region-search-backward)

;; most of the code  borrowed from isearch.el
(defun isearch-update-string (string)
  "Pull STRING into search string."
  ;; Downcase the string if not supposed to case-fold yanked strings.
  (if (and isearch-case-fold-search
       (eq 'not-yanks search-upper-case))
      (setq string (downcase string)))
  (if isearch-regexp (setq string (regexp-quote string)))
  (setq isearch-string string
                isearch-message (mapconcat 'isearch-text-char-description
                                     string "")
    ;; Don't move cursor in reverse search.
    isearch-yank-flag t)
  (isearch-search-and-update))
(defun region-search-forward (beg end)
    (interactive "r")
    (region-search beg end t))
(defun region-search-backward (beg end)
    (interactive "r")
    (region-search beg end nil))

(defun region-search (beg end &optional direction)
    (setq deactivate-mark t)
    ;;(goto-char beg) ;;this would highlight the region as the first search result
    (setq isearch-forward direction
                isearch-regexp nil
                isearch-word t
                isearch-op-fun nil
                isearch-last-case-fold-search isearch-case-fold-search
                isearch-case-fold-search case-fold-search
                isearch-string (buffer-substring beg end)
                isearch-message ""
                isearch-cmds nil
                isearch-success t
                isearch-wrapped nil
                isearch-barrier (point)
                isearch-adjusted nil
                isearch-yank-flag nil
                isearch-error nil
                isearch-slow-terminal-mode (and (<= baud-rate search-slow-speed)
                                                                                (> (window-height)
                                                                                     (* 4
                                                                                            (abs search-slow-window-lines))))
                isearch-other-end nil
                isearch-small-window nil
                isearch-just-started t
                isearch-start-hscroll (window-hscroll)
                isearch-opoint (point)
                search-ring-yank-pointer nil
                isearch-opened-overlays nil
                isearch-input-method-function input-method-function
                isearch-input-method-local-p (local-variable-p 'input-method-function)
                regexp-search-ring-yank-pointer nil
                isearch-original-minibuffer-message-timeout minibuffer-message-timeout
                minibuffer-message-timeout nil)
    (looking-at "")
  (setq isearch-window-configuration
                (if isearch-slow-terminal-mode (current-window-configuration) nil))
      (let ((frame (window-frame (minibuffer-window))))
    (unless (memq (frame-live-p frame) '(nil t))
      (unless (frame-visible-p frame)
    (make-frame-visible frame))
      (if minibuffer-auto-raise
      (raise-frame frame))))
        (setq  isearch-mode " Isearch")  ;; forward? regexp?
        (force-mode-line-update)
        (isearch-push-state)
        (setq overriding-terminal-local-map isearch-mode-map)
        (run-hooks 'isearch-mode-hook)
        (isearch-update)
        (add-hook 'mouse-leave-buffer-hook 'isearch-done)
        (add-hook 'kbd-macro-termination-hook 'isearch-done)
        (isearch-update-string isearch-string))

(provide 'region-search)

reply via email to

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