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()
set sel = ActiveDocument.Selection
sel.Cancel
sel.FindText "^{", dsMatchBackWard + dsMatchRegExpE
sel.StartOfLine
End Sub
Sub GoToEndOfFunction()
set sel = ActiveDocument.Selection
sel.Cancel
sel.FindText "^}", dsMatchRegExpE
End Sub
Sub FindFunction()
Set sel = ActiveDocument.Selection
sel.WordRight
sel.WordLeft dsExtend
str = sel
if (right(activedocument.name, 2) = ".h") then
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
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
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