blob: 79c43f5cb0a13a09928c76466b4353e455e578fd [file] [log] [blame]
Bram Moolenaara5792f52005-11-23 21:25:05 +00001*if_tcl.txt* For Vim version 7.0aa. Last change: 2005 Oct 14
Bram Moolenaar071d4272004-06-13 20:20:40 +00002
3
4 VIM REFERENCE MANUAL by Ingo Wilken
5
6
7The Tcl Interface to Vim *tcl* *Tcl* *TCL*
8
91. Commands |tcl-ex-commands|
102. Tcl commands |tcl-commands|
113. Tcl variables |tcl-variables|
124. Tcl window commands |tcl-window-cmds|
135. Tcl buffer commands |tcl-buffer-cmds|
146. Miscellaneous; Output from Tcl |tcl-misc| |tcl-output|
157. Known bugs & problems |tcl-bugs|
168. Examples |tcl-examples|
Bram Moolenaara5792f52005-11-23 21:25:05 +0000179. Dynamic loading |tcl-dynamic|
Bram Moolenaar071d4272004-06-13 20:20:40 +000018
19{Vi does not have any of these commands} *E280* *E281*
20
21The Tcl interface only works when Vim was compiled with the |+tcl| feature.
22
23WARNING: There are probably still some bugs. Please send bug reports,
24comments, ideas etc to <Ingo.Wilken@informatik.uni-oldenburg.de>
25
26==============================================================================
271. Commands *tcl-ex-commands* *E571* *E572*
28
29 *:tcl* *:tc*
30:tc[l] {cmd} Execute Tcl command {cmd}.
31
32:[range]tc[l] << {endmarker}
33{script}
34{endmarker}
35 Execute Tcl script {script}.
36 Note: This command doesn't work when the Tcl feature
37 wasn't compiled in. To avoid errors, see
38 |script-here|.
39
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000040{endmarker} must NOT be preceded by any white space. If {endmarker} is
Bram Moolenaar071d4272004-06-13 20:20:40 +000041omitted from after the "<<", a dot '.' must be used after {script}, like for
42the |:append| and |:insert| commands.
43This form of the |:tcl| command is mainly useful for including tcl code in Vim
44scripts.
45
46Example: >
47 function! DefineDate()
48 tcl << EOF
49 proc date {} {
50 return [clock format [clock seconds]]
51 }
52 EOF
53 endfunction
54<
55
56 *:tcldo* *:tcld*
57:[range]tcld[o] {cmd} Execute Tcl command {cmd} for each line in [range]
58 with the variable "line" being set to the text of each
59 line in turn, and "lnum" to the line number. Setting
60 "line" will change the text, but note that it is not
61 possible to add or delete lines using this command.
62 If {cmd} returns an error, the command is interrupted.
63 The default for [range] is the whole file: "1,$".
64 See |tcl-var-line| and |tcl-var-lnum|. {not in Vi}
65
66 *:tclfile* *:tclf*
67:tclf[ile] {file} Execute the Tcl script in {file}. This is the same as
68 ":tcl source {file}", but allows file name completion.
69 {not in Vi}
70
71
72Note that Tcl objects (like variables) persist from one command to the next,
73just as in the Tcl shell.
74
75Executing Tcl commands is not possible in the |sandbox|.
76
77==============================================================================
782. Tcl commands *tcl-commands*
79
80Tcl code gets all of its access to vim via commands in the "::vim" namespace.
81The following commands are implemented: >
82
83 ::vim::beep # Guess.
84 ::vim::buffer {n} # Create Tcl command for one buffer.
85 ::vim::buffer list # Create Tcl commands for all buffers.
86 ::vim::command [-quiet] {cmd} # Execute an ex command.
87 ::vim::expr {expr} # Use Vim's expression evaluator.
88 ::vim::option {opt} # Get vim option.
89 ::vim::option {opt} {val} # Set vim option.
90 ::vim::window list # Create Tcl commands for all windows.
91
92Commands:
93 ::vim::beep *tcl-beep*
94 Honk. Does not return a result.
95
96 ::vim::buffer {n} *tcl-buffer*
97 ::vim::buffer exists {n}
98 ::vim::buffer list
99 Provides access to vim buffers. With an integer argument, creates a
100 buffer command (see |tcl-buffer-cmds|) for the buffer with that
101 number, and returns its name as the result. Invalid buffer numbers
102 result in a standard Tcl error. To test for valid buffer numbers,
103 vim's internal functions can be used: >
104 set nbufs [::vim::expr bufnr("$")]
105 set isvalid [::vim::expr "bufexists($n)"]
106< The "list" option creates a buffer command for each valid buffer, and
107 returns a list of the command names as the result.
108 Example: >
109 set bufs [::vim::buffer list]
110 foreach b $bufs { $b append end "The End!" }
111< The "exists" option checks if a buffer with the given number exists.
112 Example: >
113 if { [::vim::buffer exists $n] } { ::vim::command ":e #$n" }
114< This command might be replaced by a variable in future versions.
115 See also |tcl-var-current| for the current buffer.
116
117 ::vim::command {cmd} *tcl-command*
118 ::vim::command -quiet {cmd}
119 Execute the vim (ex-mode) command {cmd}. Any ex command that affects
120 a buffer or window uses the current buffer/current window. Does not
121 return a result other than a standard Tcl error code. After this
122 command is completed, the "::vim::current" variable is updated.
123 The "-quiet" flag suppresses any error messages from vim.
124 Examples: >
125 ::vim::command "set ts=8"
126 ::vim::command "%s/foo/bar/g"
127< To execute normal-mode commands, use "normal" (see |:normal|): >
128 set cmd "jj"
129 ::vim::command "normal $cmd"
130< See also |tcl-window-command| and |tcl-buffer-command|.
131
132 ::vim::expr {expr} *tcl-expr*
133 Evaluates the expression {expr} using vim's internal expression
134 evaluator (see |expression|). Any expression that queries a buffer
135 or window property uses the current buffer/current window. Returns
136 the result as a string.
137 Examples: >
138 set perl_available [::vim::expr has("perl")]
139< See also |tcl-window-expr| and |tcl-buffer-expr|.
140
141 ::vim::option {opt} *tcl-option*
142 ::vim::option {opt} {value}
143 Without second argument, queries the value of a vim option. With this
144 argument, sets the vim option to {value}, and returns the previous
145 value as the result. Any options that are marked as 'local to buffer'
146 or 'local to window' affect the current buffer/current window. The
147 global value is not changed, use the ":set" command for that. For
148 boolean options, {value} should be "0" or "1", or any of the keywords
149 "on", "off" or "toggle". See |option-summary| for a list of options.
150 Example: >
151 ::vim::option ts 8
152< See also |tcl-window-option| and |tcl-buffer-option|.
153
154 ::vim::window {option} *tcl-window*
155 Provides access to vim windows. Currently only the "list" option is
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000156 implemented. This creates a window command (see |tcl-window-cmds|) for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 each window, and returns a list of the command names as the result.
158 Example: >
159 set wins [::vim::window list]
160 foreach w $wins { $w height 4 }
161< This command might be replaced by a variable in future versions.
162 See also |tcl-var-current| for the current window.
163
164==============================================================================
1653. Tcl variables *tcl-variables*
166
167The ::vim namespace contains a few variables. These are created when the Tcl
168interpreter is called from vim and set to current values. >
169
170 ::vim::current # array containing "current" objects
171 ::vim::lbase # number of first line
172 ::vim::range # array containing current range numbers
173 line # current line as a string (:tcldo only)
174 lnum # current line number (:tcldo only)
175
176Variables:
177 ::vim::current *tcl-var-current*
178 This is an array providing access to various "current" objects
179 available in vim. The contents of this array are updated after
180 "::vim::command" is called, as this might change vim's current
181 settings (e.g., by deleting the current buffer).
182 The "buffer" element contains the name of the buffer command for the
183 current buffer. This can be used directly to invoke buffer commands
184 (see |tcl-buffer-cmds|). This element is read-only.
185 Example: >
186 $::vim::current(buffer) insert begin "Hello world"
187< The "window" element contains the name of the window command for the
188 current window. This can be used directly to invoke window commands
189 (see |tcl-window-cmds|). This element is read-only.
190 Example: >
191 $::vim::current(window) height 10
192<
193 ::vim::lbase *tcl-var-lbase*
194 This variable controls how Tcl treats line numbers. If it is set to
195 '1', then lines and columns start at 1. This way, line numbers from
196 Tcl commands and vim expressions are compatible. If this variable is
197 set to '0', then line numbers and columns start at 0 in Tcl. This is
198 useful if you want to treat a buffer as a Tcl list or a line as a Tcl
199 string and use standard Tcl commands that return an index ("lsort" or
200 "string first", for example). The default value is '1'. Currently,
201 any non-zero values is treated as '1', but your scripts should not
202 rely on this. See also |tcl-linenumbers|.
203
204 ::vim::range *tcl-var-range*
205 This is an array with three elements, "start", "begin" and "end". It
206 contains the line numbers of the start and end row of the current
207 range. "begin" is the same as "start". This variable is read-only.
208 See |tcl-examples|.
209
210 line *tcl-var-line*
211 lnum *tcl-var-lnum*
212 These global variables are only available if the ":tcldo" ex command
213 is being executed. They contain the text and line number of the
214 current line. When the Tcl command invoked by ":tcldo" is completed,
215 the current line is set to the contents of the "line" variable, unless
216 the variable was unset by the Tcl command. The "lnum" variable is
217 read-only. These variables are not in the "::vim" namespace so they
218 can be used in ":tcldo" without much typing (this might be changed in
219 future versions). See also |tcl-linenumbers|.
220
221==============================================================================
2224. Tcl window commands *tcl-window-cmds*
223
224Window commands represent vim windows. They are created by several commands:
225 ::vim::window list |tcl-window|
226 "windows" option of a buffer command |tcl-buffer-windows|
227The ::vim::current(window) variable contains the name of the window command
228for the current window. A window command is automatically deleted when the
229corresponding vim window is closed.
230
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000231Let's assume the name of the window command is stored in the Tcl variable "win",
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232i.e. "$win" calls the command. The following options are available: >
233
234 $win buffer # Create Tcl command for window's buffer.
235 $win command {cmd} # Execute ex command in windows context.
236 $win cursor # Get current cursor position.
237 $win cursor {var} # Set cursor position from array variable.
238 $win cursor {row} {col} # Set cursor position.
239 $win delcmd {cmd} # Call Tcl command when window is closed.
240 $win expr {expr} # Evaluate vim expression in windows context.
241 $win height # Report the window's height.
242 $win height {n} # Set the window's height.
243 $win option {opt} [val] # Get/Set vim option in windows context.
244
245Options:
246 $win buffer *tcl-window-buffer*
247 Creates a Tcl command for the window's buffer, and returns its name as
248 the result. The name should be stored in a variable: >
249 set buf [$win buffer]
250< $buf is now a valid Tcl command. See |tcl-buffer-cmds| for the
251 available options.
252
253 $win cursor *tcl-window-cursor*
254 $win cursor {var}
255 $win cursor {row} {col}
256 Without argument, reports the current cursor position as a string.
257 This can be converted to a Tcl array variable: >
258 array set here [$win cursor]
259< "here(row)" and "here(column)" now contain the cursor position.
260 With a single argument, the argument is interpreted as the name of a
261 Tcl array variable, which must contain two elements "row" and "column".
262 These are used to set the cursor to the new position: >
263 $win cursor here ;# not $here !
264< With two arguments, sets the cursor to the specified row and column: >
265 $win cursor $here(row) $here(column)
266< Invalid positions result in a standard Tcl error, which can be caught
267 with "catch". The row and column values depend on the "::vim::lbase"
268 variable. See |tcl-var-lbase|.
269
270 $win delcmd {cmd} *tcl-window-delcmd*
271 Registers the Tcl command {cmd} as a deletion callback for the window.
272 This command is executed (in the global scope) just before the window
273 is closed. Complex commands should be build with "list": >
274 $win delcmd [list puts vimerr "window deleted"]
275< See also |tcl-buffer-delcmd|.
276
277 $win height *tcl-window-height*
278 $win height {n}
279 Without argument, reports the window's current height. With an
280 argument, tries to set the window's height to {n}, then reports the
281 new height (which might be different from {n}).
282
283 $win command [-quiet] {cmd} *tcl-window-command*
284 $win expr {expr} *tcl-window-expr*
285 $win option {opt} [val] *tcl-window-option*
286 These are similar to "::vim::command" etc., except that everything is
287 done in the context of the window represented by $win, instead of the
288 current window. For example, setting an option that is marked 'local
289 to window' affects the window $win. Anything that affects or queries
290 a buffer uses the buffer displayed in this window (i.e. the buffer
291 that is represented by "$win buffer"). See |tcl-command|, |tcl-expr|
292 and |tcl-option| for more information.
293 Example: >
294 $win option number on
295
296==============================================================================
2975. Tcl buffer commands *tcl-buffer-cmds*
298
299Buffer commands represent vim buffers. They are created by several commands:
300 ::vim::buffer {N} |tcl-buffer|
301 ::vim::buffer list |tcl-buffer|
302 "buffer" option of a window command |tcl-window-buffer|
303The ::vim::current(buffer) variable contains the name of the buffer command
304for the current buffer. A buffer command is automatically deleted when the
305corresponding vim buffer is destroyed. Whenever the buffer's contents are
306changed, all marks in the buffer are automatically adjusted. Any changes to
307the buffer's contents made by Tcl commands can be undone with the "undo" vim
308command (see |undo|).
309
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000310Let's assume the name of the buffer command is stored in the Tcl variable "buf",
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311i.e. "$buf" calls the command. The following options are available: >
312
313 $buf append {n} {str} # Append a line to buffer, after line {n}.
314 $buf command {cmd} # Execute ex command in buffers context.
315 $buf count # Report number of lines in buffer.
316 $buf delcmd {cmd} # Call Tcl command when buffer is deleted.
317 $buf delete {n} # Delete a single line.
318 $buf delete {n} {m} # Delete several lines.
319 $buf expr {expr} # Evaluate vim expression in buffers context.
320 $buf get {n} # Get a single line as a string.
321 $buf get {n} {m} # Get several lines as a list.
322 $buf insert {n} {str} # Insert a line in buffer, as line {n}.
323 $buf last # Report line number of last line in buffer.
324 $buf mark {mark} # Report position of buffer mark.
325 $buf name # Report name of file in buffer.
326 $buf number # Report number of this buffer.
327 $buf option {opt} [val] # Get/Set vim option in buffers context.
328 $buf set {n} {text} # Replace a single line.
329 $buf set {n} {m} {list} # Replace several lines.
330 $buf windows # Create Tcl commands for buffer's windows.
331<
332 *tcl-linenumbers*
333Most buffer commands take line numbers as arguments. How Tcl treats these
334numbers depends on the "::vim::lbase" variable (see |tcl-var-lbase|). Instead
335of line numbers, several keywords can be also used: "top", "start", "begin",
336"first", "bottom", "end" and "last".
337
338Options:
339 $buf append {n} {str} *tcl-buffer-append*
340 $buf insert {n} {str} *tcl-buffer-insert*
341 Add a line to the buffer. With the "insert" option, the string
342 becomes the new line {n}, with "append" it is inserted after line {n}.
343 Example: >
344 $buf insert top "This is the beginning."
345 $buf append end "This is the end."
346< To add a list of lines to the buffer, use a loop: >
347 foreach line $list { $buf append $num $line ; incr num }
348<
349 $buf count *tcl-buffer-count*
350 Reports the total number of lines in the buffer.
351
352 $buf delcmd {cmd} *tcl-buffer-delcmd*
353 Registers the Tcl command {cmd} as a deletion callback for the buffer.
354 This command is executed (in the global scope) just before the buffer
355 is deleted. Complex commands should be build with "list": >
356 $buf delcmd [list puts vimerr "buffer [$buf number] gone"]
357< See also |tcl-window-delcmd|.
358
359 $buf delete {n} *tcl-buffer-delete*
360 $buf delete {n} {m}
361 Deletes line {n} or lines {n} through {m} from the buffer.
362 This example deletes everything except the last line: >
363 $buf delete first [expr [$buf last] - 1]
364<
365 $buf get {n} *tcl-buffer-get*
366 $buf get {n} {m}
367 Gets one or more lines from the buffer. For a single line, the result
368 is a string; for several lines, a list of strings.
369 Example: >
370 set topline [$buf get top]
371<
372 $buf last *tcl-buffer-last*
373 Reports the line number of the last line. This value depends on the
374 "::vim::lbase" variable. See |tcl-var-lbase|.
375
376 $buf mark {mark} *tcl-buffer-mark*
377 Reports the position of the named mark as a string, similar to the
378 cursor position of the "cursor" option of a window command (see
379 |tcl-window-cursor|). This can be converted to a Tcl array variable: >
380 array set mpos [$buf mark "a"]
381< "mpos(column)" and "mpos(row)" now contain the position of the mark.
382 If the mark is not set, a standard Tcl error results.
383
384 $buf name
385 Reports the name of the file in the buffer. For a buffer without a
386 file, this is an empty string.
387
388 $buf number
389 Reports the number of this buffer. See |:buffers|.
390 This example deletes a buffer from vim: >
391 ::vim::command "bdelete [$buf number]"
392<
393 $buf set {n} {string} *tcl-buffer-set*
394 $buf set {n} {m} {list}
395 Replace one or several lines in the buffer. If the list contains more
396 elements than there are lines to replace, they are inserted into the
397 buffer. If the list contains fewer elements, any unreplaced line is
398 deleted from the buffer.
399
400 $buf windows *tcl-buffer-windows*
401 Creates a window command for each window that displays this buffer, and
402 returns a list of the command names as the result.
403 Example: >
404 set winlist [$buf windows]
405 foreach win $winlist { $win height 4 }
406< See |tcl-window-cmds| for the available options.
407
408 $buf command [-quiet] {cmd} *tcl-buffer-command*
409 $buf expr {exr} *tcl-buffer-expr*
410 $buf option {opt} [val] *tcl-buffer-option*
411 These are similar to "::vim::command" etc., except that everything is
412 done in the context of the buffer represented by $buf, instead of the
413 current buffer. For example, setting an option that is marked 'local
414 to buffer' affects the buffer $buf. Anything that affects or queries
415 a window uses the first window in vim's window list that displays this
416 buffer (i.e. the first entry in the list returned by "$buf windows").
417 See |tcl-command|, |tcl-expr| and |tcl-option| for more information.
418 Example: >
419 if { [$buf option modified] } { $buf command "w" }
420
421==============================================================================
4226. Miscellaneous; Output from Tcl *tcl-misc* *tcl-output*
423
424The standard Tcl commands "exit" and "catch" are replaced by custom versions.
425"exit" terminates the current Tcl script and returns to vim, which deletes the
426Tcl interpreter. Another call to ":tcl" then creates a new Tcl interpreter.
427"exit" does NOT terminate vim! "catch" works as before, except that it does
428not prevent script termination from "exit". An exit code != 0 causes the ex
429command that invoked the Tcl script to return an error.
430
431Two new I/O streams are available in Tcl, "vimout" and "vimerr". All output
432directed to them is displayed in the vim message area, as information messages
433and error messages, respectively. The standard Tcl output streams stdout and
434stderr are mapped to vimout and vimerr, so that a normal "puts" command can be
435used to display messages in vim.
436
437==============================================================================
4387. Known bugs & problems *tcl-bugs*
439
440Calling one of the Tcl ex commands from inside Tcl (via "::vim::command") may
441have unexpected side effects. The command creates a new interpreter, which
442has the same abilities as the standard interpreter - making "::vim::command"
443available in a safe child interpreter therefore makes the child unsafe. (It
444would be trivial to block nested :tcl* calls or ensure that such calls from a
445safe interpreter create only new safe interpreters, but quite pointless -
446depending on vim's configuration, "::vim::command" may execute arbitrary code
447in any number of other scripting languages.) A call to "exit" within this new
448interpreter does not affect the old interpreter; it only terminates the new
449interpreter, then script processing continues normally in the old interpreter.
450
451Input from stdin is currently not supported.
452
453==============================================================================
4548. Examples: *tcl-examples*
455
456Here are a few small (and maybe useful) Tcl scripts.
457
458This script sorts the lines of the entire buffer (assume it contains a list
459of names or something similar):
460 set buf $::vim::current(buffer)
461 set lines [$buf get top bottom]
462 set lines [lsort -dictionary $lines]
463 $buf set top bottom $lines
464
465This script reverses the lines in the buffer. Note the use of "::vim::lbase"
466and "$buf last" to work with any line number setting.
467 set buf $::vim::current(buffer)
468 set t $::vim::lbase
469 set b [$buf last]
470 while { $t < $b } {
471 set tl [$buf get $t]
472 set bl [$buf get $b]
473 $buf set $t $bl
474 $buf set $b $tl
475 incr t
476 incr b -1
477 }
478
479This script adds a consecutive number to each line in the current range:
480 set buf $::vim::current(buffer)
481 set i $::vim::range(start)
482 set n 1
483 while { $i <= $::vim::range(end) } {
484 set line [$buf get $i]
485 $buf set $i "$n\t$line"
486 incr i ; incr n
487 }
488
489The same can also be done quickly with two ex commands, using ":tcldo":
490 :tcl set n 1
491 :[range]tcldo set line "$n\t$line" ; incr n
492
493This procedure runs an ex command on each buffer (idea stolen from Ron Aaron):
494 proc eachbuf { cmd } {
495 foreach b [::vim::buffer list] {
496 $b command $cmd
497 }
498 }
499Use it like this:
500 :tcl eachbuf %s/foo/bar/g
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000501Be careful with Tcl's string and backslash substitution, tough. If in doubt,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502surround the ex command with curly braces.
503
504
505If you want to add some Tcl procedures permanently to vim, just place them in
506a file (e.g. "~/.vimrc.tcl" on Unix machines), and add these lines to your
507startup file (usually "~/.vimrc" on Unix):
508 if has("tcl")
509 tclfile ~/.vimrc.tcl
510 endif
511
512==============================================================================
Bram Moolenaara5792f52005-11-23 21:25:05 +00005139. Dynamic loading *tcl-dynamic*
514
515On MS-Windows the Tcl library can be loaded dynamically. The |:version|
516output then includes |+tcl/dyn|.
517
518This means that Vim will search for the Tcl DLL file only when needed. When
519you don't use the Tcl interface you don't need it, thus you can use Vim
520without this DLL file.
521
522To use the Tcl interface the Tcl DLL must be in your search path. In a
523console window type "path" to see what directories are used.
524
525The name of the DLL must match the Tcl version Vim was compiled with.
526Currently the name is "tcl83.dll". That is for Tcl 8.3. To know for sure
527edit "gvim.exe" and search for "tcl\d*.dll\c".
528
529==============================================================================
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 vim:tw=78:ts=8:ft=help:norl: