2013-10-25 68 views
0

我正在使用Emacs 24.2激活了包线。如何在窗口中显示整行?

当我读到日志包含类似信息的各种模拟的文件:“错误:...一些消息......”,我执行增量搜索:CS错误RET,铯,铯...

我觉得很讨厌,搜索(字错误)的结果突出显示在屏幕底部显示的,和所有的附加包线不能被看到:

enter image description here

我会喜欢添加修改,以确保整行文本将显示在缓冲区中,如下所示:

enter image description here

我发现this question关于搜索结果的重新定心。看起来我可以使用相同的defadvice语句作为搜索函数,但是不需要重新定位该行,我只需要按屏幕向下滚动屏幕上的部分即可。

如何做到这一点?

回答

0

打了一下,与根据@ juanleon的建议代码后,我结束了这一点:

;; Execute after each update in isearch-mode 
(setq isearch-update-post-hook 'show-whole-line) 


(defun show-whole-line() 
    "Scroll such that the whole line (which contains the point) will be visible." 

    ;; If it is the top part which is truncated 
    (if (not (pos-visible-in-window-p (line-beginning-position))) 
     (let 
     ((amount 
     ;; the required number of lines to scroll 
     (ceiling (/ 
       (- (window-start) 
      (line-beginning-position)) 
       (float (window-body-width)))))) 
    ;; don't scroll at all if the search result will be scrolled out 
    (if (< amount (/ 
       (- (window-end) 
       (point)) 
       (float (window-body-width)))) 
     (scroll-down amount))) 

    ;; Else 
    (if (not (pos-visible-in-window-p (line-end-position))) 
    (let 
     ((amount 
      (min 
      ;; the required number of lines to scroll 
      (ceiling (/ 
      (- 
       (line-end-position) 
       (window-end (selected-window) t)) 
      (float (window-body-width)))) 
      ;; however not to scroll out the first line 
      (/ (- (line-beginning-position) (window-start)) (window-body-width))))) 
     (scroll-up amount))))) 

一些解释:

  • 设置对于isearch-forward,是不够的 - 当您向搜索字符串添加字符时,不会再次调用此函数。在对isearch.el.gz软件包进行了快速审查之后,我决定向isearch-update函数提出建议。这也消除了为isearch-repeat-forward等添加单独建议的需要。稍后我注意到在isearch-update中存在预定义的挂钩,因此在此处不需要defadvice
  • show-whole-line函数检查当前行的开始是否可见。如果不是,则向下滚动以显示该行的开始,除非该滚动会导致隐藏搜索匹配本身。
  • 如果行的开头是可见的,则show-whole-line检查行的结尾是否也是可见的。如果不是,则滚动显示该行的结尾,除非该滚动会导致隐藏行的开头。我更喜欢能够看到该行的开头。

这个函数和钩子工作得很好,但有一个恼人的事情:当您最初按C-s(在键入任何搜索字符串之前)时调用该函数。这意味着如果该点位于窗口开始或结束的一条线上,则简单调用C-s将导致以某种方式滚动。

虽然不是关键,但我很乐意听到有关如何消除上述副作用的建议。

1

您可以使用您引用的问题的解决方案,但这个高度未经测试的函数变化recenter-top-bottom

(defun scroll-if-truncated() 
    (scroll-up 
    (/ (- (save-excursion 
      (end-of-line) (point)) 
     (save-excursion 
      (beginning-of-line) (point))) 
     (window-body-width)))) 
+0

感谢您的指导! – Vasiliy

0

你应该能够使用变量scroll-conservativelyscroll-margin,特别是后者得到你想要的行为。