同步两个窗口 [英] Sync two windows

查看:131
本文介绍了同步两个窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码是部分的工作。
我正在尝试同步两个窗口,所以不管你在哪一个窗口将同步并开始相应的移动。
我看到的不一致是围绕页边界;如果您将光标移动到一个窗口中,直到您再次滚动到另一个页面,然后直接再次上升一行,您将注意到这两个窗口将不同步。我试着调试没有运气。不知道是什么导致这个奇怪的行为。



这是代码:

 (defun Xsync-window 
同步点位置当前框架中的其他窗口
只有当活动的文件中正好有两个窗口不计入minibuffer时,才起作用。
(交互式)
(when(=(count-windows'noMiniBuf)2)
(let((p(line-number-at-pos))
(start(line-number-at-pos (或display-start(window-start))))
(vscroll(window-vscroll)))
(other-window 1)
(goto-char(point-min))
(setq start(line-begin-position start))
(forward-line(1- p))
(set-window-start(selected-window)s​​tart)
(set-window-vscroll(selected-window)vscroll)
(other-window 1)
(除非display-start
(重新显示t))
)))

(define-minor-mode sync-window-mode
两个并排窗口中两个缓冲区的同步视图
:group Windows
:打开⇕
(除非(boundp'sync-window-mode-active)
(setq sync-window-mode-active nil))
-window-mode
(progn
(add-hook'post-command-hook'sync-window-wrapper'append t)
(add-to-list'window-scroll-functions 'sync-window-wrapper'
(Xsync-window)

(remove-hook'post-command-hook'sync-window-wrapper t)
(setq window -scroll-functions(删除'sync-window-wrapper window-scroll-functions))
))

(defun sync-window-wrapper(& optional window display-start)
这个包装器确保只有当活动窗口的缓冲区处于sync-window-mode状态时,才会从`post-command-hook'
触发'sync-window'。
(除非sync-window-mode-active
(setq sync-window-mode-active t)
(with-selected-window(或window(selected-window))
sync-window-mode
(Xsync-window display-sta rt)))
(setq sync-window-mode-active nil))


(defun sync-window-dual()
切换同步视图
(交互式)
(if(not(=(count-windows'noMiniBuf)2))
(错误限制为两个窗口)
(let((mode(if sync-window-mode 0 1)))
(同步窗口模式)
(with-selected-window(selected-窗口)
(其他窗口1)
(同步窗口模式)))))


解决方案

当光标在窗口外部结束时,Emacs将重新定位窗口。但是,在 之后发生 ,调用 post-command-hook



您在 post-command-hook 中调用(sit-for 0),窗口将被重新显示,您将获取 window-start 等的新值。


I have this code that is "partially" working. I am trying to sync two windows, so regardless which window you are in the other will sync and start moving accordingly. The inconsistency I am seeing are around page boundaries; if you move the cursor in one window all the way down till you scroll one more into the next page then directly go up again one line you will notice that both windows will go out of sync. I tried debugging this with no luck. Not sure what is causing this weird behavior.

Here is the code:

(defun Xsync-window (&optional display-start)
  "Synchronize point position other window in current frame.
Only works if there are exactly two windows in the active wrame not counting the minibuffer."
  (interactive)
  (when (= (count-windows 'noMiniBuf) 2)
    (let ((p (line-number-at-pos))
      (start (line-number-at-pos (or display-start (window-start))))
      (vscroll (window-vscroll)))
      (other-window 1)
      (goto-char (point-min))
      (setq start (line-beginning-position start))
      (forward-line (1- p))
      (set-window-start (selected-window) start)
      (set-window-vscroll (selected-window) vscroll)
      (other-window 1)
      (unless display-start
    (redisplay t))
      )))

(define-minor-mode sync-window-mode
  "Synchronized view of two buffers in two side-by-side windows."
  :group 'windows
  :lighter " ⇕"
  (unless (boundp 'sync-window-mode-active)
    (setq sync-window-mode-active nil))
  (if sync-window-mode
      (progn
        (add-hook 'post-command-hook 'sync-window-wrapper 'append t)
        (add-to-list 'window-scroll-functions 'sync-window-wrapper)
        (Xsync-window)
    )
    (remove-hook 'post-command-hook 'sync-window-wrapper t)
    (setq window-scroll-functions (remove 'sync-window-wrapper window-scroll-functions))
    ))

(defun sync-window-wrapper (&optional window display-start)
  "This wrapper makes sure that `sync-window' is fired from `post-command-hook'
only when the buffer of the active window is in `sync-window-mode'."
  (unless sync-window-mode-active
    (setq sync-window-mode-active t)
    (with-selected-window (or window (selected-window))
      (when sync-window-mode
        (Xsync-window display-start)))
    (setq sync-window-mode-active nil))
  )

(defun sync-window-dual ()
  "Toggle synchronized view of two buffers in two side-by-side windows simultaneously."
  (interactive)
  (if (not (= (count-windows 'noMiniBuf) 2))
      (error "restricted to two windows")
    (let ((mode (if sync-window-mode 0 1)))
      (sync-window-mode mode)
      (with-selected-window (selected-window)
        (other-window 1)
        (sync-window-mode mode)))))

解决方案

When the cursor ends up outside a window, Emacs will reposition the window. However, this occurs after the post-command-hook is called.

If you call (sit-for 0) in your post-command-hook, the window will be redisplayed, and you get the new value for window-start etc.

这篇关于同步两个窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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