Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/extern/isocline/src/undo.c
2727 views
1
/* ----------------------------------------------------------------------------
2
Copyright (c) 2021, Daan Leijen
3
This is free software; you can redistribute it and/or modify it
4
under the terms of the MIT License. A copy of the license can be
5
found in the "LICENSE" file at the root of this distribution.
6
-----------------------------------------------------------------------------*/
7
#include <string.h>
8
#include <stdio.h>
9
10
#include "../include/isocline.h"
11
#include "common.h"
12
#include "env.h"
13
#include "stringbuf.h"
14
#include "completions.h"
15
#include "undo.h"
16
17
18
19
//-------------------------------------------------------------
20
// edit state
21
//-------------------------------------------------------------
22
struct editstate_s {
23
struct editstate_s* next;
24
const char* input; // input
25
ssize_t pos; // cursor position
26
};
27
28
ic_private void editstate_init( editstate_t** es ) {
29
*es = NULL;
30
}
31
32
ic_private void editstate_done( alloc_t* mem, editstate_t** es ) {
33
while (*es != NULL) {
34
editstate_t* next = (*es)->next;
35
mem_free(mem, (*es)->input);
36
mem_free(mem, *es );
37
*es = next;
38
}
39
*es = NULL;
40
}
41
42
ic_private void editstate_capture( alloc_t* mem, editstate_t** es, const char* input, ssize_t pos) {
43
if (input==NULL) input = "";
44
// alloc
45
editstate_t* entry = mem_zalloc_tp(mem, editstate_t);
46
if (entry == NULL) return;
47
// initialize
48
entry->input = mem_strdup( mem, input);
49
entry->pos = pos;
50
if (entry->input == NULL) { mem_free(mem, entry); return; }
51
// and push
52
entry->next = *es;
53
*es = entry;
54
}
55
56
// caller should free *input
57
ic_private bool editstate_restore( alloc_t* mem, editstate_t** es, const char** input, ssize_t* pos ) {
58
if (*es == NULL) return false;
59
// pop
60
editstate_t* entry = *es;
61
*es = entry->next;
62
*input = entry->input;
63
*pos = entry->pos;
64
mem_free(mem, entry);
65
return true;
66
}
67
68
69