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