Emacs: Bookmarks in Registers
I was wondering the other day if there was some way to store a bookmark-style position in a register. In Emacs 28.2, there is point-to-register and register-to-point:
point-to-register C-x r C-@, C-x r C-SPC, C-x r SPC
Store current location of point in register REGISTER.
register-to-point M-x ... RET
Move point to location stored in a register.
This sounds like just what you need. However, the problem with these is that if the buffer in-question changes, then running register-to-point will not take you where you want to go. E.g., if you run point-to-register inside an *info* buffer, and then move the buffer to some other info page, then this will not take you back to the original info page.
Of course, you can just use a regular bookmark instead, but then you must name the bookmark, and then the bookmark clutters up your list of bookmarks. What I want is to be able to store an anonymous bookmark in a register, which is handy if you only need a bookmark for a little while.
With a bit of experimentation and looking at the register.el source code, I was able to throw this together:
(defun bookmark-point-to-register ()
(interactive)
(set-register
(register-read-with-preview "register: ")
(bookmark-make-record)))
(defun bookmark-register-to-point ()
(interactive)
(bookmark--jump-via
(get-register
(register-read-with-preview "register: "))
'pop-to-buffer-same-window))
This seems to work great. The only downside is that `list-bookmarks' does not understand what is stored in the register, so the preview display will be incorrect. When I tested this, it interpreted it as a rectange data structure. I think to fix this, you would need to add a new `register-val-describe' method like in register.el. But I'm not quite sure how to code that.