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