author | Mikael Berthe <mikael@lilotux.net> |
Sun, 02 Jul 2006 22:54:18 +0200 | |
changeset 924 | a07bd351008b |
parent 898 | c65b71dcda94 |
child 930 | a75f7a13df7b |
permissions | -rw-r--r-- |
71 | 1 |
/* |
2 |
* hbuf.c -- History buffer implementation |
|
393 | 3 |
* |
699 | 4 |
* Copyright (C) 2005, 2006 Mikael Berthe <bmikael@lists.lilotux.net> |
71 | 5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
|
7 |
* it under the terms of the GNU General Public License as published by |
|
8 |
* the Free Software Foundation; either version 2 of the License, or (at |
|
9 |
* your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, but |
|
12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
* General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
|
19 |
* USA |
|
20 |
*/ |
|
21 |
||
22 |
#include <string.h> |
|
23 |
||
24 |
#include "hbuf.h" |
|
804
339c76ed5c0d
Remove a warning on Cygwin (implicit strcasestr() declaration)
Mikael Berthe <mikael@lilotux.net>
parents:
790
diff
changeset
|
25 |
#include "utils.h" |
71 | 26 |
|
27 |
||
28 |
/* This is a private structure type */ |
|
29 |
||
30 |
typedef struct { |
|
31 |
char *ptr; |
|
182 | 32 |
char *ptr_end; // beginning of the block |
33 |
char *ptr_end_alloc; // end of the current persistent block |
|
71 | 34 |
guchar flags; |
35 |
||
36 |
// XXX This should certainly be a pointer, and be allocated only when needed |
|
37 |
// (for ex. when HBB_FLAG_PERSISTENT is set). |
|
38 |
struct { // hbuf_line_info |
|
184 | 39 |
time_t timestamp; |
850
d0781ca2dd08
Use new status buffer window
Mikael Berthe <mikael@lilotux.net>
parents:
804
diff
changeset
|
40 |
guint flags; |
184 | 41 |
} prefix; |
71 | 42 |
} hbuf_block; |
43 |
||
44 |
||
184 | 45 |
// hbuf_add_line(p_hbuf, text, prefix_flags, width) |
71 | 46 |
// Add a line to the given buffer. If width is not null, then lines are |
47 |
// wrapped at this length. |
|
48 |
// |
|
49 |
// Note 1: Splitting according to width won't work if there are tabs; they |
|
50 |
// should be expanded before. |
|
51 |
// Note 2: width does not include the ending \0. |
|
184 | 52 |
void hbuf_add_line(GList **p_hbuf, const char *text, time_t timestamp, |
53 |
guint prefix_flags, guint width) |
|
71 | 54 |
{ |
55 |
GList *hbuf = *p_hbuf; |
|
56 |
char *line, *cr, *end; |
|
83 | 57 |
hbuf_block *hbuf_block_elt; |
71 | 58 |
|
59 |
if (!text) return; |
|
60 |
||
83 | 61 |
hbuf_block_elt = g_new0(hbuf_block, 1); |
184 | 62 |
hbuf_block_elt->prefix.timestamp = timestamp; |
63 |
hbuf_block_elt->prefix.flags = prefix_flags; |
|
71 | 64 |
if (!hbuf) { |
390 | 65 |
hbuf_block_elt->ptr = g_new(char, HBB_BLOCKSIZE); |
71 | 66 |
hbuf_block_elt->flags = HBB_FLAG_ALLOC | HBB_FLAG_PERSISTENT; |
182 | 67 |
hbuf_block_elt->ptr_end_alloc = hbuf_block_elt->ptr + HBB_BLOCKSIZE; |
71 | 68 |
*p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt); |
69 |
} else { |
|
502
3134b4960cdb
Fix mixed declaration and code
Mikael Berthe <mikael@lilotux.net>
parents:
464
diff
changeset
|
70 |
hbuf_block *hbuf_b_prev; |
392
6329c9601704
Speed up hbuf_add_line()
Mikael Berthe <mikael@lilotux.net>
parents:
390
diff
changeset
|
71 |
// Set p_hbuf to the end of the list, to speed up history loading |
6329c9601704
Speed up hbuf_add_line()
Mikael Berthe <mikael@lilotux.net>
parents:
390
diff
changeset
|
72 |
// (or CPU time will be used by g_list_last() for each line) |
6329c9601704
Speed up hbuf_add_line()
Mikael Berthe <mikael@lilotux.net>
parents:
390
diff
changeset
|
73 |
hbuf = *p_hbuf = g_list_last(*p_hbuf); |
502
3134b4960cdb
Fix mixed declaration and code
Mikael Berthe <mikael@lilotux.net>
parents:
464
diff
changeset
|
74 |
hbuf_b_prev = hbuf->data; |
71 | 75 |
hbuf_block_elt->ptr = hbuf_b_prev->ptr_end; |
76 |
hbuf_block_elt->flags = HBB_FLAG_PERSISTENT; |
|
182 | 77 |
hbuf_block_elt->ptr_end_alloc = hbuf_b_prev->ptr_end_alloc; |
790
5d4ad1d77da4
Kill a few gcc4 warnings
Mikael Berthe <mikael@lilotux.net>
parents:
733
diff
changeset
|
78 |
*p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt); |
71 | 79 |
} |
80 |
||
81 |
if (strlen(text) >= HBB_BLOCKSIZE) { |
|
82 |
// Too long |
|
83 |
text = "[ERR:LINE_TOO_LONG]"; |
|
197 | 84 |
hbuf_block_elt->prefix.flags |= HBB_PREFIX_INFO; |
71 | 85 |
} |
182 | 86 |
if (hbuf_block_elt->ptr + strlen(text) >= hbuf_block_elt->ptr_end_alloc) { |
71 | 87 |
// Too long for the current allocated bloc, we need another one |
390 | 88 |
hbuf_block_elt->ptr = g_new0(char, HBB_BLOCKSIZE); |
71 | 89 |
hbuf_block_elt->flags = HBB_FLAG_ALLOC | HBB_FLAG_PERSISTENT; |
182 | 90 |
hbuf_block_elt->ptr_end_alloc = hbuf_block_elt->ptr + HBB_BLOCKSIZE; |
71 | 91 |
} |
92 |
||
93 |
line = hbuf_block_elt->ptr; |
|
94 |
// Ok, now we can copy the text.. |
|
95 |
strcpy(line, text); |
|
96 |
hbuf_block_elt->ptr_end = line + strlen(line) + 1; |
|
97 |
end = hbuf_block_elt->ptr_end; |
|
98 |
||
99 |
// Let's add non-persistent blocs if necessary |
|
100 |
// - If there are '\n' in the string |
|
101 |
// - If length > width (and width != 0) |
|
102 |
cr = strchr(line, '\n'); |
|
103 |
while (cr || (width && strlen(line) > width)) { |
|
104 |
hbuf_block *hbuf_b_prev = hbuf_block_elt; |
|
105 |
||
106 |
if (!width || (cr && (cr - line <= (int)width))) { |
|
107 |
// Carriage return |
|
108 |
*cr = 0; |
|
109 |
hbuf_block_elt->ptr_end = cr; |
|
110 |
// Create another persistent block |
|
111 |
hbuf_block_elt = g_new0(hbuf_block, 1); |
|
112 |
hbuf_block_elt->ptr = hbuf_b_prev->ptr_end + 1; // == cr+1 |
|
113 |
hbuf_block_elt->ptr_end = end; |
|
114 |
hbuf_block_elt->flags = HBB_FLAG_PERSISTENT; |
|
182 | 115 |
hbuf_block_elt->ptr_end_alloc = hbuf_b_prev->ptr_end_alloc; |
790
5d4ad1d77da4
Kill a few gcc4 warnings
Mikael Berthe <mikael@lilotux.net>
parents:
733
diff
changeset
|
116 |
*p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt); |
71 | 117 |
line = hbuf_block_elt->ptr; |
118 |
} else { |
|
119 |
// We need to break where we can find a space char |
|
120 |
char *br; // break pointer |
|
121 |
for (br = line + width; br > line && *br != 32 && *br != 9; br--) |
|
122 |
; |
|
123 |
if (br <= line) |
|
124 |
br = line + width; |
|
125 |
else |
|
126 |
br++; |
|
127 |
hbuf_block_elt->ptr_end = br; |
|
128 |
// Create another block, non-persistent |
|
129 |
hbuf_block_elt = g_new0(hbuf_block, 1); |
|
130 |
hbuf_block_elt->ptr = hbuf_b_prev->ptr_end; // == br |
|
131 |
hbuf_block_elt->ptr_end = end; |
|
132 |
hbuf_block_elt->flags = 0; |
|
182 | 133 |
hbuf_block_elt->ptr_end_alloc = hbuf_b_prev->ptr_end_alloc; |
790
5d4ad1d77da4
Kill a few gcc4 warnings
Mikael Berthe <mikael@lilotux.net>
parents:
733
diff
changeset
|
134 |
*p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt); |
71 | 135 |
line = hbuf_block_elt->ptr; |
136 |
} |
|
137 |
cr = strchr(line, '\n'); |
|
138 |
} |
|
139 |
} |
|
140 |
||
141 |
// hbuf_free() |
|
142 |
// Destroys all hbuf list. |
|
143 |
void hbuf_free(GList **p_hbuf) |
|
144 |
{ |
|
145 |
hbuf_block *hbuf_b_elt; |
|
146 |
GList *hbuf_elt; |
|
147 |
GList *first_elt = g_list_first(*p_hbuf); |
|
148 |
||
149 |
for (hbuf_elt = first_elt; hbuf_elt; hbuf_elt = g_list_next(hbuf_elt)) { |
|
150 |
hbuf_b_elt = (hbuf_block*)(hbuf_elt->data); |
|
151 |
if (hbuf_b_elt->flags & HBB_FLAG_ALLOC) { |
|
152 |
g_free(hbuf_b_elt->ptr); |
|
153 |
} |
|
390 | 154 |
g_free(hbuf_b_elt); |
71 | 155 |
} |
156 |
||
893
92aaf2af786b
Fix a small memory leak in /buffer purge
Mikael Berthe <mikael@lilotux.net>
parents:
852
diff
changeset
|
157 |
g_list_free(first_elt); |
71 | 158 |
*p_hbuf = NULL; |
159 |
} |
|
160 |
||
161 |
// hbuf_rebuild() |
|
162 |
// Rebuild all hbuf list, with the new width. |
|
163 |
// If width == 0, lines are not wrapped. |
|
164 |
void hbuf_rebuild(GList **p_hbuf, unsigned int width) |
|
165 |
{ |
|
166 |
GList *first_elt, *curr_elt, *next_elt; |
|
167 |
hbuf_block *hbuf_b_curr, *hbuf_b_next; |
|
168 |
||
150 | 169 |
// *p_hbuf needs to be the head of the list |
170 |
first_elt = *p_hbuf = g_list_first(*p_hbuf); |
|
71 | 171 |
|
172 |
// #1 Remove non-persistent blocks (ptr_end should be updated!) |
|
173 |
curr_elt = first_elt; |
|
174 |
while (curr_elt) { |
|
175 |
next_elt = g_list_next(curr_elt); |
|
176 |
// Last element? |
|
177 |
if (!next_elt) |
|
178 |
break; |
|
179 |
hbuf_b_curr = (hbuf_block*)(curr_elt->data); |
|
180 |
hbuf_b_next = (hbuf_block*)(next_elt->data); |
|
181 |
// Is next line not-persistent? |
|
182 |
if (!(hbuf_b_next->flags & HBB_FLAG_PERSISTENT)) { |
|
183 |
hbuf_b_curr->ptr_end = hbuf_b_next->ptr_end; |
|
390 | 184 |
g_free(hbuf_b_next); |
790
5d4ad1d77da4
Kill a few gcc4 warnings
Mikael Berthe <mikael@lilotux.net>
parents:
733
diff
changeset
|
185 |
curr_elt = g_list_delete_link(curr_elt, next_elt); |
393 | 186 |
} else |
71 | 187 |
curr_elt = next_elt; |
188 |
} |
|
189 |
// #2 Go back to head and create non-persistent blocks when needed |
|
190 |
if (width) { |
|
191 |
char *line, *end; |
|
192 |
curr_elt = first_elt; |
|
193 |
||
194 |
while (curr_elt) { |
|
195 |
hbuf_b_curr = (hbuf_block*)(curr_elt->data); |
|
196 |
line = hbuf_b_curr->ptr; |
|
197 |
if (strlen(line) > width) { |
|
198 |
hbuf_block *hbuf_b_prev = hbuf_b_curr; |
|
199 |
||
200 |
// We need to break where we can find a space char |
|
201 |
char *br; // break pointer |
|
202 |
for (br = line + width; br > line && *br != 32 && *br != 9; br--) |
|
203 |
; |
|
204 |
if (br <= line) |
|
205 |
br = line + width; |
|
206 |
else |
|
207 |
br++; |
|
208 |
end = hbuf_b_curr->ptr_end; |
|
209 |
hbuf_b_curr->ptr_end = br; |
|
210 |
// Create another block, non-persistent |
|
211 |
hbuf_b_curr = g_new0(hbuf_block, 1); |
|
212 |
hbuf_b_curr->ptr = hbuf_b_prev->ptr_end; // == br |
|
213 |
hbuf_b_curr->ptr_end = end; |
|
214 |
hbuf_b_curr->flags = 0; |
|
182 | 215 |
hbuf_b_curr->ptr_end_alloc = hbuf_b_prev->ptr_end_alloc; |
150 | 216 |
// This is OK because insert_before(NULL) == append(): |
217 |
*p_hbuf = g_list_insert_before(*p_hbuf, curr_elt->next, hbuf_b_curr); |
|
71 | 218 |
} |
219 |
curr_elt = g_list_next(curr_elt); |
|
220 |
} |
|
221 |
} |
|
222 |
} |
|
223 |
||
189 | 224 |
// hbuf_previous_persistent() |
225 |
// Returns the previous persistent block (line). If the given line is |
|
226 |
// persistent, then it is returned. |
|
227 |
// This function is used for example when resizing a buffer. If the top of the |
|
228 |
// screen is on a non-persistent block, then a screen resize could destroy this |
|
229 |
// line... |
|
230 |
GList *hbuf_previous_persistent(GList *l_line) |
|
231 |
{ |
|
232 |
hbuf_block *hbuf_b_elt; |
|
233 |
||
234 |
while (l_line) { |
|
235 |
hbuf_b_elt = (hbuf_block*)l_line->data; |
|
236 |
if (hbuf_b_elt->flags & HBB_FLAG_PERSISTENT) |
|
237 |
return l_line; |
|
238 |
l_line = g_list_previous(l_line); |
|
239 |
} |
|
240 |
||
241 |
return NULL; |
|
242 |
} |
|
243 |
||
368
da50f08ea058
Update hbuf_get_lines() comment
Mikael Berthe <mikael@lilotux.net>
parents:
197
diff
changeset
|
244 |
// hbuf_get_lines(hbuf, n) |
370
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
245 |
// Returns an array of n hbb_line pointers |
71 | 246 |
// (The first line will be the line currently pointed by hbuf) |
898
c65b71dcda94
Fix memory leak in scr_UpdateWindow()
Mikael Berthe <mikael@lilotux.net>
parents:
893
diff
changeset
|
247 |
// Note: The caller should free the array, the hbb_line pointers and the |
c65b71dcda94
Fix memory leak in scr_UpdateWindow()
Mikael Berthe <mikael@lilotux.net>
parents:
893
diff
changeset
|
248 |
// text pointers after use. |
184 | 249 |
hbb_line **hbuf_get_lines(GList *hbuf, unsigned int n) |
71 | 250 |
{ |
251 |
unsigned int i; |
|
729
39f67cade02c
Use bold font for outgoing messages
Mikael Berthe <mikael@lilotux.net>
parents:
699
diff
changeset
|
252 |
hbuf_block *blk; |
39f67cade02c
Use bold font for outgoing messages
Mikael Berthe <mikael@lilotux.net>
parents:
699
diff
changeset
|
253 |
guchar last_persist_prefixflags = 0; |
733
c502bfe67c31
Fix highlighting of outgoing multi-line messages
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
254 |
GList *last_persist; // last persistent flags |
852
a3b8956db7bc
hbuf: minor fixes and typos
Mikael Berthe <mikael@lilotux.net>
parents:
850
diff
changeset
|
255 |
hbb_line **array, **array_elt; |
729
39f67cade02c
Use bold font for outgoing messages
Mikael Berthe <mikael@lilotux.net>
parents:
699
diff
changeset
|
256 |
|
733
c502bfe67c31
Fix highlighting of outgoing multi-line messages
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
257 |
// To be able to correctly highlight multi-line messages, |
c502bfe67c31
Fix highlighting of outgoing multi-line messages
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
258 |
// we need to look at the last non-null prefix, which should be the first |
c502bfe67c31
Fix highlighting of outgoing multi-line messages
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
259 |
// line of the message. |
729
39f67cade02c
Use bold font for outgoing messages
Mikael Berthe <mikael@lilotux.net>
parents:
699
diff
changeset
|
260 |
last_persist = hbuf_previous_persistent(hbuf); |
733
c502bfe67c31
Fix highlighting of outgoing multi-line messages
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
261 |
while (last_persist) { |
c502bfe67c31
Fix highlighting of outgoing multi-line messages
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
262 |
blk = (hbuf_block*)last_persist->data; |
c502bfe67c31
Fix highlighting of outgoing multi-line messages
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
263 |
if ((blk->flags & HBB_FLAG_PERSISTENT) && blk->prefix.flags) { |
c502bfe67c31
Fix highlighting of outgoing multi-line messages
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
264 |
last_persist_prefixflags = blk->prefix.flags; |
c502bfe67c31
Fix highlighting of outgoing multi-line messages
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
265 |
break; |
c502bfe67c31
Fix highlighting of outgoing multi-line messages
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
266 |
} |
c502bfe67c31
Fix highlighting of outgoing multi-line messages
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
267 |
last_persist = g_list_previous(last_persist); |
729
39f67cade02c
Use bold font for outgoing messages
Mikael Berthe <mikael@lilotux.net>
parents:
699
diff
changeset
|
268 |
} |
71 | 269 |
|
852
a3b8956db7bc
hbuf: minor fixes and typos
Mikael Berthe <mikael@lilotux.net>
parents:
850
diff
changeset
|
270 |
array = g_new0(hbb_line*, n); |
a3b8956db7bc
hbuf: minor fixes and typos
Mikael Berthe <mikael@lilotux.net>
parents:
850
diff
changeset
|
271 |
array_elt = array; |
71 | 272 |
|
852
a3b8956db7bc
hbuf: minor fixes and typos
Mikael Berthe <mikael@lilotux.net>
parents:
850
diff
changeset
|
273 |
for (i = 0 ; i < n ; i++) { |
71 | 274 |
if (hbuf) { |
275 |
int maxlen; |
|
729
39f67cade02c
Use bold font for outgoing messages
Mikael Berthe <mikael@lilotux.net>
parents:
699
diff
changeset
|
276 |
blk = (hbuf_block*)(hbuf->data); |
71 | 277 |
maxlen = blk->ptr_end - blk->ptr; |
184 | 278 |
*array_elt = (hbb_line*)g_new(hbb_line, 1); |
279 |
(*array_elt)->timestamp = blk->prefix.timestamp; |
|
280 |
(*array_elt)->flags = blk->prefix.flags; |
|
281 |
(*array_elt)->text = g_strndup(blk->ptr, maxlen); |
|
282 |
||
733
c502bfe67c31
Fix highlighting of outgoing multi-line messages
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
283 |
if ((blk->flags & HBB_FLAG_PERSISTENT) && blk->prefix.flags) { |
729
39f67cade02c
Use bold font for outgoing messages
Mikael Berthe <mikael@lilotux.net>
parents:
699
diff
changeset
|
284 |
last_persist_prefixflags = blk->prefix.flags; |
39f67cade02c
Use bold font for outgoing messages
Mikael Berthe <mikael@lilotux.net>
parents:
699
diff
changeset
|
285 |
} else { |
852
a3b8956db7bc
hbuf: minor fixes and typos
Mikael Berthe <mikael@lilotux.net>
parents:
850
diff
changeset
|
286 |
// Propagate highlighting flag |
729
39f67cade02c
Use bold font for outgoing messages
Mikael Berthe <mikael@lilotux.net>
parents:
699
diff
changeset
|
287 |
(*array_elt)->flags |= last_persist_prefixflags & HBB_PREFIX_HLIGHT; |
39f67cade02c
Use bold font for outgoing messages
Mikael Berthe <mikael@lilotux.net>
parents:
699
diff
changeset
|
288 |
} |
39f67cade02c
Use bold font for outgoing messages
Mikael Berthe <mikael@lilotux.net>
parents:
699
diff
changeset
|
289 |
|
71 | 290 |
hbuf = g_list_next(hbuf); |
291 |
} else |
|
184 | 292 |
break; |
293 |
||
294 |
array_elt++; |
|
71 | 295 |
} |
296 |
||
297 |
return array; |
|
298 |
} |
|
299 |
||
370
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
300 |
// hbuf_search(hbuf, direction, string) |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
301 |
// Look backward/forward for a line containing string in the history buffer |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
302 |
// Search starts at hbuf, and goes forward if direction == 1, backward if -1 |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
303 |
GList *hbuf_search(GList *hbuf, int direction, const char *string) |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
304 |
{ |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
305 |
hbuf_block *blk; |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
306 |
|
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
307 |
for (;;) { |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
308 |
if (direction > 0) |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
309 |
hbuf = g_list_next(hbuf); |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
310 |
else |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
311 |
hbuf = g_list_previous(hbuf); |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
312 |
|
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
313 |
if (!hbuf) break; |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
314 |
|
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
315 |
blk = (hbuf_block*)(hbuf->data); |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
316 |
// XXX blk->ptr is (maybe) not really correct, because the match should |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
317 |
// not be after ptr_end. We should check that... |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
318 |
if (strcasestr(blk->ptr, string)) |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
319 |
break; |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
320 |
} |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
321 |
|
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
322 |
return hbuf; |
dd9e2eb52916
Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents:
368
diff
changeset
|
323 |
} |
462 | 324 |
|
464 | 325 |
// hbuf_jump_date(hbuf, t) |
326 |
// Return a pointer to the first line after date t in the history buffer |
|
327 |
GList *hbuf_jump_date(GList *hbuf, time_t t) |
|
328 |
{ |
|
329 |
hbuf_block *blk; |
|
330 |
||
331 |
hbuf = g_list_first(hbuf); |
|
332 |
||
333 |
for ( ; hbuf && g_list_next(hbuf); hbuf = g_list_next(hbuf)) { |
|
334 |
blk = (hbuf_block*)(hbuf->data); |
|
335 |
if (blk->prefix.timestamp >= t) break; |
|
336 |
} |
|
337 |
||
338 |
return hbuf; |
|
339 |
} |
|
340 |
||
462 | 341 |
// hbuf_jump_percent(hbuf, pc) |
342 |
// Return a pointer to the line at % pc of the history buffer |
|
343 |
GList *hbuf_jump_percent(GList *hbuf, int pc) |
|
344 |
{ |
|
345 |
guint hlen; |
|
346 |
||
347 |
hbuf = g_list_first(hbuf); |
|
348 |
hlen = g_list_length(hbuf); |
|
349 |
||
350 |
return g_list_nth(hbuf, pc*hlen/100); |
|
351 |
} |
|
576 | 352 |
|
580 | 353 |
/* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |