Windows 配置到寄存器

发布于 2024-11-03 17:56:50 字数 184 浏览 1 评论 0原文

我开始大量使用命令 Cx rw 和 Cx rj 将窗口配置存储到寄存器并在稍后调用它们,但我发现有点烦人的是,光标位置是根据窗口配置的时间存储的已保存。

基本上,我希望不存储光标位置(或自动更新),这样每当我“跳转”到存储的窗口配置时,我都会得到与上次访问它时相同的视图,而不是与创建它时相同的视图。

有什么想法吗? 天使

I'm starting to use quite heavily the commands C-x r w and C-x r j to store windows configuration to registers and recall them at a later point, but I find a bit annoying that the cursor positions are stored as per the time when the window configuration was saved.

Basically I would like that the cursor positions are not stored (or are updated automatically), so that whenever I "jump" to a stored window configuration I get the same view as when I last visited it, not as when I created it.

Any ideas?
Ángel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

离不开的别离 2024-11-10 17:56:50

我也发现这很烦人,只是编写了一个解决方案。使用正常功能(当前窗口配置或要注册的窗口配置)存储窗口配置。然后,在应用保存的窗口配置(或寄存器)之前,

  • 存储所有打开的缓冲区的点。
  • 恢复窗口配置。
  • 将存储的点应用到当前窗口。

下面的代码执行此操作。不过,您必须自己将恢复窗口配置连接到注册代码。

(defun buffer-point-map ()
  (save-excursion
    (mapcar (lambda (buffer) (cons (buffer-name buffer)
                                   (progn (set-buffer buffer) (point))))
            (buffer-list))))

(defun apply-buffer-points (buff-point-map)
  (mapc (lambda (window) (let* ((buffer (window-buffer window))
                                (buffer-point (cdr (assoc (buffer-name buffer) buff-point-map))))
                           (when buffer-point (set-window-point window buffer-point))))
        (window-list))
  nil)

(defun restore-window-configuration (window-config)
  (let ((points (buffer-point-map)))
    (set-window-configuration window-config)
    (apply-buffer-points points)))

I also found this very annoying and just coded up a solution. Store the window config using the normal functionality (current-window-configuration or window-configuration-to-register). Then, before applying the saved window config (or register),

  • Store the points of all open buffers.
  • Restore the window config.
  • Apply the stored points to the current windows.

The code below does this. You'll have to hook up restore-window-configuration to the register code yourself though.

(defun buffer-point-map ()
  (save-excursion
    (mapcar (lambda (buffer) (cons (buffer-name buffer)
                                   (progn (set-buffer buffer) (point))))
            (buffer-list))))

(defun apply-buffer-points (buff-point-map)
  (mapc (lambda (window) (let* ((buffer (window-buffer window))
                                (buffer-point (cdr (assoc (buffer-name buffer) buff-point-map))))
                           (when buffer-point (set-window-point window buffer-point))))
        (window-list))
  nil)

(defun restore-window-configuration (window-config)
  (let ((points (buffer-point-map)))
    (set-window-configuration window-config)
    (apply-buffer-points points)))
遮云壑 2024-11-10 17:56:50

如果您查看源代码,

(defun window-configuration-to-register (register &optional arg)
  ...
  (set-register register (list (current-window-configuration) (point-marker))))

您会发现它将一个点存储为第二个参数。
只需重新定义它,

(defun my-window-configuration-to-register (register &optional arg)
  (interactive "cWindow configuration to register: \nP")
  (set-register register (list (current-window-configuration) nil)))

然后重新定义一个 Cx rw 快捷方式即可使用 my-window-configuration-to-register

(define-key (current-global-map) (kbd "C-x r w") 'my-window-configuration-to-register)

或定义一个建议

(defadvice window-configuration-to-register (after window-configuration-to-register-no-point activate)
  "Avoid storing current buffer's position in the register. We want to stay on the last used position, not to jump to the saved one"
  (set-register register (list (current-window-configuration) nil)))

唯一的问题是,当您跳转时它会显示错误消息到它。您可以重新定义jump-to-register来避免它

If you take a look into a source code

(defun window-configuration-to-register (register &optional arg)
  ...
  (set-register register (list (current-window-configuration) (point-marker))))

you'll see that it stores a point as the second argument.
Just re-define it like

(defun my-window-configuration-to-register (register &optional arg)
  (interactive "cWindow configuration to register: \nP")
  (set-register register (list (current-window-configuration) nil)))

and redefine a C-x r w shortcut as well to use my-window-configuration-to-register

(define-key (current-global-map) (kbd "C-x r w") 'my-window-configuration-to-register)

Or define an advice

(defadvice window-configuration-to-register (after window-configuration-to-register-no-point activate)
  "Avoid storing current buffer's position in the register. We want to stay on the last used position, not to jump to the saved one"
  (set-register register (list (current-window-configuration) nil)))

The only problem is that it brings up an error message when you jump to it. You may redefine jump-to-register to avoid it

寂寞陪衬 2024-11-10 17:56:50

我将添加另一个使用不同方法的答案。

在跳转到另一个寄存器之前,您可以存储当前的窗口配置。这样它就会在你跳转之前存储你最新的缓冲区位置。

然而,这并非在所有情况下都有效。例如,如果您只是切换到另一个缓冲区或使用 Mx dired 或其他内容创建缓冲区,那么它将不会存储当前的窗口配置。

(defvar current-window-conf-register nil)

(defadvice window-configuration-to-register (after window-configuration-to-register-current-reg activate)
  (setq current-window-conf-register register))

(defadvice jump-to-register (before jump-to-register-store-window-conf activate)
  (if current-window-conf-register (window-configuration-to-register current-window-conf-register))
  (setq current-window-conf-register register))

I'll add another answer which uses different approach.

Before you jump to another register you may store the current window configuration. This way it will store your latest buffers position just before you jump.

This will not work in all cases, however. For example if you just switch to another buffer or create a buffer with M-x dired or something then it will not store the current window config.

(defvar current-window-conf-register nil)

(defadvice window-configuration-to-register (after window-configuration-to-register-current-reg activate)
  (setq current-window-conf-register register))

(defadvice jump-to-register (before jump-to-register-store-window-conf activate)
  (if current-window-conf-register (window-configuration-to-register current-window-conf-register))
  (setq current-window-conf-register register))
一抹微笑 2024-11-10 17:56:50

作为对您问题的间接回答,您可以考虑使用 revive.el 来代替,它支持在 Emacs 重新启动时保存和恢复窗口配置,但(我相信)不会存储要点。

As a indirect answer to your question, you might consider using revive.el instead, which supports saving and restoring window configurations across Emacs restart, but doesn't (I believe) store the point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文