Thursday, August 10, 2006

Emacs style indent line

One of the best features that Emacs provides is auto-formating of source code. When I moved to developing in Visual Studio 6, Justin provided the following macro that I mapped to the tab key that provided the same behavior.

Sub EmacsTab()
ext = GetFileExt(ActiveDocument.FullName)
if ext = "cpp" or ext = "h" or ext = "cxx" or ext = "hxx" or ext = "hpp" Then
With ActiveDocument.Selection
If .BottomLine > .TopLine Then
.SmartFormat
Else
col = .CurrentColumn
.StartOfLine dsFirstText
first = .CurrentColumn
.Indent
.SmartFormat
If col > first Then
.MoveTo .CurrentLine, col
End If
End If
End With
Else
ActiveDocument.Selection.Indent
End If
End Sub

When I moved to Visual Studio .Net 2003, the above macro no longer worked. So the following was created. Unfortunately you can no longer map the tab key, so I mapped it to ctrl-;.

Sub EmacsStyleIndentLine()
Dim ts As TextSelection = DTE.ActiveWindow.Selection
ts.SelectLine()
ts.SmartFormat()
ts.WordRight()
End Sub

Recently I moved to Visual Studio .Net 2005 and have been doing some C# coding. I found the above macro does not seem to work. Today I changed it to the following which appears to work just fine.

Sub EmacsStyleIndentLine()
With DTE.ActiveDocument.Selection
.LineUp()
.EndOfLine()
.Delete()
.NewLine()
.LineDown()
DTE.ExecuteCommand("Edit.FormatSelection")
End With
End Sub

No comments: