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