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