Bug 462112

Summary: Restore unsaved (newly opened - "Untitled") document doesn't work when Plasma session is restored
Product: [Applications] kate Reporter: Piotr Mierzwinski <piotr.mierzwinski>
Component: sessionsAssignee: KWrite Developers <kwrite-bugs-null>
Status: RESOLVED FIXED    
Severity: normal CC: bfs5030, gwaewion, KDE, kubry, nate, obskyr, piotr.mierzwinski, qazerr, samjnaa, thubble+kdebugs, track-kdebugs, trozemo, zmogas
Priority: NOR    
Version: unspecified   
Target Milestone: ---   
Platform: Neon   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:
Attachments: kate_session_configuration-page

Description Piotr Mierzwinski 2022-11-21 23:28:27 UTC
Created attachment 153935 [details]
kate_session_configuration-page

SUMMARY
Restore unsaved (newly opened - "Untilted") document doesn't work for me if kate is stopped by Plasma and started again with new session (after relogin or restart and login  I have turned on restore session of Plasma and I experience loss data in Kate (for untilted documents) after Kate is restored by Plasma. All is fine if I close Kate by "X" button (clear quit).
Please notice I don't have any activities and virtual desktops With activity it also doesn't work.. In session I opened also couple other documents and I'm afraid to modify then without save to don't loss data after restoring session.
This is really annoying. I have turned on proper option in settings of kate (check attached screen shot)


STEPS TO REPRODUCE
1. Turn on restore session in KDE/Plasma
2. start Kata
3. in new session open existing document
4. open new ("untilted")
5. put some text into new unsaved document
6. log out and log in to Plasma session or restart PC and log in again

OBSERVED RESULT
Empty Untilted document

EXPECTED RESULT
Untilted document should be restored

SOFTWARE/OS VERSIONS
Linux/KDE Plasma: 
(available in About System)
KDE Plasma Version: 5.26.80
KDE Frameworks Version: .101.0
Qt Version: 5.15.7

ADDITIONAL INFORMATION
Kate version: 23.03.70  (update on 2022/11/21)
OS: Neon Unstable
Graphics Platform: X11
Comment 1 Tiernan Hubble 2023-01-16 01:51:47 UTC
I'm not sure if this comment should be split off into another bug - it's related, but a much larger scope.

This is due to the implementation of Kate's "stash unsaved documents" feature. In Notepad++'s equivalent feature (which I personally much prefer), it operates much like Kate's (actually KTextEditor's) "swap files". It stashes untitled files (and created files with unsaved modifications) in the config directory on a configurable timer (e.g. every 15 seconds). This way, even if there's a system crash or sudden power loss, all untitled files will be preserved (except for changes since the last timer tick, but that's several seconds by default). I assume Notepad++ also saves these files when quitting normally.

Kate's implementation stashes untitled files in a directory sort of like Notepad++ does, but it only does so when the app exits. So if the system hard-crashes or there's a loss of power, the stash isn't done. The worst part though, is that when opening stashed untitled files, Kate actually deletes them - see KateStashManager::popDocument(). I assume the theory is that it will re-create all of them when quitting normally the next time, and maybe this is done to avoid complexity when reopening the session - but the result is that if Kate (or the system) unexpectedly crashes, you lose ALL untitled files. Even the ones that had already been stashed and re-loaded successfully on the last Kate launch!

Kate's "swap files" functionality operates exactly like Notepad++'s, but unfortunately it's only used for previously-created files with unsaved changes, not untitled files. I took a look to see if I could use the existing "swap files" functionality for unsaved files, but swap files is actually a KTextEditor framework feature, while "stash unsaved" is implemented in Kate itself.

Various changes have been made to handle the different ways in which Kate can be closed (https://invent.kde.org/utilities/kate/-/merge_requests/590, https://invent.kde.org/utilities/kate/-/merge_requests/649). It's possible that this specific case (Plasma session save/restore) could be handled as well, but that's yet another place that handles a specific scenario of Kate being launched/exited. And it wouldn't fix the underlying issue that untitled files will be lost if there's a hard-crash/power loss.

Has anyone considered changing the "stash unsaved" feature to be more like Notepad++'s? I took a look at the code and couldn't think of an easy way to do it. At the very least, we'd need to get rid of the behaviour that deletes files when un-stashing them, and I'm not sure how that would interact with Kate's existing session code. I noticed that when an unexpected crash/shutdown happens, Kate keeps the list of open "Untitled" files, but they're all blank due to being deleted on the previous Kate launch - so there must be some functionality in sessions that at least preserves the list of untitled files. Come to think of it, I'm not sure if even sessions themselves (e.g. list of open documents) can be saved on a timer rather than on shutdown - that might be the first step.
Comment 2 Piotr Mierzwinski 2023-01-23 13:26:01 UTC
Handle of session when we have opened "untitled" file(s), which contain some text this is one problem. Result is opened duplicate of document (which is usual empty)  after reopen Kate (clicked "X" on window and run again Kate). In this case content of "untitled" file is preserved.
Second thing is handle similar situation when Plasma session is restored (user logout and login again). In this case content of "untitled" is loss and additionally we get duplicate of "untitled" (empty).  Please notice that with another relogin we can get another empty documents calling "untitled (n+1)"
Comment 3 datenritter 2023-10-11 10:21:12 UTC
Same problem in Debian 12.2.
Comment 4 vujo 2024-04-09 16:18:55 UTC
Thank you very much to all the Kate and KDE developers. I have been using various programs from KDE for a long time.

I have also been observing this problem for more than a year on FreeBSD 13.1, 13.2 and many other Linux distributions.
If you close kate via File -> Exit, the contents of unsaved files are saved, but if you log out or restart the computer, the contents of all unsaved files are lost.
Please add the ability to save the contents of unsaved files after restarting the user session.
Thx.
Comment 5 Byron Sanchez 2024-04-12 22:32:38 UTC
Would like to add my +1 to this request. I was trying out this feature, and noticed that some test data I was playing around with was lost. Took a look and saw the behavior Tiernan described- the files do get successfully stashed on the filesystem, and they get loaded when the session is restored. But, if for any reason, Kate exits in an unexpected manner (eg. killall -9 kate), you lose all that data.

It'd be nice to have a reliable-enough store of these sorts of "scratch notes" without having to think about concretely defining where they should live in a file hierarchy.  Was using Notepad++ for this previously on Windows, and am now looking for a Linux replacement for it.

I get that you should save data that you care about. But if this is something that can be implemented, I think it would be a great experience improvement for a sort of stash/stream of notes that are effectively auto-saved in a temporary location until you're ready to concretely define where you want it saved.
Comment 6 Christoph Cullmann 2024-09-10 09:03:54 UTC
Git commit bb268e2be0ff118c277d2e5bffb98b6e00ece3ea by Christoph Cullmann.
Committed on 10/09/2024 at 09:03.
Pushed by cullmann into branch 'master'.

fix session restore of tabs/views of untitled documents

will allow us to restore the right tabs even for untitled documents
and avoids that we create new untitled documents on session restore
Related: bug 464703, bug 462523, bug 468573

M  +22   -1    apps/lib/katedocmanager.cpp
M  +9    -0    apps/lib/katedocmanager.h
M  +37   -16   apps/lib/kateviewspace.cpp

https://invent.kde.org/utilities/kate/-/commit/bb268e2be0ff118c277d2e5bffb98b6e00ece3ea
Comment 7 Christoph Cullmann 2024-09-13 16:56:36 UTC
Git commit be856dbb927f2c4082218bc4e0360eb677c9c44f by Christoph Cullmann.
Committed on 13/09/2024 at 16:56.
Pushed by cullmann into branch 'release/24.08'.

fix session restore of tabs/views of untitled documents

will allow us to restore the right tabs even for untitled documents
and avoids that we create new untitled documents on session restore
Related: bug 464703, bug 462523, bug 468573


(cherry picked from commit bb268e2be0ff118c277d2e5bffb98b6e00ece3ea)

1866ba36 store the id from the session we did load/save
06c49bc4 write out the id of the document and not the url
2da4f29e read both the new session stored with id and the old with url

Co-authored-by: Christoph Cullmann <christoph@cullmann.io>

M  +22   -1    apps/lib/katedocmanager.cpp
M  +9    -0    apps/lib/katedocmanager.h
M  +37   -16   apps/lib/kateviewspace.cpp

https://invent.kde.org/utilities/kate/-/commit/be856dbb927f2c4082218bc4e0360eb677c9c44f
Comment 8 Piotr Mierzwinski 2024-10-23 23:00:59 UTC
Sorry, but this still doesn't work.
I reported separate bug here: https://bugs.kde.org/show_bug.cgi?id=493665 (reopened)
Additionally I added two posts in https://bugs.kde.org/show_bug.cgi?id=468573