reddit / emacs

Auto Updating Bookmark Location in File

[deleted] · 2022-10-18 17:09
[deleted]
00-11 · 2022-10-18 18:24
So you don't want to bookmark a location in file A. You want a bookmark that, when you use it (jump to it), it moves to file A but at the line number of the buffer (e.g. file B) you were in when you jumped? (Why?) * You can do that by using `bookmark-after-jump-hook` (assuming you stash the line number somewhere that your hook function can get to it). * Or you can do it by defining a *function* bookmark that does just what you want (whatever - including this). You can define function bookmarks if you use library [Bookmark+](https://www.emacswiki.org/emacs/BookmarkPlus) lets
Weird_Locksmith9662 · 2022-10-18 18:39
Thanks for the reply. I don’t think i’m explaining myself clearly. If I create a bookmark in file A, I want that bookmark to remember which line I am on. So if I switch to another buffer, I can quickly type ‘<name of mark> to jump back to the line i was last on in file A. The reason I want to do this is because I can have multiple marks on the home row and switch to any file that I want quickly.
eleven_cupfuls · 2022-10-18 19:00
Updating the bookmark's position is easy; that's just (defun wl/reposition-current-bookmark () (when bookmark-current-bookmark (bookmark-set-position bookmark-current-bookmark (point)))) Then you just need to decide when you want it to run. The easiest thing would be to install this as a `kill-buffer-hook`. Using `run-with-idle-timer` would be another option, to make the current buffer's bookmark (if any) constantly auto-update. There is not, as far as I know, a single straightforward way to run a function for all the different methods of switching the current buffer.
Weird_Locksmith9662 · 2022-10-18 19:25
Thanks for the reply. I’ve been trying to get this to work, do you have an example of how to use a hook with this function ? I tried and it doesn’t seem to be updating the position of the bookmarks
00-11 · 2022-10-18 19:43
1. That's what bookmarks already do, by default. They return to the place where you set them. They're even typically able to find that place contextually, if you edited some text around them. 2. FWIW, I don't understand what you say about your reason.
thetemp_ · 2022-10-18 19:51
There is a simpler way to get to the last location you were at in a file. [save-place-mode](https://www.emacswiki.org/emacs/SavePlace) keeps track of the last location in every file you visit and automatically takes you there the next time you open the same file. So that would take care of half of the problem. The next thing is just an easy way to open the file that doesn't interfere with `save-place-mode`. There are many options for that, such as [recentf-mode](https://www.emacswiki.org/emacs/RecentFiles). Bookmarks might even be able to do that, but I'm not sure.
Weird_Locksmith9662 · 2022-10-18 20:18
I don’t want to return to the place i set them. I want a bookmark to be set in a file then update its position based on where I move in the file
00-11 · 2022-10-19 01:15
Sorry; not clear to me at all what you're doing or what you want to do instead. Hopefully someone else will understand and be able to help.
eleven_cupfuls · 2022-10-19 02:54
Well, the `kill-buffer-hook` would just be (add-hook 'kill-buffer-hook #'wl/reposition-current-bookmark) For the idle timer, something like (run-with-idle-timer 0.75 'repeat #'wl/reposition-current-bookmark) Which should update the bookmark for whatever buffer is active when the timer fires. The `0.75` means it runs whenever Emacs is idle for 3/4 of a second. I'm not too sure what an appropriate number would be.
Weird_Locksmith9662 · 2022-10-19 03:04
thank u!
oantolin · 2022-10-19 11:19
I think what OP wants is a bookmark that only remembers the file and not the location within the file. What should happen when you jump to the bookmark is that the file is displayed in the buffer and point is placed at the last location visited in that file (not where point was when the bookmark was created).
Weird_Locksmith9662 · 2022-10-19 11:53
>(defun wl/reposition-current-bookmark () (when bookmark-current-bookmark (bookmark-set-position bookmark-current-bookmark (point)))) Exactly. Would you know how to do this? I tried using this function and attaching it to a hook, but it does not seem to be working.
00-11 · 2022-10-19 15:38
* If the file is already visited in some buffer, just switch to that buffer (`C-x b` or `C-x C-f`). * If not, then you need to have, somewhere, *saved* the *"last location visited in that file"*. In that case, my answer above applies: use `bookmark-after-jump-hook` or (if you use [Bookmark+](https://www.emacswiki.org/emacs/BookmarkPlus)) a *function* bookmark that moves to that saved last-visited position after visiting the file. But it sounds like you don't need a bookmark at all for this - just use something like [save place](https://www.emacswiki.org/emacs/SavePlace).
Weird_Locksmith9662 · 2022-10-19 17:04
thank u for the reply. i will give it a try!