emacs isearch-forward默认搜索字符串

时间:2013-09-06 03:18:08

标签: emacs elisp

我有一个关于Emacs Lisp的问题,我想实现这个功能:'在光标下突出显示一个单词,然后当我按下C-s C-s时,我可以跳转到下一个突出显示的单词'。
所以在我突出显示一个单词后,我希望 isearch-string 可以设置为与我高亮的单词相同,即默认**命令**的搜索字符串isearch-向前 isearch-backward 可以是我突出显示的单词 我的代码就像:

(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)))

但它总是提示:[没有以前的搜索字符串]
你能帮助我吗?谢谢!

2 个答案:

答案 0 :(得分:2)

我认为你需要为isearch-mode添加钩子,然后你的功能就可以了。

(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)

答案 1 :(得分:2)

这就是你要找的东西(对我来说不太清楚)?

(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)

这就是C-w做的事情,除了它在光标处拾取整个单词,而不仅仅是从光标到单词结尾的文本。