Is there a SCI_SETLINE type message (How to update a line)
-
I’m developing a Notepad++ plugin that allows a user to select a set of lines and then invoke the plugin to manipulate the lines and then update in Notepad++.
I use the selection information to identify the lines to process. I iterate through the selected lines. Each line is read using SCI_GETLINE, modified, and then written back to Notepad++. The modification may cause the line length to increase but it will never decrease. What is the simplest way to write the line back to Notepad++? I was looking for a message like SCI_SETLINE which I did not find.
For another plugin, I have used the following code to update text that is selected but in this case I want to update a single line. I am new to Notepad++ plugin development. If the below way is the only/preferred way to update text, the how would I set the selectionStart and selectionEnd to correspond to specific line?
Code that I have used to modify selected text (but I need to modify by line, not selection):
::SendMessage(curScint, SCI_SETTARGETRANGE, selectionStart, selectionEnd); ::SendMessage(curScint, SCI_REPLACETARGET, strLen, reinterpret_cast<LPARAM>(selectedStr)); ::SendMessage(curScint, SCI_SETSEL, selectionStart, selectionEnd);
Sample Code:
// Get selected text start and end int selectionStart = (int)::SendMessage(curScint, SCI_GETSELECTIONSTART, 0, 0); int selectionEnd = (int)::SendMessage(curScint, SCI_GETSELECTIONEND, 0, 0); // identify the lines the selected text is on. int line1 = (int)::SendMessage(curScint,SCI_LINEFROMPOSITION, selectionStart,0); int line2 = (int)::SendMessage(curScint, SCI_LINEFROMPOSITION, selectionEnd, 0); // Loop through each line, get the line, modify the line and write the line back int length; for (int i = line1; i <= line2; i++) { ::SendMessage(curScint, SCI_GOTOLINE, line1, 0); length = (int)::SendMessage(curScint, SCI_GETCURLINE, 0, 0); char* selectedStr = new char[max(length + 1,200)]; ::SendMessage(curScint, SCI_GETCURLINE, length, reinterpret_cast<LPARAM>(selectedStr)); // Do something with selectedStr // Update Line in Notepad++ - How? }
I’m using Notepad++ version 8.6.
Thank you!
-
I believe what you want to do is:
- get the starting position of the line of interest
- get the ending position of the line of interest (in all likelihood you want the position before the line ending characters)
- delete the text between the two positions
- insert your new text at the starting position
In other words, there isn’t (I don’t believe) a function for directly doing it in one step.
But, you could make such a C++ function, and publish it here, and then future plugin developers could use it and thus benefit.
EDIT after reading @Coises 's response below: I didn’t mention “replace target” as an option, because apparently OP knows about that (it’s in his posting).
-
You can use SCI_POSITIONFROMLINE and SCI_GETLINEENDPOSITION to get the character positions that correspond to a given line number. (No need to use SCI_GOTOLINE.) Then use SCI_SETTARGETRANGE to set the target range, SCI_GETTARGETTEXT to retrieve the current contents, and SCI_REPLACETARGET to change it.
If you will never add line-ending characters, you can just iterate through the selected line numbers. If the replacements could include line ending characters, you will have to be careful.
-
The PythonScript plugin made a wrapper/helper function called
replaceLine()
: you can find the source code for that function here – it is essentially following the suggestions that @Coises made …And I had been thinking along @Alan-Kilborn’s lines, with doing the SCI_DELETERANGE then doing a SCI_INSERTTEXT at the old start-position.
edited phrasing once I saw the subtle difference between Alan’s and Coises’ suggestions
-
@PeterJones said in Is there a SCI_SETLINE type message (How to update a line):
following the suggestions that both @Alan-Kilborn and @Coises made nearly simultaneously.
Perhaps ALL THREE of us need to go AFK and get out and enjoy the weekend! :-)
-
@Alan-Kilborn said in Is there a SCI_SETLINE type message (How to update a line):
Perhaps ALL THREE of us need to go AFK and get out and enjoy the weekend! :-)
I already got in my walk this morning, between the downpours.
/me listens to the intense rainfall.
Right now would be a really bad time for going out. :-)
-
Thanks everyone for the input… I’m glad you’re not AFK. :) It’s a crappy day here in ATL so. I’ll give your suggestions a try.
Thank you!
-
Solved!
This is what I came up with. I’m not a c++ guy so please don’t ding me for the code. Thank you for your suggestions. That really helped.Use Case:
Provide capability that allows the user to select one or more lines to be reformatted per specs. The plugin will get the text for each line, manipulate it, and update in Scintilla.If you see any errors, please let me know.
void UpdateText() { ::MessageBox(nppData._nppHandle, TEXT("Comment Plugin"), TEXT("The Comment Plugin"), MB_OK); // Get handle to Scintilla editor int currentEdit; ::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)¤tEdit); HWND curScint = (currentEdit == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle; // Get selection start and end positions. // If selectionStr and selectionEnd are equal then there is no selection, // just process the line the caret is on. int strSel = (int)::SendMessage(curScint, SCI_GETSELECTIONSTART, 0, 0); int endSel = (int)::SendMessage(curScint, SCI_GETSELECTIONEND, 0, 0); // Get the line numbers associated with the start and end positions. // The strLine and endLine may be the same line. int strLine = (int)::SendMessage(curScint,SCI_LINEFROMPOSITION, strSel,0); int endLine = (int)::SendMessage(curScint, SCI_LINEFROMPOSITION, endSel, 0); size_t lineLen; int strPos; int endPos; char* selLine = 0; for (int lineNum = strLine; lineNum <= endLine; lineNum++) { // Calculate line size by getting the line start and end positions strPos = (int) ::SendMessage(curScint, SCI_POSITIONFROMLINE, lineNum, 0); endPos = (int) ::SendMessage(curScint, SCI_GETLINEENDPOSITION, lineNum, 0); // Get the current line. lineLen = endPos - strPos + 1; selLine = new char[lineLen]; ::SendMessage(curScint, SCI_SETTARGETRANGE, strPos, endPos); ::SendMessage(curScint, SCI_GETTARGETTEXT, 0, reinterpret_cast<LPARAM>(selLine)); // Move char* to string for easier (for me) string handling // First make sure line text is selLine[lineLen - 1] = '\0'; std::string lineText = selLine; // Do something. selLine = new char[lineText.length()]; strcpy(selLine, lineText.c_str()); ::SendMessage(curScint, SCI_REPLACETARGET, static_cast<WPARAM>(-1), reinterpret_cast<LPARAM>(selLine)); } return; }
-
@Paul-Baker said in Is there a SCI_SETLINE type message (How to update a line):
If you see any errors, please let me know.
You are leaking memory; you allocate new C-style strings, but you never delete them.
Recommendation: Save yourself some grief and use the fact that as of C++17 you can modify the data of a std::string. (You can even overwrite the trailing null that comes one after the length, so long as you overwrite it with another null.)
// Get the current line. lineLen = endPos - strPos; std::string lineText(lineLen, 0); ::SendMessage(curScint, SCI_SETTARGETRANGE, strPos, endPos); ::SendMessage(curScint, SCI_GETTARGETTEXT, 0, reinterpret_cast<LPARAM>(lineText.data())); // Do something. ::SendMessage(curScint, SCI_REPLACETARGET, static_cast<WPARAM>(-1), reinterpret_cast<LPARAM>(lineText.data()));
-
@Coises Thanks, I struggled with that. I’ll try your suggestions.
-
@Paul-Baker said in Is there a SCI_SETLINE type message (How to update a line):
I’m not a c++ guy
Maybe you should consider writing your plugin in a language you are comfortable in?
Caveat: Perhaps getting started without a template could be a hurdle…Or consider scripting instead of a plugin?
There’s no shame in scripting. :-) -
@Paul-Baker Updated without mem leaks
void updateCOBOLComments() { // Get handle to Scintilla editor int currentEdit; ::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)¤tEdit); HWND curScint = (currentEdit == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle; // Get selection start and end positions. // If selectionStr and selectionEnd are equal then there is no selection, // just process the line the caret is on. int strSel = (int)::SendMessage(curScint, SCI_GETSELECTIONSTART, 0, 0); int endSel = (int)::SendMessage(curScint, SCI_GETSELECTIONEND, 0, 0); // Get the line numbers associated with the start and end positions. // The strLine and endLine may be the same line. int strLine = (int)::SendMessage(curScint, SCI_LINEFROMPOSITION, strSel, 0); int endLine = (int)::SendMessage(curScint, SCI_LINEFROMPOSITION, endSel, 0); size_t lineLen; std::string lineText = ""; int strPos; int endPos; for (int lineNum = strLine; lineNum <= endLine; lineNum++) { // Calculate line size by getting the line start and end positions strPos = (int) ::SendMessage(curScint, SCI_POSITIONFROMLINE, lineNum, 0); endPos = (int) ::SendMessage(curScint, SCI_GETLINEENDPOSITION, lineNum, 0); // Get the current line. lineLen = endPos - strPos + 1; lineText.resize(lineLen, ' '); ::SendMessage(curScint, SCI_SETTARGETRANGE, strPos, endPos); ::SendMessage(curScint, SCI_GETTARGETTEXT, 0, reinterpret_cast<LPARAM>(lineText.data())); // ********************************* // ** Modify lineText as needed., * // ********************************* ::SendMessage(curScint, SCI_REPLACETARGET, static_cast<WPARAM>(-1), reinterpret_cast<LPARAM>(lineText.data())); } return; }
-
@Alan-Kilborn No shame… haha… I use to be a scripting basher (no pub intended). My comfortable language is java, then python. Imperative languages are all similar, if-then-else. There are lots of how to blogs for creating npp plugins using cpp and I did not want to make a project out of it. I’m sure there are easy ways to do this for many languages, I just took the path of least resistance. All comments appreciated. Thanks!
-
@Paul-Baker said in Is there a SCI_SETLINE type message (How to update a line):
My comfortable language is java
In that case I recommend this C# plugin template.
I’ve used it for two plugins and helped the development of another. C# is great, and very similar to Java.
-
@Paul-Baker said in Is there a SCI_SETLINE type message (How to update a line):
/* . . . */ int strSel = (int)::SendMessage(curScint, SCI_GETSELECTIONSTART, 0, 0); int endSel = (int)::SendMessage(curScint, SCI_GETSELECTIONEND, 0, 0);
Those type casts are only safe for 32-bit plugins. Scintilla has supported multi-gigabyte files since 4.1.6 (the current version is 5.4.1). While a character position beyond
0x7fffffff
isn’t likely, a 64-bit application would immediately crash if your plugin made one of the above calls in a document over 2 GB in size.It’s best to store all position values as the dynamically sized
Sci_Position
type (an alias forintptr_t
, which is what the API functions actually return).The type definition is probably in your project files already; all recent versions of the main Scintilla header include
Sci_Position.h
. -
@rdipardo Thank you!!! I’ll make that change.
-
@Mark-Olson said in Is there a SCI_SETLINE type message (How to update a line):
In that case I recommend this C# plugin template
Hi Mark, thank you and everyone that has helped me get started with Visual Studio and NPP Plugin development. I looked into c# and yes it is easier.
Here is the same logic I’ve been working on in c#. All works well.
static void toggleCobolComment() { // Get selection start and end positions. // If selStr and selEnd are equal then there is no selection, // just process the line the caret is on. var strSel = editor.GetSelectionStart(); var strEnd = editor.GetSelectionEnd(); // Get the line numbers associated with the start and end positions. // The strLine and endLine may be the same line. var strLine = editor.LineFromPosition(strSel); var endLine = editor.LineFromPosition(strEnd); string lineText = ""; char[] lineTextAsChars; int lineLen = 0; int strPos = 0; int endPos = 0; // Loop through each line and toggle the comment char "*" // in column 7. Skip lines that are shorter than 7 characters. for (var lineNum = strLine; lineNum <= endLine; lineNum++) { // Calculate line size by getting the line start and end positions strPos = editor.PositionFromLine(lineNum); endPos = editor.GetLineEndPosition(lineNum); // Get the current line length lineLen = endPos - strPos + 1; // Skip lines that are empty. if (lineLen <= 6) continue; // Get line text and convert to char array editor.SetTargetRange(strPos, endPos); lineText = editor.GetTargetText(); lineTextAsChars = lineText.ToCharArray(); // Toggle comment character. if (lineTextAsChars[6] == '*') lineTextAsChars[6] = ' '; else lineTextAsChars[6] = '*'; // Convert char array back to string // and update line in editor. lineText = new string(lineTextAsChars); editor.ReplaceTarget(lineText.Length, lineText); } return; }
Thanks again… This thread is closed (for me).
-
@Paul-Baker
I’m glad it’s working!One note I would make (not just for you, for the general public) is that
interacting with Scintilla is really slow compared to the .NET runtime and if you are concerned about performance, you will be much faster if you just slurp all the text out in one go, process it in C#, and then dump it all back into the file. -
@Mark-Olson said in Is there a SCI_SETLINE type message (How to update a line):
much faster if you just slurp all the text out in one go, process it in C#, and then dump it all back into the file
…and watch your change-history go “all orange”, registering every character in the file as “changed”. Secondary concern: It also nullifies a plugin’s opportunity to provide step-by-step undo for possible individual modifications it makes.
For me this would be a big problem, and I wouldn’t use a plugin that did this to me.
Sure, for some people this would be absolutely no problem.
But I wouldn’t make this approach a general recommendation.