12
$\begingroup$

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.

$\endgroup$
21
  • $\begingroup$ 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. $\endgroup$
    – Ajasja
    May 11 '12 at 14:24
  • 2
    $\begingroup$ 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. $\endgroup$
    – user21
    May 11 '12 at 14:28
  • 2
    $\begingroup$ 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. $\endgroup$ May 11 '12 at 14:35
  • 1
    $\begingroup$ 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? $\endgroup$
    – Szabolcs
    May 11 '12 at 14:41
  • 1
    $\begingroup$ @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. $\endgroup$
    – Ajasja
    May 11 '12 at 14:52
5
$\begingroup$

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"}]]]] <> ":: *) " ]
    }],
    MenuEvaluator->Automatic
],

Item[KeyEvent["Keypad1", Modifiers -> {Control}],
    FrontEndExecute[{
        FrontEnd`NotebookFind[FrontEnd`InputNotebook[], "(* ::BOOKMARK:"]
    }]
], 
$\endgroup$

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .