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