Mercurial > vim
annotate src/if_tcl.c @ 21454:c50b04b76bc2
Added tag v8.2.1277 for changeset 40210bb08e3f3e696b33154b2ec02e1debb66e3c
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Thu, 23 Jul 2020 15:00:05 +0200 |
parents | 3af71cbcfdbe |
children | c0880eafe162 |
rev | line source |
---|---|
10042
4aead6a9b7a9
commit https://github.com/vim/vim/commit/edf3f97ae2af024708ebb4ac614227327033ca47
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
1 /* vi:set ts=8 sts=4 sw=4 noet: |
7 | 2 * |
3 * VIM - Vi IMproved by Bram Moolenaar | |
4 * | |
5 * Do ":help uganda" in Vim to read copying and usage conditions. | |
6 * Do ":help credits" in Vim to see a list of people who contributed. | |
7 * See README.txt for an overview of the Vim source code. | |
8 */ | |
9 | |
10 /* | |
11 * Tcl extensions by Ingo Wilken <Ingo.Wilken@informatik.uni-oldenburg.de> | |
12 * Last modification: Wed May 10 21:28:44 CEST 2000 | |
13 * Requires Tcl 8.0 or higher. | |
14 * | |
15 * Variables: | |
16 * ::vim::current(buffer) # Name of buffer command for current buffer. | |
17 * ::vim::current(window) # Name of window command for current window. | |
18 * ::vim::range(start) # Start of current range (line number). | |
19 * ::vim::range(end) # End of current range (line number). | |
20 * ::vim::lbase # Start of line/column numbers (1 or 0). | |
21 * | |
22 * Commands: | |
23 * ::vim::command {cmd} # Execute ex command {cmd}. | |
24 * ::vim::option {opt} [val] # Get/Set option {opt}. | |
25 * ::vim::expr {expr} # Evaluate {expr} using vim's evaluator. | |
26 * ::vim::beep # Guess. | |
27 * | |
28 * set buf [::vim::buffer {n}] # Create Tcl command for buffer N. | |
29 * set bl [::vim::buffer list] # Get list of Tcl commands of all buffers. | |
30 * ::vim::buffer exists {n} # True if buffer {n} exists. | |
31 * | |
32 * set wl [::vim::window list] # Get list of Tcl commands of all windows. | |
33 * | |
34 * set n [$win height] # Report window height. | |
35 * $win height {n} # Set window height to {n}. | |
36 * array set pos [$win cursor] # Get cursor position. | |
37 * $win cursor {row} {col} # Set cursor position. | |
38 * $win cursor pos # Set cursor position from array var "pos" | |
39 * $win delcmd {cmd} # Register callback command for closed window. | |
40 * $win option {opt} [val] # Get/Set vim option in context of $win. | |
41 * $win command {cmd} # Execute ex command in context of $win. | |
42 * $win expr {expr} # Evaluate vim expression in context of $win. | |
43 * set buf [$win buffer] # Create Tcl command for window's buffer. | |
44 * | |
45 * $buf name # Reports file name in buffer. | |
46 * $buf number # Reports buffer number. | |
47 * set l [$buf get {n}] # Get buffer line {n} as a string. | |
48 * set L [$buf get {n} {m}] # Get lines {n} through {m} as a list. | |
49 * $buf count # Reports number of lines in buffer. | |
50 * $buf last # Reports number of last line in buffer. | |
51 * $buf delete {n} # Delete line {n}. | |
52 * $buf delete {n} {m} # Delete lines {n} through {m}. | |
53 * $buf set {n} {l} # Set line {n} to string {l}. | |
54 * $buf set {n} {m} {L} # Set lines {n} through {m} from list {L}. | |
55 * # Delete/inserts lines as appropriate. | |
56 * $buf option {opt} [val] # Get/Set vim option in context of $buf. | |
57 * $buf command {cmd} # Execute ex command in context of $buf | |
58 * $buf expr {cmd} # Evaluate vim expression in context of $buf. | |
59 * array set pos [$buf mark {m}] # Get position of mark. | |
60 * $buf append {n} {str} # Append string {str} to buffer,after line {n}. | |
61 * $buf insert {n} {str} # Insert string {str} in buffer as line {n}. | |
62 * $buf delcmd {cmd} # Register callback command for deleted buffer. | |
63 * set wl [$buf windows] # Get list of Tcl commands for all windows of | |
64 * # this buffer. | |
65 TODO: | |
66 * ::vim::buffer new # create new buffer + Tcl command | |
67 */ | |
68 | |
69 #include "vim.h" | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
70 #undef EXTERN // tcl.h defines it too |
7 | 71 |
72 #ifdef DYNAMIC_TCL | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
73 # define USE_TCL_STUBS // use tcl's stubs mechanism |
7 | 74 #endif |
75 | |
76 #include <tcl.h> | |
77 #include <string.h> | |
78 | |
79 typedef struct | |
80 { | |
81 Tcl_Interp *interp; | |
3369 | 82 int exitvalue; |
7 | 83 int range_start, range_end; |
84 int lbase; | |
85 char *curbuf, *curwin; | |
86 } tcl_info; | |
87 | |
3369 | 88 static tcl_info tclinfo = { NULL, 0, 0, 0, 0, NULL, NULL }; |
7 | 89 |
90 #define VAR_RANGE1 "::vim::range(start)" | |
91 #define VAR_RANGE2 "::vim::range(begin)" | |
92 #define VAR_RANGE3 "::vim::range(end)" | |
93 #define VAR_CURBUF "::vim::current(buffer)" | |
94 #define VAR_CURWIN "::vim::current(window)" | |
95 #define VAR_LBASE "::vim::lbase" | |
96 #define VAR_CURLINE "line" | |
97 #define VAR_CURLNUM "lnum" | |
98 #define VARNAME_SIZE 64 | |
99 | |
100 #define row2tcl(x) ((x) - (tclinfo.lbase==0)) | |
101 #define row2vim(x) ((x) + (tclinfo.lbase==0)) | |
102 #define col2tcl(x) ((x) + (tclinfo.lbase!=0)) | |
103 #define col2vim(x) ((x) - (tclinfo.lbase!=0)) | |
104 | |
105 | |
106 #define VIMOUT ((ClientData)1) | |
107 #define VIMERR ((ClientData)2) | |
108 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
109 // This appears to be new in Tcl 8.4. |
135 | 110 #ifndef CONST84 |
111 # define CONST84 | |
112 #endif | |
113 | |
7 | 114 /* |
115 * List of Tcl interpreters who reference a vim window or buffer. | |
15034
6e4e0d43b20b
patch 8.1.0528: various typos in comments
Bram Moolenaar <Bram@vim.org>
parents:
14395
diff
changeset
|
116 * Each buffer and window has its own list in the w_tcl_ref or b_tcl_ref |
502 | 117 * struct member. We need this because Tcl can create sub-interpreters with |
118 * the "interp" command, and each interpreter can reference all windows and | |
119 * buffers. | |
7 | 120 */ |
121 struct ref | |
122 { | |
123 struct ref *next; | |
124 | |
125 Tcl_Interp *interp; | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
126 Tcl_Command cmd; // Tcl command that represents this object |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
127 Tcl_Obj *delcmd; // Tcl command to call when object is being del. |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
128 void *vimobj; // Vim window or buffer (win_T* or buf_T*) |
7 | 129 }; |
130 static char * tclgetbuffer _ANSI_ARGS_((Tcl_Interp *interp, buf_T *buf)); | |
131 static char * tclgetwindow _ANSI_ARGS_((Tcl_Interp *interp, win_T *win)); | |
132 static int tclsetdelcmd _ANSI_ARGS_((Tcl_Interp *interp, struct ref *reflist, void *vimobj, Tcl_Obj *delcmd)); | |
133 static int tclgetlinenum _ANSI_ARGS_ ((Tcl_Interp *interp, Tcl_Obj *obj, int *valueP, buf_T *buf)); | |
134 static win_T *tclfindwin _ANSI_ARGS_ ((buf_T *buf)); | |
135 static int tcldoexcommand _ANSI_ARGS_ ((Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[], int objn)); | |
136 static int tclsetoption _ANSI_ARGS_ ((Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[], int objn)); | |
137 static int tclvimexpr _ANSI_ARGS_ ((Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[], int objn)); | |
138 static void tcldelthisinterp _ANSI_ARGS_ ((void)); | |
139 | |
140 static int vimerror _ANSI_ARGS_((Tcl_Interp *interp)); | |
141 static void tclmsg _ANSI_ARGS_((char *text)); | |
142 static void tclerrmsg _ANSI_ARGS_((char *text)); | |
143 static void tclupdatevars _ANSI_ARGS_((void)); | |
144 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
145 static struct ref refsdeleted; // dummy object for deleted ref list |
7 | 146 |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
147 ////////////////////////////////////////////////////////////////////////////// |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
148 // TCL interface manager |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
149 //////////////////////////////////////////////////////////////////////////// |
7 | 150 |
151 #if defined(DYNAMIC_TCL) || defined(PROTO) | |
152 # ifndef DYNAMIC_TCL_DLL | |
153 # define DYNAMIC_TCL_DLL "tcl83.dll" | |
154 # endif | |
155 # ifndef DYNAMIC_TCL_VER | |
156 # define DYNAMIC_TCL_VER "8.3" | |
157 # endif | |
158 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
159 # ifndef DYNAMIC_TCL // Just generating prototypes |
7 | 160 typedef int HANDLE; |
161 # endif | |
162 | |
15868
7fad90423bd2
patch 8.1.0941: macros for MS-Windows are inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15543
diff
changeset
|
163 # ifndef MSWIN |
7538
c9fc24b76293
commit https://github.com/vim/vim/commit/8a5115cf18751022387af2085f374d38c60dde83
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
164 # include <dlfcn.h> |
c9fc24b76293
commit https://github.com/vim/vim/commit/8a5115cf18751022387af2085f374d38c60dde83
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
165 # define HANDLE void* |
c9fc24b76293
commit https://github.com/vim/vim/commit/8a5115cf18751022387af2085f374d38c60dde83
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
166 # define TCL_PROC void* |
c9fc24b76293
commit https://github.com/vim/vim/commit/8a5115cf18751022387af2085f374d38c60dde83
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
167 # define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL) |
c9fc24b76293
commit https://github.com/vim/vim/commit/8a5115cf18751022387af2085f374d38c60dde83
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
168 # define symbol_from_dll dlsym |
c9fc24b76293
commit https://github.com/vim/vim/commit/8a5115cf18751022387af2085f374d38c60dde83
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
169 # define close_dll dlclose |
c9fc24b76293
commit https://github.com/vim/vim/commit/8a5115cf18751022387af2085f374d38c60dde83
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
170 # else |
c9fc24b76293
commit https://github.com/vim/vim/commit/8a5115cf18751022387af2085f374d38c60dde83
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
171 # define TCL_PROC FARPROC |
c9fc24b76293
commit https://github.com/vim/vim/commit/8a5115cf18751022387af2085f374d38c60dde83
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
172 # define load_dll vimLoadLib |
c9fc24b76293
commit https://github.com/vim/vim/commit/8a5115cf18751022387af2085f374d38c60dde83
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
173 # define symbol_from_dll GetProcAddress |
c9fc24b76293
commit https://github.com/vim/vim/commit/8a5115cf18751022387af2085f374d38c60dde83
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
174 # define close_dll FreeLibrary |
c9fc24b76293
commit https://github.com/vim/vim/commit/8a5115cf18751022387af2085f374d38c60dde83
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
175 # endif |
c9fc24b76293
commit https://github.com/vim/vim/commit/8a5115cf18751022387af2085f374d38c60dde83
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
176 |
7 | 177 /* |
1890 | 178 * Declare HANDLE for tcl.dll and function pointers. |
7 | 179 */ |
180 static HANDLE hTclLib = NULL; | |
181 Tcl_Interp* (*dll_Tcl_CreateInterp)(); | |
5390 | 182 void (*dll_Tcl_FindExecutable)(const void *); |
7 | 183 |
184 /* | |
185 * Table of name to function pointer of tcl. | |
186 */ | |
187 static struct { | |
188 char* name; | |
189 TCL_PROC* ptr; | |
190 } tcl_funcname_table[] = { | |
191 {"Tcl_CreateInterp", (TCL_PROC*)&dll_Tcl_CreateInterp}, | |
5390 | 192 {"Tcl_FindExecutable", (TCL_PROC*)&dll_Tcl_FindExecutable}, |
7 | 193 {NULL, NULL}, |
194 }; | |
195 | |
196 /* | |
197 * Make all runtime-links of tcl. | |
198 * | |
199 * 1. Get module handle using LoadLibraryEx. | |
1890 | 200 * 2. Get pointer to tcl function by GetProcAddress. |
7 | 201 * 3. Repeat 2, until get all functions will be used. |
202 * | |
203 * Parameter 'libname' provides name of DLL. | |
204 * Return OK or FAIL. | |
205 */ | |
206 static int | |
207 tcl_runtime_link_init(char *libname, int verbose) | |
208 { | |
209 int i; | |
210 | |
211 if (hTclLib) | |
212 return OK; | |
7538
c9fc24b76293
commit https://github.com/vim/vim/commit/8a5115cf18751022387af2085f374d38c60dde83
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
213 if (!(hTclLib = load_dll(libname))) |
7 | 214 { |
215 if (verbose) | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
216 semsg(_(e_loadlib), libname); |
7 | 217 return FAIL; |
218 } | |
219 for (i = 0; tcl_funcname_table[i].ptr; ++i) | |
220 { | |
7538
c9fc24b76293
commit https://github.com/vim/vim/commit/8a5115cf18751022387af2085f374d38c60dde83
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
221 if (!(*tcl_funcname_table[i].ptr = symbol_from_dll(hTclLib, |
7 | 222 tcl_funcname_table[i].name))) |
223 { | |
7538
c9fc24b76293
commit https://github.com/vim/vim/commit/8a5115cf18751022387af2085f374d38c60dde83
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
224 close_dll(hTclLib); |
7 | 225 hTclLib = NULL; |
226 if (verbose) | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
227 semsg(_(e_loadfunc), tcl_funcname_table[i].name); |
7 | 228 return FAIL; |
229 } | |
230 } | |
231 return OK; | |
232 } | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
233 #endif // defined(DYNAMIC_TCL) || defined(PROTO) |
7 | 234 |
235 #ifdef DYNAMIC_TCL | |
236 static char *find_executable_arg = NULL; | |
237 #endif | |
238 | |
239 void | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
240 vim_tcl_init(char *arg) |
7 | 241 { |
242 #ifndef DYNAMIC_TCL | |
243 Tcl_FindExecutable(arg); | |
244 #else | |
245 find_executable_arg = arg; | |
246 #endif | |
247 } | |
248 | |
249 #if defined(DYNAMIC_TCL) || defined(PROTO) | |
250 | |
251 static int stubs_initialized = FALSE; | |
252 | |
253 /* | |
254 * Return TRUE if the TCL interface can be used. | |
255 */ | |
256 int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
257 tcl_enabled(int verbose) |
7 | 258 { |
259 if (!stubs_initialized && find_executable_arg != NULL | |
7538
c9fc24b76293
commit https://github.com/vim/vim/commit/8a5115cf18751022387af2085f374d38c60dde83
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
260 && tcl_runtime_link_init((char *)p_tcldll, verbose) == OK) |
7 | 261 { |
262 Tcl_Interp *interp; | |
263 | |
5390 | 264 dll_Tcl_FindExecutable(find_executable_arg); |
265 | |
7538
c9fc24b76293
commit https://github.com/vim/vim/commit/8a5115cf18751022387af2085f374d38c60dde83
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
266 if ((interp = dll_Tcl_CreateInterp()) != NULL) |
7 | 267 { |
268 if (Tcl_InitStubs(interp, DYNAMIC_TCL_VER, 0)) | |
269 { | |
270 Tcl_DeleteInterp(interp); | |
271 stubs_initialized = TRUE; | |
272 } | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
273 // FIXME: When Tcl_InitStubs() was failed, how delete interp? |
7 | 274 } |
275 } | |
276 return stubs_initialized; | |
277 } | |
278 #endif | |
279 | |
280 void | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
281 tcl_end(void) |
7 | 282 { |
283 } | |
284 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
285 ///////////////////////////////////////////////////////////////////////////// |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
286 // Tcl commands |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
287 //////////////////////////////////////////////////////////////////////////// |
7 | 288 |
289 /* | |
3369 | 290 * Replace standard "exit" command. |
7 | 291 * |
3369 | 292 * Delete the Tcl interpreter; a new one will be created with the next |
293 * :tcl command). The exit code is saved (and retrieved in tclexit()). | |
294 * Since Tcl's exit is never expected to return and this replacement | |
295 * does, then (except for a trivial case) additional Tcl commands will | |
296 * be run. Since the interpreter is now marked as deleted, an error | |
297 * will be returned -- typically "attempt to call eval in deleted | |
298 * interpreter". Hopefully, at this point, checks for TCL_ERROR take | |
299 * place and control percolates back up to Vim -- but with this new error | |
300 * string in the interpreter's result value. Therefore it would be | |
301 * useless for this routine to return the exit code via Tcl_SetResult(). | |
7 | 302 */ |
303 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
304 exitcmd( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
305 ClientData dummy UNUSED, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
306 Tcl_Interp *interp, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
307 int objc, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
308 Tcl_Obj *CONST objv[]) |
7 | 309 { |
310 int value = 0; | |
311 | |
312 switch (objc) | |
313 { | |
314 case 2: | |
315 if (Tcl_GetIntFromObj(interp, objv[1], &value) != TCL_OK) | |
316 break; | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
317 // FALLTHROUGH |
7 | 318 case 1: |
3369 | 319 tclinfo.exitvalue = value; |
320 | |
321 Tcl_DeleteInterp(interp); | |
322 break; | |
7 | 323 default: |
324 Tcl_WrongNumArgs(interp, 1, objv, "?returnCode?"); | |
325 } | |
326 return TCL_ERROR; | |
327 } | |
328 | |
329 /* | |
330 * "::vim::beep" - what Vi[m] does best :-) | |
331 */ | |
332 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
333 beepcmd( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
334 ClientData dummy UNUSED, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
335 Tcl_Interp *interp, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
336 int objc, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
337 Tcl_Obj *CONST objv[]) |
7 | 338 { |
339 if (objc != 1) | |
340 { | |
341 Tcl_WrongNumArgs(interp, 1, objv, NULL); | |
342 return TCL_ERROR; | |
343 } | |
6949 | 344 vim_beep(BO_LANG); |
7 | 345 return TCL_OK; |
346 } | |
347 | |
348 /* | |
349 * "::vim::buffer list" - create a list of buffer commands. | |
1222 | 350 * "::vim::buffer {N}" - create buffer command for buffer N. |
3369 | 351 * "::vim::buffer exists {N}" - test if buffer N exists. |
7 | 352 * "::vim::buffer new" - create a new buffer (not implemented) |
353 */ | |
354 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
355 buffercmd( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
356 ClientData dummy UNUSED, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
357 Tcl_Interp *interp, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
358 int objc, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
359 Tcl_Obj *CONST objv[]) |
7 | 360 { |
361 char *name; | |
362 buf_T *buf; | |
363 Tcl_Obj *resobj; | |
364 int err, n, idx; | |
365 enum {BCMD_EXISTS, BCMD_LIST}; | |
135 | 366 static CONST84 char *bcmdoptions[] = |
7 | 367 { |
368 "exists", "list", (char *)0 | |
369 }; | |
370 | |
371 if (objc < 2) | |
372 { | |
373 Tcl_WrongNumArgs(interp, 1, objv, "option"); | |
374 return TCL_ERROR; | |
375 } | |
376 err = Tcl_GetIntFromObj(interp, objv[1], &n); | |
377 if (err == TCL_OK) | |
378 { | |
379 if (objc != 2) | |
380 { | |
381 Tcl_WrongNumArgs(interp, 1, objv, "bufNumber"); | |
382 return TCL_ERROR; | |
383 } | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
7823
diff
changeset
|
384 FOR_ALL_BUFFERS(buf) |
7 | 385 { |
386 if (buf->b_fnum == n) | |
387 { | |
388 name = tclgetbuffer(interp, buf); | |
389 if (name == NULL) | |
390 return TCL_ERROR; | |
391 Tcl_SetResult(interp, name, TCL_VOLATILE); | |
392 return TCL_OK; | |
393 } | |
394 } | |
395 Tcl_SetResult(interp, _("invalid buffer number"), TCL_STATIC); | |
396 return TCL_ERROR; | |
397 } | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
398 Tcl_ResetResult(interp); // clear error from Tcl_GetIntFromObj |
7 | 399 |
400 err = Tcl_GetIndexFromObj(interp, objv[1], bcmdoptions, "option", 0, &idx); | |
401 if (err != TCL_OK) | |
402 return err; | |
403 switch (idx) | |
404 { | |
405 case BCMD_LIST: | |
406 if (objc != 2) | |
407 { | |
408 Tcl_WrongNumArgs(interp, 2, objv, ""); | |
409 err = TCL_ERROR; | |
410 break; | |
411 } | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
7823
diff
changeset
|
412 FOR_ALL_BUFFERS(buf) |
7 | 413 { |
414 name = tclgetbuffer(interp, buf); | |
415 if (name == NULL) | |
416 { | |
417 err = TCL_ERROR; | |
418 break; | |
419 } | |
420 Tcl_AppendElement(interp, name); | |
421 } | |
422 break; | |
423 | |
424 case BCMD_EXISTS: | |
425 if (objc != 3) | |
426 { | |
427 Tcl_WrongNumArgs(interp, 2, objv, "bufNumber"); | |
428 err = TCL_ERROR; | |
429 break; | |
430 } | |
431 err = Tcl_GetIntFromObj(interp, objv[2], &n); | |
432 if (err == TCL_OK) | |
433 { | |
434 buf = buflist_findnr(n); | |
435 resobj = Tcl_NewIntObj(buf != NULL); | |
436 Tcl_SetObjResult(interp, resobj); | |
437 } | |
438 break; | |
439 | |
440 default: | |
441 Tcl_SetResult(interp, _("not implemented yet"), TCL_STATIC); | |
442 err = TCL_ERROR; | |
443 } | |
444 return err; | |
445 } | |
446 | |
447 /* | |
448 * "::vim::window list" - create list of window commands. | |
449 */ | |
450 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
451 windowcmd( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
452 ClientData dummy UNUSED, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
453 Tcl_Interp *interp, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
454 int objc, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
455 Tcl_Obj *CONST objv[]) |
7 | 456 { |
457 char *what, *string; | |
458 win_T *win; | |
459 | |
460 if (objc != 2) | |
461 { | |
462 Tcl_WrongNumArgs(interp, 1, objv, "option"); | |
463 return TCL_ERROR; | |
464 } | |
465 what = Tcl_GetStringFromObj(objv[1], NULL); | |
466 if (strcmp(what, "list") == 0) | |
467 { | |
468 FOR_ALL_WINDOWS(win) | |
469 { | |
470 string = tclgetwindow(interp, win); | |
471 if (string == NULL) | |
472 return TCL_ERROR; | |
473 Tcl_AppendElement(interp, string); | |
474 } | |
475 return TCL_OK; | |
476 } | |
477 Tcl_SetResult(interp, _("unknown option"), TCL_STATIC); | |
478 return TCL_ERROR; | |
479 } | |
480 | |
481 /* | |
482 * flags for bufselfcmd and winselfcmd to indicate outstanding actions. | |
483 */ | |
484 #define FL_UPDATE_SCREEN (1<<0) | |
485 #define FL_UPDATE_CURBUF (1<<1) | |
486 #define FL_ADJUST_CURSOR (1<<2) | |
487 | |
488 /* | |
489 * This function implements the buffer commands. | |
490 */ | |
491 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
492 bufselfcmd( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
493 ClientData ref, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
494 Tcl_Interp *interp, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
495 int objc, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
496 Tcl_Obj *CONST objv[]) |
7 | 497 { |
498 int opt, err, idx, flags; | |
499 int val1, val2, n, i; | |
500 buf_T *buf, *savebuf; | |
501 win_T *win, *savewin; | |
502 Tcl_Obj *resobj; | |
503 pos_T *pos; | |
504 char *line; | |
505 | |
506 enum | |
507 { | |
508 BUF_APPEND, BUF_COMMAND, BUF_COUNT, BUF_DELCMD, BUF_DELETE, BUF_EXPR, | |
509 BUF_GET, BUF_INSERT, BUF_LAST, BUF_MARK, BUF_NAME, BUF_NUMBER, | |
510 BUF_OPTION, BUF_SET, BUF_WINDOWS | |
511 }; | |
135 | 512 static CONST84 char *bufoptions[] = |
7 | 513 { |
514 "append", "command", "count", "delcmd", "delete", "expr", | |
515 "get", "insert", "last", "mark", "name", "number", | |
516 "option", "set", "windows", (char *)0 | |
517 }; | |
518 | |
519 if (objc < 2) | |
520 { | |
521 Tcl_WrongNumArgs(interp, 1, objv, "option ?arg ...?"); | |
522 return TCL_ERROR; | |
523 } | |
524 | |
525 err = Tcl_GetIndexFromObj(interp, objv[1], bufoptions, "option", 0, &idx); | |
526 if (err != TCL_OK) | |
527 return err; | |
528 | |
529 buf = (buf_T *)((struct ref *)ref)->vimobj; | |
530 savebuf = curbuf; curbuf = buf; | |
531 savewin = curwin; curwin = tclfindwin(buf); | |
532 flags = 0; | |
533 opt = 0; | |
534 | |
535 switch (idx) | |
536 { | |
537 case BUF_COMMAND: | |
538 err = tcldoexcommand(interp, objc, objv, 2); | |
539 flags |= FL_UPDATE_SCREEN; | |
540 break; | |
541 | |
542 case BUF_OPTION: | |
543 err = tclsetoption(interp, objc, objv, 2); | |
544 flags |= FL_UPDATE_SCREEN; | |
545 break; | |
546 | |
547 case BUF_EXPR: | |
548 err = tclvimexpr(interp, objc, objv, 2); | |
549 break; | |
550 | |
551 case BUF_NAME: | |
552 /* | |
553 * Get filename of buffer. | |
554 */ | |
555 if (objc != 2) | |
556 { | |
557 Tcl_WrongNumArgs(interp, 2, objv, NULL); | |
558 err = TCL_ERROR; | |
559 break; | |
560 } | |
561 if (buf->b_ffname) | |
562 Tcl_SetResult(interp, (char *)buf->b_ffname, TCL_VOLATILE); | |
563 else | |
564 Tcl_SetResult(interp, "", TCL_STATIC); | |
565 break; | |
566 | |
567 case BUF_LAST: | |
568 /* | |
569 * Get line number of last line. | |
570 */ | |
571 opt = 1; | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
572 // fallthrough |
7 | 573 case BUF_COUNT: |
574 /* | |
575 * Get number of lines in buffer. | |
576 */ | |
577 if (objc != 2) | |
578 { | |
579 Tcl_WrongNumArgs(interp, 2, objv, NULL); | |
580 err = TCL_ERROR; | |
581 break; | |
582 } | |
583 val1 = (int)buf->b_ml.ml_line_count; | |
584 if (opt) | |
585 val1 = row2tcl(val1); | |
586 | |
587 resobj = Tcl_NewIntObj(val1); | |
588 Tcl_SetObjResult(interp, resobj); | |
589 break; | |
590 | |
591 case BUF_NUMBER: | |
592 /* | |
593 * Get buffer's number. | |
594 */ | |
595 if (objc != 2) | |
596 { | |
597 Tcl_WrongNumArgs(interp, 2, objv, NULL); | |
598 err = TCL_ERROR; | |
599 break; | |
600 } | |
601 resobj = Tcl_NewIntObj((int)buf->b_fnum); | |
602 Tcl_SetObjResult(interp, resobj); | |
603 break; | |
604 | |
605 case BUF_GET: | |
606 if (objc != 3 && objc != 4) | |
607 { | |
608 Tcl_WrongNumArgs(interp, 2, objv, "lineNumber ?lineNumber?"); | |
609 err = TCL_ERROR; | |
610 break; | |
611 } | |
612 err = tclgetlinenum(interp, objv[2], &val1, buf); | |
613 if (err != TCL_OK) | |
614 break; | |
615 if (objc == 4) | |
616 { | |
617 err = tclgetlinenum(interp, objv[3], &val2, buf); | |
618 if (err != TCL_OK) | |
619 break; | |
620 if (val1 > val2) | |
621 { | |
622 n = val1; val1 = val2; val2 = n; | |
623 } | |
624 Tcl_ResetResult(interp); | |
625 | |
626 for (n = val1; n <= val2 && err == TCL_OK; n++) | |
627 { | |
628 line = (char *)ml_get_buf(buf, (linenr_T)n, FALSE); | |
629 if (line) | |
630 Tcl_AppendElement(interp, line); | |
631 else | |
632 err = TCL_ERROR; | |
633 } | |
634 } | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
635 else { // objc == 3 |
7 | 636 line = (char *)ml_get_buf(buf, (linenr_T)val1, FALSE); |
637 Tcl_SetResult(interp, line, TCL_VOLATILE); | |
638 } | |
639 break; | |
640 | |
641 case BUF_SET: | |
642 if (objc != 4 && objc != 5) | |
643 { | |
644 Tcl_WrongNumArgs(interp, 3, objv, "lineNumber ?lineNumber? stringOrList"); | |
645 err = TCL_ERROR; | |
646 break; | |
647 } | |
648 err = tclgetlinenum(interp, objv[2], &val1, buf); | |
649 if (err != TCL_OK) | |
650 return TCL_ERROR; | |
651 if (objc == 4) | |
652 { | |
653 /* | |
654 * Replace one line with a string. | |
655 * $buf set {n} {string} | |
656 */ | |
657 line = Tcl_GetStringFromObj(objv[3], NULL); | |
658 if (u_savesub((linenr_T)val1) != OK) | |
659 { | |
660 Tcl_SetResult(interp, _("cannot save undo information"), TCL_STATIC); | |
661 err = TCL_ERROR; | |
662 } | |
663 else | |
664 if (ml_replace((linenr_T)val1, (char_u *)line, TRUE) != OK) | |
665 { | |
666 Tcl_SetResult(interp, _("cannot replace line"), TCL_STATIC); | |
667 err = TCL_ERROR; | |
668 } | |
669 else | |
670 { | |
671 changed_bytes((linenr_T)val1, 0); | |
672 flags |= FL_UPDATE_CURBUF; | |
673 } | |
674 break; | |
675 } | |
676 else | |
677 { | |
678 /* | |
679 * Replace several lines with the elements of a Tcl list. | |
680 * $buf set {n} {m} {list} | |
681 * If the list contains more than {m}-{n}+1 elements, they | |
682 * are * inserted after line {m}. If the list contains fewer | |
683 * elements, * the lines from {n}+length({list}) through {m} | |
684 * are deleted. | |
685 */ | |
686 int lc; | |
687 Tcl_Obj **lv; | |
688 | |
689 err = tclgetlinenum(interp, objv[3], &val2, buf); | |
690 if (err != TCL_OK) | |
691 break; | |
692 err = Tcl_ListObjGetElements(interp, objv[4], &lc, &lv); | |
693 if (err != TCL_OK) | |
694 break; | |
695 if (val1 > val2) | |
696 { | |
697 n = val1; | |
698 val1 = val2; | |
699 val2 = n; | |
700 } | |
701 | |
702 n = val1; | |
703 if (u_save((linenr_T)(val1 - 1), (linenr_T)(val2 + 1)) != OK) | |
704 { | |
705 Tcl_SetResult(interp, _("cannot save undo information"), | |
706 TCL_STATIC); | |
707 err = TCL_ERROR; | |
708 break; | |
709 } | |
710 flags |= FL_UPDATE_CURBUF; | |
711 | |
712 for (i = 0; i < lc && n <= val2; i++) | |
713 { | |
714 line = Tcl_GetStringFromObj(lv[i], NULL); | |
715 if (ml_replace((linenr_T)n, (char_u *)line, TRUE) != OK) | |
716 goto setListError; | |
717 ++n; | |
718 } | |
719 if (i < lc) | |
720 { | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
721 // append lines |
7 | 722 do |
723 { | |
724 line = Tcl_GetStringFromObj(lv[i], NULL); | |
725 if (ml_append((linenr_T)(n - 1), | |
726 (char_u *)line, 0, FALSE) != OK) | |
727 goto setListError; | |
728 ++n; | |
729 ++i; | |
730 } while (i < lc); | |
731 } | |
732 else if (n <= val2) | |
733 { | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
734 // did not replace all lines, delete |
7 | 735 i = n; |
736 do | |
737 { | |
20599
d571231175b4
patch 8.2.0853: ml_delete() often called with FALSE argument
Bram Moolenaar <Bram@vim.org>
parents:
20369
diff
changeset
|
738 if (ml_delete((linenr_T)i) != OK) |
7 | 739 goto setListError; |
740 ++n; | |
741 } while (n <= val2); | |
742 } | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
743 lc -= val2 - val1 + 1; // number of lines to be replaced |
7 | 744 mark_adjust((linenr_T)val1, (linenr_T)val2, (long)MAXLNUM, |
745 (long)lc); | |
746 changed_lines((linenr_T)val1, 0, (linenr_T)val2 + 1, (long)lc); | |
747 break; | |
748 setListError: | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
749 u_undo(1); // ??? |
7 | 750 Tcl_SetResult(interp, _("cannot set line(s)"), TCL_STATIC); |
751 err = TCL_ERROR; | |
752 } | |
753 break; | |
754 | |
755 case BUF_DELETE: | |
756 if (objc != 3 && objc != 4) | |
757 { | |
758 Tcl_WrongNumArgs(interp, 3, objv, "lineNumber ?lineNumber?"); | |
759 err = TCL_ERROR; | |
760 break; | |
761 } | |
762 err = tclgetlinenum(interp, objv[2], &val1, buf); | |
763 if (err != TCL_OK) | |
764 break; | |
765 val2 = val1; | |
766 if (objc == 4) | |
767 { | |
768 err = tclgetlinenum(interp, objv[3], &val2, buf); | |
769 if (err != TCL_OK) | |
770 return err; | |
771 if (val1 > val2) | |
772 { | |
773 i = val1; val1 = val2; val2 = i; | |
774 } | |
775 } | |
776 n = val2 - val1 + 1; | |
777 if (u_savedel((linenr_T)val1, (long)n) != OK) | |
778 { | |
779 Tcl_SetResult(interp, _("cannot save undo information"), | |
780 TCL_STATIC); | |
781 err = TCL_ERROR; | |
782 break; | |
783 } | |
784 for (i = 0; i < n; i++) | |
785 { | |
20599
d571231175b4
patch 8.2.0853: ml_delete() often called with FALSE argument
Bram Moolenaar <Bram@vim.org>
parents:
20369
diff
changeset
|
786 ml_delete((linenr_T)val1); |
7 | 787 err = vimerror(interp); |
788 if (err != TCL_OK) | |
789 break; | |
790 } | |
791 if (i > 0) | |
792 deleted_lines_mark((linenr_T)val1, (long)i); | |
793 flags |= FL_ADJUST_CURSOR|FL_UPDATE_SCREEN; | |
794 break; | |
795 | |
796 case BUF_MARK: | |
797 if (objc != 3) | |
798 { | |
799 Tcl_WrongNumArgs(interp, 2, objv, "markName"); | |
800 err = TCL_ERROR; | |
801 break; | |
802 } | |
803 line = Tcl_GetStringFromObj(objv[2], NULL); | |
804 | |
805 pos = NULL; | |
806 if (line[0] != '\0' && line[1] == '\0') | |
807 pos = getmark(line[0], FALSE); | |
808 if (pos == NULL) | |
809 { | |
810 Tcl_SetResult(interp, _("invalid mark name"), TCL_STATIC); | |
811 err = TCL_ERROR; | |
812 break; | |
813 } | |
814 err = vimerror(interp); | |
815 if (err != TCL_OK) | |
816 break; | |
817 if (pos->lnum <= 0) | |
818 { | |
819 Tcl_SetResult(interp, _("mark not set"), TCL_STATIC); | |
820 err = TCL_ERROR; | |
821 } | |
822 else | |
823 { | |
824 char rbuf[64]; | |
274 | 825 |
826 sprintf(rbuf, _("row %d column %d"), | |
827 (int)row2tcl(pos->lnum), (int)col2tcl(pos->col)); | |
7 | 828 Tcl_SetResult(interp, rbuf, TCL_VOLATILE); |
829 } | |
830 break; | |
831 | |
832 case BUF_INSERT: | |
833 opt = 1; | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
834 // fallthrough |
7 | 835 case BUF_APPEND: |
836 if (objc != 4) | |
837 { | |
838 Tcl_WrongNumArgs(interp, 2, objv, "lineNum text"); | |
839 err = TCL_ERROR; | |
840 break; | |
841 } | |
842 err = tclgetlinenum(interp, objv[2], &val1, buf); | |
843 if (err != TCL_OK) | |
844 break; | |
845 if (opt) | |
846 --val1; | |
847 if (u_save((linenr_T)val1, (linenr_T)(val1+1)) != OK) | |
848 { | |
274 | 849 Tcl_SetResult(interp, _("cannot save undo information"), |
850 TCL_STATIC); | |
7 | 851 err = TCL_ERROR; |
852 break; | |
853 } | |
854 | |
855 line = Tcl_GetStringFromObj(objv[3], NULL); | |
856 if (ml_append((linenr_T)val1, (char_u *)line, 0, FALSE) != OK) | |
857 { | |
274 | 858 Tcl_SetResult(interp, _("cannot insert/append line"), |
859 TCL_STATIC); | |
7 | 860 err = TCL_ERROR; |
861 break; | |
862 } | |
863 appended_lines_mark((linenr_T)val1, 1L); | |
864 flags |= FL_UPDATE_SCREEN; | |
865 break; | |
866 | |
867 case BUF_WINDOWS: | |
868 /* | |
869 * Return list of window commands. | |
870 */ | |
871 if (objc != 2) | |
872 { | |
873 Tcl_WrongNumArgs(interp, 2, objv, NULL); | |
874 err = TCL_ERROR; | |
875 break; | |
876 } | |
877 Tcl_ResetResult(interp); | |
878 FOR_ALL_WINDOWS(win) | |
879 { | |
880 if (win->w_buffer == buf) | |
881 { | |
882 line = tclgetwindow(interp, win); | |
883 if (line != NULL) | |
884 Tcl_AppendElement(interp, line); | |
885 else | |
886 { | |
887 err = TCL_ERROR; | |
888 break; | |
889 } | |
890 } | |
891 } | |
892 break; | |
893 | |
894 case BUF_DELCMD: | |
895 /* | |
896 * Register deletion callback. | |
897 * TODO: Should be able to register multiple callbacks | |
898 */ | |
899 if (objc != 3) | |
900 { | |
901 Tcl_WrongNumArgs(interp, 2, objv, "command"); | |
902 err = TCL_ERROR; | |
903 break; | |
904 } | |
502 | 905 err = tclsetdelcmd(interp, buf->b_tcl_ref, (void *)buf, objv[2]); |
7 | 906 break; |
907 | |
908 default: | |
909 Tcl_SetResult(interp, _("not implemented yet"), TCL_STATIC); | |
910 err = TCL_ERROR; | |
911 } | |
912 | |
913 if (flags & FL_UPDATE_CURBUF) | |
914 redraw_curbuf_later(NOT_VALID); | |
915 curbuf = savebuf; | |
916 curwin = savewin; | |
917 if (flags & FL_ADJUST_CURSOR) | |
918 check_cursor(); | |
919 if (flags & (FL_UPDATE_SCREEN | FL_UPDATE_CURBUF)) | |
920 update_screen(NOT_VALID); | |
921 | |
922 return err; | |
923 } | |
924 | |
925 /* | |
926 * This function implements the window commands. | |
927 */ | |
928 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
929 winselfcmd( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
930 ClientData ref, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
931 Tcl_Interp *interp, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
932 int objc, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
933 Tcl_Obj *CONST objv[]) |
7 | 934 { |
935 int err, idx, flags; | |
936 int val1, val2; | |
937 Tcl_Obj *resobj; | |
938 win_T *savewin, *win; | |
939 buf_T *savebuf; | |
940 char *str; | |
941 | |
942 enum | |
943 { | |
944 WIN_BUFFER, WIN_COMMAND, WIN_CURSOR, WIN_DELCMD, WIN_EXPR, | |
945 WIN_HEIGHT, WIN_OPTION | |
946 }; | |
135 | 947 static CONST84 char *winoptions[] = |
7 | 948 { |
949 "buffer", "command", "cursor", "delcmd", "expr", | |
950 "height", "option", (char *)0 | |
951 }; | |
952 | |
953 if (objc < 2) | |
954 { | |
955 Tcl_WrongNumArgs(interp, 1, objv, "option ?arg ...?"); | |
956 return TCL_ERROR; | |
957 } | |
958 | |
959 err = Tcl_GetIndexFromObj(interp, objv[1], winoptions, "option", 0, &idx); | |
960 if (err != TCL_OK) | |
961 return TCL_ERROR; | |
962 | |
963 win = (win_T *)((struct ref *)ref)->vimobj; | |
964 savewin = curwin; curwin = win; | |
965 savebuf = curbuf; curbuf = win->w_buffer; | |
966 flags = 0; | |
967 | |
968 switch (idx) | |
969 { | |
970 case WIN_OPTION: | |
971 err = tclsetoption(interp, objc, objv, 2); | |
972 flags |= FL_UPDATE_SCREEN; | |
973 break; | |
974 | |
975 case WIN_COMMAND: | |
976 err = tcldoexcommand(interp, objc, objv, 2); | |
977 flags |= FL_UPDATE_SCREEN; | |
978 break; | |
979 | |
980 case WIN_EXPR: | |
981 err = tclvimexpr(interp, objc, objv, 2); | |
982 break; | |
983 | |
984 case WIN_HEIGHT: | |
985 if (objc == 3) | |
986 { | |
987 err = Tcl_GetIntFromObj(interp, objv[2], &val1); | |
988 if (err != TCL_OK) | |
989 break; | |
990 #ifdef FEAT_GUI | |
991 need_mouse_correct = TRUE; | |
992 #endif | |
993 win_setheight(val1); | |
994 err = vimerror(interp); | |
995 if (err != TCL_OK) | |
996 break; | |
997 } | |
998 else | |
999 if (objc != 2) | |
1000 { | |
1001 Tcl_WrongNumArgs(interp, 2, objv, "?value?"); | |
1002 err = TCL_ERROR; | |
1003 break; | |
1004 } | |
1005 | |
1006 resobj = Tcl_NewIntObj((int)(win->w_height)); | |
1007 Tcl_SetObjResult(interp, resobj); | |
1008 break; | |
1009 | |
1010 case WIN_BUFFER: | |
1011 if (objc != 2) | |
1012 { | |
1013 Tcl_WrongNumArgs(interp, 2, objv, NULL); | |
1014 err = TCL_ERROR; | |
1015 break; | |
1016 } | |
1017 str = tclgetbuffer(interp, win->w_buffer); | |
1018 if (str) | |
1019 Tcl_SetResult(interp, str, TCL_VOLATILE); | |
1020 else | |
1021 err = TCL_ERROR; | |
1022 break; | |
1023 | |
1024 case WIN_DELCMD: | |
1025 if (objc != 3) | |
1026 { | |
1027 Tcl_WrongNumArgs(interp, 2, objv, "command"); | |
1028 err = TCL_ERROR; | |
1029 break; | |
1030 } | |
502 | 1031 err = tclsetdelcmd(interp, win->w_tcl_ref, (void *)win, objv[2]); |
7 | 1032 break; |
1033 | |
1034 case WIN_CURSOR: | |
1035 if (objc > 4) | |
1036 { | |
1037 Tcl_WrongNumArgs(interp, 2, objv, "?arg1 ?arg2??"); | |
1038 err = TCL_ERROR; | |
1039 break; | |
1040 } | |
1041 if (objc == 2) | |
1042 { | |
1043 char buf[64]; | |
274 | 1044 |
7 | 1045 sprintf(buf, _("row %d column %d"), (int)row2tcl(win->w_cursor.lnum), (int)col2tcl(win->w_cursor.col)); |
1046 Tcl_SetResult(interp, buf, TCL_VOLATILE); | |
1047 break; | |
1048 } | |
1049 else if (objc == 3) | |
1050 { | |
1051 Tcl_Obj *part, *var; | |
1052 | |
1053 part = Tcl_NewStringObj("row", -1); | |
1054 var = Tcl_ObjGetVar2(interp, objv[2], part, TCL_LEAVE_ERR_MSG); | |
1055 if (var == NULL) | |
1056 { | |
1057 err = TCL_ERROR; | |
1058 break; | |
1059 } | |
1060 err = tclgetlinenum(interp, var, &val1, win->w_buffer); | |
1061 if (err != TCL_OK) | |
1062 break; | |
1063 part = Tcl_NewStringObj("column", -1); | |
1064 var = Tcl_ObjGetVar2(interp, objv[2], part, TCL_LEAVE_ERR_MSG); | |
1065 if (var == NULL) | |
1066 { | |
1067 err = TCL_ERROR; | |
1068 break; | |
1069 } | |
1070 err = Tcl_GetIntFromObj(interp, var, &val2); | |
1071 if (err != TCL_OK) | |
1072 break; | |
1073 } | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1074 else { // objc == 4 |
7 | 1075 err = tclgetlinenum(interp, objv[2], &val1, win->w_buffer); |
1076 if (err != TCL_OK) | |
1077 break; | |
1078 err = Tcl_GetIntFromObj(interp, objv[3], &val2); | |
1079 if (err != TCL_OK) | |
1080 break; | |
1081 } | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1082 // TODO: should check column |
7 | 1083 win->w_cursor.lnum = val1; |
1084 win->w_cursor.col = col2vim(val2); | |
14395
c15bef307de6
patch 8.1.0212: preferred cursor column not set in interfaces
Christian Brabandt <cb@256bit.org>
parents:
14264
diff
changeset
|
1085 win->w_set_curswant = TRUE; |
7 | 1086 flags |= FL_UPDATE_SCREEN; |
1087 break; | |
1088 | |
1089 default: | |
1090 Tcl_SetResult(interp, _("not implemented yet"), TCL_STATIC); | |
1091 break; | |
1092 } | |
1093 | |
1094 curwin = savewin; | |
1095 curbuf = savebuf; | |
1096 if (flags & FL_UPDATE_SCREEN) | |
1097 update_screen(NOT_VALID); | |
1098 | |
1099 return err; | |
1100 } | |
1101 | |
1102 | |
1103 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1104 commandcmd( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1105 ClientData dummy UNUSED, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1106 Tcl_Interp *interp, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1107 int objc, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1108 Tcl_Obj *CONST objv[]) |
7 | 1109 { |
1110 int err; | |
1111 | |
1112 err = tcldoexcommand(interp, objc, objv, 1); | |
1113 update_screen(VALID); | |
1114 return err; | |
1115 } | |
1116 | |
1117 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1118 optioncmd( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1119 ClientData dummy UNUSED, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1120 Tcl_Interp *interp, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1121 int objc, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1122 Tcl_Obj *CONST objv[]) |
7 | 1123 { |
1124 int err; | |
1125 | |
1126 err = tclsetoption(interp, objc, objv, 1); | |
1127 update_screen(VALID); | |
1128 return err; | |
1129 } | |
1130 | |
1131 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1132 exprcmd( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1133 ClientData dummy UNUSED, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1134 Tcl_Interp *interp, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1135 int objc, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1136 Tcl_Obj *CONST objv[]) |
7 | 1137 { |
1138 return tclvimexpr(interp, objc, objv, 1); | |
1139 } | |
1140 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1141 ///////////////////////////////////////////////////////////////////////////// |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1142 // Support functions for Tcl commands |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1143 //////////////////////////////////////////////////////////////////////////// |
7 | 1144 |
1145 /* | |
1146 * Get a line number from 'obj' and convert it to vim's range. | |
1147 */ | |
1148 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1149 tclgetlinenum( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1150 Tcl_Interp *interp, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1151 Tcl_Obj *obj, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1152 int *valueP, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1153 buf_T *buf) |
7 | 1154 { |
1155 int err, i; | |
1156 | |
1157 enum { LN_BEGIN, LN_BOTTOM, LN_END, LN_FIRST, LN_LAST, LN_START, LN_TOP }; | |
1158 | |
135 | 1159 static CONST84 char *keyw[] = |
7 | 1160 { |
1161 "begin", "bottom", "end", "first", "last", "start", "top", (char *)0 | |
1162 }; | |
1163 | |
1164 err = Tcl_GetIndexFromObj(interp, obj, keyw, "", 0, &i); | |
1165 if (err == TCL_OK) | |
1166 { | |
1167 switch (i) | |
1168 { | |
1169 case LN_BEGIN: | |
1170 case LN_FIRST: | |
1171 case LN_START: | |
1172 case LN_TOP: | |
1173 *valueP = 1; | |
1174 break; | |
1175 case LN_BOTTOM: | |
1176 case LN_END: | |
1177 case LN_LAST: | |
1178 *valueP = buf->b_ml.ml_line_count; | |
1179 break; | |
1180 } | |
1181 return TCL_OK; | |
1182 } | |
1183 Tcl_ResetResult(interp); | |
1184 | |
1185 err = Tcl_GetIntFromObj(interp, obj, &i); | |
1186 if (err != TCL_OK) | |
1187 return err; | |
1188 i = row2vim(i); | |
1189 if (i < 1 || i > buf->b_ml.ml_line_count) | |
1190 { | |
1191 Tcl_SetResult(interp, _("line number out of range"), TCL_STATIC); | |
1192 return TCL_ERROR; | |
1193 } | |
1194 *valueP = i; | |
1195 return TCL_OK; | |
1196 } | |
1197 | |
1198 /* | |
1199 * Find the first window in the window list that displays the buffer. | |
1200 */ | |
1201 static win_T * | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1202 tclfindwin(buf_T *buf) |
7 | 1203 { |
1204 win_T *win; | |
1205 | |
1206 FOR_ALL_WINDOWS(win) | |
1207 { | |
1208 if (win->w_buffer == buf) | |
1209 return win; | |
1210 } | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1211 return curwin; // keep current window context |
7 | 1212 } |
1213 | |
1214 /* | |
1215 * Do-it-all function for "::vim::command", "$buf command" and "$win command". | |
1216 */ | |
1217 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1218 tcldoexcommand( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1219 Tcl_Interp *interp, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1220 int objc, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1221 Tcl_Obj *CONST objv[], |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1222 int objn) |
7 | 1223 { |
1224 tcl_info saveinfo; | |
1225 int err, flag, nobjs; | |
1226 char *arg; | |
1227 | |
1228 nobjs = objc - objn; | |
1229 if (nobjs < 1 || nobjs > 2) | |
1230 { | |
1231 Tcl_WrongNumArgs(interp, objn, objv, "?-quiet? exCommand"); | |
1232 return TCL_ERROR; | |
1233 } | |
1234 | |
1235 flag = 0; | |
1236 if (nobjs == 2) | |
1237 { | |
1238 arg = Tcl_GetStringFromObj(objv[objn], NULL); | |
1239 if (strcmp(arg, "-quiet") == 0) | |
1240 flag = 1; | |
1241 else | |
1242 { | |
1243 Tcl_ResetResult(interp); | |
1244 Tcl_AppendResult(interp, _("unknown flag: "), arg, (char *)0); | |
1245 return TCL_ERROR; | |
1246 } | |
1247 ++objn; | |
1248 } | |
1249 | |
1250 memcpy(&saveinfo, &tclinfo, sizeof(tcl_info)); | |
1251 tclinfo.interp = NULL; | |
1252 tclinfo.curwin = NULL; | |
1253 tclinfo.curbuf = NULL; | |
1254 | |
1255 arg = Tcl_GetStringFromObj(objv[objn], NULL); | |
1256 if (flag) | |
1257 ++emsg_off; | |
1258 do_cmdline_cmd((char_u *)arg); | |
1259 if (flag) | |
1260 --emsg_off; | |
1261 err = vimerror(interp); | |
1262 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1263 // If the ex command created a new Tcl interpreter, remove it |
7 | 1264 if (tclinfo.interp) |
1265 tcldelthisinterp(); | |
1266 memcpy(&tclinfo, &saveinfo, sizeof(tcl_info)); | |
1267 tclupdatevars(); | |
1268 | |
1269 return err; | |
1270 } | |
1271 | |
1272 /* | |
1273 * Do-it-all function for "::vim::option", "$buf option" and "$win option". | |
1274 */ | |
1275 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1276 tclsetoption( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1277 Tcl_Interp *interp, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1278 int objc, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1279 Tcl_Obj *CONST objv[], |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1280 int objn) |
7 | 1281 { |
1282 int err, nobjs, idx; | |
1283 char_u *option; | |
1284 int isnum; | |
1285 long lval; | |
1286 char_u *sval; | |
1287 Tcl_Obj *resobj; | |
1288 | |
1289 enum { OPT_OFF, OPT_ON, OPT_TOGGLE }; | |
135 | 1290 static CONST84 char *optkw[] = { "off", "on", "toggle", (char *)0 }; |
7 | 1291 |
1292 nobjs = objc - objn; | |
1293 if (nobjs != 1 && nobjs != 2) | |
1294 { | |
1295 Tcl_WrongNumArgs(interp, objn, objv, "vimOption ?value?"); | |
1296 return TCL_ERROR; | |
1297 } | |
1298 | |
1299 option = (char_u *)Tcl_GetStringFromObj(objv[objn], NULL); | |
1300 ++objn; | |
1301 isnum = get_option_value(option, &lval, &sval, 0); | |
1302 err = TCL_OK; | |
1303 switch (isnum) | |
1304 { | |
1305 case 0: | |
1306 Tcl_SetResult(interp, (char *)sval, TCL_VOLATILE); | |
1307 vim_free(sval); | |
1308 break; | |
1309 case 1: | |
1310 resobj = Tcl_NewLongObj(lval); | |
1311 Tcl_SetObjResult(interp, resobj); | |
1312 break; | |
1313 default: | |
1314 Tcl_SetResult(interp, _("unknown vimOption"), TCL_STATIC); | |
1315 return TCL_ERROR; | |
1316 } | |
1317 if (nobjs == 2) | |
1318 { | |
1319 if (isnum) | |
1320 { | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1321 sval = NULL; // avoid compiler warning |
7 | 1322 err = Tcl_GetIndexFromObj(interp, objv[objn], optkw, "", 0, &idx); |
1323 if (err != TCL_OK) | |
1324 { | |
1325 Tcl_ResetResult(interp); | |
1326 err = Tcl_GetLongFromObj(interp, objv[objn], &lval); | |
1327 } | |
1328 else | |
1329 switch (idx) | |
1330 { | |
1331 case OPT_ON: | |
1332 lval = 1; | |
1333 break; | |
1334 case OPT_OFF: | |
1335 lval = 0; | |
1336 break; | |
1337 case OPT_TOGGLE: | |
1338 lval = !lval; | |
1339 break; | |
1340 } | |
1341 } | |
1342 else | |
1343 sval = (char_u *)Tcl_GetStringFromObj(objv[objn], NULL); | |
1344 if (err == TCL_OK) | |
1345 { | |
1346 set_option_value(option, lval, sval, OPT_LOCAL); | |
1347 err = vimerror(interp); | |
1348 } | |
1349 } | |
1350 return err; | |
1351 } | |
1352 | |
1353 /* | |
1354 * Do-it-all function for "::vim::expr", "$buf expr" and "$win expr". | |
1355 */ | |
1356 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1357 tclvimexpr( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1358 Tcl_Interp *interp, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1359 int objc, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1360 Tcl_Obj *CONST objv[], |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1361 int objn) |
7 | 1362 { |
1363 #ifdef FEAT_EVAL | |
1364 char *expr, *str; | |
1365 #endif | |
1366 int err; | |
1367 | |
1368 if (objc - objn != 1) | |
1369 { | |
1370 Tcl_WrongNumArgs(interp, objn, objv, "vimExpr"); | |
1371 return TCL_ERROR; | |
1372 } | |
1373 | |
1374 #ifdef FEAT_EVAL | |
1375 expr = Tcl_GetStringFromObj(objv[objn], NULL); | |
20996
3af71cbcfdbe
patch 8.2.1049: Vim9: leaking memory when using continuation line
Bram Moolenaar <Bram@vim.org>
parents:
20599
diff
changeset
|
1376 str = (char *)eval_to_string((char_u *)expr, TRUE); |
7 | 1377 if (str == NULL) |
1378 Tcl_SetResult(interp, _("invalid expression"), TCL_STATIC); | |
1379 else | |
14264
534836186b15
patch 8.1.0148: memory leak when using :tcl expr command
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
1380 { |
7 | 1381 Tcl_SetResult(interp, str, TCL_VOLATILE); |
14264
534836186b15
patch 8.1.0148: memory leak when using :tcl expr command
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
1382 vim_free(str); |
534836186b15
patch 8.1.0148: memory leak when using :tcl expr command
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
1383 } |
7 | 1384 err = vimerror(interp); |
1385 #else | |
1386 Tcl_SetResult(interp, _("expressions disabled at compile time"), TCL_STATIC); | |
1387 err = TCL_ERROR; | |
1388 #endif | |
1389 | |
1390 return err; | |
1391 } | |
1392 | |
1393 /* | |
1394 * Check for internal vim errors. | |
1395 */ | |
1396 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1397 vimerror(Tcl_Interp *interp) |
7 | 1398 { |
1399 if (got_int) | |
1400 { | |
1401 Tcl_SetResult(interp, _("keyboard interrupt"), TCL_STATIC); | |
1402 return TCL_ERROR; | |
1403 } | |
1404 else if (did_emsg) | |
1405 { | |
20369
6e1e4d7a7b39
patch 8.2.0740: minor message mistakes
Bram Moolenaar <Bram@vim.org>
parents:
19846
diff
changeset
|
1406 Tcl_SetResult(interp, _("Vim error"), TCL_STATIC); |
7 | 1407 return TCL_ERROR; |
1408 } | |
1409 return TCL_OK; | |
1410 } | |
1411 | |
1412 /* | |
1413 * Functions that handle the reference lists: | |
1414 * delref() - callback for Tcl's DeleteCommand | |
1415 * tclgetref() - find/create Tcl command for a win_T* or buf_T* object | |
1416 * tclgetwindow() - window frontend for tclgetref() | |
1417 * tclgetbuffer() - buffer frontend for tclgetref() | |
1418 * tclsetdelcmd() - add Tcl callback command to a vim object | |
1419 */ | |
1420 static void | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1421 delref(ClientData cref) |
7 | 1422 { |
1423 struct ref *ref = (struct ref *)cref; | |
1424 | |
1425 if (ref->delcmd) | |
1426 { | |
1427 Tcl_DecrRefCount(ref->delcmd); | |
1428 ref->delcmd = NULL; | |
1429 } | |
1430 ref->interp = NULL; | |
1431 } | |
1432 | |
1433 static char * | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1434 tclgetref( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1435 Tcl_Interp *interp, |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1436 void **refstartP, // ptr to w_tcl_ref/b_tcl-ref member of |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1437 // win_T/buf_T struct |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1438 char *prefix, // "win" or "buf" |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1439 void *vimobj, // win_T* or buf_T* |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1440 Tcl_ObjCmdProc *proc) // winselfcmd or bufselfcmd |
7 | 1441 { |
1442 struct ref *ref, *unused = NULL; | |
1443 static char name[VARNAME_SIZE]; | |
1444 Tcl_Command cmd; | |
1445 | |
1446 ref = (struct ref *)(*refstartP); | |
1447 if (ref == &refsdeleted) | |
1448 { | |
1449 Tcl_SetResult(interp, _("cannot create buffer/window command: object is being deleted"), TCL_STATIC); | |
1450 return NULL; | |
1451 } | |
1452 | |
1453 while (ref != NULL) | |
1454 { | |
1455 if (ref->interp == interp) | |
1456 break; | |
1457 if (ref->interp == NULL) | |
1458 unused = ref; | |
1459 ref = ref->next; | |
1460 } | |
1461 | |
1462 if (ref) | |
274 | 1463 vim_snprintf(name, sizeof(name), "::vim::%s", |
1464 Tcl_GetCommandName(interp, ref->cmd)); | |
7 | 1465 else |
1466 { | |
1467 if (unused) | |
1468 ref = unused; | |
1469 else | |
1470 { | |
1471 ref = (struct ref *)Tcl_Alloc(sizeof(struct ref)); | |
1472 ref->interp = NULL; | |
1473 ref->next = (struct ref *)(*refstartP); | |
1474 (*refstartP) = (void *)ref; | |
1475 } | |
1476 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1477 // This might break on some exotic systems... |
274 | 1478 vim_snprintf(name, sizeof(name), "::vim::%s_%lx", |
1479 prefix, (unsigned long)vimobj); | |
7 | 1480 cmd = Tcl_CreateObjCommand(interp, name, proc, |
1481 (ClientData)ref, (Tcl_CmdDeleteProc *)delref); | |
1482 if (!cmd) | |
1483 return NULL; | |
1484 | |
1485 ref->interp = interp; | |
1486 ref->cmd = cmd; | |
1487 ref->delcmd = NULL; | |
1488 ref->vimobj = vimobj; | |
1489 } | |
1490 return name; | |
1491 } | |
1492 | |
1493 static char * | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1494 tclgetwindow(Tcl_Interp *interp, win_T *win) |
7 | 1495 { |
502 | 1496 return tclgetref(interp, &(win->w_tcl_ref), "win", (void *)win, winselfcmd); |
7 | 1497 } |
1498 | |
1499 static char * | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1500 tclgetbuffer(Tcl_Interp *interp, buf_T *buf) |
7 | 1501 { |
502 | 1502 return tclgetref(interp, &(buf->b_tcl_ref), "buf", (void *)buf, bufselfcmd); |
7 | 1503 } |
1504 | |
1505 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1506 tclsetdelcmd( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1507 Tcl_Interp *interp, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1508 struct ref *reflist, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1509 void *vimobj, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1510 Tcl_Obj *delcmd) |
7 | 1511 { |
1512 if (reflist == &refsdeleted) | |
1513 { | |
1514 Tcl_SetResult(interp, _("cannot register callback command: buffer/window is already being deleted"), TCL_STATIC); | |
1515 return TCL_ERROR; | |
1516 } | |
1517 | |
1518 while (reflist != NULL) | |
1519 { | |
1520 if (reflist->interp == interp && reflist->vimobj == vimobj) | |
1521 { | |
1522 if (reflist->delcmd) | |
1523 Tcl_DecrRefCount(reflist->delcmd); | |
1524 Tcl_IncrRefCount(delcmd); | |
1525 reflist->delcmd = delcmd; | |
1526 return TCL_OK; | |
1527 } | |
1528 reflist = reflist->next; | |
1529 } | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1530 // This should never happen. Famous last word? |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
1531 emsg(_("E280: TCL FATAL ERROR: reflist corrupt!? Please report this to vim-dev@vim.org")); |
7 | 1532 Tcl_SetResult(interp, _("cannot register callback command: buffer/window reference not found"), TCL_STATIC); |
1533 return TCL_ERROR; | |
1534 } | |
1535 | |
1536 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1537 //////////////////////////////////////////// |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1538 // I/O Channel |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1539 //////////////////////////////////////////// |
7 | 1540 |
1541 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1542 tcl_channel_close(ClientData instance, Tcl_Interp *interp UNUSED) |
7 | 1543 { |
1544 int err = 0; | |
1545 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1546 // currently does nothing |
7 | 1547 |
1548 if (instance != VIMOUT && instance != VIMERR) | |
1549 { | |
1550 Tcl_SetErrno(EBADF); | |
1551 err = EBADF; | |
1552 } | |
1553 return err; | |
1554 } | |
1555 | |
1556 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1557 tcl_channel_input( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1558 ClientData instance UNUSED, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1559 char *buf UNUSED, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1560 int bufsiz UNUSED, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1561 int *errptr) |
7 | 1562 { |
1563 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1564 // input is currently not supported |
7 | 1565 |
1566 Tcl_SetErrno(EINVAL); | |
1567 if (errptr) | |
1568 *errptr = EINVAL; | |
1569 return -1; | |
1570 } | |
1571 | |
1572 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1573 tcl_channel_output( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1574 ClientData instance, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1575 const char *buf, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1576 int bufsiz, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1577 int *errptr) |
7 | 1578 { |
1579 char_u *str; | |
1580 int result; | |
1581 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1582 // The buffer is not guaranteed to be 0-terminated, and we don't if |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1583 // there is enough room to add a '\0'. So we have to create a copy |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1584 // of the buffer... |
7 | 1585 str = vim_strnsave((char_u *)buf, bufsiz); |
1586 if (!str) | |
1587 { | |
1588 Tcl_SetErrno(ENOMEM); | |
1589 if (errptr) | |
1590 *errptr = ENOMEM; | |
1591 return -1; | |
1592 } | |
1593 | |
1594 result = bufsiz; | |
1595 if (instance == VIMOUT) | |
1596 tclmsg((char *)str); | |
1597 else | |
1598 if (instance == VIMERR) | |
1599 tclerrmsg((char *)str); | |
1600 else | |
1601 { | |
1602 Tcl_SetErrno(EBADF); | |
1603 if (errptr) | |
1604 *errptr = EBADF; | |
1605 result = -1; | |
1606 } | |
1607 vim_free(str); | |
1608 return result; | |
1609 } | |
1610 | |
1611 static void | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1612 tcl_channel_watch(ClientData instance UNUSED, int mask UNUSED) |
7 | 1613 { |
1614 Tcl_SetErrno(EINVAL); | |
1615 } | |
1616 | |
1617 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1618 tcl_channel_gethandle( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1619 ClientData instance UNUSED, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1620 int direction UNUSED, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1621 ClientData *handleptr UNUSED) |
7 | 1622 { |
1623 Tcl_SetErrno(EINVAL); | |
1624 return EINVAL; | |
1625 } | |
1626 | |
1627 | |
7776
d30f4f9b1024
commit https://github.com/vim/vim/commit/0d6f835683bede8bfa171c2518dce10832eb8226
Christian Brabandt <cb@256bit.org>
parents:
7564
diff
changeset
|
1628 static Tcl_ChannelType tcl_channel_type = |
7 | 1629 { |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1630 "vimmessage", // typeName |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1631 TCL_CHANNEL_VERSION_2, // version |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1632 tcl_channel_close, // closeProc |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1633 tcl_channel_input, // inputProc |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1634 tcl_channel_output, // outputProc |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1635 NULL, // seekProc |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1636 NULL, // setOptionProc |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1637 NULL, // getOptionProc |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1638 tcl_channel_watch, // watchProc |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1639 tcl_channel_gethandle, // getHandleProc |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1640 NULL, // close2Proc |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1641 NULL, // blockModeProc |
1890 | 1642 #ifdef TCL_CHANNEL_VERSION_2 |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1643 NULL, // flushProc |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1644 NULL, // handlerProc |
1890 | 1645 #endif |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1646 // The following should not be necessary since TCL_CHANNEL_VERSION_2 was |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1647 // set above |
1890 | 1648 #ifdef TCL_CHANNEL_VERSION_3 |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1649 NULL, // wideSeekProc |
1890 | 1650 #endif |
1651 #ifdef TCL_CHANNEL_VERSION_4 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1652 NULL, // threadActionProc |
1890 | 1653 #endif |
1654 #ifdef TCL_CHANNEL_VERSION_5 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1655 NULL // truncateProc |
1890 | 1656 #endif |
7 | 1657 }; |
1658 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1659 /////////////////////////////////// |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1660 // Interface to vim |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1661 ////////////////////////////////// |
7 | 1662 |
1663 static void | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1664 tclupdatevars(void) |
7 | 1665 { |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1666 char varname[VARNAME_SIZE]; // must be writeable |
7 | 1667 char *name; |
1668 | |
1669 strcpy(varname, VAR_RANGE1); | |
1670 Tcl_UpdateLinkedVar(tclinfo.interp, varname); | |
1671 strcpy(varname, VAR_RANGE2); | |
1672 Tcl_UpdateLinkedVar(tclinfo.interp, varname); | |
1673 strcpy(varname, VAR_RANGE3); | |
1674 Tcl_UpdateLinkedVar(tclinfo.interp, varname); | |
1675 | |
1676 strcpy(varname, VAR_LBASE); | |
1677 Tcl_UpdateLinkedVar(tclinfo.interp, varname); | |
1678 | |
1679 name = tclgetbuffer(tclinfo.interp, curbuf); | |
1680 strcpy(tclinfo.curbuf, name); | |
1681 strcpy(varname, VAR_CURBUF); | |
1682 Tcl_UpdateLinkedVar(tclinfo.interp, varname); | |
1683 | |
1684 name = tclgetwindow(tclinfo.interp, curwin); | |
1685 strcpy(tclinfo.curwin, name); | |
1686 strcpy(varname, VAR_CURWIN); | |
1687 Tcl_UpdateLinkedVar(tclinfo.interp, varname); | |
1688 } | |
1689 | |
1690 | |
1691 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1692 tclinit(exarg_T *eap) |
7 | 1693 { |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1694 char varname[VARNAME_SIZE]; // Tcl_LinkVar requires writeable varname |
7 | 1695 char *name; |
1696 | |
1697 #ifdef DYNAMIC_TCL | |
1698 if (!tcl_enabled(TRUE)) | |
1699 { | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
1700 emsg(_("E571: Sorry, this command is disabled: the Tcl library could not be loaded.")); |
7 | 1701 return FAIL; |
1702 } | |
1703 #endif | |
1704 | |
1705 if (!tclinfo.interp) | |
1706 { | |
1707 Tcl_Interp *interp; | |
1708 static Tcl_Channel ch1, ch2; | |
1709 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1710 // Create replacement channels for stdout and stderr; this has to be |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1711 // done each time an interpreter is created since the channels are closed |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1712 // when the interpreter is deleted |
7776
d30f4f9b1024
commit https://github.com/vim/vim/commit/0d6f835683bede8bfa171c2518dce10832eb8226
Christian Brabandt <cb@256bit.org>
parents:
7564
diff
changeset
|
1713 ch1 = Tcl_CreateChannel(&tcl_channel_type, "vimout", VIMOUT, TCL_WRITABLE); |
d30f4f9b1024
commit https://github.com/vim/vim/commit/0d6f835683bede8bfa171c2518dce10832eb8226
Christian Brabandt <cb@256bit.org>
parents:
7564
diff
changeset
|
1714 ch2 = Tcl_CreateChannel(&tcl_channel_type, "vimerr", VIMERR, TCL_WRITABLE); |
7 | 1715 Tcl_SetStdChannel(ch1, TCL_STDOUT); |
1716 Tcl_SetStdChannel(ch2, TCL_STDERR); | |
1717 | |
1718 interp = Tcl_CreateInterp(); | |
1719 Tcl_Preserve(interp); | |
1720 if (Tcl_Init(interp) == TCL_ERROR) | |
1721 { | |
1722 Tcl_Release(interp); | |
1723 Tcl_DeleteInterp(interp); | |
1724 return FAIL; | |
1725 } | |
1726 #if 0 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1727 // VIM sure is interactive |
7 | 1728 Tcl_SetVar(interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY); |
1729 #endif | |
1730 | |
1731 Tcl_SetChannelOption(interp, ch1, "-buffering", "line"); | |
15868
7fad90423bd2
patch 8.1.0941: macros for MS-Windows are inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15543
diff
changeset
|
1732 #ifdef MSWIN |
3369 | 1733 Tcl_SetChannelOption(interp, ch1, "-translation", "lf"); |
1734 #endif | |
7 | 1735 Tcl_SetChannelOption(interp, ch2, "-buffering", "line"); |
15868
7fad90423bd2
patch 8.1.0941: macros for MS-Windows are inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15543
diff
changeset
|
1736 #ifdef MSWIN |
3369 | 1737 Tcl_SetChannelOption(interp, ch2, "-translation", "lf"); |
1738 #endif | |
7 | 1739 |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1740 // replace standard Tcl exit command |
7 | 1741 Tcl_DeleteCommand(interp, "exit"); |
1742 Tcl_CreateObjCommand(interp, "exit", exitcmd, | |
1743 (ClientData)NULL, (Tcl_CmdDeleteProc *)NULL); | |
1744 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1745 // new commands, in ::vim namespace |
7 | 1746 Tcl_CreateObjCommand(interp, "::vim::buffer", buffercmd, |
1747 (ClientData)NULL, (Tcl_CmdDeleteProc *)NULL); | |
1748 Tcl_CreateObjCommand(interp, "::vim::window", windowcmd, | |
1749 (ClientData)NULL, (Tcl_CmdDeleteProc *)NULL); | |
1750 Tcl_CreateObjCommand(interp, "::vim::command", commandcmd, | |
1751 (ClientData)NULL, (Tcl_CmdDeleteProc *)NULL); | |
1752 Tcl_CreateObjCommand(interp, "::vim::beep", beepcmd, | |
1753 (ClientData)NULL, (Tcl_CmdDeleteProc *)NULL); | |
1754 Tcl_CreateObjCommand(interp, "::vim::option", optioncmd, | |
1755 (ClientData)NULL, (Tcl_CmdDeleteProc *)NULL); | |
1756 Tcl_CreateObjCommand(interp, "::vim::expr", exprcmd, | |
1757 (ClientData)NULL, (Tcl_CmdDeleteProc *)NULL); | |
1758 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1759 // "lbase" variable |
7 | 1760 tclinfo.lbase = 1; |
1761 strcpy(varname, VAR_LBASE); | |
1762 Tcl_LinkVar(interp, varname, (char *)&tclinfo.lbase, TCL_LINK_INT); | |
1763 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1764 // "range" variable |
7 | 1765 tclinfo.range_start = eap->line1; |
1766 strcpy(varname, VAR_RANGE1); | |
1767 Tcl_LinkVar(interp, varname, (char *)&tclinfo.range_start, TCL_LINK_INT|TCL_LINK_READ_ONLY); | |
1768 strcpy(varname, VAR_RANGE2); | |
1769 Tcl_LinkVar(interp, varname, (char *)&tclinfo.range_start, TCL_LINK_INT|TCL_LINK_READ_ONLY); | |
1770 tclinfo.range_end = eap->line2; | |
1771 strcpy(varname, VAR_RANGE3); | |
1772 Tcl_LinkVar(interp, varname, (char *)&tclinfo.range_end, TCL_LINK_INT|TCL_LINK_READ_ONLY); | |
1773 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1774 // "current" variable |
7 | 1775 tclinfo.curbuf = Tcl_Alloc(VARNAME_SIZE); |
1776 tclinfo.curwin = Tcl_Alloc(VARNAME_SIZE); | |
1777 name = tclgetbuffer(interp, curbuf); | |
1778 strcpy(tclinfo.curbuf, name); | |
1779 strcpy(varname, VAR_CURBUF); | |
1780 Tcl_LinkVar(interp, varname, (char *)&tclinfo.curbuf, TCL_LINK_STRING|TCL_LINK_READ_ONLY); | |
1781 name = tclgetwindow(interp, curwin); | |
1782 strcpy(tclinfo.curwin, name); | |
1783 strcpy(varname, VAR_CURWIN); | |
1784 Tcl_LinkVar(interp, varname, (char *)&tclinfo.curwin, TCL_LINK_STRING|TCL_LINK_READ_ONLY); | |
1785 | |
1786 tclinfo.interp = interp; | |
1787 } | |
1788 else | |
1789 { | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1790 // Interpreter already exists, just update variables |
7 | 1791 tclinfo.range_start = row2tcl(eap->line1); |
1792 tclinfo.range_end = row2tcl(eap->line2); | |
1793 tclupdatevars(); | |
1794 } | |
3369 | 1795 |
1796 tclinfo.exitvalue = 0; | |
7 | 1797 return OK; |
1798 } | |
1799 | |
1800 static void | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1801 tclerrmsg(char *text) |
7 | 1802 { |
1803 char *next; | |
1804 | |
1805 while ((next=strchr(text, '\n'))) | |
1806 { | |
1807 *next++ = '\0'; | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
1808 emsg(text); |
7 | 1809 text = next; |
1810 } | |
1811 if (*text) | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
1812 emsg(text); |
7 | 1813 } |
1814 | |
1815 static void | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1816 tclmsg(char *text) |
7 | 1817 { |
1818 char *next; | |
1819 | |
1820 while ((next=strchr(text, '\n'))) | |
1821 { | |
1822 *next++ = '\0'; | |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
1823 msg(text); |
7 | 1824 text = next; |
1825 } | |
1826 if (*text) | |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
1827 msg(text); |
7 | 1828 } |
1829 | |
1830 static void | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1831 tcldelthisinterp(void) |
7 | 1832 { |
1833 if (!Tcl_InterpDeleted(tclinfo.interp)) | |
1834 Tcl_DeleteInterp(tclinfo.interp); | |
1835 Tcl_Release(tclinfo.interp); | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1836 // The interpreter is now gets deleted. All registered commands (esp. |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1837 // window and buffer commands) are deleted, triggering their deletion |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1838 // callback, which deletes all refs pointing to this interpreter. |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1839 // We could garbage-collect the unused ref structs in all windows and |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1840 // buffers, but unless the user creates hundreds of sub-interpreters |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1841 // all referring to lots of windows and buffers, this is hardly worth |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1842 // the effort. Unused refs are recycled by other interpreters, and |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1843 // all refs are free'd when the window/buffer gets closed by vim. |
7 | 1844 |
1845 tclinfo.interp = NULL; | |
1846 Tcl_Free(tclinfo.curbuf); | |
1847 Tcl_Free(tclinfo.curwin); | |
1848 tclinfo.curbuf = tclinfo.curwin = NULL; | |
1849 } | |
1850 | |
1851 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1852 tclexit(int error) |
7 | 1853 { |
1854 int newerr = OK; | |
1855 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1856 if (Tcl_InterpDeleted(tclinfo.interp) // True if we intercepted Tcl's exit command |
3369 | 1857 #if (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION >= 5) || TCL_MAJOR_VERSION > 8 |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1858 || Tcl_LimitExceeded(tclinfo.interp) // True if the interpreter cannot continue |
3369 | 1859 #endif |
1860 ) | |
7 | 1861 { |
274 | 1862 char buf[50]; |
7 | 1863 |
3369 | 1864 sprintf(buf, _("E572: exit code %d"), tclinfo.exitvalue); |
1865 tclerrmsg(buf); | |
1866 if (tclinfo.exitvalue == 0) | |
7 | 1867 { |
3369 | 1868 did_emsg = 0; |
1869 newerr = OK; | |
7 | 1870 } |
1871 else | |
3369 | 1872 newerr = FAIL; |
7 | 1873 |
1874 tcldelthisinterp(); | |
1875 } | |
1876 else | |
1877 { | |
1878 char *result; | |
1879 | |
135 | 1880 result = (char *)Tcl_GetStringResult(tclinfo.interp); |
7 | 1881 if (error == TCL_OK) |
1882 { | |
1883 tclmsg(result); | |
1884 newerr = OK; | |
1885 } | |
1886 else | |
1887 { | |
1888 tclerrmsg(result); | |
1889 newerr = FAIL; | |
1890 } | |
1891 } | |
1892 | |
1893 return newerr; | |
1894 } | |
1895 | |
1896 /* | |
1897 * ":tcl" | |
1898 */ | |
1899 void | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1900 ex_tcl(exarg_T *eap) |
7 | 1901 { |
1902 char_u *script; | |
1903 int err; | |
1904 | |
1905 script = script_get(eap, eap->arg); | |
1906 if (!eap->skip) | |
1907 { | |
1908 err = tclinit(eap); | |
1909 if (err == OK) | |
1910 { | |
1911 Tcl_AllowExceptions(tclinfo.interp); | |
1912 if (script == NULL) | |
1913 err = Tcl_Eval(tclinfo.interp, (char *)eap->arg); | |
1914 else | |
1915 err = Tcl_Eval(tclinfo.interp, (char *)script); | |
1916 err = tclexit(err); | |
1917 } | |
1918 } | |
1919 vim_free(script); | |
1920 } | |
1921 | |
1922 /* | |
1923 * ":tclfile" | |
1924 */ | |
1925 void | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1926 ex_tclfile(exarg_T *eap) |
7 | 1927 { |
1928 char *file = (char *)eap->arg; | |
1929 int err; | |
1930 | |
1931 err = tclinit(eap); | |
1932 if (err == OK) | |
1933 { | |
1934 Tcl_AllowExceptions(tclinfo.interp); | |
1935 err = Tcl_EvalFile(tclinfo.interp, file); | |
1936 err = tclexit(err); | |
1937 } | |
1938 } | |
1939 | |
1940 /* | |
1941 * ":tcldo" | |
1942 */ | |
1943 void | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
1944 ex_tcldo(exarg_T *eap) |
7 | 1945 { |
1946 char *script, *line; | |
1947 int err, rs, re, lnum; | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
1948 char var_lnum[VARNAME_SIZE]; // must be writeable memory |
7 | 1949 char var_line[VARNAME_SIZE]; |
1950 linenr_T first_line = 0; | |
1951 linenr_T last_line = 0; | |
10763
ae0bbbbe2a38
patch 8.0.0271: may get ml_get error when :tcldo deletes lines
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
1952 buf_T *was_curbuf = curbuf; |
7 | 1953 |
1954 rs = eap->line1; | |
1955 re = eap->line2; | |
1956 script = (char *)eap->arg; | |
1957 strcpy(var_lnum, VAR_CURLNUM); | |
1958 strcpy(var_line, VAR_CURLINE); | |
1959 | |
1960 err = tclinit(eap); | |
1961 if (err != OK) | |
1962 return; | |
1963 | |
1964 lnum = row2tcl(rs); | |
1965 Tcl_LinkVar(tclinfo.interp, var_lnum, (char *)&lnum, TCL_LINK_INT|TCL_LINK_READ_ONLY); | |
1966 err = TCL_OK; | |
1967 if (u_save((linenr_T)(rs-1), (linenr_T)(re+1)) != OK) | |
1968 { | |
1969 Tcl_SetResult(tclinfo.interp, _("cannot save undo information"), TCL_STATIC); | |
1970 err = TCL_ERROR; | |
1971 } | |
1972 while (err == TCL_OK && rs <= re) | |
1973 { | |
10763
ae0bbbbe2a38
patch 8.0.0271: may get ml_get error when :tcldo deletes lines
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
1974 if ((linenr_T)rs > curbuf->b_ml.ml_line_count) |
ae0bbbbe2a38
patch 8.0.0271: may get ml_get error when :tcldo deletes lines
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
1975 break; |
7 | 1976 line = (char *)ml_get_buf(curbuf, (linenr_T)rs, FALSE); |
1977 if (!line) | |
1978 { | |
1979 Tcl_SetResult(tclinfo.interp, _("cannot get line"), TCL_STATIC); | |
1980 err = TCL_ERROR; | |
1981 break; | |
1982 } | |
1983 Tcl_SetVar(tclinfo.interp, var_line, line, 0); | |
1984 Tcl_AllowExceptions(tclinfo.interp); | |
1985 err = Tcl_Eval(tclinfo.interp, script); | |
3369 | 1986 if (err != TCL_OK |
1987 || Tcl_InterpDeleted(tclinfo.interp) | |
1988 #if (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION >= 5) || TCL_MAJOR_VERSION > 8 | |
1989 || Tcl_LimitExceeded(tclinfo.interp) | |
1990 #endif | |
10763
ae0bbbbe2a38
patch 8.0.0271: may get ml_get error when :tcldo deletes lines
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
1991 || curbuf != was_curbuf) |
7 | 1992 break; |
135 | 1993 line = (char *)Tcl_GetVar(tclinfo.interp, var_line, 0); |
7 | 1994 if (line) |
1995 { | |
1996 if (ml_replace((linenr_T)rs, (char_u *)line, TRUE) != OK) | |
1997 { | |
1998 Tcl_SetResult(tclinfo.interp, _("cannot replace line"), TCL_STATIC); | |
1999 err = TCL_ERROR; | |
2000 break; | |
2001 } | |
2002 if (first_line == 0) | |
2003 first_line = rs; | |
2004 last_line = rs; | |
2005 } | |
2006 ++rs; | |
2007 ++lnum; | |
2008 Tcl_UpdateLinkedVar(tclinfo.interp, var_lnum); | |
2009 } | |
2010 if (first_line) | |
2011 changed_lines(first_line, 0, last_line + 1, (long)0); | |
2012 | |
2013 Tcl_UnsetVar(tclinfo.interp, var_line, 0); | |
2014 Tcl_UnlinkVar(tclinfo.interp, var_lnum); | |
2015 if (err == TCL_OK) | |
2016 Tcl_ResetResult(tclinfo.interp); | |
2017 | |
2018 (void)tclexit(err); | |
2019 } | |
2020 | |
2021 static void | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
2022 tcldelallrefs(struct ref *ref) |
7 | 2023 { |
2024 struct ref *next; | |
2025 int err; | |
2026 char *result; | |
2027 | |
7564
aee06f1762e0
commit https://github.com/vim/vim/commit/858b96f382eeb8f1eab5100639e7b09523a6a2a1
Christian Brabandt <cb@256bit.org>
parents:
7538
diff
changeset
|
2028 #ifdef DYNAMIC_TCL |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2029 // TODO: this code currently crashes Vim on exit |
7538
c9fc24b76293
commit https://github.com/vim/vim/commit/8a5115cf18751022387af2085f374d38c60dde83
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
2030 if (exiting) |
c9fc24b76293
commit https://github.com/vim/vim/commit/8a5115cf18751022387af2085f374d38c60dde83
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
2031 return; |
7564
aee06f1762e0
commit https://github.com/vim/vim/commit/858b96f382eeb8f1eab5100639e7b09523a6a2a1
Christian Brabandt <cb@256bit.org>
parents:
7538
diff
changeset
|
2032 #endif |
7538
c9fc24b76293
commit https://github.com/vim/vim/commit/8a5115cf18751022387af2085f374d38c60dde83
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
2033 |
7 | 2034 while (ref != NULL) |
2035 { | |
2036 next = ref->next; | |
2037 if (ref->interp) | |
2038 { | |
2039 if (ref->delcmd) | |
2040 { | |
2041 err = Tcl_GlobalEvalObj(ref->interp, ref->delcmd); | |
2042 if (err != TCL_OK) | |
2043 { | |
135 | 2044 result = (char *)Tcl_GetStringResult(ref->interp); |
7 | 2045 if (result) |
2046 tclerrmsg(result); | |
2047 } | |
2048 Tcl_DecrRefCount(ref->delcmd); | |
2049 ref->delcmd = NULL; | |
2050 } | |
2051 Tcl_DeleteCommandFromToken(ref->interp, ref->cmd); | |
2052 } | |
2053 Tcl_Free((char *)ref); | |
2054 ref = next; | |
2055 } | |
2056 } | |
2057 | |
2058 void | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
2059 tcl_buffer_free(buf_T *buf) |
7 | 2060 { |
2061 struct ref *reflist; | |
2062 | |
2063 #ifdef DYNAMIC_TCL | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2064 if (!stubs_initialized) // Not using Tcl, nothing to do. |
7 | 2065 return; |
2066 #endif | |
2067 | |
502 | 2068 reflist = (struct ref *)(buf->b_tcl_ref); |
7 | 2069 if (reflist != &refsdeleted) |
2070 { | |
502 | 2071 buf->b_tcl_ref = (void *)&refsdeleted; |
7 | 2072 tcldelallrefs(reflist); |
502 | 2073 buf->b_tcl_ref = NULL; |
7 | 2074 } |
2075 } | |
2076 | |
2077 void | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7776
diff
changeset
|
2078 tcl_window_free(win_T *win) |
7 | 2079 { |
2080 struct ref *reflist; | |
2081 | |
2082 #ifdef DYNAMIC_TCL | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2083 if (!stubs_initialized) // Not using Tcl, nothing to do. |
7 | 2084 return; |
2085 #endif | |
2086 | |
502 | 2087 reflist = (struct ref*)(win->w_tcl_ref); |
7 | 2088 if (reflist != &refsdeleted) |
2089 { | |
502 | 2090 win->w_tcl_ref = (void *)&refsdeleted; |
7 | 2091 tcldelallrefs(reflist); |
502 | 2092 win->w_tcl_ref = NULL; |
7 | 2093 } |
2094 } | |
2095 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2096 // The End |