if_ole.txt 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. *if_ole.txt* For Vim version 9.0. Last change: 2022 Oct 08
  2. VIM REFERENCE MANUAL by Paul Moore
  3. The OLE Interface to Vim *ole-interface*
  4. 1. Activation |ole-activation|
  5. 2. Methods |ole-methods|
  6. 3. The "normal" command |ole-normal|
  7. 4. Registration |ole-registration|
  8. 5. MS Visual Studio integration |MSVisualStudio|
  9. {only available when compiled with the |+ole| feature. See
  10. src/if_ole.INSTALL}
  11. An alternative is using the client-server communication |clientserver|.
  12. ==============================================================================
  13. 1. Activation *ole-activation*
  14. Vim acts as an OLE automation server, accessible from any automation client,
  15. for example, Visual Basic, Python, or Perl. The Vim application "name" (its
  16. "ProgID", in OLE terminology) is "Vim.Application".
  17. Hence, in order to start a Vim instance (or connect to an already running
  18. instance), code similar to the following should be used:
  19. [Visual Basic] >
  20. Dim Vim As Object
  21. Set Vim = CreateObject("Vim.Application")
  22. [Python] >
  23. from win32com.client.dynamic import Dispatch
  24. vim = Dispatch('Vim.Application')
  25. [Perl] >
  26. use Win32::OLE;
  27. $vim = new Win32::OLE 'Vim.Application';
  28. [C#] >
  29. // Add a reference to Vim in your project.
  30. // Choose the COM tab.
  31. // Select "Vim Ole Interface 1.1 Type Library"
  32. Vim.Vim vimobj = new Vim.Vim();
  33. Vim does not support acting as a "hidden" OLE server, like some other OLE
  34. Automation servers. When a client starts up an instance of Vim, that instance
  35. is immediately visible. Simply closing the OLE connection to the Vim instance
  36. is not enough to shut down the Vim instance - it is necessary to explicitly
  37. execute a quit command (for example, :qa!, :wqa).
  38. ==============================================================================
  39. 2. Methods *ole-methods*
  40. Vim exposes four methods for use by clients.
  41. *ole-sendkeys*
  42. SendKeys(keys) Execute a series of keys.
  43. This method takes a single parameter, which is a string of keystrokes. These
  44. keystrokes are executed exactly as if they had been types in at the keyboard.
  45. Special keys can be given using their <..> names, as for the right hand side
  46. of a mapping. Note: Execution of the Ex "normal" command is not supported -
  47. see below |ole-normal|.
  48. Examples (Visual Basic syntax) >
  49. Vim.SendKeys "ihello<Esc>"
  50. Vim.SendKeys "ma1GV4jy`a"
  51. These examples assume that Vim starts in Normal mode. To force Normal mode,
  52. start the key sequence with CTRL-\ CTRL-N as in >
  53. Vim.SendKeys "<C-\><C-N>ihello<Esc>"
  54. CTRL-\ CTRL-N returns Vim to Normal mode, when in Insert or Command-line mode.
  55. Note that this doesn't work halfway a Vim command
  56. *ole-eval*
  57. Eval(expr) Evaluate an expression.
  58. This method takes a single parameter, which is an expression in Vim's normal
  59. format (see |expression|). It returns a string, which is the result of
  60. evaluating the expression. A |List| is turned into a string by joining the
  61. items and inserting line breaks.
  62. Examples (Visual Basic syntax) >
  63. Line20 = Vim.Eval("getline(20)")
  64. Twelve = Vim.Eval("6 + 6") ' Note this is a STRING
  65. Font = Vim.Eval("&guifont")
  66. <
  67. *ole-setforeground*
  68. SetForeground() Make the Vim window come to the foreground
  69. This method takes no arguments. No value is returned.
  70. Example (Visual Basic syntax) >
  71. Vim.SetForeground
  72. <
  73. *ole-gethwnd*
  74. GetHwnd() Return the handle of the Vim window.
  75. This method takes no arguments. It returns the hwnd of the main Vimwindow.
  76. You can use this if you are writing something which needs to manipulate the
  77. Vim window, or to track it in the z-order, etc.
  78. Example (Visual Basic syntax) >
  79. Vim_Hwnd = Vim.GetHwnd
  80. <
  81. ==============================================================================
  82. 3. The "normal" command *ole-normal*
  83. Due to the way Vim processes OLE Automation commands, combined with the method
  84. of implementation of the Ex command :normal, it is not possible to execute the
  85. :normal command via OLE automation. Any attempt to do so will fail, probably
  86. harmlessly, although possibly in unpredictable ways.
  87. There is currently no practical way to trap this situation, and users must
  88. simply be aware of the limitation.
  89. ==============================================================================
  90. 4. Registration *ole-registration* *E243*
  91. Before Vim will act as an OLE server, it must be registered in the system
  92. registry. In order to do this, Vim should be run with a single parameter of
  93. "-register".
  94. *-register* >
  95. gvim -register
  96. If gvim with OLE support is run and notices that no Vim OLE server has been
  97. registered, it will present a dialog and offers you the choice to register by
  98. clicking "Yes".
  99. In some situations registering is not possible. This happens when the
  100. registry is not writable. If you run into this problem you need to run gvim
  101. as "Administrator".
  102. Once vim is registered, the application path is stored in the registry.
  103. Before moving, deleting, or upgrading Vim, the registry entries should be
  104. removed using the "-unregister" switch.
  105. *-unregister* >
  106. gvim -unregister
  107. The OLE mechanism will use the first registered Vim it finds. If a Vim is
  108. already running, this one will be used. If you want to have (several) Vim
  109. sessions open that should not react to OLE commands, use the non-OLE version,
  110. and put it in a different directory. The OLE version should then be put in a
  111. directory that is not in your normal path, so that typing "gvim" will start
  112. the non-OLE version.
  113. *-silent*
  114. To avoid the message box that pops up to report the result, prepend "-silent":
  115. >
  116. gvim -silent -register
  117. gvim -silent -unregister
  118. ==============================================================================
  119. 5. MS Visual Studio integration *MSVisualStudio*
  120. The old "VisVim" integration was removed from Vim in patch 9.0.0698.
  121. Using Vim with Visual Studio .Net~
  122. .Net studio has support for external editors. Follow these directions:
  123. In .Net Studio choose from the menu Tools->External Tools...
  124. Add
  125. Title - Vim
  126. Command - c:\vim\vim63\gvim.exe
  127. Arguments - --servername VS_NET --remote-silent "+call cursor($(CurLine), $(CurCol))" $(ItemPath)
  128. Init Dir - Empty
  129. Now, when you open a file in .Net, you can choose from the .Net menu:
  130. Tools->Vim
  131. That will open the file in Vim.
  132. You can then add this external command as an icon and place it anywhere you
  133. like. You might also be able to set this as your default editor.
  134. If you refine this further, please post back to the Vim maillist so we have a
  135. record of it.
  136. --servername VS_NET
  137. This will create a new instance of vim called VS_NET. So if you open multiple
  138. files from VS, they will use the same instance of Vim. This allows you to
  139. have multiple copies of Vim running, but you can control which one has VS
  140. files in it.
  141. --remote-silent "+call cursor(10, 27)"
  142. - Places the cursor on line 10 column 27
  143. In Vim >
  144. :h --remote-silent for more details
  145. [.Net remarks provided by Dave Fishburn and Brian Sturk]
  146. ==============================================================================
  147. vim:tw=78:ts=8:noet:ft=help:norl: