comparison runtime/doc/if_tcl.txt @ 7:3fc0f57ecb91 v7.0001

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