aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/doc.c
blob: 4e41e8ab0cd443956a71957e1e580dc481698332 (plain)
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
/*
 * Copyright (C) 2012-2024 Robin Haberkorn
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <glib.h>

#include <Scintilla.h>

#include "sciteco.h"
#include "view.h"
#include "undo.h"
#include "qreg.h"
#include "doc.h"

static inline teco_doc_scintilla_t *
teco_doc_get_scintilla(teco_doc_t *ctx)
{
	if (G_UNLIKELY(!ctx->doc))
		ctx->doc = (teco_doc_scintilla_t *)teco_view_ssm(teco_qreg_view, SCI_CREATEDOCUMENT, 0, 0);
	return ctx->doc;
}

/** @memberof teco_doc_t */
void
teco_doc_edit(teco_doc_t *ctx)
{
	teco_view_ssm(teco_qreg_view, SCI_SETDOCPOINTER, 0,
	              (sptr_t)teco_doc_get_scintilla(ctx));
	teco_view_ssm(teco_qreg_view, SCI_SETFIRSTVISIBLELINE, ctx->first_line, 0);
	teco_view_ssm(teco_qreg_view, SCI_SETXOFFSET, ctx->xoffset, 0);
	teco_view_ssm(teco_qreg_view, SCI_SETSEL, ctx->anchor, (sptr_t)ctx->dot);

	/*
	 * NOTE: Thanks to a custom Scintilla patch, se representations
	 * do not get reset after SCI_SETDOCPOINTER, so they have to be
	 * initialized only once.
	 */
	//teco_view_set_representations(teco_qreg_view);

	/*
	 * Documents are UTF-8 by default and all UTF-8 documents
	 * are expected to have a character index.
	 *
	 * FIXME: This apparently gets reset with every SCI_SETDOCPOINTER.
	 * Does that mean the index needs to be recalculated repeatedly as well?
	 * What if the document/register is made non-UTF-8 afterwards?
	 */
	teco_view_ssm(teco_qreg_view, SCI_ALLOCATELINECHARACTERINDEX,
	              SC_LINECHARACTERINDEX_UTF32, 0);
}

/** @memberof teco_doc_t */
void
teco_doc_undo_edit(teco_doc_t *ctx)
{
	/*
	 * NOTE: see above in teco_doc_edit()
	 */
	//undo__teco_view_set_representations(teco_qreg_view);

	undo__teco_view_ssm(teco_qreg_view, SCI_SETSEL, ctx->anchor, (sptr_t)ctx->dot);
	undo__teco_view_ssm(teco_qreg_view, SCI_SETXOFFSET, ctx->xoffset, 0);
	undo__teco_view_ssm(teco_qreg_view, SCI_SETFIRSTVISIBLELINE, ctx->first_line, 0);
	undo__teco_view_ssm(teco_qreg_view, SCI_SETDOCPOINTER, 0,
	               (sptr_t)teco_doc_get_scintilla(ctx));
}

/** @memberof teco_doc_t */
void
teco_doc_set_string(teco_doc_t *ctx, const gchar *str, gsize len)
{
	if (teco_qreg_current)
		teco_doc_update(&teco_qreg_current->string, teco_qreg_view);

	teco_doc_reset(ctx);
	teco_doc_edit(ctx);

	teco_view_ssm(teco_qreg_view, SCI_BEGINUNDOACTION, 0, 0);
	teco_view_ssm(teco_qreg_view, SCI_CLEARALL, 0, 0);
	teco_view_ssm(teco_qreg_view, SCI_APPENDTEXT, len, (sptr_t)(str ? : ""));
	teco_view_ssm(teco_qreg_view, SCI_ENDUNDOACTION, 0, 0);

	if (teco_qreg_current)
		teco_doc_edit(&teco_qreg_current->string);
}

/** @memberof teco_doc_t */
void
teco_doc_undo_set_string(teco_doc_t *ctx)
{
	/*
	 * Necessary, so that upon rubout the
	 * string's parameters are restored.
	 */
	teco_doc_update(ctx, teco_qreg_view);

	if (teco_qreg_current && teco_qreg_current->must_undo) // FIXME
		teco_doc_undo_edit(&teco_qreg_current->string);

	teco_doc_undo_reset(ctx);
	undo__teco_view_ssm(teco_qreg_view, SCI_UNDO, 0, 0);

	teco_doc_undo_edit(ctx);
}

/**
 * Get a document as a string.
 *
 * @param ctx The document.
 * @param str Pointer to a variable to hold the return string.
 *            It can be NULL if you are interested only in the string's length.
 *            Strings must be freed via g_free().
 * @param len Where to store the string's length (mandatory).
 *
 * @see teco_qreg_vtable_t::get_string()
 * @memberof teco_doc_t
 */
void
teco_doc_get_string(teco_doc_t *ctx, gchar **str, gsize *len)
{
	if (!ctx->doc) {
		if (str)
			*str = NULL;
		*len = 0;
		return;
	}

	if (teco_qreg_current)
		teco_doc_update(&teco_qreg_current->string, teco_qreg_view);

	teco_doc_edit(ctx);

	*len = teco_view_ssm(teco_qreg_view, SCI_GETLENGTH, 0, 0);
	if (str) {
		*str = g_malloc(*len + 1);
		teco_view_ssm(teco_qreg_view, SCI_GETTEXT, *len + 1, (sptr_t)*str);
	}

	if (teco_qreg_current)
		teco_doc_edit(&teco_qreg_current->string);
}

/** @memberof teco_doc_t */
void
teco_doc_update_from_view(teco_doc_t *ctx, teco_view_t *from)
{
	ctx->anchor = teco_view_ssm(from, SCI_GETANCHOR, 0, 0);
	ctx->dot = teco_view_ssm(from, SCI_GETCURRENTPOS, 0, 0);
	ctx->first_line = teco_view_ssm(from, SCI_GETFIRSTVISIBLELINE, 0, 0);
	ctx->xoffset = teco_view_ssm(from, SCI_GETXOFFSET, 0, 0);
}

/** @memberof teco_doc_t */
void
teco_doc_update_from_doc(teco_doc_t *ctx, const teco_doc_t *from)
{
	ctx->anchor = from->anchor;
	ctx->dot = from->dot;
	ctx->first_line = from->first_line;
	ctx->xoffset = from->xoffset;
}

/**
 * Only for teco_qreg_stack_pop() which does some clever
 * exchanging of document data (without any deep copying)
 *
 * @memberof teco_doc_t
 */
void
teco_doc_exchange(teco_doc_t *ctx, teco_doc_t *other)
{
	teco_doc_t temp;
	memcpy(&temp, ctx, sizeof(temp));
	memcpy(ctx, other, sizeof(*ctx));
	memcpy(other, &temp, sizeof(*other));
}

/** @memberof teco_doc_t */
void
teco_doc_clear(teco_doc_t *ctx)
{
	if (ctx->doc)
		teco_view_ssm(teco_qreg_view, SCI_RELEASEDOCUMENT, 0, (sptr_t)ctx->doc);
}