3

I have a question about Emacs Lisp, I want to achieve this functionality: ‘highlight a word under cursor, then when I push C-s C-s, I can jump to the next highlighted word’.
So after I highlight a word, I hope the isearch-string can be set as the same as the word I have high lightened, i.e. the default ** search string for command **isearch-forward or isearch-backward can be my highlighted word.

My code is like:

(defun highlight-current-word()  
  "highlight the word under cursor"
  (interactive)
  (let (head-point tail-point word) 
    (skip-chars-forward "-_A-Za-z0-9")
    (setq tail-point (point))   
    (skip-chars-backward "-_A-Za-z0-9")
    (setq head-point (point))
    (setq word (buffer-substring-no-properties head-point tail-point))
    (setq isearch-string word)      ; no use
    (isearch-search-and-update) ; no use
    (highlight-regexp word 'hi-yellow)))

But it always prompts: [No previous search string]
Can you help me? Thank you!

7
  • 1
    Have you tried C-s C-w? This should auto-highlight the current word (or multiple words for more C-w) and highlight the same search string as you move forward (or backward) with C-s (C-r)... Commented Sep 6, 2013 at 3:22
  • 1
    I concur with abiessu, but you can also simply your own code if you obtain the word with (thing-at-pt 'word) (or bounds-of-thing-at-point if you still need to remove properties manually). Commented Sep 6, 2013 at 4:31
  • 1
    That would be search-ring Commented Sep 6, 2013 at 6:39
  • 1
    @juanleon, do you know is there any 'highlight-regexp-ring' kind of thing to store highlighted word history? Commented Sep 6, 2013 at 7:13
  • 1
    I don't think so (there is regexp-search-ring, but that has nothing to do with highlighting) Commented Sep 6, 2013 at 8:14

2 Answers 2

3

I think you need add hook to isearch-mode, then your function will be work.

(defun highlight-current-word()
  "highlight the word under cursor"
  (interactive)
  (let (head-point tail-point word)
    (skip-chars-forward "-_A-Za-z0-9")
    (setq tail-point (point))
    (skip-chars-backward "-_A-Za-z0-9")
    (setq head-point (point))
    (setq word (buffer-substring-no-properties head-point tail-point))
    (setq isearch-string word)
    (isearch-search-and-update)))

(add-hook 'isearch-mode-hook 'highlight-current-word)
Sign up to request clarification or add additional context in comments.

1 Comment

but this makes your normal C-s fail, you need search on space area or M-n/p to search other words
3

Is this all you are looking for (not too clear to me)?

(defun foo ()
  (interactive)
  (skip-chars-backward "-_A-Za-z0-9")
  (isearch-yank-internal (lambda () (forward-word 1) (point))))

(define-key isearch-mode-map (kbd "C-o") 'foo)

That does what C-w does, except that it picks up the whole word at the cursor, not just the text from the cursor to word end.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.