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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
#include <HTUtils.h>
#include <HTHistory.h>
#include <LYLeaks.h>
static HTList * history; /* List of visited anchors */
/* Navigation
** ==========
*/
/* Record the jump to an anchor
** ----------------------------
*/
void HTHistory_record
ARGS1 (HTAnchor *,destination)
{
if (destination) {
if (! history)
history = HTList_new();
HTList_addObject (history, destination);
}
}
/* Go back in history (find the last visited node)
** ------------------
*/
HTAnchor * HTHistory_backtrack
NOARGS /* FIXME: Should we add a `sticky' option ? */
{
if (HTHistory_canBacktrack())
HTList_removeLastObject(history);
return(HTAnchor *)HTList_lastObject(history); /* is Home if can't backtrack */
}
BOOL HTHistory_canBacktrack
NOARGS
{
return (HTList_objectAt (history, 1) != NULL);
}
/* Browse through references in the same parent node
** -------------------------------------------------
**
** Take the n-th child's link after or before the one we took to get here.
** Positive offset means go towards most recently added children.
*/
HTAnchor * HTHistory_moveBy
ARGS1 (int,offset)
{
HTAnchor * last = (HTAnchor *)HTList_objectAt (history, 1);
if (! last)
return NULL; /* No last visited node */
if (last != (HTAnchor *) last->parent) { /* Was a child */
HTList * kids = last->parent->children;
int i = HTList_indexOf (kids, last);
HTAnchor * nextOne = (HTAnchor *)HTList_objectAt (kids, i - offset);
if (nextOne) {
HTAnchor * destination = HTAnchor_followMainLink (nextOne);
if (destination) {
HTList_removeLastObject (history);
HTList_removeLastObject (history);
HTList_addObject (history, nextOne);
HTList_addObject (history, destination);
}
return destination;
} else {
CTRACE(tfp, "HTHistory_moveBy: offset by %+d goes out of list %p.\n",
offset, (void*)kids);
return NULL;
}
} else { /* Was a parent */
return NULL; /* FIXME we could possibly follow the next link... */
}
}
BOOL HTHistory_canMoveBy
ARGS1 (int,offset)
{
HTAnchor * last = (HTAnchor *)HTList_objectAt (history, 1);
if (! last)
return NO; /* No last visited node */
if (last != (HTAnchor *) last->parent) { /* Was a child */
HTList * kids = last->parent->children;
int i = HTList_indexOf (kids, last);
return (HTList_objectAt (kids, i - offset) != NULL);
} else { /* Was a parent */
return NO; /* FIXME we could possibly follow the next link... */
}
}
/* Retrieval
** =========
*/
/* Read numbered visited anchor (1 is the oldest)
** ----------------------------
*/
HTAnchor * HTHistory_read
ARGS1 (int,number)
{
return (HTAnchor *)HTList_objectAt(history, HTList_count (history) - number);
}
/* Recall numbered visited anchor (1 is the oldest)
** ------------------------------
** This reads the anchor and stores it again in the list, except if last.
*/
HTAnchor * HTHistory_recall
ARGS1 (int,number)
{
HTAnchor * destination =
(HTAnchor *)HTList_objectAt (history, HTList_count (history) - number);
if (destination && destination != (HTAnchor *)HTList_lastObject (history))
HTList_addObject (history, destination);
return destination;
}
/* Number of Anchors stored
** ------------------------
**
** This is needed in order to check the validity of certain commands
** for menus, etc.
(not needed for now. Use canBacktrack, etc.)
int HTHistory_count
NOARGS
{
return HTList_count (history);
}
*/
/* Change last history entry
** -------------------------
**
** Sometimes we load a node by one anchor but leave by a different
** one, and it is the one we left from which we want to remember.
*/
void HTHistory_leavingFrom
ARGS1 (HTAnchor *,anchor)
{
if (HTList_removeLastObject (history)) {
HTList_addObject (history, anchor);
} else {
CTRACE(tfp, "HTHistory_leavingFrom: empty history !\n");
}
}
|