7
|
1 /* vi:set ts=8 sts=4 sw=4:
|
|
2 *
|
|
3 * VIM - Vi IMproved by Bram Moolenaar
|
|
4 *
|
|
5 * Do ":help uganda" in Vim to read copying and usage conditions.
|
|
6 * Do ":help credits" in Vim to see a list of people who contributed.
|
|
7 */
|
|
8 /*
|
|
9 * if_perl.xs: Main code for Perl interface support.
|
|
10 * Mostly written by Sven Verdoolaege.
|
|
11 */
|
|
12
|
|
13 #define _memory_h /* avoid memset redeclaration */
|
|
14 #define IN_PERL_FILE /* don't include if_perl.pro from proto.h */
|
|
15
|
|
16 #include "vim.h"
|
|
17
|
|
18
|
|
19 /*
|
|
20 * Work around clashes between Perl and Vim namespace. proto.h doesn't
|
|
21 * include if_perl.pro and perlsfio.pro when IN_PERL_FILE is defined, because
|
|
22 * we need the CV typedef. proto.h can't be moved to after including
|
|
23 * if_perl.h, because we get all sorts of name clashes then.
|
|
24 */
|
|
25 #ifndef PROTO
|
|
26 #ifndef __MINGW32__
|
|
27 # include "proto/if_perl.pro"
|
|
28 # include "proto/if_perlsfio.pro"
|
|
29 #endif
|
|
30 #endif
|
|
31
|
|
32 /* Perl compatibility stuff. This should ensure compatibility with older
|
|
33 * versions of Perl.
|
|
34 */
|
|
35
|
|
36 #ifndef PERL_VERSION
|
|
37 # include <patchlevel.h>
|
|
38 # define PERL_REVISION 5
|
|
39 # define PERL_VERSION PATCHLEVEL
|
|
40 # define PERL_SUBVERSION SUBVERSION
|
|
41 #endif
|
|
42
|
|
43 #ifndef pTHX
|
|
44 # define pTHX void
|
|
45 # define pTHX_
|
|
46 #endif
|
|
47
|
|
48 #ifndef EXTERN_C
|
|
49 # define EXTERN_C
|
|
50 #endif
|
|
51
|
|
52 /* Compatibility hacks over */
|
|
53
|
|
54 static PerlInterpreter *perl_interp = NULL;
|
|
55 static void xs_init __ARGS((pTHX));
|
|
56 static void VIM_init __ARGS((void));
|
|
57 EXTERN_C void boot_DynaLoader __ARGS((pTHX_ CV*));
|
|
58
|
|
59 /*
|
|
60 * For dynamic linked perl. (Windows)
|
|
61 */
|
|
62 #if defined(DYNAMIC_PERL) || defined(PROTO)
|
|
63 /*
|
|
64 * Wrapper defines
|
|
65 */
|
|
66 # define perl_alloc dll_perl_alloc
|
|
67 # define perl_construct dll_perl_construct
|
|
68 # define perl_parse dll_perl_parse
|
|
69 # define perl_run dll_perl_run
|
|
70 # define perl_destruct dll_perl_destruct
|
|
71 # define perl_free dll_perl_free
|
|
72 # define Perl_get_context dll_Perl_get_context
|
|
73 # define Perl_croak dll_Perl_croak
|
|
74 # ifndef PROTO
|
|
75 # define Perl_croak_nocontext dll_Perl_croak_nocontext
|
|
76 # define Perl_call_argv dll_Perl_call_argv
|
|
77 # define Perl_call_pv dll_Perl_call_pv
|
|
78 # define Perl_eval_sv dll_Perl_eval_sv
|
|
79 # define Perl_get_sv dll_Perl_get_sv
|
|
80 # define Perl_eval_pv dll_Perl_eval_pv
|
|
81 # define Perl_call_method dll_Perl_call_method
|
|
82 # endif
|
|
83 # define Perl_dowantarray dll_Perl_dowantarray
|
|
84 # define Perl_free_tmps dll_Perl_free_tmps
|
|
85 # define Perl_gv_stashpv dll_Perl_gv_stashpv
|
|
86 # define Perl_markstack_grow dll_Perl_markstack_grow
|
|
87 # define Perl_mg_find dll_Perl_mg_find
|
|
88 # define Perl_newXS dll_Perl_newXS
|
|
89 # define Perl_newSV dll_Perl_newSV
|
|
90 # define Perl_newSViv dll_Perl_newSViv
|
|
91 # define Perl_newSVpv dll_Perl_newSVpv
|
|
92 # define Perl_pop_scope dll_Perl_pop_scope
|
|
93 # define Perl_push_scope dll_Perl_push_scope
|
|
94 # define Perl_save_int dll_Perl_save_int
|
|
95 # define Perl_stack_grow dll_Perl_stack_grow
|
|
96 # define Perl_set_context dll_Perl_set_context
|
|
97 # define Perl_sv_2bool dll_Perl_sv_2bool
|
|
98 # define Perl_sv_2iv dll_Perl_sv_2iv
|
|
99 # define Perl_sv_2mortal dll_Perl_sv_2mortal
|
|
100 # if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
|
|
101 # define Perl_sv_2pv_flags dll_Perl_sv_2pv_flags
|
|
102 # define Perl_sv_2pv_nolen dll_Perl_sv_2pv_nolen
|
|
103 # else
|
|
104 # define Perl_sv_2pv dll_Perl_sv_2pv
|
|
105 # endif
|
|
106 # define Perl_sv_bless dll_Perl_sv_bless
|
|
107 # if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
|
|
108 # define Perl_sv_catpvn_flags dll_Perl_sv_catpvn_flags
|
|
109 # else
|
|
110 # define Perl_sv_catpvn dll_Perl_sv_catpvn
|
|
111 # endif
|
|
112 # define Perl_sv_free dll_Perl_sv_free
|
|
113 # define Perl_sv_isa dll_Perl_sv_isa
|
|
114 # define Perl_sv_magic dll_Perl_sv_magic
|
|
115 # define Perl_sv_setiv dll_Perl_sv_setiv
|
|
116 # define Perl_sv_setpv dll_Perl_sv_setpv
|
|
117 # define Perl_sv_setpvn dll_Perl_sv_setpvn
|
|
118 # if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
|
|
119 # define Perl_sv_setsv_flags dll_Perl_sv_setsv_flags
|
|
120 # else
|
|
121 # define Perl_sv_setsv dll_Perl_sv_setsv
|
|
122 # endif
|
|
123 # define Perl_sv_upgrade dll_Perl_sv_upgrade
|
|
124 # define Perl_Tstack_sp_ptr dll_Perl_Tstack_sp_ptr
|
|
125 # define Perl_Top_ptr dll_Perl_Top_ptr
|
|
126 # define Perl_Tstack_base_ptr dll_Perl_Tstack_base_ptr
|
|
127 # define Perl_Tstack_max_ptr dll_Perl_Tstack_max_ptr
|
|
128 # define Perl_Ttmps_ix_ptr dll_Perl_Ttmps_ix_ptr
|
|
129 # define Perl_Ttmps_floor_ptr dll_Perl_Ttmps_floor_ptr
|
|
130 # define Perl_Tmarkstack_ptr_ptr dll_Perl_Tmarkstack_ptr_ptr
|
|
131 # define Perl_Tmarkstack_max_ptr dll_Perl_Tmarkstack_max_ptr
|
|
132 # define Perl_TSv_ptr dll_Perl_TSv_ptr
|
|
133 # define Perl_TXpv_ptr dll_Perl_TXpv_ptr
|
|
134 # define Perl_Tna_ptr dll_Perl_Tna_ptr
|
|
135 # define Perl_Idefgv_ptr dll_Perl_Idefgv_ptr
|
|
136 # define Perl_Ierrgv_ptr dll_Perl_Ierrgv_ptr
|
|
137 # define Perl_Isv_yes_ptr dll_Perl_Isv_yes_ptr
|
|
138 # define boot_DynaLoader dll_boot_DynaLoader
|
|
139
|
|
140 #ifndef DYNAMIC_PERL /* just generating prototypes */
|
|
141 typedef int HANDLE;
|
|
142 typedef int XSINIT_t;
|
|
143 typedef int XSUBADDR_t;
|
|
144 #endif
|
|
145
|
|
146 /*
|
|
147 * Declare HANDLE for perl.dll and function pointers.
|
|
148 */
|
|
149 static HANDLE hPerlLib = NULL;
|
|
150
|
|
151 static PerlInterpreter* (*perl_alloc)();
|
|
152 static void (*perl_construct)(PerlInterpreter*);
|
|
153 static void (*perl_destruct)(PerlInterpreter*);
|
|
154 static void (*perl_free)(PerlInterpreter*);
|
|
155 static int (*perl_run)(PerlInterpreter*);
|
|
156 static int (*perl_parse)(PerlInterpreter*, XSINIT_t, int, char**, char**);
|
|
157 static void* (*Perl_get_context)(void);
|
|
158 static void (*Perl_croak)(pTHX_ const char*, ...) __attribute__((noreturn));
|
|
159 static void (*Perl_croak_nocontext)(const char*, ...) __attribute__((noreturn));
|
|
160 static I32 (*Perl_dowantarray)(pTHX);
|
|
161 static void (*Perl_free_tmps)(pTHX);
|
|
162 static HV* (*Perl_gv_stashpv)(pTHX_ const char*, I32);
|
|
163 static void (*Perl_markstack_grow)(pTHX);
|
|
164 static MAGIC* (*Perl_mg_find)(pTHX_ SV*, int);
|
|
165 static CV* (*Perl_newXS)(pTHX_ char*, XSUBADDR_t, char*);
|
|
166 static SV* (*Perl_newSV)(pTHX_ STRLEN);
|
|
167 static SV* (*Perl_newSViv)(pTHX_ IV);
|
|
168 static SV* (*Perl_newSVpv)(pTHX_ const char*, STRLEN);
|
|
169 static I32 (*Perl_call_argv)(pTHX_ const char*, I32, char**);
|
|
170 static I32 (*Perl_call_pv)(pTHX_ const char*, I32);
|
|
171 static I32 (*Perl_eval_sv)(pTHX_ SV*, I32);
|
|
172 static SV* (*Perl_get_sv)(pTHX_ const char*, I32);
|
|
173 static SV* (*Perl_eval_pv)(pTHX_ const char*, I32);
|
|
174 static SV* (*Perl_call_method)(pTHX_ const char*, I32);
|
|
175 static void (*Perl_pop_scope)(pTHX);
|
|
176 static void (*Perl_push_scope)(pTHX);
|
|
177 static void (*Perl_save_int)(pTHX_ int*);
|
|
178 static SV** (*Perl_stack_grow)(pTHX_ SV**, SV**p, int);
|
|
179 static SV** (*Perl_set_context)(void*);
|
|
180 static bool (*Perl_sv_2bool)(pTHX_ SV*);
|
|
181 static IV (*Perl_sv_2iv)(pTHX_ SV*);
|
|
182 static SV* (*Perl_sv_2mortal)(pTHX_ SV*);
|
|
183 #if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
|
|
184 static char* (*Perl_sv_2pv_flags)(pTHX_ SV*, STRLEN*, I32);
|
|
185 static char* (*Perl_sv_2pv_nolen)(pTHX_ SV*);
|
|
186 #else
|
|
187 static char* (*Perl_sv_2pv)(pTHX_ SV*, STRLEN*);
|
|
188 #endif
|
|
189 static SV* (*Perl_sv_bless)(pTHX_ SV*, HV*);
|
|
190 #if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
|
|
191 static void (*Perl_sv_catpvn_flags)(pTHX_ SV* , const char*, STRLEN, I32);
|
|
192 #else
|
|
193 static void (*Perl_sv_catpvn)(pTHX_ SV*, const char*, STRLEN);
|
|
194 #endif
|
|
195 static void (*Perl_sv_free)(pTHX_ SV*);
|
|
196 static int (*Perl_sv_isa)(pTHX_ SV*, const char*);
|
|
197 static void (*Perl_sv_magic)(pTHX_ SV*, SV*, int, const char*, I32);
|
|
198 static void (*Perl_sv_setiv)(pTHX_ SV*, IV);
|
|
199 static void (*Perl_sv_setpv)(pTHX_ SV*, const char*);
|
|
200 static void (*Perl_sv_setpvn)(pTHX_ SV*, const char*, STRLEN);
|
|
201 #if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
|
|
202 static void (*Perl_sv_setsv_flags)(pTHX_ SV*, SV*, I32);
|
|
203 #else
|
|
204 static void (*Perl_sv_setsv)(pTHX_ SV*, SV*);
|
|
205 #endif
|
|
206 static bool (*Perl_sv_upgrade)(pTHX_ SV*, U32);
|
|
207 static SV*** (*Perl_Tstack_sp_ptr)(register PerlInterpreter*);
|
|
208 static OP** (*Perl_Top_ptr)(register PerlInterpreter*);
|
|
209 static SV*** (*Perl_Tstack_base_ptr)(register PerlInterpreter*);
|
|
210 static SV*** (*Perl_Tstack_max_ptr)(register PerlInterpreter*);
|
|
211 static I32* (*Perl_Ttmps_ix_ptr)(register PerlInterpreter*);
|
|
212 static I32* (*Perl_Ttmps_floor_ptr)(register PerlInterpreter*);
|
|
213 static I32** (*Perl_Tmarkstack_ptr_ptr)(register PerlInterpreter*);
|
|
214 static I32** (*Perl_Tmarkstack_max_ptr)(register PerlInterpreter*);
|
|
215 static SV** (*Perl_TSv_ptr)(register PerlInterpreter*);
|
|
216 static XPV** (*Perl_TXpv_ptr)(register PerlInterpreter*);
|
|
217 static STRLEN* (*Perl_Tna_ptr)(register PerlInterpreter*);
|
|
218 static GV** (*Perl_Idefgv_ptr)(register PerlInterpreter*);
|
|
219 static GV** (*Perl_Ierrgv_ptr)(register PerlInterpreter*);
|
|
220 static SV* (*Perl_Isv_yes_ptr)(register PerlInterpreter*);
|
|
221 static void (*boot_DynaLoader)_((pTHX_ CV*));
|
|
222
|
|
223
|
|
224 /*
|
|
225 * Table of name to function pointer of perl.
|
|
226 */
|
|
227 #define PERL_PROC FARPROC
|
|
228 static struct {
|
|
229 char* name;
|
|
230 PERL_PROC* ptr;
|
|
231 } perl_funcname_table[] = {
|
|
232 {"perl_alloc", (PERL_PROC*)&perl_alloc},
|
|
233 {"perl_construct", (PERL_PROC*)&perl_construct},
|
|
234 {"perl_destruct", (PERL_PROC*)&perl_destruct},
|
|
235 {"perl_free", (PERL_PROC*)&perl_free},
|
|
236 {"perl_run", (PERL_PROC*)&perl_run},
|
|
237 {"perl_parse", (PERL_PROC*)&perl_parse},
|
|
238 {"Perl_get_context", (PERL_PROC*)&Perl_get_context},
|
|
239 {"Perl_croak", (PERL_PROC*)&Perl_croak},
|
|
240 {"Perl_croak_nocontext", (PERL_PROC*)&Perl_croak_nocontext},
|
|
241 {"Perl_dowantarray", (PERL_PROC*)&Perl_dowantarray},
|
|
242 {"Perl_free_tmps", (PERL_PROC*)&Perl_free_tmps},
|
|
243 {"Perl_gv_stashpv", (PERL_PROC*)&Perl_gv_stashpv},
|
|
244 {"Perl_markstack_grow", (PERL_PROC*)&Perl_markstack_grow},
|
|
245 {"Perl_mg_find", (PERL_PROC*)&Perl_mg_find},
|
|
246 {"Perl_newXS", (PERL_PROC*)&Perl_newXS},
|
|
247 {"Perl_newSV", (PERL_PROC*)&Perl_newSV},
|
|
248 {"Perl_newSViv", (PERL_PROC*)&Perl_newSViv},
|
|
249 {"Perl_newSVpv", (PERL_PROC*)&Perl_newSVpv},
|
|
250 {"Perl_call_argv", (PERL_PROC*)&Perl_call_argv},
|
|
251 {"Perl_call_pv", (PERL_PROC*)&Perl_call_pv},
|
|
252 {"Perl_eval_sv", (PERL_PROC*)&Perl_eval_sv},
|
|
253 {"Perl_get_sv", (PERL_PROC*)&Perl_get_sv},
|
|
254 {"Perl_eval_pv", (PERL_PROC*)&Perl_eval_pv},
|
|
255 {"Perl_call_method", (PERL_PROC*)&Perl_call_method},
|
|
256 {"Perl_pop_scope", (PERL_PROC*)&Perl_pop_scope},
|
|
257 {"Perl_push_scope", (PERL_PROC*)&Perl_push_scope},
|
|
258 {"Perl_save_int", (PERL_PROC*)&Perl_save_int},
|
|
259 {"Perl_stack_grow", (PERL_PROC*)&Perl_stack_grow},
|
|
260 {"Perl_set_context", (PERL_PROC*)&Perl_set_context},
|
|
261 {"Perl_sv_2bool", (PERL_PROC*)&Perl_sv_2bool},
|
|
262 {"Perl_sv_2iv", (PERL_PROC*)&Perl_sv_2iv},
|
|
263 {"Perl_sv_2mortal", (PERL_PROC*)&Perl_sv_2mortal},
|
|
264 #if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
|
|
265 {"Perl_sv_2pv_flags", (PERL_PROC*)&Perl_sv_2pv_flags},
|
|
266 {"Perl_sv_2pv_nolen", (PERL_PROC*)&Perl_sv_2pv_nolen},
|
|
267 #else
|
|
268 {"Perl_sv_2pv", (PERL_PROC*)&Perl_sv_2pv},
|
|
269 #endif
|
|
270 {"Perl_sv_bless", (PERL_PROC*)&Perl_sv_bless},
|
|
271 #if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
|
|
272 {"Perl_sv_catpvn_flags", (PERL_PROC*)&Perl_sv_catpvn_flags},
|
|
273 #else
|
|
274 {"Perl_sv_catpvn", (PERL_PROC*)&Perl_sv_catpvn},
|
|
275 #endif
|
|
276 {"Perl_sv_free", (PERL_PROC*)&Perl_sv_free},
|
|
277 {"Perl_sv_isa", (PERL_PROC*)&Perl_sv_isa},
|
|
278 {"Perl_sv_magic", (PERL_PROC*)&Perl_sv_magic},
|
|
279 {"Perl_sv_setiv", (PERL_PROC*)&Perl_sv_setiv},
|
|
280 {"Perl_sv_setpv", (PERL_PROC*)&Perl_sv_setpv},
|
|
281 {"Perl_sv_setpvn", (PERL_PROC*)&Perl_sv_setpvn},
|
|
282 #if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
|
|
283 {"Perl_sv_setsv_flags", (PERL_PROC*)&Perl_sv_setsv_flags},
|
|
284 #else
|
|
285 {"Perl_sv_setsv", (PERL_PROC*)&Perl_sv_setsv},
|
|
286 #endif
|
|
287 {"Perl_sv_upgrade", (PERL_PROC*)&Perl_sv_upgrade},
|
|
288 {"Perl_Tstack_sp_ptr", (PERL_PROC*)&Perl_Tstack_sp_ptr},
|
|
289 {"Perl_Top_ptr", (PERL_PROC*)&Perl_Top_ptr},
|
|
290 {"Perl_Tstack_base_ptr", (PERL_PROC*)&Perl_Tstack_base_ptr},
|
|
291 {"Perl_Tstack_max_ptr", (PERL_PROC*)&Perl_Tstack_max_ptr},
|
|
292 {"Perl_Ttmps_ix_ptr", (PERL_PROC*)&Perl_Ttmps_ix_ptr},
|
|
293 {"Perl_Ttmps_floor_ptr", (PERL_PROC*)&Perl_Ttmps_floor_ptr},
|
|
294 {"Perl_Tmarkstack_ptr_ptr", (PERL_PROC*)&Perl_Tmarkstack_ptr_ptr},
|
|
295 {"Perl_Tmarkstack_max_ptr", (PERL_PROC*)&Perl_Tmarkstack_max_ptr},
|
|
296 {"Perl_TSv_ptr", (PERL_PROC*)&Perl_TSv_ptr},
|
|
297 {"Perl_TXpv_ptr", (PERL_PROC*)&Perl_TXpv_ptr},
|
|
298 {"Perl_Tna_ptr", (PERL_PROC*)&Perl_Tna_ptr},
|
|
299 {"Perl_Idefgv_ptr", (PERL_PROC*)&Perl_Idefgv_ptr},
|
|
300 {"Perl_Ierrgv_ptr", (PERL_PROC*)&Perl_Ierrgv_ptr},
|
|
301 {"Perl_Isv_yes_ptr", (PERL_PROC*)&Perl_Isv_yes_ptr},
|
|
302 {"boot_DynaLoader", (PERL_PROC*)&boot_DynaLoader},
|
|
303 {"", NULL},
|
|
304 };
|
|
305
|
|
306 /*
|
|
307 * Make all runtime-links of perl.
|
|
308 *
|
|
309 * 1. Get module handle using LoadLibraryEx.
|
|
310 * 2. Get pointer to perl function by GetProcAddress.
|
|
311 * 3. Repeat 2, until get all functions will be used.
|
|
312 *
|
|
313 * Parameter 'libname' provides name of DLL.
|
|
314 * Return OK or FAIL.
|
|
315 */
|
|
316 static int
|
|
317 perl_runtime_link_init(char *libname, int verbose)
|
|
318 {
|
|
319 int i;
|
|
320
|
|
321 if (hPerlLib != NULL)
|
|
322 return OK;
|
|
323 if (!(hPerlLib = LoadLibraryEx(libname, NULL, 0)))
|
|
324 {
|
|
325 if (verbose)
|
|
326 EMSG2(_("E370: Could not load library %s"), libname);
|
|
327 return FAIL;
|
|
328 }
|
|
329 for (i = 0; perl_funcname_table[i].ptr; ++i)
|
|
330 {
|
|
331 if (!(*perl_funcname_table[i].ptr = GetProcAddress(hPerlLib,
|
|
332 perl_funcname_table[i].name)))
|
|
333 {
|
|
334 FreeLibrary(hPerlLib);
|
|
335 hPerlLib = NULL;
|
|
336 if (verbose)
|
|
337 EMSG2(_(e_loadfunc), perl_funcname_table[i].name);
|
|
338 return FAIL;
|
|
339 }
|
|
340 }
|
|
341 return OK;
|
|
342 }
|
|
343
|
|
344 /*
|
|
345 * If runtime-link-perl(DLL) was loaded successfully, return TRUE.
|
|
346 * There were no DLL loaded, return FALSE.
|
|
347 */
|
|
348 int
|
|
349 perl_enabled(verbose)
|
|
350 int verbose;
|
|
351 {
|
|
352 return perl_runtime_link_init(DYNAMIC_PERL_DLL, verbose) == OK;
|
|
353 }
|
|
354 #endif /* DYNAMIC_PERL */
|
|
355
|
|
356 /*
|
|
357 * perl_init(): initialize perl interpreter
|
|
358 * We have to call perl_parse to initialize some structures,
|
|
359 * there's nothing to actually parse.
|
|
360 */
|
|
361 static void
|
|
362 perl_init()
|
|
363 {
|
|
364 char *bootargs[] = { "VI", NULL };
|
|
365 static char *args[] = { "", "-e", "" };
|
|
366
|
|
367 perl_interp = perl_alloc();
|
|
368 perl_construct(perl_interp);
|
|
369 perl_parse(perl_interp, xs_init, 3, args, 0);
|
|
370 perl_call_argv("VIM::bootstrap", (long)G_DISCARD, bootargs);
|
|
371 VIM_init();
|
|
372 #ifdef USE_SFIO
|
|
373 sfdisc(PerlIO_stdout(), sfdcnewvim());
|
|
374 sfdisc(PerlIO_stderr(), sfdcnewvim());
|
|
375 sfsetbuf(PerlIO_stdout(), NULL, 0);
|
|
376 sfsetbuf(PerlIO_stderr(), NULL, 0);
|
|
377 #endif
|
|
378 }
|
|
379
|
|
380 /*
|
|
381 * perl_end(): clean up after ourselves
|
|
382 */
|
|
383 void
|
|
384 perl_end()
|
|
385 {
|
|
386 if (perl_interp)
|
|
387 {
|
|
388 perl_run(perl_interp);
|
|
389 perl_destruct(perl_interp);
|
|
390 perl_free(perl_interp);
|
|
391 perl_interp = NULL;
|
|
392 }
|
|
393 #ifdef DYNAMIC_PERL
|
|
394 if (hPerlLib)
|
|
395 {
|
|
396 FreeLibrary(hPerlLib);
|
|
397 hPerlLib = NULL;
|
|
398 }
|
|
399 #endif
|
|
400 }
|
|
401
|
|
402 /*
|
|
403 * msg_split(): send a message to the message handling routines
|
|
404 * split at '\n' first though.
|
|
405 */
|
|
406 void
|
|
407 msg_split(s, attr)
|
|
408 char_u *s;
|
|
409 int attr; /* highlighting attributes */
|
|
410 {
|
|
411 char *next;
|
|
412 char *token = (char *)s;
|
|
413
|
|
414 while ((next = strchr(token, '\n')))
|
|
415 {
|
|
416 *next++ = '\0'; /* replace \n with \0 */
|
|
417 msg_attr((char_u *)token, attr);
|
|
418 token = next;
|
|
419 }
|
|
420 if (*token)
|
|
421 msg_attr((char_u *)token, attr);
|
|
422 }
|
|
423
|
|
424 #ifndef FEAT_EVAL
|
|
425 /*
|
|
426 * This stub is needed because an "#ifdef FEAT_EVAL" around Eval() doesn't
|
|
427 * work properly.
|
|
428 */
|
|
429 char_u *
|
|
430 eval_to_string(arg, nextcmd)
|
|
431 char_u *arg;
|
|
432 char_u **nextcmd;
|
|
433 {
|
|
434 return NULL;
|
|
435 }
|
|
436 #endif
|
|
437
|
|
438 /*
|
|
439 * Create a new reference to an SV pointing to the SCR structure
|
|
440 * The perl_private part of the SCR structure points to the SV,
|
|
441 * so there can only be one such SV for a particular SCR structure.
|
|
442 * When the last reference has gone (DESTROY is called),
|
|
443 * perl_private is reset; When the screen goes away before
|
|
444 * all references are gone, the value of the SV is reset;
|
|
445 * any subsequent use of any of those reference will produce
|
|
446 * a warning. (see typemap)
|
|
447 */
|
|
448 #define newANYrv(TYPE, TNAME) \
|
|
449 static SV * \
|
|
450 new ## TNAME ## rv(rv, ptr) \
|
|
451 SV *rv; \
|
|
452 TYPE *ptr; \
|
|
453 { \
|
|
454 sv_upgrade(rv, SVt_RV); \
|
|
455 if (!ptr->perl_private) \
|
|
456 { \
|
|
457 ptr->perl_private = newSV(0); \
|
|
458 sv_setiv(ptr->perl_private, (IV)ptr); \
|
|
459 } \
|
|
460 else \
|
|
461 SvREFCNT_inc(ptr->perl_private); \
|
|
462 SvRV(rv) = ptr->perl_private; \
|
|
463 SvROK_on(rv); \
|
|
464 return sv_bless(rv, gv_stashpv("VI" #TNAME, TRUE)); \
|
|
465 }
|
|
466
|
129
|
467 /* LINTED: avoid warning: cast from pointer to integer of different size */
|
7
|
468 newANYrv(win_T, WIN)
|
129
|
469 /* LINTED: avoid warning: cast from pointer to integer of different size */
|
7
|
470 newANYrv(buf_T, BUF)
|
|
471
|
|
472 /*
|
|
473 * perl_win_free
|
|
474 * Remove all refences to the window to be destroyed
|
|
475 */
|
|
476 void
|
|
477 perl_win_free(wp)
|
|
478 win_T *wp;
|
|
479 {
|
|
480 if (wp->perl_private)
|
|
481 sv_setiv((SV *)wp->perl_private, 0);
|
|
482 return;
|
|
483 }
|
|
484
|
|
485 void
|
|
486 perl_buf_free(bp)
|
|
487 buf_T *bp;
|
|
488 {
|
|
489 if (bp->perl_private)
|
|
490 sv_setiv((SV *)bp->perl_private, 0);
|
|
491 return;
|
|
492 }
|
|
493
|
|
494 #ifndef PROTO
|
|
495 # if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
|
|
496 I32 cur_val(pTHX_ IV iv, SV *sv);
|
|
497 # else
|
|
498 I32 cur_val(IV iv, SV *sv);
|
|
499 #endif
|
|
500
|
|
501 /*
|
|
502 * Handler for the magic variables $main::curwin and $main::curbuf.
|
|
503 * The handler is put into the magic vtbl for these variables.
|
|
504 * (This is effectively a C-level equivalent of a tied variable).
|
|
505 * There is no "set" function as the variables are read-only.
|
|
506 */
|
|
507 # if (PERL_REVISION == 5) && (PERL_VERSION >= 8)
|
|
508 I32 cur_val(pTHX_ IV iv, SV *sv)
|
|
509 # else
|
|
510 I32 cur_val(IV iv, SV *sv)
|
|
511 # endif
|
|
512 {
|
|
513 SV *rv;
|
|
514 if (iv == 0)
|
|
515 rv = newWINrv(newSV(0), curwin);
|
|
516 else
|
|
517 rv = newBUFrv(newSV(0), curbuf);
|
|
518 sv_setsv(sv, rv);
|
|
519 return 0;
|
|
520 }
|
|
521 #endif /* !PROTO */
|
|
522
|
|
523 struct ufuncs cw_funcs = { cur_val, 0, 0 };
|
|
524 struct ufuncs cb_funcs = { cur_val, 0, 1 };
|
|
525
|
|
526 /*
|
|
527 * VIM_init(): Vim-specific initialisation.
|
|
528 * Make the magical main::curwin and main::curbuf variables
|
|
529 */
|
|
530 static void
|
|
531 VIM_init()
|
|
532 {
|
|
533 static char cw[] = "main::curwin";
|
|
534 static char cb[] = "main::curbuf";
|
|
535 SV *sv;
|
|
536
|
|
537 sv = perl_get_sv(cw, TRUE);
|
|
538 sv_magic(sv, NULL, 'U', (char *)&cw_funcs, sizeof(cw_funcs));
|
|
539 SvREADONLY_on(sv);
|
|
540
|
|
541 sv = perl_get_sv(cb, TRUE);
|
|
542 sv_magic(sv, NULL, 'U', (char *)&cb_funcs, sizeof(cb_funcs));
|
|
543 SvREADONLY_on(sv);
|
|
544
|
|
545 /*
|
|
546 * Setup the Safe compartment.
|
|
547 * It shouldn't be a fatal error if the Safe module is missing.
|
|
548 * XXX: Only shares the 'Msg' routine (which has to be called
|
|
549 * like 'Msg(...)').
|
|
550 */
|
|
551 (void)perl_eval_pv( "if ( eval( 'require Safe' ) ) { $VIM::safe = Safe->new(); $VIM::safe->share_from( 'VIM', ['Msg'] ); }", G_DISCARD | G_VOID );
|
|
552
|
|
553 }
|
|
554
|
|
555 #ifdef DYNAMIC_PERL
|
|
556 static char *e_noperl = N_("Sorry, this command is disabled: the Perl library could not be loaded.");
|
|
557 #endif
|
|
558
|
|
559 /*
|
|
560 * ":perl"
|
|
561 */
|
|
562 void
|
|
563 ex_perl(eap)
|
|
564 exarg_T *eap;
|
|
565 {
|
|
566 char *err;
|
|
567 char *script;
|
|
568 STRLEN length;
|
|
569 SV *sv;
|
|
570 SV *safe;
|
|
571
|
|
572 script = (char *)script_get(eap, eap->arg);
|
|
573 if (eap->skip)
|
|
574 {
|
|
575 vim_free(script);
|
|
576 return;
|
|
577 }
|
|
578
|
|
579 if (perl_interp == NULL)
|
|
580 {
|
|
581 #ifdef DYNAMIC_PERL
|
|
582 if (!perl_enabled(TRUE))
|
|
583 {
|
|
584 EMSG(_(e_noperl));
|
|
585 vim_free(script);
|
|
586 return;
|
|
587 }
|
|
588 #endif
|
|
589 perl_init();
|
|
590 }
|
|
591
|
|
592 {
|
|
593 dSP;
|
|
594 ENTER;
|
|
595 SAVETMPS;
|
|
596
|
|
597 if (script == NULL)
|
|
598 sv = newSVpv((char *)eap->arg, 0);
|
|
599 else
|
|
600 {
|
|
601 sv = newSVpv(script, 0);
|
|
602 vim_free(script);
|
|
603 }
|
|
604
|
|
605 #ifdef HAVE_SANDBOX
|
|
606 if (sandbox)
|
|
607 {
|
|
608 if ((safe = perl_get_sv( "VIM::safe", FALSE )) == NULL || !SvTRUE(safe))
|
|
609 EMSG(_("E299: Perl evaluation forbidden in sandbox without the Safe module"));
|
|
610 else
|
|
611 {
|
|
612 PUSHMARK(SP);
|
|
613 XPUSHs(safe);
|
|
614 XPUSHs(sv);
|
|
615 PUTBACK;
|
|
616 perl_call_method("reval", G_DISCARD);
|
|
617 }
|
|
618 }
|
|
619 else
|
|
620 #endif
|
|
621 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
|
|
622
|
|
623 SvREFCNT_dec(sv);
|
|
624
|
|
625 err = SvPV(GvSV(PL_errgv), length);
|
|
626
|
|
627 FREETMPS;
|
|
628 LEAVE;
|
|
629
|
|
630 if (!length)
|
|
631 return;
|
|
632
|
|
633 msg_split((char_u *)err, highlight_attr[HLF_E]);
|
|
634 return;
|
|
635 }
|
|
636 }
|
|
637
|
|
638 static int
|
|
639 replace_line(line, end)
|
|
640 linenr_T *line, *end;
|
|
641 {
|
|
642 char *str;
|
|
643
|
|
644 if (SvOK(GvSV(PL_defgv)))
|
|
645 {
|
|
646 str = SvPV(GvSV(PL_defgv), PL_na);
|
|
647 ml_replace(*line, (char_u *)str, 1);
|
|
648 changed_bytes(*line, 0);
|
|
649 }
|
|
650 else
|
|
651 {
|
|
652 ml_delete(*line, FALSE);
|
|
653 deleted_lines_mark(*line, 1L);
|
|
654 --(*end);
|
|
655 --(*line);
|
|
656 }
|
|
657 return OK;
|
|
658 }
|
|
659
|
|
660 /*
|
|
661 * ":perldo".
|
|
662 */
|
|
663 void
|
|
664 ex_perldo(eap)
|
|
665 exarg_T *eap;
|
|
666 {
|
|
667 STRLEN length;
|
|
668 SV *sv;
|
|
669 char *str;
|
|
670 linenr_T i;
|
|
671
|
|
672 if (bufempty())
|
|
673 return;
|
|
674
|
|
675 if (perl_interp == NULL)
|
|
676 {
|
|
677 #ifdef DYNAMIC_PERL
|
|
678 if (!perl_enabled(TRUE))
|
|
679 {
|
|
680 EMSG(_(e_noperl));
|
|
681 return;
|
|
682 }
|
|
683 #endif
|
|
684 perl_init();
|
|
685 }
|
|
686 {
|
|
687 dSP;
|
|
688 length = strlen((char *)eap->arg);
|
129
|
689 sv = newSV(length + sizeof("sub VIM::perldo {") - 1 + 1);
|
|
690 sv_setpvn(sv, "sub VIM::perldo {", sizeof("sub VIM::perldo {") - 1);
|
7
|
691 sv_catpvn(sv, (char *)eap->arg, length);
|
|
692 sv_catpvn(sv, "}", 1);
|
|
693 perl_eval_sv(sv, G_DISCARD | G_NOARGS);
|
|
694 SvREFCNT_dec(sv);
|
|
695 str = SvPV(GvSV(PL_errgv), length);
|
|
696 if (length)
|
|
697 goto err;
|
|
698
|
|
699 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
|
|
700 return;
|
|
701
|
|
702 ENTER;
|
|
703 SAVETMPS;
|
|
704 for (i = eap->line1; i <= eap->line2; i++)
|
|
705 {
|
129
|
706 sv_setpv(GvSV(PL_defgv), (char *)ml_get(i));
|
7
|
707 PUSHMARK(sp);
|
|
708 perl_call_pv("VIM::perldo", G_SCALAR | G_EVAL);
|
|
709 str = SvPV(GvSV(PL_errgv), length);
|
|
710 if (length)
|
|
711 break;
|
|
712 SPAGAIN;
|
|
713 if (SvTRUEx(POPs))
|
|
714 {
|
|
715 if (replace_line(&i, &eap->line2) != OK)
|
|
716 {
|
|
717 PUTBACK;
|
|
718 break;
|
|
719 }
|
|
720 }
|
|
721 PUTBACK;
|
|
722 }
|
|
723 FREETMPS;
|
|
724 LEAVE;
|
|
725 check_cursor();
|
|
726 update_screen(NOT_VALID);
|
|
727 if (!length)
|
|
728 return;
|
|
729
|
|
730 err:
|
|
731 msg_split((char_u *)str, highlight_attr[HLF_E]);
|
|
732 return;
|
|
733 }
|
|
734 }
|
|
735
|
|
736 XS(XS_VIM_Msg);
|
|
737 XS(XS_VIM_SetOption);
|
|
738 XS(XS_VIM_DoCommand);
|
|
739 XS(XS_VIM_Eval);
|
|
740 XS(XS_VIM_Buffers);
|
|
741 XS(XS_VIM_Windows);
|
|
742 XS(XS_VIWIN_DESTROY);
|
|
743 XS(XS_VIWIN_Buffer);
|
|
744 XS(XS_VIWIN_SetHeight);
|
|
745 XS(XS_VIWIN_Cursor);
|
|
746 XS(XS_VIBUF_DESTROY);
|
|
747 XS(XS_VIBUF_Name);
|
|
748 XS(XS_VIBUF_Number);
|
|
749 XS(XS_VIBUF_Count);
|
|
750 XS(XS_VIBUF_Get);
|
|
751 XS(XS_VIBUF_Set);
|
|
752 XS(XS_VIBUF_Delete);
|
|
753 XS(XS_VIBUF_Append);
|
|
754 XS(boot_VIM);
|
|
755
|
|
756 static void
|
|
757 xs_init(pTHX)
|
|
758 {
|
|
759 char *file = __FILE__;
|
|
760
|
|
761 /* DynaLoader is a special case */
|
|
762 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
|
|
763 newXS("VIM::bootstrap", boot_VIM, file);
|
|
764 }
|
|
765
|
|
766 typedef win_T * VIWIN;
|
|
767 typedef buf_T * VIBUF;
|
|
768
|
|
769 MODULE = VIM PACKAGE = VIM
|
|
770
|
|
771 void
|
|
772 Msg(text, hl=NULL)
|
|
773 char *text;
|
|
774 char *hl;
|
|
775
|
|
776 PREINIT:
|
|
777 int attr;
|
|
778 int id;
|
|
779
|
|
780 PPCODE:
|
|
781 if (text != NULL)
|
|
782 {
|
|
783 attr = 0;
|
|
784 if (hl != NULL)
|
|
785 {
|
|
786 id = syn_name2id((char_u *)hl);
|
|
787 if (id != 0)
|
|
788 attr = syn_id2attr(id);
|
|
789 }
|
|
790 msg_split((char_u *)text, attr);
|
|
791 }
|
|
792
|
|
793 void
|
|
794 SetOption(line)
|
|
795 char *line;
|
|
796
|
|
797 PPCODE:
|
|
798 if (line != NULL)
|
|
799 do_set((char_u *)line, 0);
|
|
800 update_screen(NOT_VALID);
|
|
801
|
|
802 void
|
|
803 DoCommand(line)
|
|
804 char *line;
|
|
805
|
|
806 PPCODE:
|
|
807 if (line != NULL)
|
|
808 do_cmdline_cmd((char_u *)line);
|
|
809
|
|
810 void
|
|
811 Eval(str)
|
|
812 char *str;
|
|
813
|
|
814 PREINIT:
|
|
815 char_u *value;
|
|
816 PPCODE:
|
|
817 value = eval_to_string((char_u *)str, (char_u**)0);
|
|
818 if (value == NULL)
|
|
819 {
|
|
820 XPUSHs(sv_2mortal(newSViv(0)));
|
|
821 XPUSHs(sv_2mortal(newSVpv("", 0)));
|
|
822 }
|
|
823 else
|
|
824 {
|
|
825 XPUSHs(sv_2mortal(newSViv(1)));
|
|
826 XPUSHs(sv_2mortal(newSVpv((char *)value, 0)));
|
|
827 vim_free(value);
|
|
828 }
|
|
829
|
|
830 void
|
|
831 Buffers(...)
|
|
832
|
|
833 PREINIT:
|
|
834 buf_T *vimbuf;
|
|
835 int i, b;
|
|
836
|
|
837 PPCODE:
|
|
838 if (items == 0)
|
|
839 {
|
|
840 if (GIMME == G_SCALAR)
|
|
841 {
|
|
842 i = 0;
|
|
843 for (vimbuf = firstbuf; vimbuf; vimbuf = vimbuf->b_next)
|
|
844 ++i;
|
|
845
|
|
846 XPUSHs(sv_2mortal(newSViv(i)));
|
|
847 }
|
|
848 else
|
|
849 {
|
|
850 for (vimbuf = firstbuf; vimbuf; vimbuf = vimbuf->b_next)
|
|
851 XPUSHs(newBUFrv(newSV(0), vimbuf));
|
|
852 }
|
|
853 }
|
|
854 else
|
|
855 {
|
|
856 for (i = 0; i < items; i++)
|
|
857 {
|
|
858 SV *sv = ST(i);
|
|
859 if (SvIOK(sv))
|
|
860 b = SvIV(ST(i));
|
|
861 else
|
|
862 {
|
|
863 char_u *pat;
|
|
864 STRLEN len;
|
|
865
|
|
866 pat = (char_u *)SvPV(sv, len);
|
|
867 ++emsg_off;
|
|
868 b = buflist_findpat(pat, pat+len, FALSE, FALSE);
|
|
869 --emsg_off;
|
|
870 }
|
|
871
|
|
872 if (b >= 0)
|
|
873 {
|
|
874 vimbuf = buflist_findnr(b);
|
|
875 if (vimbuf)
|
|
876 XPUSHs(newBUFrv(newSV(0), vimbuf));
|
|
877 }
|
|
878 }
|
|
879 }
|
|
880
|
|
881 void
|
|
882 Windows(...)
|
|
883
|
|
884 PREINIT:
|
|
885 win_T *vimwin;
|
|
886 int i, w;
|
|
887
|
|
888 PPCODE:
|
|
889 if (items == 0)
|
|
890 {
|
|
891 if (GIMME == G_SCALAR)
|
|
892 XPUSHs(sv_2mortal(newSViv(win_count())));
|
|
893 else
|
|
894 {
|
|
895 for (vimwin = firstwin; vimwin != NULL; vimwin = W_NEXT(vimwin))
|
|
896 XPUSHs(newWINrv(newSV(0), vimwin));
|
|
897 }
|
|
898 }
|
|
899 else
|
|
900 {
|
|
901 for (i = 0; i < items; i++)
|
|
902 {
|
|
903 w = SvIV(ST(i));
|
|
904 vimwin = win_find_nr(w);
|
|
905 if (vimwin)
|
|
906 XPUSHs(newWINrv(newSV(0), vimwin));
|
|
907 }
|
|
908 }
|
|
909
|
|
910 MODULE = VIM PACKAGE = VIWIN
|
|
911
|
|
912 void
|
|
913 DESTROY(win)
|
|
914 VIWIN win
|
|
915
|
|
916 CODE:
|
|
917 if (win_valid(win))
|
|
918 win->perl_private = 0;
|
|
919
|
|
920 SV *
|
|
921 Buffer(win)
|
|
922 VIWIN win
|
|
923
|
|
924 CODE:
|
|
925 if (!win_valid(win))
|
|
926 win = curwin;
|
|
927 RETVAL = newBUFrv(newSV(0), win->w_buffer);
|
|
928 OUTPUT:
|
|
929 RETVAL
|
|
930
|
|
931 void
|
|
932 SetHeight(win, height)
|
|
933 VIWIN win
|
|
934 int height;
|
|
935
|
|
936 PREINIT:
|
|
937 win_T *savewin;
|
|
938
|
|
939 PPCODE:
|
|
940 if (!win_valid(win))
|
|
941 win = curwin;
|
|
942 savewin = curwin;
|
|
943 curwin = win;
|
|
944 win_setheight(height);
|
|
945 curwin = savewin;
|
|
946
|
|
947 void
|
|
948 Cursor(win, ...)
|
|
949 VIWIN win
|
|
950
|
|
951 PPCODE:
|
|
952 if(items == 1)
|
|
953 {
|
|
954 EXTEND(sp, 2);
|
|
955 if (!win_valid(win))
|
|
956 win = curwin;
|
|
957 PUSHs(sv_2mortal(newSViv(win->w_cursor.lnum)));
|
|
958 PUSHs(sv_2mortal(newSViv(win->w_cursor.col)));
|
|
959 }
|
|
960 else if(items == 3)
|
|
961 {
|
|
962 int lnum, col;
|
|
963
|
|
964 if (!win_valid(win))
|
|
965 win = curwin;
|
|
966 lnum = SvIV(ST(1));
|
|
967 col = SvIV(ST(2));
|
|
968 win->w_cursor.lnum = lnum;
|
|
969 win->w_cursor.col = col;
|
|
970 check_cursor(); /* put cursor on an existing line */
|
|
971 update_screen(NOT_VALID);
|
|
972 }
|
|
973
|
|
974 MODULE = VIM PACKAGE = VIBUF
|
|
975
|
|
976 void
|
|
977 DESTROY(vimbuf)
|
|
978 VIBUF vimbuf;
|
|
979
|
|
980 CODE:
|
|
981 if (buf_valid(vimbuf))
|
|
982 vimbuf->perl_private = 0;
|
|
983
|
|
984 void
|
|
985 Name(vimbuf)
|
|
986 VIBUF vimbuf;
|
|
987
|
|
988 PPCODE:
|
|
989 if (!buf_valid(vimbuf))
|
|
990 vimbuf = curbuf;
|
|
991 /* No file name returns an empty string */
|
|
992 if (vimbuf->b_fname == NULL)
|
|
993 XPUSHs(sv_2mortal(newSVpv("", 0)));
|
|
994 else
|
|
995 XPUSHs(sv_2mortal(newSVpv((char *)vimbuf->b_fname, 0)));
|
|
996
|
|
997 void
|
|
998 Number(vimbuf)
|
|
999 VIBUF vimbuf;
|
|
1000
|
|
1001 PPCODE:
|
|
1002 if (!buf_valid(vimbuf))
|
|
1003 vimbuf = curbuf;
|
|
1004 XPUSHs(sv_2mortal(newSViv(vimbuf->b_fnum)));
|
|
1005
|
|
1006 void
|
|
1007 Count(vimbuf)
|
|
1008 VIBUF vimbuf;
|
|
1009
|
|
1010 PPCODE:
|
|
1011 if (!buf_valid(vimbuf))
|
|
1012 vimbuf = curbuf;
|
|
1013 XPUSHs(sv_2mortal(newSViv(vimbuf->b_ml.ml_line_count)));
|
|
1014
|
|
1015 void
|
|
1016 Get(vimbuf, ...)
|
|
1017 VIBUF vimbuf;
|
|
1018
|
|
1019 PREINIT:
|
|
1020 char_u *line;
|
|
1021 int i;
|
|
1022 long lnum;
|
|
1023 PPCODE:
|
|
1024 if (buf_valid(vimbuf))
|
|
1025 {
|
|
1026 for (i = 1; i < items; i++)
|
|
1027 {
|
|
1028 lnum = SvIV(ST(i));
|
|
1029 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
|
|
1030 {
|
|
1031 line = ml_get_buf(vimbuf, lnum, FALSE);
|
|
1032 XPUSHs(sv_2mortal(newSVpv((char *)line, 0)));
|
|
1033 }
|
|
1034 }
|
|
1035 }
|
|
1036
|
|
1037 void
|
|
1038 Set(vimbuf, ...)
|
|
1039 VIBUF vimbuf;
|
|
1040
|
|
1041 PREINIT:
|
|
1042 int i;
|
|
1043 long lnum;
|
|
1044 char *line;
|
|
1045 buf_T *savebuf;
|
|
1046 PPCODE:
|
|
1047 if (buf_valid(vimbuf))
|
|
1048 {
|
|
1049 if (items < 3)
|
|
1050 croak("Usage: VIBUF::Set(vimbuf, lnum, @lines)");
|
|
1051
|
|
1052 lnum = SvIV(ST(1));
|
|
1053 for(i = 2; i < items; i++, lnum++)
|
|
1054 {
|
|
1055 line = SvPV(ST(i),PL_na);
|
|
1056 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
|
|
1057 {
|
|
1058 savebuf = curbuf;
|
|
1059 curbuf = vimbuf;
|
|
1060 if (u_savesub(lnum) == OK)
|
|
1061 {
|
|
1062 ml_replace(lnum, (char_u *)line, TRUE);
|
|
1063 changed_bytes(lnum, 0);
|
|
1064 }
|
|
1065 curbuf = savebuf;
|
|
1066 }
|
|
1067 }
|
|
1068 }
|
|
1069
|
|
1070 void
|
|
1071 Delete(vimbuf, ...)
|
|
1072 VIBUF vimbuf;
|
|
1073
|
|
1074 PREINIT:
|
|
1075 long i, lnum = 0, count = 0;
|
|
1076 buf_T *savebuf;
|
|
1077 PPCODE:
|
|
1078 if (buf_valid(vimbuf))
|
|
1079 {
|
|
1080 if (items == 2)
|
|
1081 {
|
|
1082 lnum = SvIV(ST(1));
|
|
1083 count = 1;
|
|
1084 }
|
|
1085 else if (items == 3)
|
|
1086 {
|
|
1087 lnum = SvIV(ST(1));
|
|
1088 count = 1 + SvIV(ST(2)) - lnum;
|
|
1089 if(count == 0)
|
|
1090 count = 1;
|
|
1091 if(count < 0)
|
|
1092 {
|
|
1093 lnum -= count;
|
|
1094 count = -count;
|
|
1095 }
|
|
1096 }
|
|
1097 if (items >= 2)
|
|
1098 {
|
|
1099 for (i = 0; i < count; i++)
|
|
1100 {
|
|
1101 if (lnum > 0 && lnum <= vimbuf->b_ml.ml_line_count)
|
|
1102 {
|
|
1103 savebuf = curbuf;
|
|
1104 curbuf = vimbuf;
|
|
1105 if (u_savedel(lnum, 1) == OK)
|
|
1106 {
|
|
1107 ml_delete(lnum, 0);
|
|
1108 deleted_lines_mark(lnum, 1L);
|
|
1109 if (savebuf == curbuf)
|
|
1110 check_cursor();
|
|
1111 }
|
|
1112 curbuf = savebuf;
|
|
1113 update_curbuf(VALID);
|
|
1114 }
|
|
1115 }
|
|
1116 }
|
|
1117 }
|
|
1118
|
|
1119 void
|
|
1120 Append(vimbuf, ...)
|
|
1121 VIBUF vimbuf;
|
|
1122
|
|
1123 PREINIT:
|
|
1124 int i;
|
|
1125 long lnum;
|
|
1126 char *line;
|
|
1127 buf_T *savebuf;
|
|
1128 PPCODE:
|
|
1129 if (buf_valid(vimbuf))
|
|
1130 {
|
|
1131 if (items < 3)
|
|
1132 croak("Usage: VIBUF::Append(vimbuf, lnum, @lines)");
|
|
1133
|
|
1134 lnum = SvIV(ST(1));
|
|
1135 for (i = 2; i < items; i++, lnum++)
|
|
1136 {
|
|
1137 line = SvPV(ST(i),PL_na);
|
|
1138 if (lnum >= 0 && lnum <= vimbuf->b_ml.ml_line_count && line != NULL)
|
|
1139 {
|
|
1140 savebuf = curbuf;
|
|
1141 curbuf = vimbuf;
|
|
1142 if (u_inssub(lnum + 1) == OK)
|
|
1143 {
|
|
1144 ml_append(lnum, (char_u *)line, (colnr_T)0, FALSE);
|
|
1145 appended_lines_mark(lnum, 1L);
|
|
1146 }
|
|
1147 curbuf = savebuf;
|
|
1148 update_curbuf(VALID);
|
|
1149 }
|
|
1150 }
|
|
1151 }
|
|
1152
|