Snapshot/History State in Notepad++ ?

Get help with using AutoHotkey and its commands and hotkeys
scriptor2016
Posts: 630
Joined: 21 Dec 2015, 02:34

Snapshot/History State in Notepad++ ?

29 Mar 2020, 01:28

Hi all, I know this isn't necessarily an ahk question, but seeing as many users here do infact use Notepad++, I was hoping someone might help me out here:

Is there a way to set a 'snapshot' of your document and then return to it later?

For example:

-type in a bunch of lines
-set your 'snapshot', or 'history state'
-continue to type in more lines for a few more minutes
-now press a key/key combo to return to your snapshot (everything that has been typed in since you set the snapshot is deleted and you return to the snapshot's position)


I hope this makes sense....is there a way to do this?
Rohwedder
Posts: 2906
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Snapshot/History State in Notepad++ ?

29 Mar 2020, 02:49

Hallo,
for this purpose i simply open another instance and make my changes there.
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Snapshot/History State in Notepad++ ?

29 Mar 2020, 03:23

I don't have Notepad++ installed but this works in Notepad.
AFAIK the control name in Notepad++ is Scintilla1 instead of Edit1.

Code: Select all

Run, notepad,,,notePID
return

#If WinActive("ahk_pid" notePID)
^z:: ; take snapshot
   ControlGetText, tx, Edit1, ahk_pid %notePID%
return

^x:: ; restore snapshot
   ControlSetText, Edit1, %tx%,ahk_pid %notePID%
   Sleep, 100
   send, ^{End}
return
#If

*Esc::
   WinClose, ahk_pid %notePID%
   ExitApp
return
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
scriptor2016
Posts: 630
Joined: 21 Dec 2015, 02:34

Re: Snapshot/History State in Notepad++ ?

29 Mar 2020, 14:40

this works great in notepad! But not so much yet in Notepad++.

Here's what I've done with the simple changes:

Code: Select all

Run, C:\Program Files (x86)\Notepad++\notepad++.exe,,,notePID
return

#If WinActive("ahk_pid" notePID)
^k:: ; take snapshot
Tooltip, SNAPSHOT TAKEN
SetTimer, TooltipOff, -500
   ControlGetText, tx, Scintilla1, ahk_pid %notePID%
return

^m:: ; restore snapshot
Tooltip, SNAPSHOT RESTORED
SetTimer, TooltipOff, -500
   ControlSetText, Scintilla1, %tx%, ahk_pid %notePID%
   Sleep, 100
   send, ^{End}
return
#If

TooltipOff:
tooltip,
Return
unfortunately right now it doesn't do anything. The tooltips aren't showing up so it must be something to do with the #If Winactive line
User avatar
boiler
Posts: 4561
Joined: 21 Dec 2014, 02:44

Re: Snapshot/History State in Notepad++ ?

29 Mar 2020, 14:57

It can't be a problem with the #If Winactive line or it wouldn't even show the tooltip. In my test, it's grabbing the text correctly with ^k (you can verify by double-clicking on the script's icon and viewing the variable contents), but it's not putting the text back in the control correctly with ^m. For me, it's only putting back the first letter.
scriptor2016
Posts: 630
Joined: 21 Dec 2015, 02:34

Re: Snapshot/History State in Notepad++ ?

29 Mar 2020, 15:02

Me too! It only pastes back in the first letter, so we are on to something here.

Here's what I've done so far. It seems to take the snapshot successfuly but it won't paste it back in properly:

Code: Select all

^k:: ; take snapshot
Tooltip, SNAPSHOT TAKEN
SetTimer, TooltipOff, 500
wingettitle, title, A
WinGet, var, PID, %title%
ControlGetText, tx, Scintilla1, ahk_pid %var%
msgbox, text is %tx%
return

^m:: ; restore snapshot
Tooltip, SNAPSHOT RESTORED
SetTimer, TooltipOff, 500
ControlSetText, Scintilla1, %tx%, ahk_pid %var%
Sleep, 100
;send, ^{End}
return
 

TooltipOff:
tooltip,
Return
Last edited by scriptor2016 on 29 Mar 2020, 15:05, edited 1 time in total.
User avatar
boiler
Posts: 4561
Joined: 21 Dec 2014, 02:44

Re: Snapshot/History State in Notepad++ ?

29 Mar 2020, 15:04

Yes, same with your script when you had the #If directive in there. It appears that Notepad++ isn't receptive to having its Scintilla1 control stuffed via ControlSetText. I suppose you could put the text in the clipboard and send ^a and ^v.
scriptor2016
Posts: 630
Joined: 21 Dec 2015, 02:34

Re: Snapshot/History State in Notepad++ ?

29 Mar 2020, 15:10

I guess this does the trick:

Code: Select all

^k:: ; take snapshot
Tooltip, SNAPSHOT TAKEN
SetTimer, TooltipOff, 500
wingettitle, title, A
WinGet, var, PID, %title%
ControlGetText, tx, Scintilla1, ahk_pid %var%
clipboard=
Clipboard:=tx
;msgbox, text is %tx%
return

^m:: ; restore snapshot
Tooltip, SNAPSHOT RESTORED
SetTimer, TooltipOff, 500
;ControlSetText, Scintilla1, %tx%, ahk_pid %var% ;this doesn't work
sendinput, ^a ;select all text
Sleep, 100
sendinput, ^v ;paste the text back in
send, ^{End}
return
 
TooltipOff:
tooltip,
Return
Only thing I would be worried about is if the clipboard/select all/paste back in somehow misfires and causes the whole text to be wiped out?
Rohwedder
Posts: 2906
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Snapshot/History State in Notepad++ ?

29 Mar 2020, 15:14

Hallo,
my version:

Code: Select all

#IfWinActive, ahk_class Notepad++
^k:: ; take snapshot
   ControlGetText, tx, Scintilla1, ahk_class Notepad++
return

^m:: ; restore snapshot
ClipSaved := ClipBoardAll
ClipBoard := tx
Sleep, 100
ControlSend, Scintilla1, ^a^v, ahk_class Notepad++
Sleep, 100
ClipBoard := ClipSaved 
return
#If
User avatar
boiler
Posts: 4561
Joined: 21 Dec 2014, 02:44

Re: Snapshot/History State in Notepad++ ?

29 Mar 2020, 15:14

I would leave the part with ^k and the ControlGetText because that part worked. Just change the ^m after to what you have with sending ^a and ^v. Just need to do this before sending: Clipboard := tx.

Then you don't have to worry about losing it because you know it's in the clipboard, so you can always manually press ^v to get the text back if it happened to misfire on the ^m. The ^k doesn't seem like it would misfire since it uses ControlGetText.
scriptor2016
Posts: 630
Joined: 21 Dec 2015, 02:34

Re: Snapshot/History State in Notepad++ ?

29 Mar 2020, 15:18

awesome,Thanks guys!

Next is to find a way to get the position of the cursor/caret *before* taking the snapshot, and then restore its position after pasting the text back in... I will start researching now :)
User avatar
boiler
Posts: 4561
Joined: 21 Dec 2014, 02:44

Re: Snapshot/History State in Notepad++ ?

29 Mar 2020, 15:22

Here's a start: Use A_CaretX and A_CaretY. Doesn't work with all editors, but it works with Notepad++.
scriptor2016
Posts: 630
Joined: 21 Dec 2015, 02:34

Re: Snapshot/History State in Notepad++ ?

29 Mar 2020, 15:48

This works, but would it reliable enough?

Code: Select all

#IfWinActive, ahk_class Notepad++
^k:: ; take snapshot
CaretX:=A_CaretX  
CaretY:=A_CaretY
Tooltip, SNAPSHOT TAKEN
SetTimer, TooltipOff, 500
   ControlGetText, tx, Scintilla1, ahk_class Notepad++
   ;msgbox, X%A_CaretX% Y%A_CaretY% 
return

^m:: ; restore snapshot
Tooltip, SNAPSHOT RESTORED
SetTimer, TooltipOff, 500
ClipSaved := ClipBoardAll
ClipBoard := tx
Sleep, 100
ControlSend, Scintilla1, ^a^v, ahk_class Notepad++
Sleep, 100
ClipBoard := ClipSaved 
Click %CaretX% %CaretY%
return
#If

TooltipOff:
tooltip,
Return
User avatar
boiler
Posts: 4561
Joined: 21 Dec 2014, 02:44

Re: Snapshot/History State in Notepad++ ?

29 Mar 2020, 16:01

I think it should be very reliable because getting the text into the variable tx using ControlGetText should be very reliable. If you get a misfire on the ^m, just try it again or use ^v at that point since it’s already in the clipboard. Just don’t hit ^k again and wipe out your prior value of tx. You could keep a history/backup of tx with some method of pasting older versions of it to guard against that. Maybe write them to backup files.
scriptor2016
Posts: 630
Joined: 21 Dec 2015, 02:34

Re: Snapshot/History State in Notepad++ ?

29 Mar 2020, 16:11

In my most recent script above, returning the caret to its original position after pasting the text back in doesn't work, unfortunately.

It clicks the proper position in the Notepad++ document in order to get the caret back at its position before the snapshot was taken, but returning it back there after pasting doesn't take into account the fact that the current document may be 1000's of lines long, therefore having a scrollbar.

And if this scrollbar was at the top when taking the snapshot, when we paste the text back in later on, it doesn't return the scrollbar to where it was, therefore clicking on the wrong line in the script (even though the coordinates for clicking are correct - they are relative to the active window, but not taking into account the position of the scrollbar as well)

whew - I have no idea if that made sense.

So I guess I would have to get the position of the Scrollbar as well as getting the position of the Caret before taking the snapshot? Or is there a better way to do this
User avatar
boiler
Posts: 4561
Joined: 21 Dec 2014, 02:44

Re: Snapshot/History State in Notepad++ ?

29 Mar 2020, 16:25

Yes, that made total sense, and you're right. One way of ensuring the cursor gets back to its previous location is to send cursor up/down and left/right until the Notepad++ status shows the line and column that it was when you took the snapshot. Here's how you grab that info:

Code: Select all

StatusBarGetText, StatusPt3, 3, ahk_exe notepad++.exe
RegExMatch(StatusPt3, "Ln : (\d+) *Col : (\d+)", Match)
MsgBox, % "Line: " Match1 "`nColumn: " Match2
Once the cursor is on the correct line and in the correct column, you can click the scroll bar up/down until the absolute caret position relative to the screen also matches where it was, if that's important to you.
scriptor2016
Posts: 630
Joined: 21 Dec 2015, 02:34

Re: Snapshot/History State in Notepad++ ?

29 Mar 2020, 16:36

thanks boiler, let me take a look at this...
scriptor2016
Posts: 630
Joined: 21 Dec 2015, 02:34

Re: Snapshot/History State in Notepad++ ?

29 Mar 2020, 16:56

Interesting, it seems the sample code only allows up to 999 lines. 1000 lines and above will not accurately get the cursor's line and column :shock:

Code: Select all

StatusBarGetText, StatusPt3, 3, ahk_exe notepad++.exe
RegExMatch(StatusPt3, "Ln : (\d+) *Col : (\d+)", Match)
MsgBox, % "Line: " Match1 "`nColumn: " Match2
scriptor2016
Posts: 630
Joined: 21 Dec 2015, 02:34

Re: Snapshot/History State in Notepad++ ?

29 Mar 2020, 17:01

OK! Getting somewhere here...

this seems to be working well. Don't know how reliable it will be, but so far so good.

Now the only limit is the 999 lines thing..

Code: Select all

#IfWinActive, ahk_class Notepad++
^k:: ; take snapshot
;CaretX:=A_CaretX  ;don't think we need this now
;CaretY:=A_CaretY ;don't think we need this now

StatusBarGetText, StatusPt3, 3, ahk_exe notepad++.exe
RegExMatch(StatusPt3, "Ln : (\d+) *Col : (\d+)", Match)
MsgBox, % "Line: " Match1 "`nColumn: " Match2


Tooltip, SNAPSHOT TAKEN
SetTimer, TooltipOff, 500
   ControlGetText, tx, Scintilla1, ahk_class Notepad++
   ;msgbox, X%A_CaretX% Y%A_CaretY% ;don't think we need this now
return

^m:: ; restore snapshot
Tooltip, SNAPSHOT RESTORED
SetTimer, TooltipOff, 500
ClipSaved := ClipBoardAll
ClipBoard := tx
Sleep, 100
ControlSend, Scintilla1, ^a^v, ahk_class Notepad++
Sleep, 500
ClipBoard := ClipSaved 

winactivate, ahk_class Notepad++
sendinput, ^{home}

sleep, 50

if Match1 = 1
{
sendinput, {right %Match2%}
}

else

{
sendinput, {down %Match1%}
sendinput, {right %Match2%}
}

;Click %CaretX% %CaretY% ;don't think we need this now
return
#If

TooltipOff:
tooltip,
Return
User avatar
boiler
Posts: 4561
Joined: 21 Dec 2014, 02:44

Re: Snapshot/History State in Notepad++ ?

29 Mar 2020, 17:02

Ah, that's because it inserts a comma for a thousands separator. This takes care of that:

Code: Select all

StatusBarGetText, StatusPt3, 3, ahk_exe notepad++.exe
StatusPt3 := StrReplace(StatusPt3, ",")
RegExMatch(StatusPt3, "Ln : (\d+) *Col : (\d+)", Match)
MsgBox, % "Line: " Match1 "`nColumn: " Match2
If you are in a country where Notepad++ might use a "." for the thousands separator (I don't know if it does), just replace the "," in the StrReplace call with a "." instead.

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, Bing [Bot], Bral, chenj209, eblanc, F4Jonatas, Gigadroid, Google [Bot], KiddoV, SilasDeVis, vsub, wyatterp, Xtra and 130 guests