将两个窗口的emacs同步在一起 [英] Sync two windows of emacs together

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

问题描述

我的dotemacs文件中有一段代码段,可以并行同步两个打开的缓冲区(感谢Tobias),在主缓冲区中滚动将导致从属缓冲区相应地移动到与光标相同的行 at。



我一直在尝试修改代码,并有一个缓冲区作为主机,当它有焦点,而另一个作为从属在铅之后。基本上,我希望两者同步,无论哪个缓冲区都在滚动。



不幸的是,在两个缓冲区上应用(Xsync-window)都失败了,因为同步了主和奴隶在一起。



这是代码:

 (defun Xsync-window(& optional display-start)
在当前帧中同步点位置其他窗口
只有当活动的wrame中只有两个窗口不在minibuffer中时,才有效
(interactive)
(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 -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-w indow 1)
(除非display-start
(重新显示t))
)))

(define-minor-mode sync-window-mode
两个并排窗口中两个缓冲区的同步视图
:group'windows
:打开⇕
(如果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'窗口包装窗口滚动功能))
))

(defun sync-window-wrapper(&可选窗口显示开始)
这个包装器确保sync-window仅在活动窗口的缓冲区处于sync-window-mode状态时从`post-command-hook'
触发。
(with-selected-window窗口(选择窗口))
(当sync-window-mode
(Xsy nc-window display-start)
))


解决方案

这应该是诀窍:

 (define-minor-mode sync-window-mode 
同步
:group'windows
:light⇕
(除非(boundp'sync-window-mode-active)
(setq sync-window-mode-active nil))
(如果sync-window-mode
(progn
(add-hook'post-command-hook'sync-window-包装器'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-窗口包装器(&可选窗口显示开始)
这个包装器确保`post-command-hook'
只有当缓冲区
(除非sync-window-mode-active
(setq sync-window-mode-active t)
(with-window-mode)选择窗口(或窗口(选择窗口))
(当sync-window-mode
(Xsync-window display-start)))
(setq sync-window-mode-active nil)))

这基本上是通过同步窗口模式激活的,无论Xsync-windows是已经工作。



您也可以同时在两个窗口中切换模式:

 (defun sync-window-dual()
同时切换两个并排窗口中两个缓冲区的同步视图
(interactive)
(if(not (=(count-windows'noMiniBuf)2))
(错误限制到两个窗口)
(let((mode(如果sync-window-mode 0 1)))
(同步窗口模式)
(with-selected-window(selected-window)
(other-window 1)
(sync-window-mode mode)))))


I have a snippet of code in my dotemacs file that would sync two opened buffers side by side (Thanks to Tobias), scrolling in the master buffer will result in moving the slave buffer accordingly matching the same "line" the cursor is at.

I have been trying to modify the code and have one buffer act as master when it has the focus and the other act as a slave following lead. Basically, I want then both to sync up no matter which buffer is scrolling.

Unfortunately, Applying (Xsync-window) on both buffers failed because of how tightly synced the master and slave together.

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 " ⇕"
  (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'."
  (with-selected-window (or window (selected-window))
    (when sync-window-mode
      (Xsync-window display-start)
      )))

解决方案

This should do the trick:

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

This basically guards via sync-window-mode-active, whether Xsync-windows is already working.

You can also toggle the mode in two windows simultaneously with:

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

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

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