Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

Stefan Kleine Stegemann

Emacs: search for the selected text by Shift-clicking with the left mouse-button

First, we need a function that searches for the text that is currently selected. Add the following to your .emacs:

(defun mouse-search-forward (begin end)
  (interactive (list (point) (mark)))
  (let ((text (filter-buffer-substring begin end nil t)))
    (goto-char (max begin end))
    (let ((found-pos (search-forward text nil t)))
      (if (not found-pos)
          (progn
            (goto-char begin)
            (error "not found"))
          (progn
            (goto-char found-pos)
            (set-mark (- found-pos (length text))))))))


Now, we have to bind the function to the mouse-button. This is done by the following code, which you most likely also want to add to your .emacs:
(define-key global-map [(shift down-mouse-1)] nil)
(define-key global-map [(shift mouse-1)] 'mouse-search-forward)