Blender V5.3
bpy_rna_text.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#define PY_SSIZE_T_CLEAN
12
13#include <Python.h>
14
15#include <optional>
16
17#include "MEM_guardedalloc.h"
18
19#include "WM_api.hh"
20
21#include "BKE_text.h"
22
23#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
24
25#include "bpy_rna.hh"
26#include "bpy_rna_text.hh" /* Declare #BPY_rna_region_as_string_method_def. */
27
28namespace blender {
29
30/* -------------------------------------------------------------------- */
33
37struct TextRegion {
38 int curl;
39 int curc;
40 int sell;
41 int selc;
42};
43
45
46/* -------------------------------------------------------------------- */
49
53static int py_parse_optional_region(PyObject *o, void *p)
54{
55 std::optional<TextRegion> *region_p = static_cast<std::optional<TextRegion> *>(p);
56 if (o == Py_None) {
57 region_p->reset();
58 return 1;
59 }
60 TextRegion region;
61 if (!PyArg_Parse(o,
62 "((ii)(ii))" /* `range` */
63 ":range",
64 &region.curl,
65 &region.curc,
66 &region.sell,
67 &region.selc))
68 {
69 return 0;
70 }
71 *region_p = region;
72 return 1;
73}
74
76 /* Wrap. */
77 bpy_rna_region_as_string_doc,
78 ".. method:: region_as_string(*, range=None)\n"
79 "\n"
80 " :param range: The region of text to be returned, "
81 "defaulting to the selection when no range is passed.\n"
82 " Each int pair represents a line and column: "
83 "((start_line, start_column), (end_line, end_column))\n"
84 " The values match Python's slicing logic "
85 "(negative values count backwards from the end, the end value is not inclusive).\n"
86 " :type range: tuple[tuple[int, int], tuple[int, int]] | None\n"
87 " :return: The specified region as a string.\n"
88 " :rtype: str\n");
89/* Receive a Python Tuple as parameter to represent the region range. */
90static PyObject *bpy_rna_region_as_string(PyObject *self, PyObject *args, PyObject *kwds)
91{
92 BPy_StructRNA *pyrna = reinterpret_cast<BPy_StructRNA *>(self);
93 Text *text = static_cast<Text *>(pyrna->ptr->data);
94 /* Parse the region range. */
95 std::optional<TextRegion> region;
96
97 static const char *_keywords[] = {"range", nullptr};
98 static _PyArg_Parser _parser = {
99 "|$" /* Optional, keyword only arguments. */
100 "O&" /* `range` */
101 ":region_as_string",
102 _keywords,
103 nullptr,
104 };
105 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwds, &_parser, py_parse_optional_region, &region)) {
106 return nullptr;
107 }
108
109 if (region) {
110 txt_sel_set(text, region->curl, region->curc, region->sell, region->selc);
111 }
112
113 /* Return an empty string if there is no selection. */
114 if (!txt_has_sel(text)) {
115 return PyUnicode_FromString("");
116 }
117 char *buf = txt_sel_to_buf(text, nullptr);
118 PyObject *sel_text = PyUnicode_FromString(buf);
119 MEM_delete(buf);
120 /* Return the selected text. */
121 return sel_text;
122}
123
124#ifdef __GNUC__
125# ifdef __clang__
126# pragma clang diagnostic push
127# pragma clang diagnostic ignored "-Wcast-function-type"
128# else
129# pragma GCC diagnostic push
130# pragma GCC diagnostic ignored "-Wcast-function-type"
131# endif
132#endif
133
135 "region_as_string",
136 reinterpret_cast<PyCFunction>(bpy_rna_region_as_string),
137 METH_VARARGS | METH_KEYWORDS,
138 bpy_rna_region_as_string_doc,
139};
140
141#ifdef __GNUC__
142# ifdef __clang__
143# pragma clang diagnostic pop
144# else
145# pragma GCC diagnostic pop
146# endif
147#endif
148
150 /* Wrap. */
151 bpy_rna_region_from_string_doc,
152 ".. method:: region_from_string(body, /, *, range=None)\n"
153 "\n"
154 " :param body: The text to be inserted.\n"
155 " :type body: str\n"
156 " :param range: The region of text to be returned, "
157 "defaulting to the selection when no range is passed.\n"
158 " Each int pair represents a line and column: "
159 "((start_line, start_column), (end_line, end_column))\n"
160 " The values match Python's slicing logic "
161 "(negative values count backwards from the end, the end value is not inclusive).\n"
162 " :type range: tuple[tuple[int, int], tuple[int, int]] | None\n");
163static PyObject *bpy_rna_region_from_string(PyObject *self, PyObject *args, PyObject *kwds)
164{
165 BPy_StructRNA *pyrna = reinterpret_cast<BPy_StructRNA *>(self);
166 Text *text = static_cast<Text *>(pyrna->ptr->data);
167
168 /* Parse the region range. */
169 const char *buf;
170 Py_ssize_t buf_len;
171 std::optional<TextRegion> region;
172
173 static const char *_keywords[] = {"", "range", nullptr};
174 static _PyArg_Parser _parser = {
175 "s#" /* `buf` (positional). */
176 "|$" /* Optional, keyword only arguments. */
177 "O&" /* `range` */
178 ":region_from_string",
179 _keywords,
180 nullptr,
181 };
182 if (!_PyArg_ParseTupleAndKeywordsFast(
183 args, kwds, &_parser, &buf, &buf_len, py_parse_optional_region, &region))
184 {
185 return nullptr;
186 }
187
188 if (region) {
189 txt_sel_set(text, region->curl, region->curc, region->sell, region->selc);
190 }
191
192 /* Set the selected text. */
193 txt_insert_buf(text, buf, buf_len);
194 /* Update the text editor. */
196
197 Py_RETURN_NONE;
198}
199
200#ifdef __GNUC__
201# ifdef __clang__
202# pragma clang diagnostic push
203# pragma clang diagnostic ignored "-Wcast-function-type"
204# else
205# pragma GCC diagnostic push
206# pragma GCC diagnostic ignored "-Wcast-function-type"
207# endif
208#endif
209
211 "region_from_string",
212 reinterpret_cast<PyCFunction>(bpy_rna_region_from_string),
213 METH_VARARGS | METH_KEYWORDS,
214 bpy_rna_region_from_string_doc,
215};
216
217#ifdef __GNUC__
218# ifdef __clang__
219# pragma clang diagnostic pop
220# else
221# pragma GCC diagnostic pop
222# endif
223#endif
224
226
227} // namespace blender
Read Blender memory allocation and freeing.
#define NA_EDITED
Definition WM_types.hh:634
#define NC_TEXT
Definition WM_types.hh:399
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
static PyObject * bpy_rna_region_from_string(PyObject *self, PyObject *args, PyObject *kwds)
PyMethodDef BPY_rna_region_from_string_method_def
void txt_insert_buf(struct Text *text, const char *in_buffer, int in_buffer_len) ATTR_NONNULL(1
Definition text.cc:1545
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
bool txt_has_sel(const struct Text *text)
void txt_sel_set(struct Text *text, int startl, int startc, int endl, int endc)
Definition text.cc:1287
static PyObject * bpy_rna_region_as_string(PyObject *self, PyObject *args, PyObject *kwds)
static int py_parse_optional_region(PyObject *o, void *p)
char * txt_sel_to_buf(const struct Text *text, size_t *r_buf_strlen)
PyMethodDef BPY_rna_region_as_string_method_def
header-only compatibility defines.
PyObject_HEAD std::optional< PointerRNA > ptr
Definition bpy_rna.hh:133