Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows configuration to registers

Tags:

emacs

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

like image 803
Angel de Vicente Avatar asked Feb 24 '23 09:02

Angel de Vicente


2 Answers

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)))
like image 187
the claw Avatar answered Mar 03 '23 13:03

the claw


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

like image 37
Oleg Pavliv Avatar answered Mar 03 '23 13:03

Oleg Pavliv