Remember current view position when document reloaded
Please remember current view position when document reloaded rather than scroll to somewhere else.
keep view position (caret row and column, visible row to toolbar offset) is default, if it not works, please provide detail reproduce steps. Note if file content diffs a lot before caret line after reloading, the saved row and column may not points to same content.
Maybe, you have some code block before caret line collapsed before file reloading. after file reloading, all line are expanded, the view position no longer matches original.
keep view position (caret row and column, visible row to toolbar offset) is default, if it not works, please provide detail reproduce steps.
It is not working for me. Steps to reproduce,
- Open a long text file.
- Scroll down a couple of pages and keep the caret to some position like row 50, column 10.
- Close and reopen the file.
- Caret resets to 0, 0 positions.
Remember Recent Files is enabled. Using version v4.21.01r3540.
Add code to save these session statuses is easy, the hard problem is where to same them. My current though is to create sections in Notepad2.ini for each file using the full path name as section name, like the following:
[C:\Program Files\7-Zip\readme.txt]
WindowPosition = x, y, w, h ; issue #280.
; this is the view position, caret line offset to toolbar bottom.
ViewTopOffset = 1
; line number and optional column
CaretPosition = 2, 3
; collapsed lines retrieved by SciCall_ContractedFoldNext().
FoldedLines = 4, 5, 6, 7, 8
; some per-file settings, these can be stored in file variables (i.e. store in the file it self).
TabWidth = 4
IndentWidth = 4
InsertTabAsSpace = 1
; manually selected scheme. on reloading, if this value is not empty, use it.
Scheme = text
; manually selected encoding. on reloading, if the file can not be detected as Unicode, use this encoding instead of ANSI.
Encoding = utf-8
TabWidth, IndentWidth and InsertTabAsSpace should be changed to three levels: global default, scheme default and per-file specific, which would be an separate issue.
This looks promise, and is easy to added new settings, how ever, it has some problems:
- The length limitation of INI section name, it's not documented. since MAX_PATH is 260, this might not a problem.
- Characters allowed INI section name, it's not documented. it's obviously,
]will confuse INI parsing, since it used to indicate end of section name. so we need a function to convert path name into valid INIT section name.
A solution to fix these problems is to use path hash as section name, and number each section when there are collisions:
[deadbeff_0]
Path = C:\Program Files\7-Zip\readme.txt
[deadbeff_1]
Path = C:\Program Files\7-Zip\History.txt
then iterate each section and test whether path matches.
string sectionPrefix = ConvertToHex(SomeHashFunction(GetFullPath(currentPath)));
string[] sectionList = GetAllSectionNames();
for (int i = 0; i < sectionCount; i++) {
string sectionName = sectionList[i];
if (StringStartsWith(sectionName, sectionPrefix)) {
string path = GetSectionString(sectionName, "Path");
if (path == currentPath) {
// parse this section
break;
}
}
}
Any how, this is too complicated, any proposals and thoughts are welcome 🙏.
It may need a separate file to store such information. Sumatra PDF is an example.
SumatraPDF saves the file settings in the settings file, not in a separate file. Anyway, saving in a separate file will make it a little bit easier to manage and delete those settings. Saving them in the .ini file looks like a hacky way. Also, it can mess up the .ini file and will make it harder to backup if someone just wants to backup only settings.
SumatraPDF save all info in SumatraPDF-settings.txt, in the array FileStates.
FileStates [
[
FilePath = The path
Favorites [
]
IsPinned = false
IsMissing = true
OpenCount = 0
UseDefaultState = false
DisplayMode = continuous
ScrollPos = -1 101
PageNo = 430
Zoom = fit width
Rotation = 0
WindowState = 2
WindowPos = 434 0 715 770
ShowToc = false
SidebarDx = 150
DisplayR2L = false
ReparseIdx = 0
TocState = 77 109
]
]
but it use a custom settings file structure (just INI like), see https://www.sumatrapdfreader.org/settings/settings3.2.html
Save all session status in separate is fine, e.g. find & replace history should saved for each file.
SciTE has following session file (SciTE.session, in home/profile folder,see https://www.scintilla.org/SciTEDoc.html):
# SciTE session file
position.left=0
position.top=0
position.width=1176
position.height=689
position.maximize=0
mru.1.path=C:\Program Files\7-Zip\readme.txt
buffer.1.path=C:\Program Files\7-Zip\readme.txt
buffer.1.position=33316
buffer.1.scroll=987
buffer.1.current=1
Remember current view position when document reloaded
Mostly fixed by d38e3fa48a4b40ab4ba94fefbe37ce4f16df1e8e, please test latest builds.