Emacs quick find (Visual Studio style)

Elisp code to get the same "fast find" as in Visual Studio: When the cursor is on a word, press Ctrl-f3 to automatically search for occurences of this word in the same file (if a region is defined when you press Ctrl-f3, the search is for the content of the region instead). Then, press f3 or shift-f3 to search backward of forward:

;; Visual Studio style "fast find":
;; Ctrl-f3 to search for the word at the point (or for the content of the region if one is defined)
;; Ctrl-Shift-f3 to search backward.
;; once a search is initiated, use Ctrl-F3 or f3 to search forward,
;; Ctrl-Shift-f3 or Shift-f3 to search backward.
(defun visualstudio-find-word (forward)
  (or mark-active
      ; following code borrowed from function current-word in /usr/share/emacs/21.4/lisp/simple.el
        (let ((oldpoint (point)) (start (point)) (end (point)))
          (skip-syntax-backward "w_") (setq start (point))
          (goto-char oldpoint)
          (skip-syntax-forward "w_") (setq end (point))
          (if (and (eq start oldpoint) (eq end oldpoint))
              ;; Point is neither within nor adjacent to a word.
                ;; Look for preceding word in same line.
                (skip-syntax-backward "^w_"
                                      (save-excursion (beginning-of-line)
                (if (bolp)
                    ;; No preceding word in same line.
                    ;; Look for following word in same line.
                      (skip-syntax-forward "^w_"
                                           (save-excursion (end-of-line)
                      (setq start (point))
                      (skip-syntax-forward "w_")
                      (setq end (point)))
                  (setq end (point))
                  (skip-syntax-backward "w_")
                  (setq start (point)))
                (goto-char start)
                (push-mark nil nil t)
                (goto-char end))
              (goto-char start)
              (push-mark nil nil t)
              (goto-char end))))))
    (let ((search (buffer-substring (region-beginning) (region-end))))
      (isearch-mode forward nil nil nil nil)
      (setq isearch-string search
            isearch-message search
            isearch-case-fold-search nil))))

(defun visualstudio-find-next-word () (interactive)
  (visualstudio-find-word t))
(defun visualstudio-find-previous-word () (interactive)
  (visualstudio-find-word nil))

(global-set-key [f3] 'isearch-forward)
(global-set-key [S-f3] 'isearch-backward)
(global-set-key [C-f3] 'visualstudio-find-next-word)
(global-set-key [C-S-f3] 'visualstudio-find-previous-word)
(define-key isearch-mode-map [f3] 'isearch-repeat-forward)
(define-key isearch-mode-map [S-f3] 'isearch-repeat-backward)
(define-key isearch-mode-map [C-f3] 'isearch-repeat-forward)
(define-key isearch-mode-map [C-S-f3] 'isearch-repeat-backward)