gpt4 book ai didi

emacs isearch-forward 默认搜索字符串

转载 作者:行者123 更新时间:2023-12-04 02:46:55 25 4
gpt4 key购买 nike

我有一个关于 Emacs Lisp 的问题,我想实现这个功能:“突出显示光标下的单词,然后当我按下 C-s C-s 时,我可以跳转到下一个突出显示的单词”。
所以在我高亮一个词后,我希望isearch-string可以设置为与我高亮的词相同,即default ** search string for command **isearch- forwardisearch-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]
你能帮助我吗?谢谢!

最佳答案

我想你需要给 isearch-mode 添加钩子(Hook),然后你的函数才能工作。

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

关于emacs isearch-forward 默认搜索字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18649336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com