Bug 462112 - Restore unsaved (newly opened - "Untitled") document doesn't work when Plasma session is restored
Summary: Restore unsaved (newly opened - "Untitled") document doesn't work when Plasma...
Status: CONFIRMED
Alias: None
Product: kate
Classification: Applications
Component: sessions (show other bugs)
Version: unspecified
Platform: Neon Linux
: NOR normal
Target Milestone: ---
Assignee: KWrite Developers
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-11-21 23:28 UTC by Piotr Mierzwinski
Modified: 2024-05-11 02:02 UTC (History)
11 users (show)

See Also:
Latest Commit:
Version Fixed In:


Attachments
kate_session_configuration-page (35.92 KB, image/png)
2022-11-21 23:28 UTC, Piotr Mierzwinski
Details

Note You need to log in before you can comment on or make changes to this bug.
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.