Skip to content
This repository was archived by the owner on Jan 13, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 95 additions & 12 deletions Demo Plugin/NppManagedPluginDemo/Demo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// NPP plugin platform for .Net v0.91.57 by Kasper B. Graversen etc.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Drawing;
Expand Down Expand Up @@ -36,10 +37,33 @@ static internal void SetToolBarIcon()

public static void OnNotification(ScNotification notification)
{
uint code = notification.Header.Code;
// This method is invoked whenever something is happening in notepad++
// use eg. as
// if (code == (uint)NppMsg.NPPN_xxx)
// { ... }
// or
//
// if (code == (uint)SciMsg.SCNxxx)
// { ... }
// when closing a file
if (code == (uint)NppMsg.NPPN_FILEBEFORECLOSE)
{
Kbg.Demo.Namespace.Main.AddToFilesClosed(notification.Header.IdFrom);
return;
}

if (notification.Header.Code == (uint)SciMsg.SCN_CHARADDED)
{
Kbg.Demo.Namespace.Main.doInsertHtmlCloseTag((char)notification.Character);
return;
}

//if (code > int.MaxValue) // windows messages
//{
// int wm = -(int)code;
// }
//}
}

internal static string PluginName { get { return Kbg.Demo.Namespace.Main.PluginName; }}
Expand All @@ -59,6 +83,7 @@ class Main
static string sessionFilePath = @"C:\text.session";
static frmGoToLine frmGoToLine = null;
static internal int idFrmGotToLine = -1;
static internal List<string> filesClosedThisSession = new List<string>();

// toolbar icons
static Bitmap tbBmp = Properties.Resources.star;
Expand Down Expand Up @@ -136,6 +161,8 @@ static internal void CommandMenuInit()
PluginBase.SetCommand(16, "---", null);

PluginBase.SetCommand(17, "Print Scroll and Row Information", PrintScrollInformation);
PluginBase.SetCommand(18, "Use NanInf class for -inf, inf, nan!!", PrintNanInf);
PluginBase.SetCommand(19, "Show files closed this session", FilesClosedThisSession);
}

/// <summary>
Expand Down Expand Up @@ -222,6 +249,7 @@ static void callbackHelloFX()

static void WhatIsNpp()
{
// from https://notepad-plus-plus.org/
string text2display = "Notepad++ is a free (as in \"free speech\" and also as in \"free beer\") " +
"source code editor and Notepad replacement that supports several languages.\n" +
"Running in the MS Windows environment, its use is governed by GPL License.\n\n" +
Expand All @@ -233,34 +261,53 @@ static void WhatIsNpp()
new Thread(new ParameterizedThreadStart(callbackWhatIsNpp)).Start(text2display);
}

/// <summary>
/// Open up a new file and slowly printing out the "What is NPP" text above.<br></br>
/// Stops printing text if the new file is closed.
/// </summary>
/// <param name="data"></param>
static void callbackWhatIsNpp(object data)
{
string text2display = (string)data;
notepad.FileNew();
string new_file_name = getCurrentPath(NppMsg.FULL_CURRENT_PATH);

Random srand = new Random(DateTime.Now.Millisecond);
int rangeMin = 0;
int rangeMax = 250;
int rangeMax = 125;
for (int i = 0; i < text2display.Length; i++)
{
Thread.Sleep(srand.Next(rangeMin, rangeMax) + 30);
// stop adding new text if the user closes or switches out of the new file.
// otherwise you get this obnoxious addition of text to existing files.
string selected_file_name = getCurrentPath(NppMsg.FULL_CURRENT_PATH);
if (selected_file_name != new_file_name) break;
editor.AppendTextAndMoveCursor(text2display[i].ToString());
}
}

static void insertCurrentFullPath()
{
insertCurrentPath(NppMsg.FULL_CURRENT_PATH);
editor.ReplaceSel(getCurrentPath(NppMsg.FULL_CURRENT_PATH));
}
static void insertCurrentFileName()
{
insertCurrentPath(NppMsg.FILE_NAME);
editor.ReplaceSel(getCurrentPath(NppMsg.FILE_NAME));
}
static void insertCurrentDirectory()
{
insertCurrentPath(NppMsg.CURRENT_DIRECTORY);
editor.ReplaceSel(getCurrentPath(NppMsg.CURRENT_DIRECTORY));
}
static void insertCurrentPath(NppMsg which)

/// <summary>
/// Returns a property of the currently open file, depending on argument:<br></br>
/// * If NppMsg.NPPM_GETFULLCURRENTPATH -> return absolute path to current file<br></br>
/// * If NppMsg.NPPM_GETFILENAME -> return file name of current file<br></br>
/// * If NppMsg.NPPM_GETCURRENTDIRECTORY -> return directory of current file<br></br>
/// </summary>
/// <param name="which"></param>
/// <returns></returns>
static string getCurrentPath(NppMsg which)
{
NppMsg msg = NppMsg.NPPM_GETFULLCURRENTPATH;
if (which == NppMsg.FILE_NAME)
Expand All @@ -269,9 +316,9 @@ static void insertCurrentPath(NppMsg which)
msg = NppMsg.NPPM_GETCURRENTDIRECTORY;

StringBuilder path = new StringBuilder(Win32.MAX_PATH);
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) msg, 0, path);
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)msg, 0, path);

editor.ReplaceSel(path.ToString());
return path.ToString();
}

static void insertShortDateTime()
Expand All @@ -293,7 +340,7 @@ static void checkInsertHtmlCloseTag()
PluginBase.CheckMenuItemToggle(9, ref doCloseTag); // 9 = menu item index
}

static Regex regex = new Regex(@"[\._\-:\w]", RegexOptions.Compiled);
static Regex XmlTagNameRegex = new Regex(@"[\._\-:\w]", RegexOptions.Compiled);

static internal void doInsertHtmlCloseTag(char newChar)
{
Expand Down Expand Up @@ -335,7 +382,7 @@ static internal void doInsertHtmlCloseTag(char newChar)

var insertString = new StringBuilder("</");

while (regex.IsMatch(buf[pCur].ToString()))
while (XmlTagNameRegex.IsMatch(buf[pCur].ToString()))
{
insertString.Append(buf[pCur]);
pCur++;
Expand All @@ -360,8 +407,12 @@ static void getFileNamesDemo()

using (ClikeStringArray cStrArray = new ClikeStringArray(nbFile, Win32.MAX_PATH))
{
// try to see if
if (Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_GETOPENFILENAMES, cStrArray.NativePointer, nbFile) != IntPtr.Zero)
foreach (string file in cStrArray.ManagedStringsUnicode) MessageBox.Show(file);
foreach (string file in cStrArray.ManagedStringsUnicode)
{
MessageBox.Show(file);
}
}
}
static void getSessionFileNamesDemo()
Expand All @@ -388,6 +439,37 @@ static void saveCurrentSessionDemo()
MessageBox.Show(sessionPath, "Saved Session File :", MessageBoxButtons.OK);
}

public static void AddToFilesClosed(IntPtr buffer_closed_id)
{
string buffer_closed = notepad.GetFilePath(buffer_closed_id);
filesClosedThisSession.Add(buffer_closed);
}

static void FilesClosedThisSession()
{
notepad.FileNew();
StringBuilder sb = new StringBuilder();
sb.AppendLine("Files closed this session:");
foreach (string file_closed in filesClosedThisSession)
sb.AppendLine(file_closed);
editor.AppendTextAndMoveCursor(sb.ToString());
}

static void PrintNanInf()
{
string naninf = $@"-infinity = NanInf.neginf = {NanInf.neginf}
infinity = NanInf.inf = {NanInf.inf}
NaN = NanInf.nan = {NanInf.nan}

If you want these constants in your plugin, you can find them in the NanInf class in PluginInfrastructure.

DO NOT USE double.PositiveInfinity, double.NegativeInfinity, or double.NaN.
You will get a compiler error if you do. ";
MessageBox.Show(naninf, "Use the NanInf class for NaN, -inf, inf!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
notepad.FileNew();
editor.AppendTextAndMoveCursor(naninf);
}

static void DockableDlgDemo()
{
// Dockable Dialog Demo
Expand All @@ -397,7 +479,8 @@ static void DockableDlgDemo()
if (frmGoToLine == null)
{
frmGoToLine = new frmGoToLine(editor);


// not sure what this block does but it's necessary
using (Bitmap newBmp = new Bitmap(16, 16))
{
Graphics g = Graphics.FromImage(newBmp);
Expand All @@ -410,7 +493,7 @@ static void DockableDlgDemo()
g.DrawImage(tbBmp_tbTab, new Rectangle(0, 0, 16, 16), 0, 0, 16, 16, GraphicsUnit.Pixel, attr);
tbIcon = Icon.FromHandle(newBmp.GetHicon());
}

NppTbData _nppTbData = new NppTbData();
_nppTbData.hClient = frmGoToLine.Handle;
_nppTbData.pszName = "Go To Line #";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
<DependentUpon>frmGoToLine.cs</DependentUpon>
</Compile>
<Compile Include="Demo.cs" />
<Compile Include="PluginInfrastructure\NanInf.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<DependentUpon>Resources.resx</DependentUpon>
Expand Down
30 changes: 30 additions & 0 deletions Demo Plugin/NppManagedPluginDemo/PluginInfrastructure/NanInf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;

namespace Kbg.NppPluginNET.PluginInfrastructure
{
/// <summary>
/// holds NaN, Infinity, and -Infinity as things generated at runtime<br></br>
/// because the compiler freaks out if it sees any of<br></br>
/// double.NegativeInfinity, double.PositiveInfinity, or double.NaN<br></br>
/// or any statically analyzed function of two constants that makes one of those constants<br></br>
/// like 1d/0d, 0d/0d, or -1d/0d.
/// </summary>
public class NanInf
{
/// <summary>
/// a/b<br></br>
/// may be necessary to generate infinity or nan at runtime
/// to avoid the compiler pre-computing things<br></br>
/// since if the compiler sees literal 1d/0d in the code
/// it just pre-computes it at compile time
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static double Divide(double a, double b) { return a / b; }

public static readonly double inf = Divide(1d, 0d);
public static readonly double neginf = Divide(-1d, 0d);
public static readonly double nan = Divide(0d, 0d);
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ This is a fork of UFO's plugin package updated for VS2015, 2017 and 2019
* https://github.com/wakatime/notepadpp-wakatime
* https://github.com/alex-ilin/WebEdit
* https://github.com/Fruchtzwerg94/PlantUmlViewer
* https://github.com/molsonkiko/JsonToolsNppPlugin

If your plugin is not on the list, please make a PR with a link to it.. :-)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2516,7 +2516,10 @@ public interface IScintillaGateway
/// <summary>Set up the key words used by the lexer. (Scintilla feature 4005)</summary>
unsafe void SetKeyWords(int keyWordSet, string keyWords);

/// <summary>Set the lexing language of the document based on string name. (Scintilla feature 4006)</summary>
/// <summary>Set the lexing language of the document based on string name. (Scintilla feature 4006)<br></br>
/// WARNING! This does not appear to do anything.<br></br>
/// Use INotepadPPGateway.SetCurrentLanguage instead.
/// </summary>
unsafe void SetLexerLanguage(string language);

/// <summary>Load a lexer library (dll / so). (Scintilla feature 4007)</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface INotepadPPGateway
string GetNppPath();
string GetPluginConfigPath();
string GetCurrentFilePath();
unsafe string GetFilePath(int bufferId);
unsafe string GetFilePath(IntPtr bufferId); // use IntPtr to work with 32-bit and 64-bit Notepad++
void SetCurrentLanguage(LangType language);
bool OpenFile(string path);
}
Expand Down Expand Up @@ -108,7 +108,7 @@ public bool OpenFile(string path)
/// <summary>
/// Gets the path of the current document.
/// </summary>
public unsafe string GetFilePath(int bufferId)
public unsafe string GetFilePath(IntPtr bufferId)
{
var path = new StringBuilder(2000);
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_GETFULLPATHFROMBUFFERID, bufferId, path);
Expand Down