3

我有一个关于 Emacs Lisp 的问题,我想实现这个功能:'突出显示光标下的一个单词,然后当我按下 Cs Cs 时,我可以跳转到下一个突出显示的单词'。
所以在我高亮一个词之后,我希望isearch-string可以设置为和我高亮的词一样,即命令**isearch-forward或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)))

但它总是提示:[No previous search string]
你能帮帮我吗?谢谢!

4

2 回答 2

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)
于 2013-09-06T06:27:50.263 回答
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除了它会在光标处拾取整个单词,而不仅仅是从光标到单词结尾的文本,它会做什么。

于 2013-09-06T14:52:59.137 回答