Blender  V3.3
bpy_rna_text.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
9 #define PY_SSIZE_T_CLEAN
10 
11 #include <Python.h>
12 
13 #include "DNA_text_types.h"
14 
15 #include "MEM_guardedalloc.h"
16 
17 #include "WM_api.h"
18 
19 #include "BKE_text.h"
20 
21 #include "bpy_capi_utils.h"
22 #include "bpy_rna.h"
23 #include "bpy_rna_text.h"
24 
25 /* -------------------------------------------------------------------- */
32 typedef struct TextRegion {
33  int curl;
34  int curc;
35  int sell;
36  int selc;
38 
41 /* -------------------------------------------------------------------- */
45 PyDoc_STRVAR(bpy_rna_region_as_string_doc,
46  ".. method:: region_as_string(range=None)\n"
47  "\n"
48  " :arg range: The region of text to be returned, "
49  "defaulting to the selection when no range is passed.\n"
50  " Each int pair represents a line and column: "
51  "((start_line, start_column), (end_line, end_column))\n"
52  " The values match Python's slicing logic "
53  "(negative values count backwards from the end, the end value is not inclusive).\n"
54  " :type range: Two pairs of ints\n"
55  " :return: The specified region as a string.\n"
56  " :rtype: str.\n");
57 /* Receive a Python Tuple as parameter to represent the region range. */
58 static PyObject *bpy_rna_region_as_string(PyObject *self, PyObject *args, PyObject *kwds)
59 {
60  BPy_StructRNA *pyrna = (BPy_StructRNA *)self;
61  Text *text = pyrna->ptr.data;
62  /* Parse the region range. */
63  TextRegion region;
64 
65  static const char *_keywords[] = {"range", NULL};
66  static _PyArg_Parser _parser = {
67  "|$" /* Optional keyword only arguments. */
68  "((ii)(ii))" /* `range` */
69  ":region_as_string",
70  _keywords,
71  0,
72  };
73  if (!_PyArg_ParseTupleAndKeywordsFast(
74  args, kwds, &_parser, &region.curl, &region.curc, &region.sell, &region.selc)) {
75  return NULL;
76  }
77 
78  if (PyDict_GET_SIZE(kwds) > 0) {
79  txt_sel_set(text, region.curl, region.curc, region.sell, region.selc);
80  }
81 
82  /* Return an empty string if there is no selection. */
83  if (!txt_has_sel(text)) {
84  return PyUnicode_FromString("");
85  }
86  char *buf = txt_sel_to_buf(text, NULL);
87  PyObject *sel_text = PyUnicode_FromString(buf);
88  MEM_freeN(buf);
89  /* Return the selected text. */
90  return sel_text;
91 }
92 
94  "region_as_string",
95  (PyCFunction)bpy_rna_region_as_string,
96  METH_VARARGS | METH_KEYWORDS,
97  bpy_rna_region_as_string_doc,
98 };
99 
100 PyDoc_STRVAR(bpy_rna_region_from_string_doc,
101  ".. method:: region_from_string(body, range=None)\n"
102  "\n"
103  " :arg body: The text to be inserted.\n"
104  " :type body: str\n"
105  " :arg range: The region of text to be returned, "
106  "defaulting to the selection when no range is passed.\n"
107  " Each int pair represents a line and column: "
108  "((start_line, start_column), (end_line, end_column))\n"
109  " The values match Python's slicing logic "
110  "(negative values count backwards from the end, the end value is not inclusive).\n"
111  " :type range: Two pairs of ints\n");
112 static PyObject *bpy_rna_region_from_string(PyObject *self, PyObject *args, PyObject *kwds)
113 {
114  BPy_StructRNA *pyrna = (BPy_StructRNA *)self;
115  Text *text = pyrna->ptr.data;
116 
117  /* Parse the region range. */
118  const char *buf;
119  Py_ssize_t buf_len;
120  TextRegion region;
121 
122  static const char *_keywords[] = {"", "range", NULL};
123  static _PyArg_Parser _parser = {
124  "s#" /* `buf` (positional). */
125  "|$" /* Optional keyword only arguments. */
126  "((ii)(ii))" /* `range` */
127  ":region_from_string",
128  _keywords,
129  0,
130  };
131  if (!_PyArg_ParseTupleAndKeywordsFast(args,
132  kwds,
133  &_parser,
134  &buf,
135  &buf_len,
136  &region.curl,
137  &region.curc,
138  &region.sell,
139  &region.selc)) {
140  return NULL;
141  }
142 
143  if (PyDict_GET_SIZE(kwds) > 0) {
144  txt_sel_set(text, region.curl, region.curc, region.sell, region.selc);
145  }
146 
147  /* Set the selected text. */
148  txt_insert_buf(text, buf, buf_len);
149  /* Update the text editor. */
151 
152  Py_RETURN_NONE;
153 }
154 
156  "region_from_string",
157  (PyCFunction)bpy_rna_region_from_string,
158  METH_VARARGS | METH_KEYWORDS,
159  bpy_rna_region_from_string_doc,
160 };
161 
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.c:1274
char * txt_sel_to_buf(struct Text *text, size_t *r_buf_strlen)
Definition: text.c:1454
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.h:523
#define NC_TEXT
Definition: WM_types.h:336
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: Two pairs of ints\n" " :return: The specified region as a string.\n" " :rtype: str.\n")
struct TextRegion TextRegion
PyMethodDef BPY_rna_region_from_string_method_def
Definition: bpy_rna_text.c:155
PyMethodDef BPY_rna_region_as_string_method_def
Definition: bpy_rna_text.c:93
static PyObject * bpy_rna_region_from_string(PyObject *self, PyObject *args, PyObject *kwds)
Definition: bpy_rna_text.c:112
static PyObject * bpy_rna_region_as_string(PyObject *self, PyObject *args, PyObject *kwds)
Definition: bpy_rna_text.c:58
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
PyObject_HEAD PointerRNA ptr
Definition: bpy_rna.h:111
void * data
Definition: RNA_types.h:38
void WM_main_add_notifier(unsigned int type, void *reference)