Isearch区域是否有效的emacs

时间:2012-10-26 22:43:07

标签: emacs

有没有办法告诉isearch搜索活动区域,如果有的话? 否则会定期提示输入一个字符串。

编辑:27.10.12

我最终使用了这些功能:

(defun er/isearch-word-at-point ()
  (interactive)
  (call-interactively 'isearch-forward-regexp))

(defun er/isearch-yank-word-hook ()
  (when (equal this-command 'er/isearch-word-at-point)
    (let ((string (concat "\\<"
                          (buffer-substring-no-properties
                           (progn (skip-syntax-backward "w_") (point))
                           (progn (skip-syntax-forward "w_") (point)))
                          "\\>")))
      (if (and isearch-case-fold-search
               (eq 'not-yanks search-upper-case))
          (setq string (downcase string)))
      (setq isearch-string string
            isearch-message
            (concat isearch-message
                    (mapconcat 'isearch-text-char-description
                               string ""))
            isearch-yank-flag t)
      (isearch-search-and-update))))

(defun er/isearch-yank-region ()
  (interactive)
  (isearch-yank-internal (lambda () (mark))))

(define-key isearch-mode-map (kbd "C-r") 'er/isearch-yank-region)
(define-key isearch-mode-map (kbd "C-t") 'er/isearch-word-at-point)

第一个是我在网络上找到的一个函数,将光标下的单词标记为搜索项(类似于*和vim中的#)并直接跳到下一个出现,第二个是@Oleg Pavliv的答案。

编辑#2

实际上,为什么不将它们结合起来以获得超甜味?好的!

(defun er/isearch-word-or-region-at-point ()
  (interactive)
  (if (region-active-p)
      (isearch-yank-internal (lambda () (mark)))
  (call-interactively 'isearch-forward-regexp)))

(defun er/isearch-yank-word-hook ()
  (when (equal this-command 'er/isearch-word-or-region-at-point)
    (let ((string (concat "\\<"
                          (buffer-substring-no-properties
                           (progn (skip-syntax-backward "w_") (point))
                           (progn (skip-syntax-forward "w_") (point)))
                          "\\>")))
      (if (and isearch-case-fold-search
               (eq 'not-yanks search-upper-case))
          (setq string (downcase string)))
      (setq isearch-string string
            isearch-message
            (concat isearch-message
                    (mapconcat 'isearch-text-char-description
                               string ""))
            isearch-yank-flag t)
      (isearch-search-and-update))))
(add-hook 'isearch-mode-hook 'er/isearch-yank-word-hook)
(define-key isearch-mode-map (kbd "C-r") 'er/isearch-word-or-region-at-point)

2 个答案:

答案 0 :(得分:2)

好问题。

似乎在Emacs中没有这种可能性。您可以自己实施

(defun isearch-yank-region ()
  (interactive)
  (isearch-yank-internal (lambda () (mark))))


(define-key isearch-mode-map "\C-r" 'isearch-yank-region)

现在您选择一个区域,调用增量搜索C-s并抽取区域C-r。然后,您可以继续增量搜索C-s

答案 1 :(得分:1)

实现目标的一种简单方法是在搜索之前将当前区域保存到kill-ring,然后将其作为搜索词拖入迷你缓冲区。您可以通过执行以下键序列来完成此操作:

M-w C-s M-y

转换为:

M-w :保存区域,就好像被杀死一样,但没有杀死它。

C-s :进行增量搜索。

M-y :猛拉最后一串被杀文本(或者更确切地说,之前由 M-w 保存的内容)。

它有点像手指舞,但是如果你经常使用它会成为第二天性。这个通用解决方案的好处是它只使用Emacs内置的基本功能。没有必要建议任何功能。