1

Re: "restore cursor position when file is open" for closed files

there is "restore cursor position when file is open" for closed files feature in vim, but why not in viemu? i proposed this feature but i write a macro myself
for anyone want this feature, you can try the following visualstudio macro (not viemu). hope it bring yet another piece of the good old day feeling from vim to viemu.

u can google or ask someone else on how to add macro project/script to visual studio. i'm not gonna cover it in this thread
btw, thank you viemu for the great work.

' By mOo tinys
Option Strict Off
Option Explicit Off
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Class IniFile
    ' API functions
    Private Declare Ansi Function GetPrivateProfileString _
    Lib "kernel32.dll" Alias "GetPrivateProfileStringA" _
    (ByVal lpApplicationName As String, _
    ByVal lpKeyName As String, ByVal lpDefault As String, _
    ByVal lpReturnedString As System.Text.StringBuilder, _
    ByVal nSize As Integer, ByVal lpFileName As String) _
    As Integer
    Private Declare Ansi Function WritePrivateProfileString _
    Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
    (ByVal lpApplicationName As String, _
    ByVal lpKeyName As String, ByVal lpString As String, _
    ByVal lpFileName As String) As Integer
    Private Declare Ansi Function GetPrivateProfileInt _
    Lib "kernel32.dll" Alias "GetPrivateProfileIntA" _
    (ByVal lpApplicationName As String, _
    ByVal lpKeyName As String, ByVal nDefault As Integer, _
    ByVal lpFileName As String) As Integer
    Private Declare Ansi Function FlushPrivateProfileString _
    Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
    (ByVal lpApplicationName As Integer, _
    ByVal lpKeyName As Integer, ByVal lpString As Integer, _
    ByVal lpFileName As String) As Integer
    Dim strFilename As String

    ' Constructor, accepting a filename
    Public Sub New(ByVal Filename As String)
        strFilename = Filename
    End Sub

    ' Read-only filename property
    ReadOnly Property FileName() As String
        Get
            Return strFilename
        End Get
    End Property

    Public Function GetString(ByVal Section As String, _
    ByVal Key As String, ByVal [Default] As String) As String
        ' Returns a string from your INI file
        Dim intCharCount As Integer
        Dim objResult As New System.Text.StringBuilder(256)
        intCharCount = GetPrivateProfileString(Section, Key, _
        [Default], objResult, objResult.Capacity, strFilename)
        If intCharCount > 0 Then GetString = _
        Left(objResult.ToString, intCharCount)
    End Function

    Public Function GetInteger(ByVal Section As String, _
    ByVal Key As String, ByVal [Default] As Integer) As Integer
        ' Returns an integer from your INI file
        Return GetPrivateProfileInt(Section, Key, _
        [Default], strFilename)
    End Function

    Public Function GetBoolean(ByVal Section As String, _
    ByVal Key As String, ByVal [Default] As Boolean) As Boolean
        ' Returns a boolean from your INI file
        Return (GetPrivateProfileInt(Section, Key, _
        CInt([Default]), strFilename) = 1)
    End Function

    Public Sub WriteString(ByVal Section As String, _
    ByVal Key As String, ByVal Value As String)
        ' Writes a string to your INI file
        WritePrivateProfileString(Section, Key, Value, strFilename)
        Flush()
    End Sub

    Public Sub WriteInteger(ByVal Section As String, _
    ByVal Key As String, ByVal Value As Integer)
        ' Writes an integer to your INI file
        WriteString(Section, Key, CStr(Value))
        Flush()
    End Sub

    Public Sub WriteBoolean(ByVal Section As String, _
    ByVal Key As String, ByVal Value As Boolean)
        ' Writes a boolean to your INI file
        WriteString(Section, Key, CStr(CInt(Value)))
        Flush()
    End Sub

    Private Sub Flush()
        ' Stores all the cached changes to your INI file
        FlushPrivateProfileString(0, 0, 0, strFilename)
    End Sub

End Class

Public Module EnvironmentEvents

#Region "Automatically generated code, do not modify"
    'Automatically generated code, do not modify
    'Event Sources Begin
    <System.ContextStaticAttribute()> Public WithEvents DTEEvents As EnvDTE.DTEEvents
    <System.ContextStaticAttribute()> Public WithEvents DocumentEvents As EnvDTE.DocumentEvents
    <System.ContextStaticAttribute()> Public WithEvents WindowEvents As EnvDTE.WindowEvents
    <System.ContextStaticAttribute()> Public WithEvents TaskListEvents As EnvDTE.TaskListEvents
    <System.ContextStaticAttribute()> Public WithEvents FindEvents As EnvDTE.FindEvents
    <System.ContextStaticAttribute()> Public WithEvents OutputWindowEvents As EnvDTE.OutputWindowEvents
    <System.ContextStaticAttribute()> Public WithEvents SelectionEvents As EnvDTE.SelectionEvents
    <System.ContextStaticAttribute()> Public WithEvents BuildEvents As EnvDTE.BuildEvents
    <System.ContextStaticAttribute()> Public WithEvents SolutionEvents As EnvDTE.SolutionEvents
    <System.ContextStaticAttribute()> Public WithEvents SolutionItemsEvents As EnvDTE.ProjectItemsEvents
    <System.ContextStaticAttribute()> Public WithEvents MiscFilesEvents As EnvDTE.ProjectItemsEvents
    <System.ContextStaticAttribute()> Public WithEvents DebuggerEvents As EnvDTE.DebuggerEvents
    <System.ContextStaticAttribute()> Public WithEvents ProjectsEvents As EnvDTE.ProjectsEvents
    <System.ContextStaticAttribute()> Public WithEvents TextDocumentKeyPressEvents As EnvDTE80.TextDocumentKeyPressEvents
    <System.ContextStaticAttribute()> Public WithEvents CodeModelEvents As EnvDTE80.CodeModelEvents
    <System.ContextStaticAttribute()> Public WithEvents DebuggerProcessEvents As EnvDTE80.DebuggerProcessEvents
    <System.ContextStaticAttribute()> Public WithEvents DebuggerExpressionEvaluationEvents As EnvDTE80.DebuggerExpressionEvaluationEvents
    'Event Sources End
    'End of automatically generated code
#End Region

    Public Sub DTEEvents_OnMacrosRuntimeReset() Handles DTEEvents.OnMacrosRuntimeReset
        DocumentEvents = DTE.Events.DocumentEvents
    End Sub

    Public ini As IniFile = New IniFile("VisualStudioSavedCursor.ini")

    Public Sub OnDocumentOpened(ByVal Document As EnvDTE.Document) Handles DocumentEvents.DocumentOpened
        Dim name As String = Document.FullName
        Dim selection As TextSelection = Document.Selection
        selection.MoveToLineAndOffset(ini.GetInteger(name, "startPoint.Line", 1), ini.GetInteger(name, "startPoint.Offset", 1))
        selection.TextPane.TryToShow(selection.ActivePoint.CreateEditPoint, vsPaneShowHow.vsPaneShowTop)
        selection.MoveToLineAndOffset(ini.GetInteger(name, "anchorPoint.Line", 1), ini.GetInteger(name, "anchorPoint.Offset", 1))
        selection.MoveToLineAndOffset(ini.GetInteger(name, "activePoint.Line", 1), ini.GetInteger(name, "activePoint.Offset", 1), True)
    End Sub

    Public Sub OnDocumentClosing(ByVal Document As EnvDTE.Document) Handles DocumentEvents.DocumentClosing
        Dim name As String = Document.FullName
        Dim selection As TextSelection = Document.Selection

        Dim startPoint As EnvDTE.TextPoint = selection.TextPane.StartPoint
        Dim anchorPoint As EnvDTE.VirtualPoint = selection.AnchorPoint
        Dim activePoint As EnvDTE.VirtualPoint = selection.ActivePoint

        ini.WriteInteger(name, "startPoint.Line", startPoint.Line)
        ini.WriteInteger(name, "startPoint.Offset", startPoint.LineCharOffset)
        ini.WriteInteger(name, "anchorPoint.Line", anchorPoint.Line)
        ini.WriteInteger(name, "anchorPoint.Offset", anchorPoint.LineCharOffset)
        ini.WriteInteger(name, "activePoint.Line", activePoint.Line)
        ini.WriteInteger(name, "activePoint.Offset", activePoint.LineCharOffset)
    End Sub
End Module

2

Re: "restore cursor position when file is open" for closed files

this plugin conflict with code definition window, how do i know if the document is open/close in which window, the editor or code definition window

3

Re: "restore cursor position when file is open" for closed files

mOo, thanks for posting the above contribution. It's good that it works in most cases, but you've actually hit the kind of brick wall that VS often presents, and the reason which is not easy to implement this kind of feature reliably. It's not easy to manage all extra interactions.

Maybe if you look through the DTE's interface there will be a way to get the code definition window, and then you will be able to compare window refs, hwnds, or titles, to tell which window is which. Or you could check the window's title, or the title of its tab in its tab group (if you can access that). Possibly, there are secondary attributes which you can check too (whether it's read-only, etc...), although it won't be as solid.

Good luck getting it to work, and thanks for posting your contributions here.

  -- Jon

4

Re: "restore cursor position when file is open" for closed files

well, i did a simple fix
by checking        "If activePoint.Line <> 1 Or activePoint.LineCharOffset <> 1 Then return"

    Public Sub OnDocumentOpened(ByVal Document As EnvDTE.Document) Handles DocumentEvents.DocumentOpened
        Dim name As String = Document.FullName
        Dim selection As TextSelection = Document.Selection
        Dim activePoint As EnvDTE.VirtualPoint = selection.ActivePoint
        If activePoint.Line <> 1 Or activePoint.LineCharOffset <> 1 Then
            Return
        End If

        selection.MoveToLineAndOffset(ini.GetInteger(name, "startPoint.Line", 1), ini.GetInteger(name, "startPoint.Offset", 1))
        selection.TextPane.TryToShow(selection.ActivePoint.CreateEditPoint, vsPaneShowHow.vsPaneShowTop)
        selection.MoveToLineAndOffset(ini.GetInteger(name, "anchorPoint.Line", 1), ini.GetInteger(name, "anchorPoint.Offset", 1))
        selection.MoveToLineAndOffset(ini.GetInteger(name, "activePoint.Line", 1), ini.GetInteger(name, "activePoint.Offset", 1), True)
    End Sub

Last edited by mOo (2009-04-08 01:06:50)

5

Re: "restore cursor position when file is open" for closed files

Good, that should help. I guess it won't be completely failproof, but I'm glad if it can help in most cases.

Thanks for posting it here.

  -- Jon

6

Re: "restore cursor position when file is open" for closed files

Hi, I searching plugin like this, can i have binaries for vs 2008? Because i dont know how to compile this (never used vb).
Thanks

7

Re: "restore cursor position when file is open" for closed files

Friuns, the above code is a macro you can enter into VS's macros IDE. Go to Tools | Macros -> Macros IDE, and you can enter the code there.

Regards,

  - Jon