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