forked from onivim/libvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibvim.c
More file actions
539 lines (430 loc) · 11.9 KB
/
Copy pathlibvim.c
File metadata and controls
539 lines (430 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/* vi:set ts=8 sts=4 sw=4 noet:
*
* libvim
*/
/*
* Top-level API for libvim.
*
* This provides the API surface for consumers of `libvim`.
*/
#include "vim.h"
buf_T *vimBufferOpen(char_u *ffname_arg, linenr_T lnum, int flags)
{
buf_T *buffer = buflist_new(ffname_arg, NULL, lnum, flags);
set_curbuf(buffer, DOBUF_SPLIT);
return buffer;
}
int vimBufferCheckIfChanged(buf_T *buf)
{
return buf_check_timestamp(buf, 0);
}
buf_T *vimBufferGetCurrent(void) { return curbuf; }
buf_T *vimBufferGetById(int id) { return buflist_findnr(id); }
char_u *vimBufferGetFilename(buf_T *buf) { return buf->b_ffname; }
char_u *vimBufferGetFiletype(buf_T *buf) { return buf->b_p_ft; }
void vimBufferSetCurrent(buf_T *buf) { set_curbuf(buf, DOBUF_SPLIT); }
int vimBufferGetId(buf_T *buf) { return buf->b_fnum; }
long vimBufferGetLastChangedTick(buf_T *buf) { return CHANGEDTICK(buf); }
int vimBufferGetModified(buf_T *buf) { return bufIsChanged(buf); }
int vimBufferGetModifiable(buf_T *buf) { return buf->b_p_ma; }
void vimBufferSetModifiable(buf_T *buf, int modifiable) { buf->b_p_ma = modifiable; }
int vimBufferGetFileFormat(buf_T *buf) { return get_fileformat(buf); }
void vimBufferSetFileFormat(buf_T *buf, int fileformat) { return set_fileformat_buf(buf, fileformat, OPT_LOCAL); }
int vimBufferGetReadOnly(buf_T *buf) { return buf->b_p_ro; }
void vimBufferSetReadOnly(buf_T *buf, int readonly) { buf->b_p_ro = readonly; }
char_u *vimBufferGetLine(buf_T *buf, linenr_T lnum)
{
char_u *result = ml_get_buf(buf, lnum, FALSE);
return result;
}
size_t vimBufferGetLineCount(buf_T *buf) { return buf->b_ml.ml_line_count; }
void vimBufferSetLines(buf_T *buf, linenr_T start, linenr_T end, char_u **lines, int count)
{
int originalLineCount = vimBufferGetLineCount(buf);
if (end == -1)
{
end = originalLineCount;
}
// Append in reverse order... find more efficient strategy though
// We append first, because `ml_delete_buf` can't delete the last line,
// for replacing entire an buffer contents
for (int i = count - 1; i >= 0; i--)
{
ml_append_buf(buf, start, lines[i], 0, FALSE);
}
int deleteCount = end - start;
int deleteStart = start + count + 1;
int deleteEnd = deleteStart + deleteCount;
// Delete from end to start
for (int i = deleteStart; i < deleteEnd; i++)
{
ml_delete_buf(buf, deleteStart, FALSE);
}
changed_lines_buf(buf, start, end, (end - start) - count);
++CHANGEDTICK(buf);
buf->b_changed = TRUE;
if (bufferUpdateCallback != NULL)
{
int newLineCount = vimBufferGetLineCount(buf);
int lnum = start == 0 ? 1 : start;
int lnume = end == 0 ? 1 : end + 1;
int xtra = newLineCount - originalLineCount;
bufferUpdate_T bufferUpdate;
bufferUpdate.buf = buf;
bufferUpdate.lnum = lnum;
bufferUpdate.lnume = lnume;
bufferUpdate.xtra = xtra;
bufferUpdateCallback(bufferUpdate);
}
}
void vimSetBufferUpdateCallback(BufferUpdateCallback f)
{
bufferUpdateCallback = f;
}
void vimSetAutoCommandCallback(AutoCommandCallback f)
{
autoCommandCallback = f;
}
void vimSetFileWriteFailureCallback(FileWriteFailureCallback f)
{
fileWriteFailureCallback = f;
}
void vimSetAutoIndentCallback(AutoIndentCallback f)
{
autoIndentCallback = f;
}
void vimSetMessageCallback(MessageCallback f)
{
messageCallback = f;
}
void vimSetTerminalCallback(TerminalCallback f)
{
terminalCallback = f;
}
void vimSetWindowSplitCallback(WindowSplitCallback f)
{
windowSplitCallback = f;
}
void vimSetWindowMovementCallback(WindowMovementCallback f)
{
windowMovementCallback = f;
}
void vimSetDirectoryChangedCallback(DirectoryChangedCallback f)
{
directoryChangedCallback = f;
}
void vimSetQuitCallback(QuitCallback f)
{
quitCallback = f;
}
void vimSetUnhandledEscapeCallback(VoidCallback callback)
{
unhandledEscapeCallback = callback;
}
char_u vimCommandLineGetType(void) { return ccline.cmdfirstc; }
char_u *vimCommandLineGetText(void) { return ccline.cmdbuff; }
int vimCommandLineGetPosition(void) { return ccline.cmdpos; }
void vimCommandLineGetCompletions(char_u ***completions, int *count)
{
*count = 0;
*completions = NULL;
if (!ccline.xpc)
{
return;
}
set_cmd_context(ccline.xpc,
ccline.cmdbuff,
strlen(ccline.cmdbuff),
ccline.cmdpos,
0);
expand_cmdline(ccline.xpc, ccline.cmdbuff, ccline.cmdpos, count, completions);
}
linenr_T vimCursorGetLine(void) { return curwin->w_cursor.lnum; };
colnr_T vimCursorGetColumn(void) { return curwin->w_cursor.col; };
pos_T vimCursorGetPosition(void) { return curwin->w_cursor; };
colnr_T vimCursorGetDesiredColumn(void) { return curwin->w_curswant; };
void vimCursorSetPosition(pos_T pos)
{
curwin->w_cursor.lnum = pos.lnum;
curwin->w_cursor.col = pos.col;
/* TODO: coladd? */
check_cursor();
// We also need to adjust the topline, potentially, if the cursor moved off-screen
curs_columns(TRUE);
}
void vimInputCore(int should_replace_termcodes, char_u *input)
{
if (should_replace_termcodes)
{
char_u *ptr = NULL;
char_u *cpo_save = p_cpo;
/* Set 'cpoptions' the way we want it.
* B set - backslashes are *not* treated specially
* k set - keycodes are *not* reverse-engineered
* < unset - <Key> sequences *are* interpreted
* The last but one parameter of replace_termcodes() is TRUE so that the
* <lt> sequence is recognised - needed for a real backslash.
*/
p_cpo = (char_u *)"Bk";
input = replace_termcodes((char_u *)input, &ptr, FALSE, TRUE, FALSE);
p_cpo = cpo_save;
if (*ptr != NUL) /* trailing CTRL-V results in nothing */
{
sm_execute_normal(input);
vim_free((char_u *)ptr);
}
}
else
{
sm_execute_normal(input);
}
/* Trigger CursorMoved if the cursor moved. */
if (!finish_op && (has_cursormoved()) &&
!EQUAL_POS(last_cursormoved, curwin->w_cursor))
{
if (has_cursormoved())
apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
last_cursormoved = curwin->w_cursor;
}
update_curswant();
curs_columns(TRUE);
}
void vimInput(char_u *input)
{
vimInputCore(0 /*should_replace_termcodes*/, input);
}
void vimKey(char_u *key)
{
vimInputCore(1 /*should_replace_termcodes*/, key);
}
int vimVisualIsActive(void) { return VIsual_active; }
int vimSelectIsActive(void) { return VIsual_select; }
int vimUndoSaveCursor(void)
{
return u_save_cursor();
}
int vimUndoSaveRegion(linenr_T start_lnum, linenr_T end_lnum)
{
return u_save(start_lnum, end_lnum);
}
void vimUndoSync(int force)
{
u_sync(force);
}
int vimVisualGetType(void) { return VIsual_mode; }
void vimVisualGetRange(pos_T *startPos, pos_T *endPos)
{
if (VIsual_active || VIsual_select)
{
*startPos = VIsual;
*endPos = curwin->w_cursor;
}
else
{
*startPos = curbuf->b_visual.vi_start;
*endPos = curbuf->b_visual.vi_end;
}
}
pos_T *vimSearchGetMatchingPair(int initc) { return findmatch(NULL, initc); }
typedef struct shlNode_elem shlNode_T;
struct shlNode_elem
{
searchHighlight_T highlight;
shlNode_T *next;
};
void vimSearchGetHighlights(linenr_T start_lnum, linenr_T end_lnum,
int *num_highlights,
searchHighlight_T **highlights)
{
int v = 1;
int count = 0;
pos_T lastPos;
pos_T startPos;
pos_T endPos;
startPos.lnum = start_lnum;
startPos.col = 0;
lastPos = startPos;
char_u *pattern = get_search_pat();
*num_highlights = 0;
*highlights = NULL;
if (pattern == NULL)
{
return;
}
shlNode_T *head = ALLOC_CLEAR_ONE(shlNode_T);
shlNode_T *cur = head;
while (v == 1)
{
v = searchit(NULL, curbuf, &startPos, &endPos, FORWARD, pattern, 1,
SEARCH_KEEP, RE_SEARCH, end_lnum, NULL, NULL);
if (v == 0)
{
break;
}
if (startPos.lnum < lastPos.lnum ||
(startPos.lnum == lastPos.lnum && startPos.col <= lastPos.col))
{
break;
}
shlNode_T *new = ALLOC_CLEAR_ONE(shlNode_T);
cur->next = new;
cur = new;
cur->highlight.start.lnum = startPos.lnum;
cur->highlight.start.col = startPos.col;
cur->highlight.end.lnum = endPos.lnum;
cur->highlight.end.col = endPos.col;
lastPos = startPos;
startPos = endPos;
startPos.col = startPos.col + 1;
count++;
}
if (count == 0)
{
vim_free(head);
return;
}
searchHighlight_T *ret = alloc(sizeof(searchHighlight_T) * count);
cur = head->next;
vim_free(head);
int i = 0;
while (cur != NULL)
{
ret[i] = cur->highlight;
shlNode_T *prev = cur;
cur = cur->next;
vim_free(prev);
i++;
}
*num_highlights = i;
*highlights = ret;
}
char_u *vimSearchGetPattern(void) { return get_search_pat(); }
void vimSetStopSearchHighlightCallback(VoidCallback callback)
{
stopSearchHighlightCallback = callback;
}
void vimExecute(char_u *cmd) { do_cmdline_cmd(cmd); }
void vimOptionSetTabSize(int tabSize)
{
curbuf->b_p_ts = tabSize;
curbuf->b_p_sts = tabSize;
curbuf->b_p_sw = tabSize;
}
void vimOptionSetInsertSpaces(int insertSpaces)
{
curbuf->b_p_et = insertSpaces;
if (!insertSpaces)
{
curbuf->b_p_sts = 0;
}
}
void vimOptionSetLineComment(const char_u *str)
{
vim_free(curbuf->b_oni_line_comment);
curbuf->b_oni_line_comment = (char_u *)alloc(STRLEN(str) + 1);
if (curbuf->b_oni_line_comment != NULL)
{
strcpy(curbuf->b_oni_line_comment, str);
}
}
int vimOptionGetTabSize() { return curbuf->b_p_ts; }
int vimOptionGetInsertSpaces(void) { return curbuf->b_p_et; }
int vimWindowGetWidth(void) { return curwin->w_width; }
int vimWindowGetHeight(void) { return curwin->w_height; }
int vimWindowGetTopLine(void) { return curwin->w_topline; }
int vimWindowGetLeftColumn(void) { return curwin->w_leftcol; }
void vimWindowSetTopLeft(int top, int left)
{
set_topline(curwin, top);
curwin->w_leftcol = left;
validate_botline();
}
void vimWindowSetWidth(int width)
{
if (width > Columns)
{
Columns = width;
screenalloc(FALSE);
}
win_new_width(curwin, width);
}
void vimWindowSetHeight(int height)
{
if (height > Rows)
{
Rows = height;
screenalloc(FALSE);
}
win_new_height(curwin, height);
// Set scroll value so that <c-d>/<c-u> work as expected
win_comp_scroll(curwin);
}
void vimSetClipboardGetCallback(ClipboardGetCallback callback)
{
clipboardGetCallback = callback;
}
int vimGetMode(void) { return get_real_state(); }
void vimSetFormatCallback(FormatCallback callback)
{
formatCallback = callback;
}
void vimSetGotoCallback(GotoCallback callback)
{
gotoCallback = callback;
}
void vimSetTabPageCallback(TabPageCallback callback)
{
tabPageCallback = callback;
}
void vimSetDisplayIntroCallback(VoidCallback callback)
{
displayIntroCallback = callback;
}
void vimSetDisplayVersionCallback(VoidCallback callback)
{
displayVersionCallback = callback;
}
char_u *vimEval(char_u *str)
{
char_u *copy = vim_strsave(str);
char_u *ret = eval_to_string(copy, NULL, TRUE);
vim_free(copy);
return ret;
}
void vimRegisterGet(int reg_name, int *num_lines, char_u ***lines)
{
get_yank_register_value(reg_name, num_lines, lines);
}
void vimSetYankCallback(YankCallback callback)
{
yankCallback = callback;
}
void vimInit(int argc, char **argv)
{
mparm_T params;
vim_memset(¶ms, 0, sizeof(params));
params.argc = argc;
params.argv = argv;
params.want_full_screen = TRUE;
params.window_count = -1;
mch_early_init();
common_init(¶ms);
// Ported from mch_init_c on Windows
// Is there anything else we need from there?
#ifdef MSWIN
#ifndef __MINGW32__
extern int _fmode;
#endif
_fmode = O_BINARY; /* we do our own CR-LF translation */
#endif
// Don't load viminfofile, for now.
p_viminfofile = "NONE";
// Set to 'nocompatible' so we get the expected Vim undo / redo behavior,
// rather than Vi's behavior.
// See :help cpoptions and :help compatible for details
change_compatible(FALSE);
full_screen = TRUE;
vimWindowSetWidth(80);
vimWindowSetHeight(40);
screenalloc(FALSE);
}