【问题标题】:emacs isearch-forward default search stringemacs isearch-forward 默认搜索字符串
【发布时间】:2013-09-06 03:18:07
【问题描述】:

我有一个关于 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]
你能帮助我吗?谢谢!

【问题讨论】:

  • 你试过C-s C-w吗?这应该会自动突出显示当前单词(或多个单词以获得更多 C-w)并在您使用 C-s (C-r) 向前(或向后)移动时突出显示相同的搜索字符串...
  • 我同意 abiessu,但是如果您使用(thing-at-pt 'word) 获取单词(如果您仍需要手动删除属性,则可以使用bounds-of-thing-at-point)获取单词。
  • 那就是search-ring
  • @juanleon,你知道有没有“highlight-regexp-ring”之类的东西来存储突出显示的单词历史?
  • 我不这么认为(有regexp-search-ring,但这与突出显示无关)

标签: emacs elisp


【解决方案1】:

我认为你需要在 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)

【讨论】:

  • 但这会让你正常的C-s失败,你需要搜索空间区域或M-n/p搜索其他词
【解决方案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 一样,只是它会拾取光标处的整个单词,而不仅仅是从光标到单词结尾的文本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 2021-03-26
    相关资源
    最近更新 更多