Click here to Skip to main content
Licence 
First Posted 19 Jun 2002
Views 41,478
Bookmarked 13 times

Macros to go to beginning or end of function body, and to find corresponding function definition.

By | 19 Jun 2002 | Article
Simple Macros to navigate through code

Introduction

I wrote several simple macros and have used them for months. These macros are very handy for code navigation.

The Macros

  • GoToBeginOfFunction is used to go to the beginning of the function body. This is convenient when I want to add some parameters to the function definition when editing in the middle of a long function. This sometimes get me to the beginnings of class definitions but doe not bother much.
  • GoToEndOfFunction is used to go to the end of the function definition. This doesn't use often but makes me feel well when I could figure out what to do next while just use the shortcuts to go to the beginning and end of the function repeatedly.
  • FindFunction is used to go to the function definition in *.cpp when in *.h file or reversely go to the function declaration when in *.cpp files. This is useful when the F12 doesn’t work well, e.g. when I included a static lib project (in such case I could not get to the function definition in *.cpp with F12, while with my macro, I usually press F12 to get to the .h file and then my shortcut key CTRL++ and reached the definition in .cpp). It works well with the non-static class member functions, but a little poor with global functions.

These macros only considered simple situations, but they could still be helpful if used properly.

I usually assign:

  • [Alt UpArrow] to GoToBeginOfFunction
  • [Alt DownArrow] to GoToEndOfFunction
  • [Ctrl +] to FindFunction

Code Listing

Sub GoToBeginOfFunction()
'DESCRIPTION: Go to the previous { which is at the beginning of a line.
    set sel = ActiveDocument.Selection
    sel.Cancel
    sel.FindText "^{", dsMatchBackWard + dsMatchRegExpE
    sel.StartOfLine
End Sub

Sub GoToEndOfFunction()
'DESCRIPTION: Go to the next } which is at the beginning of a line.
    set sel = ActiveDocument.Selection
    sel.Cancel
    sel.FindText "^}", dsMatchRegExpE
End Sub

Sub FindFunction()
'DESCRIPTION: Open corresponding .h or .cpp file and 
'find the function definition.

    Set sel = ActiveDocument.Selection
    sel.WordRight
    sel.WordLeft dsExtend
    str = sel

    if (right(activedocument.name, 2) = ".h") then
    ' now in .h, open cpp and find the function
        filename = activedocument.path & "\" & _
          mid(ActiveDocument.name, 1, len(ActiveDocument.name) - 2) & ".cpp"
        documents.Open filename
        set s1 = ActiveDocument.Selection
        s1.Cancel
        if s1.FindText ("::" & str & "(", dsMatchFromStart) = false then
            s1.FindText " " & str & "("
        end if

    else
    ' now in .cpp, open .h and find the function
        if (right(activedocument.name, 4) = ".cpp") then
            filename = activedocument.path & "\" & _
              mid(ActiveDocument.name, 1, len(ActiveDocument.name) - 4) & ".h"
            documents.Open filename
            set s1 = ActiveDocument.Selection
            s1.Cancel
            s1.FindText str & "(", dsMatchFromStart
        end if
    end if
End Sub

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

luandh

Web Developer

Japan Japan

Member

Graduate in Tsinghua Univ of China

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
GeneralOverloaded Functions Pinmemberchofra034:03 16 Aug '04  
GeneralIDE tips to do similar PinmemberDylan Kenneally3:05 20 Jun '02  
GeneralRe: IDE tips to do similar Pinmemberluandh4:44 20 Jun '02  
GeneralRe: IDE tips to do similar PinmemberPaolo del Mundo7:55 3 Aug '04  

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
Web04 | 2.5.120517.1 | Last Updated 20 Jun 2002
Article Copyright 2002 by luandh
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid