lintalist icon indicating copy to clipboard operation
lintalist copied to clipboard

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

Open TFWol opened this issue 2 years ago • 29 comments

I was looking at https://github.com/lintalist/lintalist/blob/fc1bea39d539baf2eb7c3f2c195e514b9b4c6bdf/include/Editor.ahk#L837-L869

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 avatar Apr 19 '24 07:04 TFWol

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.

TFWol avatar Apr 19 '24 07:04 TFWol

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)

lintalist avatar Apr 20 '24 09:04 lintalist

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.

TFWol avatar Apr 21 '24 06:04 TFWol

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

lintalist avatar Apr 21 '24 08:04 lintalist

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

TFWol avatar Apr 21 '24 10:04 TFWol

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

lintalist avatar May 25 '24 14:05 lintalist

That'll actually work fine. Simple 😃

TFWol avatar May 26 '24 07:05 TFWol

You can try the attached editor.ahk (replace the one in include\ and restart. If you want to change the extension of __tmplintalistedit.txt to __tmplintalistedit.ahk or something else, manually add an entry to session.ini under the [editor] section like so:

[editor]
EditorTempFileExtension=ahk

If SnippetEditor is not defined you may be asked to select a program as I now use Run, edit instead of just Run reason being if you do set it extension to ahk it would run the script instead of opening it.

📁 Editor.zip - updated 2

lintalist avatar Jun 02 '24 09:06 lintalist

Thanks! I'm still testing, but I noticed 'Monitor Editor' doesn't seem to work when checked. Closing the file in editor doesn't cause the tmp file to be copied then deleted.

btw, it looks like you forgot to add the window focus fix from #267

TFWol avatar Jun 02 '24 14:06 TFWol

I worked from and older branch so yes 267 was omitted, updated the above edit.ahk so should work better now. Thanks for testing.

lintalist avatar Jun 02 '24 15:06 lintalist

To add, don't keep this tab with this title active/open because it will match the title criteria and won't load the edited file 😹

lintalist avatar Jun 02 '24 15:06 lintalist

Things are better (the Editor button isn't changing when it shouldn't), but it's still unable to detect when I close the editor. It might be the way I'm launching my editor. I'll test some things out.

TFWol avatar Jun 02 '24 17:06 TFWol

If you have this tab open/active it will detect it as a valid window due to the title of your request (it includes __tmplintalistedit) - edit: removed it from the title...

lintalist avatar Jun 02 '24 18:06 lintalist

If you have this tab open/active it will detect it as a valid window due to the title of your request (it includes __tmplintalistedit) - edit: removed it from the title...

lmfao, I completely misunderstood your original statement. Got it. It's working just fine 🤣

TFWol avatar Jun 02 '24 19:06 TFWol

The code posted was more proof of concept, it doesn't work yet with syntax highlighting enabled and the repetitive code needs to be condensed as well, so re-opening so I have it on my todo list for the next update.

lintalist avatar Jun 03 '24 16:06 lintalist

Should I remove the pull request or keep it up for you to modify?

  • [x] Probably close pull request #277

TFWol avatar Jun 05 '24 03:06 TFWol

Closing it would be best :)

lintalist avatar Jun 05 '24 16:06 lintalist

Just

You can try the attached editor.ahk (replace the one in include\ and restart. If you want to change the extension of __tmplintalistedit.txt to __tmplintalistedit.ahk or something else, manually add an entry to settings.ini under the [editor] section like so:

[editor]
EditorTempFileExtension=ahk

If SnippetEditor is not defined you may be asked to select a program as I now use Run, edit instead of just Run reason being if you do set it extension to ahk it would run the script instead of opening it.

📁 Editor.zip - updated 1

Should line 975 maybe be changed from

IniRead, EditorTempFileExtension, %A_ScriptDir%\session.ini, editor, EditorTempFileExtension, txt

to this? (maybe even moved)

IniRead, EditorTempFileExtension, %A_ScriptDir%\Settings.ini, editor, EditorTempFileExtension, txt

.txt was being used instead of .ahk since the value was being referenced from session.ini and ignoring the Settings.ini

TFWol avatar Jun 06 '24 23:06 TFWol

I thought it best to keep the EditorTempFileExtension in session.ini as I believe it is rather an exotic setting that wouldn't be used (but was easy to add). For settings in settings.ini I would "need" to have a include\settings\EditorTempFileExtension.ini ( I would make it hidden, but need to add the help text regardless) and update ReadIni.ahk as well.

lintalist avatar Jun 08 '24 08:06 lintalist

Oh, I see. I had asked since you told me to put the change in Settings.ini

TFWol avatar Jun 08 '24 09:06 TFWol

Found a tiny bug in the posted Editor.ahk. Line 915 has ToolTip % whichcontrol which causes the word Script to stay stuck on the screen after you click Load From File

TFWol avatar Jun 11 '24 03:06 TFWol

Ha ha wondered if you'd spot that.. (was for debugging)

lintalist avatar Jun 11 '24 16:06 lintalist

re https://github.com/lintalist/lintalist/discussions/276 and just for testing (I'll move some code elsewhere later on):

see Update 2 editor.ahk above, to set/select another text editor in the editor:

Make an editors.ini like so in the lintalist folder:

[settings]
TextAdept=c:\Portable\TextAdept\TextAdept.exe
Pragtical=c:\Portable\pragtical\pragtical.exe

then restart lintalist, now you should see ▼ Set editor just before monitor editor checkbox which just shows a menu so you pick another editor. The Default editor is the SnippetEditor you set in ⚙ Configuration.

As I think this is rather an expert/niche option I'll keep it like this e.g. only show ▼ Set editor when there is a editors.ini

lintalist avatar Jun 14 '24 16:06 lintalist

Thanks for making changes, however, I don't see Editor Update 2 anywhere...

TFWol avatar Jun 15 '24 23:06 TFWol

weird I tested the download and it worked, now the comments seems to be back to update 1, it should be here (edited it again) https://github.com/lintalist/lintalist/issues/268#issuecomment-2143772783 and to be sure here as well Editor.zip - update 2

edit: on line 41 of edit.ahk there is a debug msgbox that should show the contents of the editors from the ini if it is empty something is wrong obviously (with the ini or location of the ini)

lintalist avatar Jun 16 '24 08:06 lintalist

Thanks, I was able to get the changed editor file.

I haven't had any major problems yet. Just a small one; the first time you launch bundle editor (after completely closing Lintalist, not reloading), the Set Editor control gets overlapped and you have to adjust the window size to make it stay until Lintalist is started again. image

I also learned to avoid putting quotes around the values in editors.ini (even if it has spaces in its path) else they get double quoted by line 932, causing it to instead open an Explorer window 😅

TFWol avatar Jun 17 '24 04:06 TFWol

Thanks for testing. I think it isn't such a problem as you can indeed make the window a tad wider (it remembers the width/height +X/Y pos between sessions). There isn't really that much room to place it elsewhere (I didn't like the placement before the edit button) and it will only be shown when there is an editors.ini and I suspect it be very rarely used. I could just use but it so tiny to click with the mouse.

So I'll leave it like this for now, unless dozens of people start complaining about it :)

lintalist avatar Jun 17 '24 16:06 lintalist

I fixed #288 and moved the set editor to the menu bar (obviously better). As it is possible to cancel the edit and because I now disable the other 2 edit buttons I've added a "Reset (enable) Edit Buttons" option to the edit menu as well: edit Edit menu only available when there is an editors.ini

lintalist avatar Jun 29 '24 12:06 lintalist

Nice. Having the Editor in a menu really does make sense. I can't believe it didn't even occur to me.

TFWol avatar Jun 30 '24 13:06 TFWol

Added to v1.9.23, I'll close the issue but feel free to report any problems https://lintalist.github.io/#Configuration-SnippetEditor

lintalist avatar Jul 13 '24 08:07 lintalist