0

bookmark.el allows basic bookmark manipulation and usage, whereas a bookmark is mostly defined as destination file and location.

Is there a similar library which allows binding originating locations in code to destination bookmarks? e.g. whenever I'm in src/file1.el at line 5, if I call location-dependent-jump, I'll land in src/lib/file2.el, line 10?

The mapping would include the originating location and the destination where to jump to and saved in a file:

(src/file1.el:5, src/lib/file2.el:10),
(src/test.el:10, src/lib/calltest.el:20),
...

I was working on a some scripts that implement this, but was wondering if this already exists.

1
  • I guess you meant bookmark.el, not bookmarks.el?
    – Drew
    Jul 20, 2023 at 17:00

1 Answer 1

1

Not sure I understand your question/request. But:

  • Bookmarks need not have any destination at all. They need not go anywhere. They can do anything.
  • Bookmarks can have destinations that are unrelated to files or locations in files.

As for destinations are are in files, bookmarks can (and by default do) take you to a particular position in a file. And the position reached isn't necessarily the exact position recorded in the bookmark: the surrounding context (text before and after) is also recorded, and if the recorded position no longer matches the recorded context then the destination searches for the context and takes you to the (moved) position. You can optionally have the recorded position updated to that new location.

What if the destination code is moved to a different file altogether? In that case, the context isn't found in the recorded file, and when you jump to the bookmark Emacs asks to tell it where the new, proper destination is. After you tell it, it takes you there, and optionally updates the recorded file.


All of that is true of even vanilla Emacs, as well as Bookmark+. The automatic relocation is a bit better with Bookmark+, and Bookmark+ also lets you bookmark a region (text selection), not just a position - which means that there are two recorded contexts, one for the region start and one for the region end.


On the other hand, from your example, maybe you're asking whether you can set a bookmark at a given location in file1.el and have the action of the bookmark take you to a particular location in file2.el.

The answer to that is yes. You just have to decide what the logic is behind the bookmark behavior. As I said above, a bookmark can do anything.

E.g., you can define a bookmark-setting command that prompts you for the destination location (e.g., a position in file2.el), and records that in the bookmark's action function. This could be dependent on the starting file (e.g. file1.el) or independent of it (jumps to the file2.el location from anywhere else).

Specify the exact behavior you want, then define yourself a command that sets the bookmark you need for that behavior. If you have specific questions about doing that, you can post them here (one question per post, please).

5
  • What functions inside bookmark.el would be used for specifying a bookmark's action function (which is non-location based)? Could you link an example of custom bookmark action function code from elsewhere?
    – anon2328
    Jul 20, 2023 at 17:21
  • Again, not sure what you're asking. But there are several bookmarking commands defined in bookmark+-1.el that set a destination other than the current buffer/file. E.g., command bmkp-autofile-set sets a bookmark at the beginning of a file whose name it prompts for - with no need to visit that file first.
    – Drew
    Jul 20, 2023 at 18:04
  • bmkp-file-target-set is a more general example of such a command than is bmkp-autofile-set. And function bmkp-make-record-for-target-file provides a function that creates a bookmark record for a given file, with the bookmarked position at the beginning of the file.
    – Drew
    Jul 20, 2023 at 18:12
  • Thank you, adding the documentation on "function-based" bookmarks in Bookmark+ emacswiki.org/emacs/…
    – anon2328
    Jul 22, 2023 at 13:41
  • I think a small wrapper around bookmark.el or bookmark.el+ library is needed to implement what's asked in the original question - the wrapper would just store bookmark pairs like the ones from the snippet. bookmark+.el region-based bookmarks would be helpful there, since in that case custom code jumps could be done over regions and not only over fixed points.
    – anon2328
    Jul 22, 2023 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.