/* Markers: examining, setting and deleting.
- Copyright (C) 1985, 1997 Free Software Foundation, Inc.
+ Copyright (C) 1985, 1997, 1998 Free Software Foundation, Inc.
This file is part of GNU Emacs.
static struct buffer *cached_buffer;
static int cached_modiff;
+static void byte_char_debug_check P_ ((struct buffer *, int, int));
+
+/* Nonzero means enable debugging checks on byte/char correspondences. */
+
+static int byte_debug_flag;
+
+void
clear_charpos_cache (b)
struct buffer *b;
{
int changed = 0; \
\
if (this_charpos == charpos) \
- return (BYTEPOS); \
+ { \
+ int value = (BYTEPOS); \
+ if (byte_debug_flag) \
+ byte_char_debug_check (b, charpos, value); \
+ return value; \
+ } \
else if (this_charpos > charpos) \
{ \
if (this_charpos < best_above) \
if (changed) \
{ \
if (best_above - best_below == best_above_byte - best_below_byte) \
- return best_below_byte + (charpos - best_below); \
+ { \
+ int value = best_below_byte + (charpos - best_below); \
+ if (byte_debug_flag) \
+ byte_char_debug_check (b, charpos, value); \
+ return value; \
+ } \
} \
}
+static void
+byte_char_debug_check (b, charpos, bytepos)
+ struct buffer *b;
+ int charpos, bytepos;
+{
+ int nchars = 0;
+
+ if (bytepos > BUF_GPT_BYTE (b))
+ {
+ nchars = multibyte_chars_in_text (BUF_BEG_ADDR (b),
+ BUF_GPT_BYTE (b) - BUF_BEG_BYTE (b));
+ nchars += multibyte_chars_in_text (BUF_GAP_END_ADDR (b),
+ bytepos - BUF_GPT_BYTE (b));
+ }
+ else
+ nchars = multibyte_chars_in_text (BUF_BEG_ADDR (b),
+ bytepos - BUF_BEG_BYTE (b));
+
+ if (charpos - 1 != nchars)
+ abort ();
+}
+
int
charpos_to_bytepos (charpos)
int charpos;
int charpos;
{
Lisp_Object tail;
- int gapend_byte = BUF_GPT_BYTE (b) + BUF_GAP_SIZE (b);
int best_above, best_above_byte;
int best_below, best_below_byte;
CONSIDER (cached_charpos, cached_bytepos);
tail = BUF_MARKERS (b);
- while (XSYMBOL (tail) != XSYMBOL (Qnil))
+ while (! NILP (tail))
{
CONSIDER (XMARKER (tail)->charpos, XMARKER (tail)->bytepos);
It will last until the next GC. */
if (record)
{
- Lisp_Object marker;
+ Lisp_Object marker, buffer;
marker = Fmake_marker ();
- set_marker_both (marker, Qnil, best_below, best_below_byte);
+ XSETBUFFER (buffer, b);
+ set_marker_both (marker, buffer, best_below, best_below_byte);
}
+ if (byte_debug_flag)
+ byte_char_debug_check (b, charpos, best_below_byte);
+
cached_buffer = b;
cached_modiff = BUF_MODIFF (b);
cached_charpos = best_below;
It will last until the next GC. */
if (record)
{
- Lisp_Object marker;
+ Lisp_Object marker, buffer;
marker = Fmake_marker ();
- set_marker_both (marker, Qnil, best_above, best_above_byte);
+ XSETBUFFER (buffer, b);
+ set_marker_both (marker, buffer, best_above, best_above_byte);
}
+ if (byte_debug_flag)
+ byte_char_debug_check (b, charpos, best_above_byte);
+
cached_buffer = b;
cached_modiff = BUF_MODIFF (b);
cached_charpos = best_above;
int changed = 0; \
\
if (this_bytepos == bytepos) \
- return (CHARPOS); \
+ { \
+ int value = (CHARPOS); \
+ if (byte_debug_flag) \
+ byte_char_debug_check (b, value, bytepos); \
+ return value; \
+ } \
else if (this_bytepos > bytepos) \
{ \
if (this_bytepos < best_above_byte) \
if (changed) \
{ \
if (best_above - best_below == best_above_byte - best_below_byte) \
- return best_below + (bytepos - best_below_byte); \
+ { \
+ int value = best_below + (bytepos - best_below_byte); \
+ if (byte_debug_flag) \
+ byte_char_debug_check (b, value, bytepos); \
+ return value; \
+ } \
} \
}
CONSIDER (cached_bytepos, cached_charpos);
tail = BUF_MARKERS (b);
- while (XSYMBOL (tail) != XSYMBOL (Qnil))
+ while (! NILP (tail))
{
CONSIDER (XMARKER (tail)->bytepos, XMARKER (tail)->charpos);
if (bytepos - best_below_byte < best_above_byte - bytepos)
{
- int record = best_above_byte - bytepos > 5000;
+ int record = bytepos - best_below_byte > 5000;
while (best_below_byte < bytepos)
{
/* If this position is quite far from the nearest known position,
cache the correspondence by creating a marker here.
- It will last until the next GC. */
- if (record)
+ It will last until the next GC.
+ But don't do it if BUF_MARKERS is nil;
+ that is a signal from Fset_buffer_multibyte. */
+ if (record && ! NILP (BUF_MARKERS (b)))
{
- Lisp_Object marker;
+ Lisp_Object marker, buffer;
marker = Fmake_marker ();
- set_marker_both (marker, Qnil, best_below, best_below_byte);
+ XSETBUFFER (buffer, b);
+ set_marker_both (marker, buffer, best_below, best_below_byte);
}
+ if (byte_debug_flag)
+ byte_char_debug_check (b, best_below, bytepos);
+
cached_buffer = b;
cached_modiff = BUF_MODIFF (b);
cached_charpos = best_below;
/* If this position is quite far from the nearest known position,
cache the correspondence by creating a marker here.
- It will last until the next GC. */
- if (record)
+ It will last until the next GC.
+ But don't do it if BUF_MARKERS is nil;
+ that is a signal from Fset_buffer_multibyte. */
+ if (record && ! NILP (BUF_MARKERS (b)))
{
- Lisp_Object marker;
+ Lisp_Object marker, buffer;
marker = Fmake_marker ();
- set_marker_both (marker, Qnil, best_above, best_above_byte);
+ XSETBUFFER (buffer, b);
+ set_marker_both (marker, buffer, best_above, best_above_byte);
}
+ if (byte_debug_flag)
+ byte_char_debug_check (b, best_above, bytepos);
+
cached_buffer = b;
cached_modiff = BUF_MODIFF (b);
cached_charpos = best_above;
register struct Lisp_Marker *m;
CHECK_MARKER (marker, 0);
- /* If position is nil or a marker that points nowhere,
- make this marker point nowhere. */
- if (NILP (charpos)
- || (MARKERP (charpos) && !XMARKER (charpos)->buffer))
- {
- unchain_marker (marker);
- return marker;
- }
- CHECK_NUMBER_COERCE_MARKER (charpos, 1);
if (NILP (buffer))
b = current_buffer;
else
if (EQ (b->name, Qnil))
abort ();
+ XMARKER (marker)->buffer = 0;
+
tail = BUF_MARKERS (b);
prev = Qnil;
- while (XSYMBOL (tail) != XSYMBOL (Qnil))
+ while (! GC_NILP (tail))
{
next = XMARKER (tail)->chain;
XUNMARK (next);
XMARKER (prev)->chain = next;
XSETMARKBIT (XMARKER (prev)->chain, omark);
}
- break;
+ /* We have removed the marker from the chain;
+ no need to scan the rest of the chain. */
+ return;
}
else
prev = tail;
tail = next;
}
- XMARKER (marker)->buffer = 0;
+
+ /* Marker was not in its chain. */
+ abort ();
}
/* Return the char position of marker MARKER, as a C integer. */
{
register Lisp_Object new;
- if (INTEGERP (marker) || MARKERP (marker))
- {
- new = Fmake_marker ();
- Fset_marker (new, marker,
- (MARKERP (marker) ? Fmarker_buffer (marker) : Qnil));
- XMARKER (new)->insertion_type = !NILP (type);
- return new;
- }
- else
+ if (! (INTEGERP (marker) || MARKERP (marker)))
marker = wrong_type_argument (Qinteger_or_marker_p, marker);
+
+ new = Fmake_marker ();
+ Fset_marker (new, marker,
+ (MARKERP (marker) ? Fmarker_buffer (marker) : Qnil));
+ XMARKER (new)->insertion_type = !NILP (type);
+ return new;
}
DEFUN ("marker-insertion-type", Fmarker_insertion_type,
charno = Z;
for (tail = BUF_MARKERS (current_buffer);
- XSYMBOL (tail) != XSYMBOL (Qnil);
+ !NILP (tail);
tail = XMARKER (tail)->chain)
if (XMARKER (tail)->charpos == charno)
return Qt;
defsubr (&Smarker_insertion_type);
defsubr (&Sset_marker_insertion_type);
defsubr (&Sbuffer_has_markers_at);
+
+ DEFVAR_BOOL ("byte-debug-flag", &byte_debug_flag,
+ "Non-nil enables debugging checks in byte/char position conversions.");
+ byte_debug_flag = 0;
+
}