Bookmark Saver macro

As far as I know, even in DevStudio 6.0 there is no built-in feature that saves the bookmarks which I have set in my source files. This is very annoying, because I use them very often and the source files I have to work with are sometimes quite long and badly arranged (except my own ones, of course ;-).
So I decided to develop a macro with VC 5 that saves me from browsing every day again for the same lines of code (or from running my PC over night just for those bookmarks which is NOT very environmental-friendly).

The line number of  each bookmark is saved in a plain text file that has the same name as the source file, but with a '_bmk' appended (eg. myfile.cpp -> myfile.cpp_bmk).
After saving or restoring the bookmarks, the old cursor position is restored.

'------------------------------------------------------------------------------
'FILE DESCRIPTION: This macro saves and restores
your precious bookmarks.
'------------------------------------------------------------------------------ 
Dim sFullName, sTitle
Dim nCurLine, nCurCol, nBmkLine, nLastBmkLine
Dim i
Dim bLock, bError, bActive

bLock = False
bActive = True
sTitle = "Bookmark Saver 1.2"

Sub ToggleActive()
    'DESCRIPTION: Switches bookmark saving on or off.
    if (bActive = True) then
    bActive = False
        MsgBox "Bookmark Saver disabled.", vbCritical, sTitle
    else
        bActive = True
        MsgBox "Bookmark Saver enabled.",vbInformation, sTitle
    End If
End Sub

'Bookmarks aus Datei lesen
Sub Application_DocumentOpen(theDoc)
    'Disabled or not a text file?
    if ((bActive = False) Or theDoc.Type <> "Text") then
        Exit Sub
    End If
 
    'Prevent macro from calling itself!
    if (bLock = True) Then
        bLock = False
        Exit Sub
    End If
 
    'Save cursor position
    nCurLine = theDoc.Selection.CurrentLine
    nCurCol = theDoc.Selection.CurrentColumn

    'Get name of bookmark file
    sFullName = theDoc.FullName
    sFullName = sFullName & "_bmk"

    'Check if bookmark file exists
    bLock = True
    On Error Resume Next
    Documents.Open sFullName, "Text"
    if Err.Number <> 0 then
        Exit Sub 'No bmk file found -> exit
    End If
 
    Set sel = ActiveDocument.Selection
 
    'Calculate number of bookmarks
    sel.EndOfDocument dsMove
    nLastBmkLine = sel.CurrentLine - 1

    'Loop through bookmark file, read and set bookmarks
    for i=1 to nLastBmkLine
        sel.GoToLine i
        sel.SelectLine
        nBmkLine = sel.Text
        theDoc.Selection.GoToLine nBmkLine
        theDoc.Selection.SetBookmark
    Next
 
    'Close bookmark file
    bLock = True
    ActiveDocument.Close

    'Restore cursor position
    theDoc.Selection.GoToLine nCurLine + 10
    theDoc.Selection.GoToLine nCurLine
    theDoc.Selection.CharRight dsMove, nCurCol
End Sub
 

'Sub Application_BeforeDocumentClose(theDoc)
'Maybe better for you, test it!
Sub Application_DocumentSave(theDoc)
    'Enabled?
    if ((bActive = False) Or theDoc.Type <> "Text") then
        Exit Sub
    End If

    'Prevent macro from calling itself!
    if (bLock = True) then
        bLock = False
        Exit Sub
    End If

    'Set bookmark file name
    sFullName = theDoc.FullName
    sFullName = sFullName & "_bmk"

    'Set object
    Set sel = theDoc.Selection

    'An error occurs when you have tried to open a nonexisting document
    On Error Resume Next
    if (sel = Null) then
        Exit Sub
    End If

    'Save cursor position
    nCurLine = sel.CurrentLine
    nCurCol = sel.CurrentColumn

    'Go to top of file
    sel.StartOfDocument dsMove
 
    'Check if bookmark file exists
    bLock = True
    On Error Resume Next
    Documents.Open sFullName, "Text"
    if Err.Number <> 0 then
        'If not, make new empty document
        bLock = True
        Documents.Add "Text"
        bError = True
    else
        'If yes, delete contents of old file
        ActiveDocument.Selection.SelectAll
        ActiveDocument.Selection.BackSpace
        bError = False
    End If

    'Search bookmarks
    nLastBmkLine = 0
    Do
        bFound = sel.NextBookmark
        if (bFound = False) then 'No bookmarks found
            if (bError = False) then 'Bookmark file existed, so save empty document
                ActiveDocument.Save sFullName, False
            End If
            bLock = True
            ActiveDocument.Close 'Bookmark file did not exist, just close document
            sel.GoToLine nCurLine
            sel.CharRight dsMove, nCurCol
            Exit Sub
        End If

        nBmkLine = sel.CurrentLine
        if (nBmkLine <= nLastBmkLine) then
            Exit Do
        End If
        nLastBmkLine = nBmkLine
        ActiveDocument.Selection = nBmkLine & vbCRLF 'Write line number to bookmark file
    Loop While (True)

    'Save and close bookmark file
    ActiveDocument.Save sFullName, False
    bLock = True
    ActiveDocument.Close

    'Restore cursor position
    sel.GoToLine nCurLine + 10
    sel.GotoLine nCurLine
    sel.CharRight dsMove, nCurCol
End Sub

Remarks:

  • Try out if you want the bookmarks to be saved when you close or when you save your source file. Both has its

  • advantages and disadvantages (like all things in life).
  • Because there is no macro function to delete a file, you should delete your empty *.*_bmk-Files manually from time to time.
  • You can't use your Recent Files menu anymore because it is crammed with bookmark list file names :-(
  • A bookmark in the first line of the document will not be saved.
Any comments, suggested improvements, greetings etc. are greatly appreciated!



Comments

  • No longer needed in VS.net (VC 7)

    Posted by Legacy on 08/08/2003 12:00am

    Originally posted by: a

    Because it saves bookmarks.

    Reply
  • BookMarks In VC++

    Posted by Legacy on 02/19/2003 12:00am

    Originally posted by: vishal

    I just saw your vb code for bookmarks.it is nice.pls tell me how we done using vc++ code.it is important for me.pls reply as soon as possible.

    regards vishala

    Reply
  • Bookmarks are saved in VC++ 6 SP5

    Posted by Legacy on 05/17/2001 12:00am

    Originally posted by: Roland

    Visual C++ 6 SP5 saves the bookmarks. I don't know about other SPx releases though.

    :)
    Roland

    Reply
  • Bookmarks WERE saved as part of the VC++ workspace

    Posted by Legacy on 05/21/1999 12:00am

    Originally posted by: Tom Wilson

    The bookmarks were saved in VC++ long before it became a developer studio! They were there in version 1.5x and possibly v2.0 (I can't remember). But by the time VC5 arrived this facility had been removed. I can't accept that it has been removed intentionally - there must be an option or undocumented command in there somewhere. I'll poke around and let you know...

    Reply
  • Anyone Have This Working on VC6?

    Posted by Legacy on 03/31/1999 12:00am

    Originally posted by: Michael Herstine

    Great idea - I could really use it. However, DevStudio 6.0
    crashes unpredictably when loading it. I've added a slew
    of PrintToOutputWindow statements to see it execute, and I
    don't see any before it crashes; therefore I assume the
    crash comes when its compiling the code. Has this happened
    to anyone else?

    I'm considering re-writing it as a DevStudio add-in to
    get around this, so I'd love to know if someone else got
    it to work!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Due to internal controls and regulations, the amount of long term archival data is increasing every year. Since magnetic tape does not need to be periodically operated or connected to a power source, there will be no data loss because of performance degradation due to the drive actuator. Read this white paper to learn about a series of tests that determined magnetic tape is a reliable long-term storage solution for up to 30 years.

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

Most Popular Programming Stories

More for Developers

RSS Feeds