在两个打开的缓冲区中并排镜像文件中的位置 [英] Mirroring location in file in two opened buffers side by side

查看:137
本文介绍了在两个打开的缓冲区中并排镜像文件中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在emacs中找到一个包/函数,它将两个文件并排打开,以便在相同的行位置镜像无论缓冲区移动的移动。
意思是,对于两个缓冲区并排打开,在其中一个缓冲区中移动(向上/向下翻页,移动光标等)将在另一个缓冲区中具有相同的移动。



更具体地说,当打开一个缓冲区(和激活这个模式)时,打开的缓冲区应该已经在另一个缓冲区窗口中已经打开的缓冲区的行位置。

解决方案

您可以尝试 scroll-all-mode 。这将切换一帧的所有窗口的平行滚动。



使用鼠标滚动条滚动条不适合我。但是所有使用键滚动(例如 Pg-Down Pg-Down 和光标移动)可以正常运行。



编辑:
您还可以尝试以下代码。它只适用于正好两个不计算minibuff的窗口的框架。您必须首先打开文件,并确保它们并排显示在两个窗口中。然后通过激活 sync-window-mode 来定义主窗口。确保两个窗口的换行都是关闭的。



编辑:修复了一个问题(有时一个按钮需要两次同步)。
解决方案:还钩到 window-scroll-functions 。在 post-command-hook 右边 window-start 位置不知道,因为重新显示还没有运行 window-start 已知的第一点是 window-scroll-functions

 (defun sync-window(& optional display-start)
同步点位置当前帧中的其他窗口

(interactive)
(when(=(count-windows'noMiniBuf))2)
(let(( p(point))
(start(或display-start(window-start)))
(vscroll(window-vscroll)))
(other-window 1)
(goto-char(min(max p(point-min))(point-max)))
(set-window-start(selected-window)s​​tart)
(set-window-vscroll选择窗口)vscroll)
(其他窗口1)
)))

(define-minor-mode sync-window-mode
同步视图两个并排窗口中的两个缓冲区。
:group'windows
:light⇕
(如果sync-window-mode
(progn
(add-hook'post-command-hook'sync-window-wrapper'append t)
(add-to-list'window-scroll-functions'sync-window-wrapper)
(sync-窗口))
(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)
这个包装器确保`sync-window' '
(with-selected-window(或window(selected-window))$ b只有当活动窗口的缓冲区处于sync-window-mode $ b(当sync-window-mode
(sync-window display-start))))

(提供'sync-window)

接下来,一个同步线而不是字符位置的版本。



如果您选择一些行在主缓冲区中,这也标记从属缓冲区中的相应行。突出显示将保持永久,直到您在主缓冲区中选择另一个区域。它甚至可以在主缓冲区中停用 sync-window-mode 。您可以通过



Mx sync-window-cleanup



在从属缓冲区中。

 (defface sync-window-face; ;(从原来的font-lock-function-name-face 
'(((color color)(min-color 88)(background light)))((前景黄色:backgroundBlue1))
((类颜色)(最小颜色88)(背景暗))(:前景红色:背景LightSkyBlue))
(((颜色)(最小颜色16)背景色)(:前景蓝色:背景黄色))
((颜色类型)(最小颜色16)(背景黑暗))(:前景LightSkyBlue )
((类颜色)(最小颜色8))(:前景蓝色:粗体t))
(t(:bold t)))
同步窗口模式中的高亮区域
:组'sync-window)

(defvar sync-window-overlay nil
当前主区域的覆盖在同步窗口模式的奴隶。)
(make-variable-buffer-local'sync-window-overlay)

(defun sync-window-cleanup()
在sync-window-mode之后清理
(交互式)
(if(overlayp sync-window-overlay)
(progn
(delete-overlay sync-window-overlay)
(setq sync-window-覆盖nil))
(remove-overlays(point-min)(point-max)'sync-window-slave t)))

(defvar sync-window-master-hook nil
主窗口中由sync-window运行的钩子)

(defun sync-window(& optional display-start)
同步点位置当前帧中的其他窗口。
只有当活动的文件中正好有两个窗口不在minibuffer中时才有效。
(interactive)
(when(=(count-windows'noMiniBuf)2)
((p(line-number-at-pos))
(start(line-number-at-pos(或display-start(window-start)))
(vscroll -vscroll))
breg ereg)
(when(use-region-p)
(setq breg(line-number-at-pos(region-beginning))
ereg (line-number-at-pos(if(look-back\\\
)(1-(region-end))(region-end)))))
(run-hooks'sync-window
(其他窗口1)
(goto-char(point-min))
(当breg
(sync-window-cleanup)
(overlay-put(setq sync-window-overlay(make-overlay(line-begin-position breg)(line-end-position ereg)))'face'sync-window-face)
(overlay-将同步窗口覆盖'sync-window-slave t))
(setq start(line-start-position start))
(forward-lin e(1-p))
(set-window-start(selected-window)s​​tart)
(set-window-vscroll(selected-window)vscroll)
(other-window 1 )
(除非display-start
(重新显示t))
)))

(defvar sync-window-mode-hook nil
Hooks在sync-window-mode的开始处运行)

(define-minor-mode sync-window-mode
两个缓冲区的同步视图
:group'windows
:light⇕
(如果sync-window-mode
(progn
(add-hook' command-hook'sync-window-wrapper'append t)
(add-to-list'window-scroll-functions'sync-window-wrapper)
(run-hooks'sync-window-mode -hook)
(sync-window))
(remove-hook'post-command-hook'sync-window-wrapper t)
(setq window-scroll-functions(删除'sync -window-wrapper window-scroll-functions))
))

(defun sync-window-wrapper(&可选窗口替换ay-start)
这个包装器确保只有当活动窗口的缓冲区处于sync-window-mode状态时,才会从`post-command-hook'
触发sync-window
(with-selected-window(或window(selected-window))
(当sync-window-mode
(sync-window display-start)))

(提供'同步窗口)


I am trying to find a package/function in emacs where it would have both files opened side by side to be at same line location mirroring the movement of whichever buffer is moving. Meaning, for two buffers opened side by side, moving in one of the buffers (page up/down, moving cursor.,..etc) would have the same movements in the other buffer.

More specifically, when opening a buffer (and while activating this mode) the opened buffer should already be at the line position of the one that is already opened in the other buffer window.

解决方案

You could try scroll-all-mode. This switches on parallel scrolling of all windows of one frame.

Scrolling with the mouse and scroll bar do not work for me. But all scrolling with keys (such as Pg-Down, Pg-Down and cursor movements) works fine.

EDIT: You could also try the following code. It works only for a frame with exactly two windows not counting the minibuffer. You have to open the files at first and make sure that they are shown in two windows side-by-side. Then define the master window by activating sync-window-mode for it. Make sure that line-wrapping is off for both windows.

EDIT: Fixed a problem (sometimes one had to press buttons twice to sync). Solution: Also hook to window-scroll-functions. At post-command-hook the right window-start position is not known since redisplay did not run yet. The fist point where the future window-start is known is at window-scroll-functions.

(defun sync-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 (point))
      (start (or display-start (window-start)))
      (vscroll (window-vscroll)))
      (other-window 1)
      (goto-char (min (max p (point-min)) (point-max)))
      (set-window-start (selected-window) start)
      (set-window-vscroll (selected-window) vscroll)
      (other-window 1)
      )))

(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)
    (sync-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
      (sync-window display-start))))

(provide 'sync-window)

Next, a version that synchronizes lines instead of character positions.

If you select some lines in the master buffer this also marks the corresponding lines in the slave buffer. The highlighting keeps permanent until you select another region in the master buffer. It even survives deactivation of sync-window-mode in the master buffer. You get ride of the highlighting by

M-x sync-window-cleanup

in the slave buffer.

(defface sync-window-face ;; originally copied from font-lock-function-name-face
  '((((class color) (min-colors 88) (background light)) (:foreground "Yellow" :background "Blue1"))
    (((class color) (min-colors 88) (background dark)) (:foreground "Red" :background  "LightSkyBlue"))
    (((class color) (min-colors 16) (background light)) (:foreground "Blue" :background "Yellow"))
    (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue" :background "Yellow"))
    (((class color) (min-colors 8)) (:foreground "blue" :bold t))
    (t (:bold t)))
  "Face used to highlight regions in `sync-window-mode' slaves."
  :group 'sync-window)

(defvar sync-window-overlay nil
  "Overlay for current master region in `sync-window-mode' slaves.")
(make-variable-buffer-local 'sync-window-overlay)

(defun sync-window-cleanup ()
  "Clean up after `sync-window-mode'."
  (interactive)
  (if (overlayp sync-window-overlay)
      (progn
    (delete-overlay sync-window-overlay)
    (setq sync-window-overlay nil))
    (remove-overlays (point-min) (point-max) 'sync-window-slave t)))

(defvar sync-window-master-hook nil
  "Hooks to be run by `sync-window' in the master window ")

(defun sync-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))
      breg ereg)
      (when (use-region-p)
    (setq breg (line-number-at-pos (region-beginning))
          ereg  (line-number-at-pos (if (looking-back "\n") (1- (region-end)) (region-end)))))
      (run-hooks 'sync-window-master-hook)
      (other-window 1)
      (goto-char (point-min))
      (when breg
    (sync-window-cleanup)
    (overlay-put (setq sync-window-overlay (make-overlay (line-beginning-position breg) (line-end-position ereg))) 'face 'sync-window-face)
    (overlay-put sync-window-overlay 'sync-window-slave t))
      (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))
      )))

(defvar sync-window-mode-hook nil
  "Hooks to be run at start of `sync-window-mode'.")

(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)
    (run-hooks 'sync-window-mode-hook)
    (sync-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
      (sync-window display-start))))

(provide 'sync-window)

这篇关于在两个打开的缓冲区中并排镜像文件中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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