TC and Notepad2 / Notepad2-mod - save editing position

Discuss and announce Total Commander plugins, addons and other useful tools here, both their usage and their development.

Moderators: Hacker, petermad, Stefan2, white

Post Reply
User avatar
Hacker
Moderator
Moderator
Posts: 13142
Joined: 2003-02-06, 14:56 UTC
Location: Bratislava, Slovakia

TC and Notepad2 / Notepad2-mod - save editing position

Post by *Hacker »

Hi *.*,
I have created a script that saves the curent editing position when Ctrl-S or Escape are pressed. Then it is read upon pressing F4 in TC and passed on to Notepad2(-mod).

Notes:
Pressing Escape on an unsaved file does nothing.
The positions are saved in a file called LnCols.txt (Line and Column for each file).
You may need to replace (escape) more chars that have special meaning in a RegEx using StringReplace.
The script only reacts to Ctrl-S and Escape. It does not react to saving nor closing the script using the mouse.
Any non-existing files are pruned from LnCols.txt upon pressing Esc in Notepad2 (unless the file is not saved, see the first note). This might spin up hard drives in standby, sorry.
If you rename a file its editing position will be lost.
I could have specified the AHK script as the Editor for F4 but running AHK which then runs Notepad2 makes things too slow for my taste. If anybody has a better solution please let me know.

Code: Select all

; Notepad2-mod
#IfWinActive, ahk_class Notepad2U
$Esc::
	WinGetTitle, FileName
	StatusBarGetText, LnCol, 1
	Send, {Escape}

	FoundPos := RegExMatch(FileName, "^\* ")
	IfEqual, FoundPos, 1
		Return

	RegExMatch(FileName, "(.+) \[(.+)\]", FileName)
	FileName = %FileName2%\%FileName1%
	FileNameRegEx = %FileName%
	StringReplace, FileNameRegEx, FileNameRegEx, \, \\, All
	StringReplace, FileNameRegEx, FileNameRegEx, [, \[, All
	StringReplace, FileNameRegEx, FileNameRegEx, ], \], All
	StringReplace, FileNameRegEx, FileNameRegEx, (, \(, All
	StringReplace, FileNameRegEx, FileNameRegEx, ), \), All

	RegExMatch(LnCol, "Ln (\d+) / .* Col (\d+)\D", LnCol)
	LnCol = %LnCol1%,%LnCol2%

	FileRead, LnCols, LnCols.txt

	NewLnCols =
	IfNotEqual, LnCol, 1,1
		IfExist, %FileName%
			NewLnCols = %FileName%*%LnCol%`n

	Loop, Parse, LnCols, `n, `r
	{
		RegExMatch(A_LoopField, ".+(?=\*)", FileNameCheckIfExists)
		IfNotExist, %FileNameCheckIfExists%
			Continue

		FoundPos := RegExMatch(A_LoopField, "^" . FileNameRegEx . "\*")
		IfNotEqual, FoundPos, 0
			Continue
		Else
		{
			RegExMatch(A_LoopField, ".+(?=\*)", FileNameCheckIfExists)
			IfExist, %FileNameCheckIfExists%
				NewLnCols = %NewLnCols%%A_LoopField%`n
		}
	}
	StringTrimRight, NewLnCols, NewLnCols, 1

	FileDelete, LnCols.txt
	IfNotEqual, NewLnCols
		FileAppend, %NewLnCols%, LnCols.txt
Return
$~^s::
	WinGetTitle, FileName
	RegExMatch(FileName, "(\* )?(.+) \[(.+)\]", FileName)
	FileName = %FileName3%\%FileName2%
	FileNameRegEx = %FileName%
	StringReplace, FileNameRegEx, FileNameRegEx, \, \\, All
	StringReplace, FileNameRegEx, FileNameRegEx, [, \[, All
	StringReplace, FileNameRegEx, FileNameRegEx, ], \], All
	StringReplace, FileNameRegEx, FileNameRegEx, (, \(, All
	StringReplace, FileNameRegEx, FileNameRegEx, ), \), All
	StringReplace, FileNameRegEx, FileNameRegEx, ., \., All
	StringReplace, FileNameRegEx, FileNameRegEx, +, \+, All

	StatusBarGetText, LnCol, 1
	RegExMatch(LnCol, "Ln (\d+) / .* Col (\d+)\D", LnCol)
	LnCol = %LnCol1%,%LnCol2%

	FileRead, LnCols, LnCols.txt

	IfNotEqual, LnCol, 1,1
		NewLnCols = %FileName%*%LnCol%`n
	Else
		NewLnCols =
	Loop, Parse, LnCols, `n, `r
	{
		FoundPos := RegExMatch(A_LoopField, FileNameRegEx)
		IfNotEqual, FoundPos, 0
			Continue
		Else
			NewLnCols = %NewLnCols%%A_LoopField%`n
	}
	StringTrimRight, NewLnCols, NewLnCols, 1

	FileDelete, LnCols.txt
	IfNotEqual, NewLnCols
		FileAppend, %NewLnCols%, LnCols.txt
Return	

; Total Commander
#IfWinActive, ahk_class TTOTAL_CMD
$F4::
	ClipboardBak = %ClipboardAll%
	Clipboard =
	PostMessage, 1075, 2018
	ClipWait
	FileName = %Clipboard%
	Clipboard = %ClipboardBak%
	
	FileNameRegEx = %FileName%
	StringReplace, FileNameRegEx, FileNameRegEx, \, \\, All
	StringReplace, FileNameRegEx, FileNameRegEx, [, \[, All
	StringReplace, FileNameRegEx, FileNameRegEx, ], \], All
	StringReplace, FileNameRegEx, FileNameRegEx, (, \(, All
	StringReplace, FileNameRegEx, FileNameRegEx, ), \), All
	StringReplace, FileNameRegEx, FileNameRegEx, ., \., All
	StringReplace, FileNameRegEx, FileNameRegEx, +, \+, All

	FileRead, LnCols, LnCols.txt
	FoundPos = 0
	Loop, Parse, LnCols, `n, `r
	{
		FoundPos := RegExMatch(A_LoopField, "(?<=^" . FileNameRegEx . "\*).+", LnCol)
		IfNotEqual, FoundPos, 0
			Break
	}
	IfNotEqual, FoundPos, 0
		Run, C:\Path\To\Notepad2\Notepad2.exe /g %LnCol% "%FileName%"
	Else
		Run, C:\Path\To\Notepad2\Notepad2.exe "%FileName%"
Return
Roman
Mal angenommen, du drückst Strg+F, wählst die FTP-Verbindung (mit gespeichertem Passwort), klickst aber nicht auf Verbinden, sondern fällst tot um.
Post Reply