0

I would like to search the current buffer for particular words. To make a function that takes the word at point and searches for that word in the current buffer. Hopefully with match word highlighting as when using isearch-forward.

Have started with the following, but I get the isearch-forward in the minibuffer as normal instead. What can I do?

(defun gala-search-forward ()
  "TODO"
  (interactive)

  (let* ( (bounds (bounds-of-thing-at-point 'word))
          (word   (downcase
                   (buffer-substring (car bounds) (cdr bounds)))))

    (isearch-forward word)))
5
  • 1
    isearch-forward's first parameter is the prefix argument (C-u), not a string to search for. I think you want something like search-forward instead, but it's not clear from your question precisely what you mean by "does a search". Aug 27, 2022 at 10:17
  • 1
    Not germane to this question but I have seen variants on this code several times now and wonder why you don't just (let ((word (downcase (thing-at-point 'word t))))) and lose the bounds. Aug 27, 2022 at 12:02
  • Thusly I can do even better with (interactive (list (downcase (thing-at-point 'word t)))). Right?
    – Dilna
    Aug 27, 2022 at 12:31
  • 1
    Sure: if you make word the argument of gala-search-forward: (defun gala-search-forward (word)...) Aug 27, 2022 at 13:07
  • 1
    @PhilHudson: Please provide your comment as an answer (it's the answer). Or OP: consider deleting the question, if you don't think it and the answer will help others.
    – Drew
    Aug 27, 2022 at 16:12

1 Answer 1

2

Use isearch-forward, but instead of typing letters to search for, just hit C-w to "yank next word or character in buffer onto the end of the search string, and search for it." (See the doc string for isearch-forward.)

Or maybe I'm misunderstanding your question?

1
  • You understood well.
    – Dilna
    Aug 28, 2022 at 4:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.