Skip to content

Commit

Permalink
add SCN_MODIFIED, NPPN_GLOBALMODIFIED callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
molsonkiko committed Feb 25, 2024
1 parent 841c8b4 commit 470b8f8
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1. Ported over (from kbilsted's old template) the [ToolsForMaintainersOfTheProjectTemplate](/ToolsForMaintainersOfTheProjectTemplate/) folder for updating some of the [PluginInfrastructure](/NppCSharpPluginPack/PluginInfrastructure/) files to stay up to date with Notepad++.
2. Added new [PopupDialog form](/docs/README.md#popup-dialog), which demonstrates how to configure a pop-up dialog that has select-able fields like textboxes or buttons.
3. Added new `Allocate indicators demo`, demonstrating how to allocate and use indicators to style text.
4. Demo on the `SCN_MODIFIED` and `NPPN_GLOBALMODIFIED` notifications, which are used to track when a document is modified.

### Changed

Expand Down
29 changes: 23 additions & 6 deletions NppCSharpPluginPack/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ class Main
// general stuff things
static Icon dockingFormIcon = null;
private static readonly string sessionFilePath = Path.Combine(PluginConfigDirectory, "savedNppSession.xml");
private static List<(string filepath, DateTime time, bool opened)> filesOpenedClosed = new List<(string filepath, DateTime time, bool opened)>();
private static List<(string filepath, DateTime time, bool opened, int modsSinceOpen)> filesOpenedClosed = new List<(string filepath, DateTime time, bool opened, int modsSinceOpen)>();
public static Settings settings = new Settings();
public static bool bufferFinishedOpening;
public static int modsSinceBufferOpened = 0;
public static string activeFname = null;
public static bool isDocTypeHTML = false;
// indicator things
Expand Down Expand Up @@ -161,7 +162,8 @@ public static void OnNotification(ScNotification notification)
// track when it was opened
IntPtr bufferOpenedId = notification.Header.IdFrom;
activeFname = Npp.notepad.GetFilePath(bufferOpenedId);
filesOpenedClosed.Add((activeFname, DateTime.Now, true));
filesOpenedClosed.Add((activeFname, DateTime.Now, true, 0));
modsSinceBufferOpened = 0;
return;
// when the lexer language changed, re-check whether this is a document where we close HTML tags.
case (uint)NppMsg.NPPN_LANGCHANGED:
Expand All @@ -171,7 +173,7 @@ public static void OnNotification(ScNotification notification)
case (uint)NppMsg.NPPN_FILEBEFORECLOSE:
IntPtr bufferClosedId = notification.Header.IdFrom;
string bufferClosedPath = Npp.notepad.GetFilePath(bufferClosedId);
filesOpenedClosed.Add((bufferClosedPath, DateTime.Now, false));
filesOpenedClosed.Add((bufferClosedPath, DateTime.Now, false, modsSinceBufferOpened));
return;
// the editor color scheme changed, so update form colors
case (uint)NppMsg.NPPN_WORDSTYLESUPDATED:
Expand All @@ -180,6 +182,21 @@ public static void OnNotification(ScNotification notification)
case (uint)SciMsg.SCN_CHARADDED:
DoInsertHtmlCloseTag(notification.Character);
break;
case (uint)SciMsg.SCN_MODIFIED:
modsSinceBufferOpened++;
break;
// this fires when the "Replace all" and "Replace in all open documents" actions of the Notepad++ find/replace form are used
// You may want to use this because beginning in Notepad++ 8.6.3,
// some kinds of SCN_MODIFIED messages are no longer sent during those actions
// (because sending messages can have a significant performance cost)
case (uint)NppMsg.NPPN_GLOBALMODIFIED:
// only increment modsSinceBufferOpened if it was a find/replace for the active file
// (this message fires once for each buffer modified in a "Replace in all open documents" action)
IntPtr bufferModifiedId = notification.Header.hwndFrom;
string bufferModified = Npp.notepad.GetFilePath(bufferModifiedId);
if (bufferModified == activeFname)
modsSinceBufferOpened++;
break;
//if (code > int.MaxValue) // windows messages
//{
// int wm = -(int)code;
Expand Down Expand Up @@ -458,12 +475,12 @@ private static void ShowFilesOpenedAndClosedThisSession()
{
Npp.notepad.FileNew();
var sb = new StringBuilder();
sb.Append("Action\tFilename\tTime\r\n");
foreach ((string filename, DateTime time, bool wasOpened) in filesOpenedClosed)
sb.Append("Action\tFilename\tTime\tModifications since buffer opened\r\n");
foreach ((string filename, DateTime time, bool wasOpened, int modsSinceBufferOpened) in filesOpenedClosed)
{
string formattedTime = time.ToString("yyyy-MM-dd HH:mm:ss");
string openClose = wasOpened ? "open" : "close";
sb.Append($"{filename}\t{formattedTime}\t{openClose}\r\n");
sb.Append($"{filename}\t{formattedTime}\t{openClose}\t{modsSinceBufferOpened}\r\n");
}
Npp.editor.SetText(sb.ToString());
}
Expand Down
35 changes: 35 additions & 0 deletions NppCSharpPluginPack/PluginInfrastructure/Msgs_h.cs
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,41 @@ public enum NppMsg : uint
/// </summary>
NPPN_FILEDELETED = NPPN_FIRST + 26,

/// <summary>
/// To notify plugins that Dark Mode was enabled/disabled
/// scnNotification->nmhdr.code = NPPN_DARKMODECHANGED;
/// scnNotification->nmhdr.hwndFrom = hwndNpp;
/// scnNotification->nmhdr.idFrom = 0;
/// </summary>
NPPN_DARKMODECHANGED = NPPN_FIRST + 27,

/// <summary>
/// To notify plugins that the new argument for plugins (via '-pluginMessage="YOUR_PLUGIN_ARGUMENT"' in command line) is available
/// scnNotification->nmhdr.code = NPPN_CMDLINEPLUGINMSG;
/// scnNotification->nmhdr.hwndFrom = hwndNpp;
/// scnNotification->nmhdr.idFrom = pluginMessage; //where pluginMessage is pointer of type wchar_t
/// </summary>
NPPN_CMDLINEPLUGINMSG = NPPN_FIRST + 28,

///<summary>
/// To notify lexer plugins that the buffer (in idFrom) is just applied to a external lexer
/// scnNotification->nmhdr.code = NPPN_EXTERNALLEXERBUFFER;
/// scnNotification->nmhdr.hwndFrom = hwndNpp;
/// scnNotification->nmhdr.idFrom = BufferID; //where pluginMessage is pointer of type wchar_t
///</summary>
NPPN_EXTERNALLEXERBUFFER = NPPN_FIRST + 29,

/// <summary>
/// To notify plugins that the current document is just modified by Replace All action.<br></br>
/// For solving the performance issue (from v8.6.4), Notepad++ doesn't trigger SCN_MODIFIED during Replace All action anymore.<br></br>
/// As a result, the plugins which monitor SCN_MODIFIED should also monitor NPPN_GLOBALMODIFIED.<br></br>
/// <strong>This notification is implemented in Notepad++ v8.6.5.</strong><br></br>
/// scnNotification->nmhdr.code = NPPN_GLOBALMODIFIED;<br></br>
/// scnNotification->nmhdr.hwndFrom = BufferID;<br></br>
/// scnNotification->nmhdr.idFrom = 0; // preserved for the future use, must be zero
/// </summary>
NPPN_GLOBALMODIFIED = NPPN_FIRST + 30,

/* --Autogenerated -- end of section automatically generated from notepad-plus-plus\PowerEditor\src\MISC\PluginsManager\Notepad_plus_msgs.h * */
}

Expand Down
4 changes: 2 additions & 2 deletions NppCSharpPluginPack/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@
// Build Number
// Revision
//
[assembly: AssemblyVersion("0.0.2.3")]
[assembly: AssemblyFileVersion("0.0.2.3")]
[assembly: AssemblyVersion("0.0.2.4")]
[assembly: AssemblyFileVersion("0.0.2.4")]
8 changes: 4 additions & 4 deletions most recent errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Test results for CSharpPluginPack v0.0.2 on Notepad++ 8.6.2 64bit
Test results for CSharpPluginPack v0.0.2.4 on Notepad++ 8.6.4 64bit
NOTE: Ctrl-F (regular expressions *on*) for "Failed [1-9]\d*" to find all failed tests
No tests failed
=========================
Expand All @@ -21,13 +21,13 @@ Testing Performance of something
Performance tests for My benchmarks (test1)
=========================

To run query "foo" on file of size 7913 into took 0 +/- 0.002 ms over 32 trials
Query times (ms): 0.01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
To run query "foo" on file of size 7913 into took 0 +/- 0 ms over 32 trials
Query times (ms): 0.002, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Preview of result: Preview of result
=========================
Performance tests for My benchmarks (test2)
=========================

To run query "bar" on file of size 7913 into took 0 +/- 0 ms over 32 trials
Query times (ms): 0.001, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Query times (ms): 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Preview of result: Preview of result

0 comments on commit 470b8f8

Please sign in to comment.