# Implementing bookmarks in the front end when editing a package

Sometimes when working on larger pieces of code or on a larger package I really miss the option to bookmark the current line/position in the code.

Since the front-end is quite extendable my question is: How could one implement bookmarks in the fronted when editing a package (.m) file?

So the requirements are that a short cut (e.g. Ctrl-Alt-Numpad 1) would set the bookmark and another short cut (e.g. Ctrl-Numpad 1) would move the cursor to the bookmark.

-
One of the possible ways I was thinking about is to insert a marker string (eg (* ::Bookmarks::1:: *) ) at the current cursor position and then somehow find the string and move the cursor there again. But of course I'd except any answer that fulfils the bookmar requirements. – Ajasja May 11 '12 at 14:24
Not what you have been asking for, but you can do this in Workbench. Seen that WB has other benefits for code development, perhaps this is an option. – user21 May 11 '12 at 14:28
The problem is that the fixed "line of code" is not a good notion for the FrontEnd. You can associate a bookmark with a given cell (by using Cell tags, for instance), but I would not try to do that with, say, 10-th (or whatever) line of code. – Leonid Shifrin May 11 '12 at 14:35
It is possible to create a hyperlink to a cell, it seems (check some links in the docs), but not to a certain position within a cell. Is a link to a cell 'good enough' for you? – Szabolcs May 11 '12 at 14:41
@Szabolcs You can insert a comment (* BOOKMARK *) and then just find it again. I don't mind if such comments are visible. The front end already uses special comment tags in packages, for example (* ::Code::Bold:: *). This is where I got my original idea from. – Ajasja May 11 '12 at 14:52

This answer implements Ajasja's suggestion from the comments to use comments (e.g., (* ::BOOKMARK::7:: *) ) as bookmarks and simply finds them with NotebookFind.

The following code (when inserted into KeyEventTranslations.tr) binds Ctrl-Alt-Numpad 1 to creating a (sequentially numbered) bookmark comment at the cursor location and Ctrl-Numpad 1 to cycling through the bookmark comments.

One could also modify this code to bind a number of named bookmarks to specific shortcuts.

Item[KeyEvent["Keypad1", Modifiers -> {Control, Command}],
FrontEndExecute[{
NotebookWrite[InputNotebook[], " (* ::BOOKMARK::" <>
Block[{Inherited = 1},
ToString[Increment[CurrentValue[InputNotebook[], {TaggingRules,
"BookmarkCounter"}]]]] <> ":: *) " ]
}],
FrontEndNotebookFind[FrontEndInputNotebook[], "(* ::BOOKMARK:"]