-
Notifications
You must be signed in to change notification settings - Fork 7
/
state.c
91 lines (78 loc) · 2.08 KB
/
state.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* state.c -- a simplistic undo/redo kind of system
*
* Copyright (C) 2003 Øyvind Kolås <pippin@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59
* Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "tree.h"
#include "file.h"
#include "prefs.h"
#include "ui_cli.h"
static Node *savedtree = NULL;
static void* save_state_cmd (int argc, char **argv, void *data)
{
Node *pos = (Node *) data;
Node *i;
Node *j;
if (savedtree != NULL) {
tree_free (savedtree);
}
savedtree = node_new ();
i = node_root (pos);
j = savedtree;
do {
j = savedtree = tree_duplicate (i, j);
i = node_down (i);
j = node_insert_down (j);
} while (i != NULL);
j = node_remove (j);
{
int no;
no = node_no (pos);
savedtree = node_root (savedtree);
while (--no)
savedtree = node_recurse (savedtree);
}
return pos;
}
static void* restore_state_cmd (int argc, char **argv, void *data)
{
Node *pos = (Node *) data;
if (savedtree != NULL) {
Node *temp;
temp = pos;
pos = savedtree;
savedtree = temp;
tree_free (savedtree);
savedtree = NULL;
}
return pos;
}
/*
!init_keepstate();
*/
void init_keepstate ()
{
cli_add_command ("save_state", save_state_cmd, "");
cli_add_help ("save_state",
"Saves a copy of the current tree and selected node in memory.");
cli_add_command ("restore_state", restore_state_cmd, "");
cli_add_help ("restore_state",
"Restores the copy of the tree saved with save_state.");
}