aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/doc.h
blob: 91663d4223d4996e17d7f6a8bfaf51a1c6278bf6 (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
/*
 * Copyright (C) 2012-2023 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/>.
 */
#pragma once

#include <string.h>

#include <glib.h>

#include <Scintilla.h>

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

/**
 * Scintilla document type.
 * The struct is never defined and only exists for improved
 * type safety.
 */
typedef struct teco_doc_scintilla_t teco_doc_scintilla_t;

/**
 * A Scintilla document.
 *
 * Also contains other attributes required to restore
 * the overall editor state when loading it into a Scintilla view.
 */
typedef struct {
	/**
	 * Underlying Scintilla document.
	 * It is created on demand in teco_doc_maybe_create_document(),
	 * so that we don't waste memory on integer-only Q-Registers.
	 */
	teco_doc_scintilla_t *doc;

	/*
	 * The so called "parameters".
	 * Updated/restored only when required
	 */
	gint anchor, dot;
	gint first_line, xoffset;
} teco_doc_t;

/** @memberof teco_doc_t */
static inline void
teco_doc_init(teco_doc_t *ctx)
{
	memset(ctx, 0, sizeof(*ctx));
}

void teco_doc_edit(teco_doc_t *ctx);
void teco_doc_undo_edit(teco_doc_t *ctx);

void teco_doc_set_string(teco_doc_t *ctx, const gchar *str, gsize len);
void teco_doc_undo_set_string(teco_doc_t *ctx);

void teco_doc_get_string(teco_doc_t *ctx, gchar **str, gsize *len);

void teco_doc_update_from_view(teco_doc_t *ctx, teco_view_t *from);
void teco_doc_update_from_doc(teco_doc_t *ctx, const teco_doc_t *from);

/** @memberof teco_doc_t */
#define teco_doc_update(CTX, FROM) \
	(_Generic((FROM), teco_view_t *      : teco_doc_update_from_view, \
	                  teco_doc_t *       : teco_doc_update_from_doc, \
	                  const teco_doc_t * : teco_doc_update_from_doc)((CTX), (FROM)))

/** @memberof teco_doc_t */
static inline void
teco_doc_reset(teco_doc_t *ctx)
{
	ctx->anchor = ctx->dot = 0;
	ctx->first_line = ctx->xoffset = 0;
}

/** @memberof teco_doc_t */
static inline void
teco_doc_undo_reset(teco_doc_t *ctx)
{
	/*
	 * NOTE: Could be rolled into one function
	 * and called with teco_undo_call() if we really
	 * wanted to save more memory.
	 */
	teco_undo_gint(ctx->anchor);
	teco_undo_gint(ctx->dot);
	teco_undo_gint(ctx->first_line);
	teco_undo_gint(ctx->xoffset);
}

void teco_doc_exchange(teco_doc_t *ctx, teco_doc_t *other);

/** @memberof teco_doc_t */
static inline void
teco_doc_undo_exchange(teco_doc_t *ctx)
{
	teco_undo_ptr(ctx->doc);
	teco_doc_undo_reset(ctx);
}

void teco_doc_clear(teco_doc_t *ctx);