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