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