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