如何在emacs中的其他缓冲区中搜索字符串?

时间:2013-10-25 13:27:45

标签: search emacs buffer

我在一个缓冲区中列出了单词,在另一个缓冲区中列出了一堆文本行。我正在寻找一个缓冲区中的选定单词之类的东西必须在另一个缓冲区中突出显示。 emacs是否支持它?

1 个答案:

答案 0 :(得分:2)

试试org-mode。 您可以将链接[[file:yourFile::yourSearchExpression]]放入组织缓冲区。 如果单击该链接,将搜索访问yourFile的缓冲区yourSearchExpression(正则表达式)。

我使用下面的扩展名。使用此扩展程序,您还可以编写[[buffer:yourBuffer::yourSearchExpression]]。这里,yourBuffer必须是缓冲区(不是文件)。 但是,您可以事先打开感兴趣的文件。 yourSearchExpression的第一个字符可以是运算符。 如果是+,则从当前点位置开始向前搜索yourBuffer。 如果是-,则从当前点位置开始向后搜索yourBuffer。如果没有给出运算符,则从缓冲区的开头开始向前搜索缓冲区。其他运营商可以轻松实施。

好处是您可以轻松地在组织文件中预定义搜索。您有一个文件,您可以在其中组织一个主题的搜索。您搜索的内容可以分布在多个缓冲区中。

如果您稍后需要再次搜索此类型,则会变得很方便。

我最近加入了高光照。因此,如果遇到问题。请给我一个便条。

;; stolen from isearch:
(defface search-highlight-face 
  '((((class color) (min-colors 88) (background light))
     ;; The background must not be too dark, for that means
     ;; the character is hard to see when the cursor is there.
     (:background "magenta3" :foreground "lightskyblue1"))
    (((class color) (min-colors 88) (background dark))
     (:background "palevioletred2" :foreground "brown4"))
    (((class color) (min-colors 16))
     (:background "magenta4" :foreground "cyan1"))
    (((class color) (min-colors 8))
     (:background "magenta4" :foreground "cyan1"))
    (t (:inverse-video t)))
  "Face for highlighting search matches."
  :group 'search-highlight)

(defface search-highlight-other-face 
  '((((class color) (min-colors 88) (background light))
     (:background "paleturquoise"))
    (((class color) (min-colors 88) (background dark))
     (:background "paleturquoise4"))
    (((class color) (min-colors 16))
     (:background "turquoise3"))
    (((class color) (min-colors 8))
     (:background "turquoise3"))
    (t (:underline t)))
  "Face for lazy highlighting of matches other than the current one."
  :group 'search-highlight)

(require 'cl) ;; for find
(defun search-highlight (se &rest opt)
  "Like the group of `search-forward' commands with highlighting of the matches.
Note, that this function should only be used in commands since it is directly visible.
The window of the current buffer must be alive and should be visible.
Options:

:back non-nil: search backward instead of forward

:re non-nil: SE is regular expression

:noerror non-nil: issue error when not found

:bound bound of search

:count search that many times (defaults to 1)

:face use this face to highlight

:others non-nil: highlight also other matches within visible area of buffer

:recenter recenter point in window vertically (before highlighting others)
"
  (interactive "sSearch expression:")
  (search-highlight-cleanup) ;; If anything went wrong prevously.
  (let* (ol
     (regexp? (when (plist-get opt :re) "-regexp"))
     (cmd (intern-soft (concat "search"
                   (if (plist-get opt :back) "-backward" "-forward")
                   regexp?
                   )))
     e)
    (when (funcall cmd se (plist-get opt :bound) (plist-get opt :noerror) (plist-get opt :count))
      (setq ol (make-overlay (match-beginning 0) (match-end 0)))
      (overlay-put ol 'face 'search-highlight-face))
    (when (plist-get opt :recenter) (recenter))
    (if (plist-get opt :others)
    (save-excursion
      (goto-char (window-start))
      (setq e (window-end nil t))
      (setq cmd (intern-soft (concat "search-forward" regexp?)))
      (while (funcall cmd se e t)
        (unless (and ol (= (match-beginning 0) (overlay-start ol)))
          (overlay-put (make-overlay (match-beginning 0) (match-end 0))
               'face 'search-highlight-other-face)))))
    (add-hook 'pre-command-hook 'search-highlight-cleanup t t)))

(defun search-highlight-cleanup ()
  "Remove highlights for search-highlight-mode."
  (interactive)
  (remove-hook 'pre-command-hook 'search-highlight-cleanup t)
  (remove-overlays 0 (buffer-size)
           'face 'search-highlight-face)
  (remove-overlays 0 (buffer-size)
           'face 'search-highlight-other-face))

(defun org-at-buffer ()
  "Check whether point is at [[buffer:BUFFER::SEARCH]].
BUFFER is just the name of an existing buffer.
You can make sure that the buffer exists by [[file:...]].
::RE is an optional regular expression.
The first character of SEARCH may be an operator:

+ Start at current point and search forward.

- Start at current point and search backward.

If the operator is missing the buffer is searched for RE starting at the beginning of BUFFER.
"
  (save-excursion
    (let ((pt (point))
      b e
      name
      re)
      (when (search-backward "[[buffer:" (line-beginning-position) 'noErr)
    (forward-char)
    (setq b (+ (point) (length "[buffer:")))
    (forward-sexp) ;; actual link
    (setq e (1- (point)))
    (when (looking-at "\\[") ;; optional description
      (forward-sexp))
    (when (>= (point) pt)
      (goto-char b)
      (if (search-forward "::" e 'noErr)
          (setq
           name (buffer-substring-no-properties b (match-beginning 0))
           re (buffer-substring-no-properties (match-end 0) e)
           )
        (setq name (buffer-substring-no-properties b e)))
      (switch-to-buffer-other-window name)
      (when re
        (if (and (> (length re) 0) (find (aref re 0) "+-"))
        (let ((op (aref re 0)))
          (setq re (substring re 1))
          (cond
           ((= op ?+)
            (when (= (point) (point-max))
              (goto-char (point-min)))
            (search-highlight re :re t :noerror t :others t :recenter t))
           ((= op ?-)
            (when (= (point) (point-min))
              (goto-char (point-max)))
            (search-highlight re :back t :re t :noerror t :others t :recenter t))
           (t
            (error "Unexpected op."))))
          (goto-char (point-min))
          (search-highlight re :re t :noerror t :others t :recenter t)))
      t)))))
(add-to-list 'org-open-at-point-functions 'org-at-buffer)