Blender V5.0
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 "MEM_guardedalloc.h"
16
17#include "WM_api.hh"
18
19#include "BKE_text.h"
20
21#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
22
23#include "bpy_rna.hh"
24#include "bpy_rna_text.hh" /* Declare #BPY_rna_region_as_string_method_def. */
25
26/* -------------------------------------------------------------------- */
29
33struct TextRegion {
34 int curl;
35 int curc;
36 int sell;
37 int selc;
38};
39
41
42/* -------------------------------------------------------------------- */
45
47 /* Wrap. */
48 bpy_rna_region_as_string_doc,
49 ".. method:: region_as_string(*, range=None)\n"
50 "\n"
51 " :arg range: The region of text to be returned, "
52 "defaulting to the selection when no range is passed.\n"
53 " Each int pair represents a line and column: "
54 "((start_line, start_column), (end_line, end_column))\n"
55 " The values match Python's slicing logic "
56 "(negative values count backwards from the end, the end value is not inclusive).\n"
57 " :type range: tuple[tuple[int, int], tuple[int, int]]\n"
58 " :return: The specified region as a string.\n"
59 " :rtype: str\n");
60/* Receive a Python Tuple as parameter to represent the region range. */
61static PyObject *bpy_rna_region_as_string(PyObject *self, PyObject *args, PyObject *kwds)
62{
64 Text *text = static_cast<Text *>(pyrna->ptr->data);
65 /* Parse the region range. */
66 TextRegion region;
67
68 static const char *_keywords[] = {"range", nullptr};
69 static _PyArg_Parser _parser = {
71 "|$" /* Optional keyword only arguments. */
72 "((ii)(ii))" /* `range` */
73 ":region_as_string",
74 _keywords,
75 nullptr,
76 };
77 if (!_PyArg_ParseTupleAndKeywordsFast(
78 args, kwds, &_parser, &region.curl, &region.curc, &region.sell, &region.selc))
79 {
80 return nullptr;
81 }
82
83 if (kwds && PyDict_GET_SIZE(kwds) > 0) {
84 txt_sel_set(text, region.curl, region.curc, region.sell, region.selc);
85 }
86
87 /* Return an empty string if there is no selection. */
88 if (!txt_has_sel(text)) {
89 return PyUnicode_FromString("");
90 }
91 char *buf = txt_sel_to_buf(text, nullptr);
92 PyObject *sel_text = PyUnicode_FromString(buf);
93 MEM_freeN(buf);
94 /* Return the selected text. */
95 return sel_text;
96}
97
98#ifdef __GNUC__
99# ifdef __clang__
100# pragma clang diagnostic push
101# pragma clang diagnostic ignored "-Wcast-function-type"
102# else
103# pragma GCC diagnostic push
104# pragma GCC diagnostic ignored "-Wcast-function-type"
105# endif
106#endif
107
109 "region_as_string",
110 (PyCFunction)bpy_rna_region_as_string,
111 METH_VARARGS | METH_KEYWORDS,
112 bpy_rna_region_as_string_doc,
113};
114
115#ifdef __GNUC__
116# ifdef __clang__
117# pragma clang diagnostic pop
118# else
119# pragma GCC diagnostic pop
120# endif
121#endif
122
124 /* Wrap. */
125 bpy_rna_region_from_string_doc,
126 ".. method:: region_from_string(body, /, *, range=None)\n"
127 "\n"
128 " :arg body: The text to be inserted.\n"
129 " :type body: str\n"
130 " :arg range: The region of text to be returned, "
131 "defaulting to the selection when no range is passed.\n"
132 " Each int pair represents a line and column: "
133 "((start_line, start_column), (end_line, end_column))\n"
134 " The values match Python's slicing logic "
135 "(negative values count backwards from the end, the end value is not inclusive).\n"
136 " :type range: tuple[tuple[int, int], tuple[int, int]]\n");
137static PyObject *bpy_rna_region_from_string(PyObject *self, PyObject *args, PyObject *kwds)
138{
139 BPy_StructRNA *pyrna = (BPy_StructRNA *)self;
140 Text *text = static_cast<Text *>(pyrna->ptr->data);
141
142 /* Parse the region range. */
143 const char *buf;
144 Py_ssize_t buf_len;
145 TextRegion region;
146
147 static const char *_keywords[] = {"", "range", nullptr};
148 static _PyArg_Parser _parser = {
150 "s#" /* `buf` (positional). */
151 "|$" /* Optional keyword only arguments. */
152 "((ii)(ii))" /* `range` */
153 ":region_from_string",
154 _keywords,
155 nullptr,
156 };
157 if (!_PyArg_ParseTupleAndKeywordsFast(args,
158 kwds,
159 &_parser,
160 &buf,
161 &buf_len,
162 &region.curl,
163 &region.curc,
164 &region.sell,
165 &region.selc))
166 {
167 return nullptr;
168 }
169
170 if (kwds && PyDict_GET_SIZE(kwds) > 0) {
171 txt_sel_set(text, region.curl, region.curc, region.sell, region.selc);
172 }
173
174 /* Set the selected text. */
175 txt_insert_buf(text, buf, buf_len);
176 /* Update the text editor. */
178
179 Py_RETURN_NONE;
180}
181
182#ifdef __GNUC__
183# ifdef __clang__
184# pragma clang diagnostic push
185# pragma clang diagnostic ignored "-Wcast-function-type"
186# else
187# pragma GCC diagnostic push
188# pragma GCC diagnostic ignored "-Wcast-function-type"
189# endif
190#endif
191
193 "region_from_string",
194 (PyCFunction)bpy_rna_region_from_string,
195 METH_VARARGS | METH_KEYWORDS,
196 bpy_rna_region_from_string_doc,
197};
198
199#ifdef __GNUC__
200# ifdef __clang__
201# pragma clang diagnostic pop
202# else
203# pragma GCC diagnostic pop
204# endif
205#endif
206
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:1283
char * txt_sel_to_buf(const struct Text *text, size_t *r_buf_strlen)
void txt_insert_buf(struct Text *text, const char *in_buffer, int in_buffer_len) ATTR_NONNULL(1
Read Guarded memory(de)allocation.
#define NA_EDITED
Definition WM_types.hh:584
#define NC_TEXT
Definition WM_types.hh:386
PyObject * self
static PyObject * bpy_rna_region_from_string(PyObject *self, PyObject *args, PyObject *kwds)
PyMethodDef BPY_rna_region_from_string_method_def
PyMethodDef BPY_rna_region_as_string_method_def
static PyObject * bpy_rna_region_as_string(PyObject *self, PyObject *args, PyObject *kwds)
PyDoc_STRVAR(bpy_rna_region_as_string_doc, ".. method:: region_as_string(*, range=None)\n" "\n" " :arg range: The region of text to be returned, " "defaulting to the selection when no range is passed.\n" " Each int pair represents a line and column: " "((start_line, start_column), (end_line, end_column))\n" " The values match Python's slicing logic " "(negative values count backwards from the end, the end value is not inclusive).\n" " :type range: tuple[tuple[int, int], tuple[int, int]]\n" " :return: The specified region as a string.\n" " :rtype: str\n")
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
header-only compatibility defines.
#define PY_ARG_PARSER_HEAD_COMPAT()
PyObject_HEAD std::optional< PointerRNA > ptr
Definition bpy_rna.hh:130
void WM_main_add_notifier(uint type, void *reference)