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

しかし、常にプロンプ​​トが表示されます: [以前の検索文字列はありません]
助けてもらえますか? ありがとうございました!

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 に答える