Click here to Skip to main content
Licence CPOL
First Posted 27 Dec 2000
Views 37,980
Bookmarked 13 times

Source Navigation Macros

By | 27 Dec 2000 | Article
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

Michael Pelts



United States United States

Member

Apply for Discover Motiva Card

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralCPPvH improvement PinmemberVitaly Belman0:55 17 Mar '01  
GeneralQuickFind changes PinmemberAnonymous4:46 5 Jan '01  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 28 Dec 2000
Article Copyright 2000 by Michael Pelts
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid