Skip to content

Commit cd26891

Browse files
committed
editor.getWord() and editor.getCurrentWord() added
- editor.getWord([position[, useOnlyWordChars]]) - editor.getCurrentWord()
1 parent d725887 commit cd26891

3 files changed

Lines changed: 48 additions & 0 deletions

File tree

PythonScript/src/ScintillaPython.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ BOOST_PYTHON_MODULE(Npp)
5757
.def("pymlsearch", &ScintillaWrapper::pymlsearchNoFlags, "Python multiline regular expression search, calling a function for each match found. The function gets called with the (zero indexed) line number, and the match object. \nNote that this runs the search on the entire text, and therefore makes at least 2 copies of the entire document, therefore it may not be suitable for large documents.\n pymlsearch(expression, function[, flags[, startPosition[, endPosition]]])")
5858
.def("pymlsearch", &ScintillaWrapper::pymlsearchNoStartEnd, "Python multiline regular expression search, calling a function for each match found. The function gets called with the (zero indexed) line number, and the match object. \nNote that this runs the search on the entire text, and therefore makes at least 2 copies of the entire document, therefore it may not be suitable for large documents.\n pymlsearch(expression, function[, flags[, startPosition[, endPosition]]])")
5959
.def("pymlsearch", &ScintillaWrapper::pymlsearchNoEnd, "Python multiline regular expression search, calling a function for each match found. The function gets called with the (zero indexed) line number, and the match object. \nNote that this runs the search on the entire text, and therefore makes at least 2 copies of the entire document, therefore it may not be suitable for large documents.\n pymlsearch(expression, function[, flags[, startPosition[, endPosition]]])")
60+
.def("getWord", &ScintillaWrapper::getWord, "getWord([position[, useOnlyWordChars]])\nGets the word at position. If position is not given or None, the current caret position is used.\nuseOnlyWordChars is a bool that is passed to Scintilla - see Scintilla rules on what is match. If not given or None, it is assumed to be true.")
61+
.def("getWord", &ScintillaWrapper::getWordNoFlags, "getWord([position[, useOnlyWordChars]])\nGets the word at position. If position is not given or None, the current caret position is used.\nuseOnlyWordChars is a bool that is passed to Scintilla - see Scintilla rules on what is match. If not given or None, it is assumed to be true.")
62+
.def("getWord", &ScintillaWrapper::getCurrentWord, "getWord([position[, useOnlyWordChars]])\nGets the word at position. If position is not given or None, the current caret position is used.\nuseOnlyWordChars is a bool that is passed to Scintilla - see Scintilla rules on what is match. If not given or None, it is assumed to be true.")
63+
.def("getCurrentWord", &ScintillaWrapper::getCurrentWord, "getCurrentWord()\nAlias for getWord(), that gets the current word at the cursor.")
6064
/* Between the autogenerated comments is, surprise, autogenerated
6165
* Do not edit the contents between these comments,
6266
* edit "CreateWrapper.py" instead, which does the generation

PythonScript/src/ScintillaWrapper.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,3 +953,39 @@ void ScintillaWrapper::pymlsearch(boost::python::object searchExp, boost::python
953953

954954

955955
}
956+
957+
958+
959+
str ScintillaWrapper::getWord(object position, object useOnlyWordChars /* = true */)
960+
{
961+
int pos;
962+
if (position.is_none())
963+
{
964+
pos = callScintilla(SCI_GETCURRENTPOS);
965+
}
966+
else
967+
{
968+
pos = extract<int>(position);
969+
}
970+
971+
bool wordChars;
972+
if (useOnlyWordChars.is_none())
973+
{
974+
wordChars = true;
975+
}
976+
else
977+
{
978+
wordChars = extract<bool>(useOnlyWordChars);
979+
}
980+
981+
int startPos = callScintilla(SCI_WORDSTARTPOSITION, pos, wordChars);
982+
int endPos = callScintilla(SCI_WORDENDPOSITION, pos, wordChars);
983+
Sci_TextRange tr;
984+
tr.chrg.cpMin = startPos;
985+
tr.chrg.cpMax = endPos;
986+
tr.lpstrText = new char[(endPos - startPos) + 1];
987+
callScintilla(SCI_GETTEXTRANGE, 0, reinterpret_cast<LPARAM>(&tr));
988+
str retVal(const_cast<const char *>(tr.lpstrText));
989+
delete[] tr.lpstrText;
990+
return retVal;
991+
}

PythonScript/src/ScintillaWrapper.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ class ScintillaWrapper : public NppPythonScript::PyProducerConsumer<CallbackExec
107107
{ pymlsearch(searchExp, callback, flags, object(), object()); };
108108
void pymlsearchNoEnd(boost::python::object searchExp, boost::python::object callback, boost::python::object flags, boost::python::object startPosition)
109109
{ pymlsearch(searchExp, callback, flags, startPosition, object()); };
110+
111+
boost::python::str ScintillaWrapper::getWord(boost::python::object position, boost::python::object useOnlyWordChars);
112+
boost::python::str ScintillaWrapper::getWordNoFlags(boost::python::object position)
113+
{ return getWord(position, boost::python::object(true)); };
114+
boost::python::str ScintillaWrapper::getCurrentWord()
115+
{ return getWord(object(), boost::python::object(true)); };
116+
117+
110118

111119
/* ++Autogenerated ---------------------------------------------------- */
112120
/** Add text to the document at current position.

0 commit comments

Comments
 (0)