Click here to Skip to main content
Click here to Skip to main content

Source Navigation Macros

, 27 Dec 2000
Rate this:
Please Sign up or sign in to vote.
Three simple macros that aid in navigating in and between your source code files.

Since I wrote these simple macros about a year ago I can't manage without. I hope you will find them useful as well.

  • QuickFind - finds current selection in the opened document or pops up a regular DevStudio's find dialog if current selection is empty.
  • GoToLastChange - scrolls current document up or down to ensure the last edited line is visible.
  • CPPvH - toggles between CPP and H files in both directions with QuickFind feature built-in. it's especcially useful when you want to jump from declaration of function to its definition and vice-versa.
'============== Navigation Macros By Michael Pelts ========================

Sub QuickFind () 
'DESCRIPTION: finds current selection in the opened document or pops up a regular 
'             DevStudio's find dialog if current selection is empty.
    Dim doc
    set doc = ActiveDocument

    ' Be sure active document is a text document
    if doc Is Nothing Then
        Exit Sub
    elseif doc.Type <> "Text" Then
        Exit Sub
    End If

    lookFor = doc.Selection
    curLine = doc.Selection.CurrentLine
    curCol = doc.Selection.CurrentColumn

    if Len(lookFor) = 0 then
        ExecuteCommand "FindNext"
        if Len(doc.Selection) = 0 then
            ExecuteCommand "Find"
        end if
    else
        doc.Selection.Cancel
        doc.Selection.MoveTo curLine, curCol 
        doc.Selection.FindText lookFor
    End if
End Sub

'==================================================================

Sub GoToLastChange () 
'DESCRIPTION: Scrolls current document up or down to ensure the last edited line is visible.
    ' Be sure active document is a text document
    if ActiveDocument Is Nothing Then
        Exit Sub
    elseif ActiveDocument.Type <> "Text" Then
        Exit Sub
    End If

    if ActiveDocument.Undo = True Then
        ActiveDocument.Redo
    else
        MsgBox("No changes found")
    End if
End Sub

'==================================================================

Sub CPPvH ()
'DESCRIPTION: toggles between CPP and H files in both directions with QuickFind feature built-in.
    on error resume next
    Dim doc
    set doc = ActiveDocument

    ' Be sure active document is a text document
    if doc Is Nothing Then
        Exit Sub
    elseif doc.Type <> "Text" Then
        Exit Sub
    End If

    lookFor = doc.Selection
    fileName = doc.FullName
    dotPos = InstrRev(fileName , ".", -1, vbTextCompare)
    
    fileType = Right(fileName, Len(fileName) - dotPos)
    fileName = Left(fileName, dotPos)

    if UCase(fileType) = UCase("cpp") then
        fileName = fileName + "h"
    elseif UCase(fileType) = UCase("h") then
        fileName = fileName + "cpp"
    else
        MsgBox("Unsupported file format: " + fileType)
        Exit Sub
    End If

    fileName = LCase(fileName)    'first try lower case
    Documents.Open fileName
    if Err.Number <> 0 then 
        Err.Clear
        fileName = UCase(fileName)    'try upper case
        Documents.Open fileName
        if Err.Number <> 0 then 
            MsgBox Err.Description, 0 , "Error"
            exit sub
        End If
    End If
    
    if Len(lookFor) > 0 then
        curLine = ActiveDocument.Selection.CurrentLine
        curCol = ActiveDocument.Selection.CurrentColumn
        lookForDef = "::" + lookFor
        ActiveDocument.Selection.FindText lookForDef, dsMatchCase
        if ActiveDocument.Selection = lookForDef then
            curLine = ActiveDocument.Selection.CurrentLine
            curCol = ActiveDocument.Selection.CurrentColumn - Len(ActiveDocument.Selection)
        End if
        ActiveDocument.Selection.Cancel
        ActiveDocument.Selection.MoveTo curLine, curCol 
        ActiveDocument.Selection.FindText lookFor, dsMatchCase
    End if
End Sub

'=======================================================================

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author


Comments and Discussions

 
GeneralCPPvH improvement PinmemberVitaly Belman17-Mar-01 0:55 
GeneralQuickFind changes PinmemberAnonymous5-Jan-01 4:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 28 Dec 2000
Article Copyright 2000 by Michael Pelts
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid