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