65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.50/5 (4 votes)

Jun 20, 2002

1 min read

viewsIcon

55941

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