【发布时间】:2014-02-27 22:15:29
【问题描述】:
我的 dotmacs 文件中有一个 sn-p 代码,它可以并排同步两个打开的缓冲区(感谢 Tobias),在主缓冲区中滚动将导致从属缓冲区相应地移动匹配相同的“行”光标在。
我一直在尝试修改代码,让一个缓冲区在获得焦点时充当主缓冲区,而另一个缓冲区则充当跟随者。基本上,无论滚动哪个缓冲区,我都希望两者同步。
不幸的是,由于主从同步的紧密程度,在两个缓冲区上应用 (Xsync-window) 失败。
代码如下:
(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)
)))
【问题讨论】: