aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/marker.c70
1 files changed, 66 insertions, 4 deletions
diff --git a/src/marker.c b/src/marker.c
index 866dd6ac9c0..dcdadd705ad 100644
--- a/src/marker.c
+++ b/src/marker.c
@@ -32,6 +32,10 @@ static int cached_bytepos;
32static struct buffer *cached_buffer; 32static struct buffer *cached_buffer;
33static int cached_modiff; 33static int cached_modiff;
34 34
35/* Nonzero means enable debugging checks on byte/char correspondences. */
36
37static int byte_debug_flag;
38
35clear_charpos_cache (b) 39clear_charpos_cache (b)
36 struct buffer *b; 40 struct buffer *b;
37{ 41{
@@ -58,7 +62,12 @@ clear_charpos_cache (b)
58 int changed = 0; \ 62 int changed = 0; \
59 \ 63 \
60 if (this_charpos == charpos) \ 64 if (this_charpos == charpos) \
61 return (BYTEPOS); \ 65 { \
66 int value = (BYTEPOS); \
67 if (byte_debug_flag) \
68 byte_char_debug_check (b, charpos, value); \
69 return value; \
70 } \
62 else if (this_charpos > charpos) \ 71 else if (this_charpos > charpos) \
63 { \ 72 { \
64 if (this_charpos < best_above) \ 73 if (this_charpos < best_above) \
@@ -78,11 +87,37 @@ clear_charpos_cache (b)
78 if (changed) \ 87 if (changed) \
79 { \ 88 { \
80 if (best_above - best_below == best_above_byte - best_below_byte) \ 89 if (best_above - best_below == best_above_byte - best_below_byte) \
81 return best_below_byte + (charpos - best_below); \ 90 { \
91 int value = best_below_byte + (charpos - best_below); \
92 if (byte_debug_flag) \
93 byte_char_debug_check (b, charpos, value); \
94 return value; \
95 } \
82 } \ 96 } \
83} 97}
84 98
85int 99int
100byte_char_debug_check (b, charpos, bytepos)
101 struct buffer *b;
102 int charpos, bytepos;
103{
104 int nchars = 0;
105
106 if (bytepos > BUF_GPT_BYTE (b))
107 {
108 nchars = chars_in_text (BUF_BEG_ADDR (b),
109 BUF_GPT_BYTE (b) - BUF_BEG_BYTE (b));
110 nchars += chars_in_text (BUF_GAP_END_ADDR (b),
111 bytepos - BUF_GPT_BYTE (b));
112 }
113 else
114 nchars = chars_in_text (BUF_BEG_ADDR (b), bytepos - BUF_BEG_BYTE (b));
115
116 if (charpos - 1 != nchars)
117 abort ();
118}
119
120int
86charpos_to_bytepos (charpos) 121charpos_to_bytepos (charpos)
87 int charpos; 122 int charpos;
88{ 123{
@@ -169,6 +204,9 @@ buf_charpos_to_bytepos (b, charpos)
169 set_marker_both (marker, Qnil, best_below, best_below_byte); 204 set_marker_both (marker, Qnil, best_below, best_below_byte);
170 } 205 }
171 206
207 if (byte_debug_flag)
208 byte_char_debug_check (b, charpos, best_below_byte);
209
172 cached_buffer = b; 210 cached_buffer = b;
173 cached_modiff = BUF_MODIFF (b); 211 cached_modiff = BUF_MODIFF (b);
174 cached_charpos = best_below; 212 cached_charpos = best_below;
@@ -196,6 +234,9 @@ buf_charpos_to_bytepos (b, charpos)
196 set_marker_both (marker, Qnil, best_above, best_above_byte); 234 set_marker_both (marker, Qnil, best_above, best_above_byte);
197 } 235 }
198 236
237 if (byte_debug_flag)
238 byte_char_debug_check (b, charpos, best_above_byte);
239
199 cached_buffer = b; 240 cached_buffer = b;
200 cached_modiff = BUF_MODIFF (b); 241 cached_modiff = BUF_MODIFF (b);
201 cached_charpos = best_above; 242 cached_charpos = best_above;
@@ -218,7 +259,12 @@ buf_charpos_to_bytepos (b, charpos)
218 int changed = 0; \ 259 int changed = 0; \
219 \ 260 \
220 if (this_bytepos == bytepos) \ 261 if (this_bytepos == bytepos) \
221 return (CHARPOS); \ 262 { \
263 int value = (CHARPOS); \
264 if (byte_debug_flag) \
265 byte_char_debug_check (b, value, bytepos); \
266 return value; \
267 } \
222 else if (this_bytepos > bytepos) \ 268 else if (this_bytepos > bytepos) \
223 { \ 269 { \
224 if (this_bytepos < best_above_byte) \ 270 if (this_bytepos < best_above_byte) \
@@ -238,7 +284,12 @@ buf_charpos_to_bytepos (b, charpos)
238 if (changed) \ 284 if (changed) \
239 { \ 285 { \
240 if (best_above - best_below == best_above_byte - best_below_byte) \ 286 if (best_above - best_below == best_above_byte - best_below_byte) \
241 return best_below + (bytepos - best_below_byte); \ 287 { \
288 int value = best_below + (bytepos - best_below_byte); \
289 if (byte_debug_flag) \
290 byte_char_debug_check (b, value, bytepos); \
291 return value; \
292 } \
242 } \ 293 } \
243} 294}
244 295
@@ -319,6 +370,9 @@ buf_bytepos_to_charpos (b, bytepos)
319 set_marker_both (marker, Qnil, best_below, best_below_byte); 370 set_marker_both (marker, Qnil, best_below, best_below_byte);
320 } 371 }
321 372
373 if (byte_debug_flag)
374 byte_char_debug_check (b, best_below, bytepos);
375
322 cached_buffer = b; 376 cached_buffer = b;
323 cached_modiff = BUF_MODIFF (b); 377 cached_modiff = BUF_MODIFF (b);
324 cached_charpos = best_below; 378 cached_charpos = best_below;
@@ -346,6 +400,9 @@ buf_bytepos_to_charpos (b, bytepos)
346 set_marker_both (marker, Qnil, best_above, best_above_byte); 400 set_marker_both (marker, Qnil, best_above, best_above_byte);
347 } 401 }
348 402
403 if (byte_debug_flag)
404 byte_char_debug_check (b, best_above, bytepos);
405
349 cached_buffer = b; 406 cached_buffer = b;
350 cached_modiff = BUF_MODIFF (b); 407 cached_modiff = BUF_MODIFF (b);
351 cached_charpos = best_above; 408 cached_charpos = best_above;
@@ -838,4 +895,9 @@ syms_of_marker ()
838 defsubr (&Smarker_insertion_type); 895 defsubr (&Smarker_insertion_type);
839 defsubr (&Sset_marker_insertion_type); 896 defsubr (&Sset_marker_insertion_type);
840 defsubr (&Sbuffer_has_markers_at); 897 defsubr (&Sbuffer_has_markers_at);
898
899 DEFVAR_BOOL ("byte-debug-flag", &byte_debug_flag,
900 "Non-nil enables debugging checks in byte/char position conversions.");
901 byte_debug_flag = 0;
902
841} 903}