Skip to content

Instantly share code, notes, and snippets.

@jorgenschaefer
Created October 23, 2012 11:34
Show Gist options
  • Save jorgenschaefer/3938300 to your computer and use it in GitHub Desktop.
Save jorgenschaefer/3938300 to your computer and use it in GitHub Desktop.
(defun search-symbol-at-point ()
"Search and move point to the next occurrence of the symbol at point."
(interactive)
;; This code is a bit more complex than it should be, mainly to get
;; the "intuitive" result in all cases. `thing-at-point' does not,
;; for example.
(let (beg end symbol found)
(setq start (point))
(save-excursion
(cond
;; On a symbol, even on the first char, use the word *after*
;; point, so foo _|_bar finds bar
((looking-at "\\sw\\|\\s_")
(forward-symbol 1)
(setq end (point))
(forward-symbol -1)
(setq start (point))
(goto-char end))
;; Else, use the symbol *before* point, so foo_|_ bar finds foo.
(t
(forward-symbol -1)
(setq start (point))
(forward-symbol 1)
(setq end (point))))
(setq symbol (buffer-substring-no-properties start end))
(if (search-forward symbol nil t)
(setq found (point))
(goto-char (point-min))
(if (search-forward symbol start t)
(setq found (point))
(error "No other occurrences found"))))
(when found
(goto-char found))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment