Blender V4.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
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
22
23#include "bpy_rna.hh"
24#include "bpy_rna_text.hh" /* Declare #BPY_rna_region_as_string_method_def. */
25
26/* -------------------------------------------------------------------- */
33struct TextRegion {
34 int curl;
35 int curc;
36 int sell;
37 int selc;
38};
39
42/* -------------------------------------------------------------------- */
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#if (defined(__GNUC__) && !defined(__clang__))
99# pragma GCC diagnostic push
100# pragma GCC diagnostic ignored "-Wcast-function-type"
101#endif
102
104 "region_as_string",
105 (PyCFunction)bpy_rna_region_as_string,
106 METH_VARARGS | METH_KEYWORDS,
107 bpy_rna_region_as_string_doc,
108};
109
110#if (defined(__GNUC__) && !defined(__clang__))
111# pragma GCC diagnostic pop
112#endif
113
115 /* Wrap. */
116 bpy_rna_region_from_string_doc,
117 ".. method:: region_from_string(body, range=None)\n"
118 "\n"
119 " :arg body: The text to be inserted.\n"
120 " :type body: str\n"
121 " :arg range: The region of text to be returned, "
122 "defaulting to the selection when no range is passed.\n"
123 " Each int pair represents a line and column: "
124 "((start_line, start_column), (end_line, end_column))\n"
125 " The values match Python's slicing logic "
126 "(negative values count backwards from the end, the end value is not inclusive).\n"
127 " :type range: tuple[tuple[int, int], tuple[int, int]]\n");
128static PyObject *bpy_rna_region_from_string(PyObject *self, PyObject *args, PyObject *kwds)
129{
130 BPy_StructRNA *pyrna = (BPy_StructRNA *)self;
131 Text *text = static_cast<Text *>(pyrna->ptr.data);
132
133 /* Parse the region range. */
134 const char *buf;
135 Py_ssize_t buf_len;
136 TextRegion region;
137
138 static const char *_keywords[] = {"", "range", nullptr};
139 static _PyArg_Parser _parser = {
141 "s#" /* `buf` (positional). */
142 "|$" /* Optional keyword only arguments. */
143 "((ii)(ii))" /* `range` */
144 ":region_from_string",
145 _keywords,
146 nullptr,
147 };
148 if (!_PyArg_ParseTupleAndKeywordsFast(args,
149 kwds,
150 &_parser,
151 &buf,
152 &buf_len,
153 &region.curl,
154 &region.curc,
155 &region.sell,
156 &region.selc))
157 {
158 return nullptr;
159 }
160
161 if (kwds && PyDict_GET_SIZE(kwds) > 0) {
162 txt_sel_set(text, region.curl, region.curc, region.sell, region.selc);
163 }
164
165 /* Set the selected text. */
166 txt_insert_buf(text, buf, buf_len);
167 /* Update the text editor. */
169
170 Py_RETURN_NONE;
171}
172
173#if (defined(__GNUC__) && !defined(__clang__))
174# pragma GCC diagnostic push
175# pragma GCC diagnostic ignored "-Wcast-function-type"
176#endif
177
179 "region_from_string",
180 (PyCFunction)bpy_rna_region_from_string,
181 METH_VARARGS | METH_KEYWORDS,
182 bpy_rna_region_from_string_doc,
183};
184
185#if (defined(__GNUC__) && !defined(__clang__))
186# pragma GCC diagnostic pop
187#endif
188
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:1282
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:550
#define NC_TEXT
Definition WM_types.hh:353
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
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")
static PyObject * bpy_rna_region_as_string(PyObject *self, PyObject *args, PyObject *kwds)
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
header-only compatibility defines.
#define PY_ARG_PARSER_HEAD_COMPAT()
PyObject_HEAD PointerRNA ptr
Definition bpy_rna.hh:124
void * data
Definition RNA_types.hh:42
void WM_main_add_notifier(uint type, void *reference)