Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving file scrolls it to the top #36

Open
tomaszs opened this issue Jul 13, 2023 · 3 comments
Open

Saving file scrolls it to the top #36

tomaszs opened this issue Jul 13, 2023 · 3 comments

Comments

@tomaszs
Copy link

tomaszs commented Jul 13, 2023

When I save the file, it scrolls to the top
Expected behavior: Saving doesn't scroll it to the top

BTW. Great extension! I appreciate your work on it!

@andrey-zotov
Copy link
Contributor

I have the same issue, otherwise the plugin is great!

I made it work locally, with one caveat - when Word wrap mode is on, Scintilla's SCI_GETFIRSTVISIBLELINE doesn't return correct results, not sure how to fix that. With Word wrap off, it works OK.

Diff:

diff --git a/src/nppcrypt.cpp b/src/nppcrypt.cpp
index ee2f4f5..1735a53 100644
--- a/src/nppcrypt.cpp
+++ b/src/nppcrypt.cpp
@@ -50,6 +50,9 @@ HINSTANCE               m_hInstance;
 CurrentOptions          current;
 cryptfilemap            crypt_files;
 NppCryptFileSave        crypt_file_cursave;
+UINT_PTR                last_known_scintilla_first_line = 0;
+UINT_PTR                last_known_scintilla_pos = 0;
+UINT_PTR                last_known_scintilla_anchor = 0;

 DlgCrypt                dlg_crypt;
 DlgHash                 dlg_hash(current.hash);
@@ -243,7 +246,9 @@ extern "C" __declspec(dllexport) void beNotified(SCNotification *notifyCode)
                 if (crypt_file_cursave != NppCryptFileSave::NO_ENCRYPTION) {
                     HWND hCurScintilla = help::scintilla::getCurrent();
                     ::SendMessage(hCurScintilla, SCI_UNDO, 0, 0);
-                    ::SendMessage(hCurScintilla, SCI_GOTOPOS, 0, 0);
+                    ::SendMessage(hCurScintilla, SCI_SETANCHOR, last_known_scintilla_anchor, 0);
+                    ::SendMessage(hCurScintilla, SCI_SETCURRENTPOS, last_known_scintilla_pos, 0);
+                    ::SendMessage(hCurScintilla, SCI_SETFIRSTVISIBLELINE, last_known_scintilla_first_line, last_known_scintilla_first_line);
                     ::SendMessage(hCurScintilla, SCI_EMPTYUNDOBUFFER, 0, 0);
                     ::SendMessage(hCurScintilla, SCI_SETSAVEPOINT, 0, 0);
                 }
@@ -282,6 +287,10 @@ extern "C" __declspec(dllexport) void beNotified(SCNotification *notifyCode)
                 ::SendMessage(nppData._nppHandle, NPPM_SWITCHTOFILE, 0, (LPARAM)path.c_str());
                 HWND hCurScintilla = help::scintilla::getCurrent();

+                last_known_scintilla_first_line = (UINT_PTR)::SendMessage(hCurScintilla, SCI_GETFIRSTVISIBLELINE, 0, 0);
+                last_known_scintilla_pos = (UINT_PTR)::SendMessage(hCurScintilla, SCI_GETCURRENTPOS, 0, 0);
+                last_known_scintilla_anchor = (UINT_PTR)::SendMessage(hCurScintilla, SCI_GETANCHOR, 0, 0);
+
                 int data_length = (int)::SendMessage(hCurScintilla, SCI_GETLENGTH , 0, 0);
                 if (data_length <= 0) {
                     throwInfo(file_empty);

@jeanpaulrichter any chance you would accept a PR?

@jeanpaulrichter
Copy link
Owner

@andrey-zotov sure, looks fine.

@andrey-zotov
Copy link
Contributor

@andrey-zotov sure, looks fine.

Here you go #38

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants