Skip to content

Instantly share code, notes, and snippets.

@trienow
Last active June 27, 2021 17:14
Show Gist options
  • Select an option

  • Save trienow/a6682a44dc1fda6c3bd1ae0a6e6d626b to your computer and use it in GitHub Desktop.

Select an option

Save trienow/a6682a44dc1fda6c3bd1ae0a6e6d626b to your computer and use it in GitHub Desktop.
DEPRECATED: A small C# class handling selections for the "Automation Scripts"-Plugin for Notepad++
public class NppSelection
{
readonly IntPtr sci;
public NppSelection(IntPtr currentScintilla) { sci = currentScintilla; }
public int Anchor
{
get { return (int)Win32.SendMessage(sci, SciMsg.SCI_GETANCHOR, 0, 0); }
set { Win32.SendMessage(sci, SciMsg.SCI_SETANCHOR, value, 0); }
}
public int Length { get { return (int)Win32.SendMessage(sci, SciMsg.SCI_GETSELTEXT, 0, 0); } }
public int Pos
{
get { return (int)Win32.SendMessage(sci, SciMsg.SCI_GETCURRENTPOS, 0, 0); }
set { Win32.SendMessage(sci, SciMsg.SCI_SETCURRENTPOS, value, 0); }
}
public string Text
{
get
{
int selLength = Length;
IntPtr ptr = Marshal.AllocHGlobal(selLength);
Win32.SendMessage(sci, SciMsg.SCI_GETSELTEXT, 0, ptr);
string selText = Marshal.PtrToStringAnsi(ptr);
Marshal.FreeHGlobal(ptr);
return selText;
}
set { Win32.SendMessage(sci, SciMsg.SCI_REPLACESEL, 0, value); }
}
public void SetSelection(int anchor, int caret) { Win32.SendMessage(sci, SciMsg.SCI_SETSEL, anchor, caret); }
}
@trienow
Copy link
Author

trienow commented Jun 27, 2021

NppScripts now comes with a handy interface.
Use Npp.Document.GetSelText() and Npp.Document.ReplaceSel(string) instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment