“1”的n“ Emacs搜索结果 [英] "1 of n" result for Emacs search

查看:105
本文介绍了“1”的n“ Emacs搜索结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在emacs中使用增量搜索时,我无法知道我在整个比赛中的位置。在Chrome浏览器中,它使用2 of 30表示位置。如何在Emacs中执行此操作?

解决方案

这是我第一次尝试实现这个。



它使用 isearch 实现的懒惰突出显示,并强制突出显示覆盖整个缓冲区(而不仅仅是可见部分) - 这可以减慢搜索大型缓冲区。然后,它更新显示以包括相对于突出显示的搜索结果的当前位置(总计)。



这有一个缺点,它取决于正在搜索的整个缓冲区并强调。而且,有时候,当您将 Cs 下一场比赛时,即使亮点仍然存在,显示更改为(0 of 1) / p>

但是,这似乎是一个合理的第一次削减。



准备大切割/粘贴:

 (require'isearch)
(defun lazy-highlight-cleanup(& optional force)
停止懒惰突出显示并从当前缓冲区中删除额外的突出显示
FORCE非nil表示无论lazy-highlight-cleanup
是否为否,退出时调用此函数
(interactive(t))
(if(or force lazy-highlight-cleanup))$ b $如果
`lazy-highlight-cleanup'不为零,则增量搜索b(while isearch-lazy-highlight-overlays
(delete-overlay(car isearch-lazy-highlight-overlays))
(setq isearch-lazy-highlight-overlays
(cdr isearch- lazy-highlight-overlays))))
(何时是earch-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()
搜索下一个或上一个匹配,为懒惰突出显示。
尝试完全按照待处理的Isearch的方式进行搜索。
(condition-case nil
(let((case-fold-search isearch-lazy-highlight-case-fold-搜索)
(isearch-regexp isearch-lazy-highlight-regexp)
(search-spaces-regexp isearch-lazy-highlight-space-regexp)
(isearch-word isearch-lazy-高亮词)
(搜索不可见的零);不匹配不可见的文本
(retry t)
(success nil)
(isearch-forward isearch-lazy-highlight (如果isearch-lazy-highlight-forward
(min(或isearch-lazy-highlight-end-limit(point-max))
-highlight-wrapped
isearch-lazy-highlight-start
(isearch-window-end)))
(max(或isearch-lazy-highlight-start-limit(point-min) )
(如果isearch-lazy-highlight-wrapped
isearch-lazy-highlight-end
(isearch-window-start))))))
;;使用isearch-search中的循环。
(同时重试
(setq success(isearch-search-string
isearch-lazy-highlight-last-string bound t))

;;清除RETRY,除非搜索谓词说
;;跳过这个搜索命中
(if(或(not success))
(=(point)bound); like(bobp)(eobp) -search'
(=(match-beginning 0)(match-end 0))
(funcall isearch-filter-predicate
(match-beginning 0)(match-end 0) )
(setq retry nil)))
成功)
(error nil))

(defun isearch-find-current-overlay()
(let((total 0)
(count 1)
(olist isearch-lazy-highlight-overlays))
(while olist
(setq 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((counting(isearch-find-current-overlay)))
(setq isearch-message-suffix-add %d)(车数)(cdr计数)))))

(defun isearch-window-start()
强制整个缓冲区
(point- min))

(defun isearch-window-end()
强制整个缓冲区
(point-max))

(defun
(let((max lazy-highlight-max-at-a-time))
(循环t)
nomore)
(with-local-quit
(save-selected-window
(if(and(window-live-p isearch-lazy-highlight-window )
(不(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-高亮起始))
(while循环
(let((found(isearch-lazy-highlight-search)))
(当最大
(setq max(1- max )
(if(< = max 0)
(setq looping nil)))
(如果找到
(let((mb(match-begin 0))
(我(match-end 0)))
(if(= mb me);零长度匹配
(如果isearch-lazy-highlight-forward
(if(= mb(如果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)))

;;非零长度匹配
(let((ov(make-overlay mb me)))
(push ov isearch-lazy-highlight-overlays)
;; 1000比ediff 100+,
;;但低于搜索主叠加的1001
(覆盖放置ov'优先级1000)
(覆盖放置ov'脸懒 - 高亮脸)
(overlay-put ov'window(selected-window))))​​
(如果isearch-lazy-highlight-forward
(setq isearch-lazy-highlight-end(point))
setq isearch-lazy-highlight-start(point)))))

;;没有找到或零长度匹配搜索绑定
(if(not found)
(if isearch-lazy-highlight-wrapped
(setq looping nil
nomore t)
(setq isearch-lazy-highlight-wrapped t)
(如果isearch-lazy-highlight-forward
(progn
(setq isearch-lazy-highlight-end(isearch-window -start))
(goto-char(max(或isearch-lazy-highlight-start-limit(point-min))
(isearch-window-start))))
setq isearch-lazy-highlight-start(isearch-window-end))
(goto-char(min(或isearch-lazy-highlight-end-limit(point-max))
(isearch- window-end)))))))))
(除非nomore
(setq isearch-lazy-hi ghlight-timer
(run-at-time lazy-highlight-interval nil
'isearch-lazy-highlight-update))))))))))


When I use incremental search in emacs I can't know where I am in whole matches. In Chrome browser it says location using "2 of 30". How can I do that in Emacs?

解决方案

Here's my first attempt to implement this.

It uses the lazy highlighting that isearch implements, and forces the highlighting to cover the entire buffer (not just the visible portions) - which can slow down the search on large buffers. It then updates the display to include a current position (of total) relative to the highlighted search results.

This has the drawback that it is dependent on the entire buffer being searched and highlighted. And, sometimes when you C-s to the next match, the display changes to (0 of 1) even though the highlights are clearly still present.

But, it seems to be a reasonable first cut.

Prepare for big cut/paste:

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

这篇关于“1”的n“ Emacs搜索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆