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