Click here to Skip to main content
15,881,173 members
Articles / Programming Languages / C++
Article

Source Navigation Macros

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
27 Dec 2000CPOL 45.9K   13   2
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.
VB.NET
'============== 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)


Written By
United States United States

Comments and Discussions

 
GeneralCPPvH improvement Pin
Vitaly Belman17-Mar-01 0:55
Vitaly Belman17-Mar-01 0:55 
Sub CPPvH()
'DESCRIPTION: toggles between CPP and H files in both directions with QuickFind feature built-in.
on error resume next

Dim myDocument
Dim a
Dim b
Dim Flag
Dim Flag1

lookFor = ActiveDocument.Selection

Flag1 = 0
Flag = 1
a = ActiveDocument.FullName
tmp = InStr(a, ".cpp")
If tmp Then
b = Left(a, Len(a) - 3) + "h"
Flag1 = 1
Else
tmp = InStr(a, ".h")
If tmp Then
b = Left(a, Len(a) - 1) + "cpp"
Flag1 = 1
End If
End If

fileName = LCase(b) '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 + dsMatchWord
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 + dsMatchWord
End if
End Sub

- Vitaly Belman
GeneralQuickFind changes Pin
5-Jan-01 4:46
suss5-Jan-01 4:46 

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

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