/*******************************************************************************
* Copyright (c) 2013-2021, Andrés Martinelli <andmarti@gmail.com> *
* All rights reserved. *
* *
* This file is a part of sc-im *
* *
* sc-im is a spreadsheet program that is based on sc. The original authors *
* of sc are James Gosling and Mark Weiser, and mods were later added by *
* Chuck Martin. *
* *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted provided that the following conditions are met: *
* 1. Redistributions of source code must retain the above copyright *
* notice, this list of conditions and the following disclaimer. *
* 2. Redistributions in binary form must reproduce the above copyright *
* notice, this list of conditions and the following disclaimer in the *
* documentation and/or other materials provided with the distribution. *
* 3. All advertising materials mentioning features or use of this software *
* must display the following acknowledgement: *
* This product includes software developed by Andrés Martinelli *
* <andmarti@gmail.com>. *
* 4. Neither the name of the Andrés Martinelli nor the *
* names of other contributors may be used to endorse or promote products *
* derived from this software without specific prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY ANDRES MARTINELLI ''AS IS'' AND ANY *
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED *
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE *
* DISCLAIMED. IN NO EVENT SHALL ANDRES MARTINELLI BE LIABLE FOR ANY *
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;*
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND *
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE *
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
*******************************************************************************/
/**
* \file undo.c
* \author Andrés Martinelli <andmarti@gmail.com>
* \date 06/06/2021
* \brief This file contains the main functions to support the undo/redo feature.
*/
/*
* UNDO and REDO feature works with an 'undo' struct list.
* Which contains:
* p_ant: pointer to 'undo' struct. If NULL, this node is the first change
* for the session.
*
* struct ent_ptr * added: 'ent' elements added by the change
*
* struct ent_ptr * removed: 'ent' elements removed by the change
*
* struct allocation_list * allocations: since we alloc over added and removed
* list in batches. we need to keep the first position in memory of each calloc.
*
* struct undo_range_shift * range_shift: range shifted by change
*
* row_hidded: integers list (int *) hidden rows on screen
*
* row_showed: integers list (int *) visible rows on screen
*
* row_frozed: integers list (int *) frozen rows on screen
*
* row_unfrozed: integers list (int *) unfrozen rows on screen
*
* col_hidded: integers list (int *) hidden columns on screen
*
* col_showed: integers list (int *) visible columns on screen
* NOTE: the first position of the lists contains (number of elements - 1) in the list
*
* col_frozed: integers list (int *) frozen cols on screen
*
* col_unfrozed: integers list (int *) unfrozen cols on screen
*
* struct undo_cols_format * cols_format: list of 'undo_col_info' elements used for
* undoing / redoing changes in columns format (fwidth, precision y realfmt)
*
* struct undo_rows_format * rows_format: list of 'undo_row_info' elements used for
* undoing / redoing changes in rows format (height)
*
* p_sig: pointer to 'undo' struct, If NULL, this node is the last change in
* the session.
*
* modflg_bef: document modflg before the change
* maxrow_bef: sheet maxrow before the change
* maxcol_bef: sheet maxcol before the change
* modflg_aft: document modflg after the change
* maxrow_aft: sheet maxrow after the change
* maxcol_aft: sheet maxcol after the change
*
* Follows one level UNDO/REDO scheme. A change (C1) is made, then an UNDO operation, and
* another change (C2). From there later changes are removed.
* Scheme:
*
* + C1 -> + -> UNDO -
* ^ \
* |_ |
* \---------------/
* |
* |
* |
* \-> C2 --> + ...
*
* undo_shift_range struct contains:
* int delta_rows: delta rows for the range shift
* int delta_cols: delta columns for the range shift
* int tlrow: Upper left row defining the range of the shift
* (As if a cell range shift is made)
* int tlcol: Upper left column defining the range of the shift
* (As if a cell range shift is made)
* int brrow: Lower right row defining the range of the shift
* (As if a cell range shift is made)
* int brcol: Lower right column defining the range of the shift
* (As if a cell range shift is made)
*
* Implemented actions for UNDO/REDO:
* 1. Remove content from cell or range
* 2. Input content into a cell
* 3. Edit a cell
* 4. Change alginment of range or cell
* 5. Paste range or cell
* 6. Shift range or cell with sh, sj, sk, sl
* 7. Insert row or column
* 8. Delete row or column
* 9. Paste row or column
* 10. Hide/show rows and columns
* 11. Sort of a range
* 12. Change in the format of a range or cell
* 13. '-' and '+' commands in normal mode
* 14. Lock and unlock of cells
* 15. datefmt command
* 16. the cellcolor command
* 17. Change in format of a column as a result of the 'f' command
* 18. Change in format of a column as a result of auto_jus
* 19. Change format of columns as a result of ic dc