Click here to Skip to main content
16,016,489 members
Articles / Programming Languages / C++
Article

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

Rate me:
Please Sign up or sign in to vote.
3.50/5 (4 votes)
19 Jun 20021 min read 55.9K   13   6
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

VBScript
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


Written By
Web Developer
Japan Japan
Graduate in Tsinghua Univ of China

Comments and Discussions

 
QuestionHere is Macro Code for VS2013/15 with "Macros for Visual Studio" installed Pin
topochicho7-Jun-17 5:59
topochicho7-Jun-17 5:59 
NewsHere is the macro code for VS2010 Pin
Pandalien24-Jan-14 17:01
professionalPandalien24-Jan-14 17:01 
GeneralOverloaded Functions Pin
chofra0316-Aug-04 4:03
chofra0316-Aug-04 4:03 
GeneralIDE tips to do similar Pin
-Dy20-Jun-02 3:05
-Dy20-Jun-02 3:05 
GeneralRe: IDE tips to do similar Pin
luandh20-Jun-02 4:44
luandh20-Jun-02 4:44 
GeneralRe: IDE tips to do similar Pin
pdelmundo3-Aug-04 7:55
pdelmundo3-Aug-04 7:55 

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.