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

[Request] Snippet Editor - Check __tmplintalistedit temp file for external changes and update in Snippet Script box #268

Open
TFWol opened this issue Apr 19, 2024 · 7 comments

Comments

@TFWol
Copy link
Contributor

TFWol commented Apr 19, 2024

I was looking at

FileDelete, %TmpDir%\__tmplintalistedit.txt
FileAppend, %ToFile%, %TmpDir%\__tmplintalistedit.txt, UTF-8
If (SnippetEditor = "")
Run, "%TmpDir%\__tmplintalistedit.txt"
else
Run, "%SnippetEditor%" "%TmpDir%\__tmplintalistedit.txt"
WinWait, __tmplintalistedit
SetTimer, CheckEdit, 500, On
Return
}
CheckEdit:
IfWinExist, __tmplintalistedit
Return
SetTimer, CheckEdit, Off
FileRead, NewText, %TmpDir%\__tmplintalistedit.txt
FileDelete, %TmpDir%\__tmplintalistedit.txt
WinActivate, Lintalist bundle editor
Gui, 71:Default
If EditorSyntaxHL
{
RC%RCID%.Value:=NewText
GuiControl, Focus, % RC%RCID%.hWnd
}
else
{
GuiControl, ,%WhichControl%, %NewText%
ControlFocus, %WhichControl%, Lintalist bundle editor
}
NewText=
WhichControl=
RCID=
Return

To stop losing work by accident, could the __tmplintalistedit temp file be checked for changes to be copied when Modified Date is different?
The snippet Script box could periodically sync with the file until it's deleted.

Something like this maybe?

Note

I have my temp file named as .ahk rather than .txt

check_date(){
	global TmpDir
	FileGetTime, current_modified , %TmpDir%\__tmplintalistedit.ahk, M

	if (prev_modified = current_modified)
		return
	
	prev_modified := current_modified
	FileRead, NewText, %TmpDir%\__tmplintalistedit.ahk
	return
}
@TFWol
Copy link
Contributor Author

TFWol commented Apr 19, 2024

I had been looking into it because the tmp file gets deleted at line 853 since changing tabs in VScode will change the Window Title that's being monitored.

@lintalist
Copy link
Owner

I realise it is not ideal, but it is a relatively easy solution. As is is now the snippet editor is activated again when the "window" is no longer available, not sure how to solve that if you don't use the WinExist

  1. Using WatchFolder() might be a good way to monitor for file changes (size, time) https://www.autohotkey.com/boards/viewtopic.php?f=6&t=8384
  2. Update the edit control when changes are detected
  3. Delete the __tmplintalistedit.txt file when the snippet editor closes

That leaves us with the problem how detect when the text editor is done/closed so the snippet editor is focused again.

The .txt is useful as on nearly all systems there will be an editor assigned to .txt files so run should open the set editor (even if it is just notepad)

@TFWol
Copy link
Contributor Author

TFWol commented Apr 21, 2024

Using WatchFolder() might be a good way to monitor for file changes

I was looking into that, but didn't want to add too much extra code since I was thinking of solutions for a possible Pull. I guess whatever is less resource intensive would work.

That leaves us with the problem how detect when the text editor is done/closed so the snippet editor is focused again

Yeah, that's been difficult with vscode since I haven't been able to tell if that file is open in a different tab. Even the process commandline doesn't show the file if the window is shared.

The .txt is useful as on nearly all systems there will be an editor assigned to .txt files so run should open the set editor (even if it is just notepad)

That's fine, I was just mentioning it because my example code had .ahk.
I had made the change for myself so I wouldn't have to keep switching the Language Mode to ahk when it's opened in an IDE. Maybe a setting for the extension could be added where the optional Editor is defined 🤔
Not all that important though.

@lintalist
Copy link
Owner

I don't use vscode but in my editor I simply say .txt files should be treated as .ahk files re syntax highlighting ;)

@TFWol
Copy link
Contributor Author

TFWol commented Apr 21, 2024

I would do that, but I use vscode (well, vscodium) as my general text editor too 😅

@lintalist
Copy link
Owner

I've tried some advanced code I found on the ahk forum to get a list of all opened files by a process.
Indeed it shows for notepad and some editors that the temp file is open and of course when it is closed. But not for a portable version of vscodium for example, so I suspect that that is also not a fail safe method.

Therefore I'm thinking about adding a checkbox "Monitor editor" that when checked (default) uses the current behaviour with the timer.

When unchecked, the button which launched the editor gets a new text and you have to click that button to load the edited file manually, and then it will be deleted (or attempt made, the file could still be open but in that case I think it is user error)

load-editor

@TFWol
Copy link
Contributor Author

TFWol commented May 26, 2024

That'll actually work fine. Simple 😃

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

2 participants