2013-02-08 8 views
5

私が増分検索をemacsで使用するとき、私は完全一致のどこにいるのかわかりません。 Chromeブラウザでは、「2/30」を使用している場所が表示されます。 Emacsでどうしたらいいですか?少なくとも、残念ながらあなたがにしている試合を追跡するために十分にスマートでない場合であっても(試合の合計数を確認するためにisearchingながら"1 of n"がEmacs検索の結果

答えて

6

これを初めて実装しようとしています。

isearchが実装している遅延強調表示を使用して、バッファ全体(表示部分だけでなく)を強調表示するようにします。これにより、大きなバッファで検索が遅くなる可能性があります。次に、強調表示された検索結果に対する現在の位置(合計)を含むように表示を更新します。

これは、それが検索され強調表示されているバッファ全体に依存するという欠点があります。また、次の一致までC-sと表示されたときに、ハイライトがはっきりしていても表示が(0 of 1)に変わることがあります。

しかし、それは合理的な最初のカットと思われる。

大きなカット/ペーストの準備:

(require 'isearch) 
(defun lazy-highlight-cleanup (&optional force) 
    "Stop lazy highlighting and remove extra highlighting from current buffer. 
FORCE non-nil means do it whether or not `lazy-highlight-cleanup' 
is nil. This function is called when exiting an incremental search if 
`lazy-highlight-cleanup' is non-nil." 
    (interactive '(t)) 
    (if (or force lazy-highlight-cleanup) 
     (while isearch-lazy-highlight-overlays 
     (delete-overlay (car isearch-lazy-highlight-overlays)) 
     (setq isearch-lazy-highlight-overlays 
       (cdr isearch-lazy-highlight-overlays)))) 
    (when isearch-lazy-highlight-timer 
    (cancel-timer isearch-lazy-highlight-timer) 
    (setq isearch-message-suffix-add "") 
    (setq isearch-lazy-highlight-timer nil))) 

(defun isearch-lazy-highlight-search() 
    "Search ahead for the next or previous match, for lazy highlighting. 
Attempt to do the search exactly the way the pending Isearch would." 
    (condition-case nil 
     (let ((case-fold-search isearch-lazy-highlight-case-fold-search) 
      (isearch-regexp isearch-lazy-highlight-regexp) 
      (search-spaces-regexp isearch-lazy-highlight-space-regexp) 
      (isearch-word isearch-lazy-highlight-word) 
      (search-invisible nil) ; don't match invisible text 
      (retry t) 
      (success nil) 
      (isearch-forward isearch-lazy-highlight-forward) 
      (bound (if isearch-lazy-highlight-forward 
         (min (or isearch-lazy-highlight-end-limit (point-max)) 
          (if isearch-lazy-highlight-wrapped 
           isearch-lazy-highlight-start 
           (isearch-window-end))) 
        (max (or isearch-lazy-highlight-start-limit (point-min)) 
          (if isearch-lazy-highlight-wrapped 
           isearch-lazy-highlight-end 
          (isearch-window-start)))))) 
     ;; Use a loop like in `isearch-search'. 
     (while retry 
      (setq success (isearch-search-string 
         isearch-lazy-highlight-last-string bound t)) 

      ;; Clear RETRY unless the search predicate says 
      ;; to skip this search hit. 
      (if (or (not success) 
        (= (point) bound) ; like (bobp) (eobp) in `isearch-search'. 
        (= (match-beginning 0) (match-end 0)) 
        (funcall isearch-filter-predicate 
          (match-beginning 0) (match-end 0))) 
       (setq retry nil))) 
     success) 
    (error nil))) 

(defun isearch-find-current-overlay() 
    (let ((total 0) 
     (count 1) 
     (olist isearch-lazy-highlight-overlays)) 
    (while olist 
     (setq total (1+ total)) 
     (if (< (overlay-end (car olist)) (point)) 
      (setq count (1+ count))) 
     (setq olist 
      (cdr olist))) 
    (cons count total))) 

(add-hook 'isearch-update-post-hook 'isearch-count-message) 

(defun isearch-count-message() 
    (let ((counts (isearch-find-current-overlay))) 
    (setq isearch-message-suffix-add (format " (%d of %d)" (car counts) (cdr counts))))) 

(defun isearch-window-start() 
    "force highlight entire buffer" 
    (point-min)) 

(defun isearch-window-end() 
    "force highlight entire buffer" 
    (point-max)) 

(defun isearch-lazy-highlight-update() 
    "Update highlighting of other matches for current search." 
    (let ((max lazy-highlight-max-at-a-time) 
     (looping t) 
     nomore) 
    (with-local-quit 
     (save-selected-window 
     (if (and (window-live-p isearch-lazy-highlight-window) 
       (not (eq (selected-window) isearch-lazy-highlight-window))) 
      (select-window isearch-lazy-highlight-window)) 
     (save-excursion 
      (save-match-data 
      (goto-char (if isearch-lazy-highlight-forward 
          isearch-lazy-highlight-end 
         isearch-lazy-highlight-start)) 
      (while looping 
       (let ((found (isearch-lazy-highlight-search))) 
       (when max 
        (setq max (1- max)) 
        (if (<= max 0) 
         (setq looping nil))) 
       (if found 
        (let ((mb (match-beginning 0)) 
          (me (match-end 0))) 
         (if (= mb me) ;zero-length match 
          (if isearch-lazy-highlight-forward 
           (if (= mb (if isearch-lazy-highlight-wrapped 
              isearch-lazy-highlight-start 
              (isearch-window-end))) 
            (setq found nil) 
           (forward-char 1)) 
          (if (= mb (if isearch-lazy-highlight-wrapped 
              isearch-lazy-highlight-end 
             (isearch-window-start))) 
           (setq found nil) 
           (forward-char -1))) 

         ;; non-zero-length match 
         (let ((ov (make-overlay mb me))) 
          (push ov isearch-lazy-highlight-overlays) 
          ;; 1000 is higher than ediff's 100+, 
          ;; but lower than isearch main overlay's 1001 
          (overlay-put ov 'priority 1000) 
          (overlay-put ov 'face lazy-highlight-face) 
          (overlay-put ov 'window (selected-window)))) 
         (if isearch-lazy-highlight-forward 
          (setq isearch-lazy-highlight-end (point)) 
         (setq isearch-lazy-highlight-start (point))))) 

       ;; not found or zero-length match at the search bound 
       (if (not found) 
        (if isearch-lazy-highlight-wrapped 
         (setq looping nil 
           nomore t) 
         (setq isearch-lazy-highlight-wrapped t) 
         (if isearch-lazy-highlight-forward 
          (progn 
          (setq isearch-lazy-highlight-end (isearch-window-start)) 
          (goto-char (max (or isearch-lazy-highlight-start-limit (point-min)) 
              (isearch-window-start)))) 
         (setq isearch-lazy-highlight-start (isearch-window-end)) 
         (goto-char (min (or isearch-lazy-highlight-end-limit (point-max)) 
             (isearch-window-end)))))))) 
      (unless nomore 
       (setq isearch-lazy-highlight-timer 
        (run-at-time lazy-highlight-interval nil 
           'isearch-lazy-highlight-update))))))))) 
+0

ああ、それはかなりきちんとしています。 – phils

+0

isearch-count-message()の '[re] -search- [forward | backward]'をなぜ呼び出さないのですか?このためにオーバーレイをスキャンする必要はありません。 –

+0

'isearch-count-message'が呼び出された時点で、既にバッファ全体で検索が行われているため、検索をもう一度呼び出すと、処理が大幅に遅くなる可能性があります。オーバーレイが存在し、カウント。 検索された語句が頻繁に出現する場合を考えてください。次の出現箇所に50回ジャンプすると、最初から50回目までバッファーをスキャンする必要があります。 '1 + 2 + 3 + ... + 50 = 1275' - 多くの検索で、あなたが50回目に出会ったと判断します。 –

1

ボックスのうち、あなたは oを女史を使用することができます元のバッファ)。

5

私はsawこの他の日:

https://github.com/syohex/emacs-anzu

anzu.elがanzu.vimのEmacsのポートです。 anzu.elはマイナーモードを提供し、現在のマッチとトータルマッチ情報をさまざまな検索モードでモードラインに表示します。

githubページのスクリーンショットは、その機能のgifアニメーションです。

0

emacsのinitファイルに以下を追加してください。カウントマッチの使用にもかかわらず、それは私のラップトップで非常に速く動作します。私は非常に大きなファイルでそれを試していません。

(defun my-isearch-update-post-hook() 
    (let (suffix num-before num-after num-total) 
    (setq num-before (count-matches isearch-string (point-min) (point))) 
    (setq num-after (count-matches isearch-string (point) (point-max))) 
    (setq num-total (+ num-before num-after)) 
    (setq suffix (if (= num-total 0) 
        "" 
        (format " [%d of %d]" num-before num-total))) 
    (setq isearch-message-suffix-add suffix) 
    (isearch-message))) 

(add-hook 'isearch-update-post-hook 'my-isearch-update-post-hook) 
関連する問題