diff options
author | Kaz Kyheku <kaz@kylheku.com> | 2020-02-18 06:40:15 -0800 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2020-02-18 06:40:15 -0800 |
commit | e41669c53b57fa19f614e22a328fdf181d9c8f46 (patch) | |
tree | ab774f89fafa02a26ae4cacddde504fa85962fbd /txr.1 | |
parent | 3080fb5f28e0337b5859f4d6eb19bda9207a65fa (diff) | |
download | txr-e41669c53b57fa19f614e22a328fdf181d9c8f46.tar.gz txr-e41669c53b57fa19f614e22a328fdf181d9c8f46.tar.bz2 txr-e41669c53b57fa19f614e22a328fdf181d9c8f46.zip |
listener: append to .txr_history instead of clobbering.
This patch addresses the problem of history loss that
occurs when a user juggles multiple TXR sessions that
all clobber the same history file.
* linenoise/linenoise.c (struct lino_state): New member,
loaded_lines, keeping track of how many of the lines in the
history came from loading the history file. Lines between
[0] and [loaded_lines - 1] are loaded lines. New lines
occur between [loaded_lines] and [history_len - 1].
(lino_hist_add): Reset loaded_lines to zero when creating
history for the first time. Not really necessary since the
structure starts zero-filled. When a line of history is
erased, then it must be a loaded line, unless loaded_lines
is zero. Thus, then decrement loaded_lines to account for a
loss of a loaded line, but don't decrement below zero.
(lino_hist_set_max_len): Setting the max length can cause
history to be trimmed, so we must adjust loaded_lines to
account for any loaded lines that get discarded.
(lino_hist_save): Takes a new parameter which indicates
whether to just save the new history by appending it to the
given file, or to overwrite the file with the entire history.
In either case, once we save the history, we assume that all
of our lines are loaded lines and set loaded_lines to
hist_len. In the future, this last step will help implement
incremental saving mid-way through a sesssion.
(lino_hist_load): Error out if there is already a history.
With this loaded_lines logic, it really wouldn't make sense to
read history more than once. After loading, set loaded_lines
to hist_len.
* linenoise/linenoise.h (enum lino_file_mode): New enumeration
lino_append.
(lino_hist_save): Declaration updated.
* parser.c (repl): Implement new history saving protocol.
The history file is read using a temporary instance of
linenoise, which has the effect of trimming it to the required
number of lines. This is written to a temporary file, to which
the newly entered lines are appended, and which is finally
renamed to replace the history file.
(lino_mode_str): Add "a" entry corresponding to lino_append.
(lino_open): Do the fchmod in the lino_append case also.
* txr.1: Documented the new handling of the history file.
Diffstat (limited to 'txr.1')
-rw-r--r-- | txr.1 | 27 |
1 files changed, 25 insertions, 2 deletions
@@ -72025,9 +72025,32 @@ option isn't present. The history is maintained in a text file called .code .txr_history in the user's home directory. Whenever the interactive listener terminates, -this file is overwritten with the history contents stored in the listener's +this file is updated with the history contents stored in the listener's memory. The next time the listener starts, it first re-loads the history from -this file, making the commands of a previous session available for recall. +this file, making the most recent +.code *listener-hist-len* +expressions of a previous session available for recall. + +The history file is maintained in a way that is somewhat +robust against the loss of history arising from the situation that a user +manages multiple simultaneous \*(TX sessions. When a session terminates, it +doesn't blindly overwrite the history file, which may have already been updated +with new history produced by another session. Rather, it appends new entries +to the history file. New entries are those that had not been previously read +from the history file, but have been newly entered into the listener. + +An effort is made to keep the history file trimmed to no more than +twice the number of entries specified in +.codn *listener-hist-len* . +The terminating session first makes a temporary copy of the existing +history, which is trimmed to the most recent +.code *listener-hist-len* +entries. New entries are then appended to this temporary file. +Finally, the actual history file is replaced with this temporary file by a +.code rename-path +a rename operation. This algorithm doesn't use locking, and is therefore not +robust against the situation when a two or more multiple interactive \*(TX +sessions belonging to the same user terminate at around the same time. The home directory is determined from the contents of the |