aboutsummaryrefslogtreecommitdiffstats
path: root/src/marker.c
diff options
context:
space:
mode:
authorDmitry Antipov <[email protected]>2012-07-22 09:37:24 +0400
committerDmitry Antipov <[email protected]>2012-07-22 09:37:24 +0400
commitd36d71df9c550e69d8a469139714a188f221aea8 (patch)
tree0e8aaf407ee05e20925765a434bbbcc99d8a9aa9 /src/marker.c
parent894bd8732db202ab5a2dd43cd0e949289b20c3f5 (diff)
Simplify and cleanup markers positioning code.
* marker.c (attach_marker): More useful eassert. (live_buffer, set_marker_internal): New function. (Fset_marker, set_marker_restricted): Use set_marker_internal. (set_marker_both, set_marker_restricted_both): Use live_buffer.
Diffstat (limited to 'src/marker.c')
-rw-r--r--src/marker.c215
1 files changed, 82 insertions, 133 deletions
diff --git a/src/marker.c b/src/marker.c
index 72f48a2d7b..0a93f4c180 100644
--- a/src/marker.c
+++ b/src/marker.c
@@ -431,8 +431,12 @@ static inline void
attach_marker (struct Lisp_Marker *m, struct buffer *b,
ptrdiff_t charpos, ptrdiff_t bytepos)
{
- /* Every character is at least one byte. */
- eassert (charpos <= bytepos);
+ /* In a single-byte buffer, two positions must be equal.
+ Otherwise, every character is at least one byte. */
+ if (BUF_Z (b) == BUF_Z_BYTE (b))
+ eassert (charpos == bytepos);
+ else
+ eassert (charpos <= bytepos);
m->charpos = charpos;
m->bytepos = bytepos;
@@ -446,191 +450,136 @@ attach_marker (struct Lisp_Marker *m, struct buffer *b,
}
}
-DEFUN ("set-marker", Fset_marker, Sset_marker, 2, 3, 0,
- doc: /* Position MARKER before character number POSITION in BUFFER.
-BUFFER defaults to the current buffer.
-If POSITION is nil, makes marker point nowhere.
-Then it no longer slows down editing in any buffer.
-Returns MARKER. */)
- (Lisp_Object marker, Lisp_Object position, Lisp_Object buffer)
-{
- register ptrdiff_t charpos;
- register ptrdiff_t bytepos;
- register struct buffer *b;
- register struct Lisp_Marker *m;
+/* If BUFFER is nil, return current buffer pointer. Next, check
+ whether BUFFER is a buffer object and return buffer pointer
+ corresponding to BUFFER if BUFFER is live, or NULL otherwise. */
- CHECK_MARKER (marker);
- m = XMARKER (marker);
+static inline struct buffer *
+live_buffer (Lisp_Object buffer)
+{
+ struct buffer *b;
- /* If position is nil or a marker that points nowhere,
- make this marker point nowhere. */
- if (NILP (position)
- || (MARKERP (position) && !XMARKER (position)->buffer))
+ if (NILP (buffer))
{
- unchain_marker (m);
- return marker;
+ b = current_buffer;
+ eassert (!NILP (BVAR (b, name)));
}
-
- if (NILP (buffer))
- b = current_buffer;
else
{
CHECK_BUFFER (buffer);
b = XBUFFER (buffer);
- /* If buffer is dead, set marker to point nowhere. */
- if (EQ (BVAR (b, name), Qnil))
- {
- unchain_marker (m);
- return marker;
- }
+ if (NILP (BVAR (b, name)))
+ b = NULL;
}
-
- /* Optimize the special case where we are copying the position
- of an existing marker, and MARKER is already in the same buffer. */
- if (MARKERP (position) && b == XMARKER (position)->buffer
- && b == m->buffer)
- {
- m->bytepos = XMARKER (position)->bytepos;
- m->charpos = XMARKER (position)->charpos;
- return marker;
- }
-
- CHECK_NUMBER_COERCE_MARKER (position);
- charpos = clip_to_bounds (BUF_BEG (b), XINT (position), BUF_Z (b));
- bytepos = buf_charpos_to_bytepos (b, charpos);
-
- attach_marker (m, b, charpos, bytepos);
- return marker;
+ return b;
}
-/* This version of Fset_marker won't let the position
- be outside the visible part. */
+/* Internal function to set MARKER in BUFFER at POSITION. Non-zero
+ RESTRICTED means limit the POSITION by the visible part of BUFFER. */
-Lisp_Object
-set_marker_restricted (Lisp_Object marker, Lisp_Object pos, Lisp_Object buffer)
+static inline Lisp_Object
+set_marker_internal (Lisp_Object marker, Lisp_Object position,
+ Lisp_Object buffer, int restricted)
{
- register ptrdiff_t charpos;
- register ptrdiff_t bytepos;
- register struct buffer *b;
register struct Lisp_Marker *m;
+ register struct buffer *b = live_buffer (buffer);
CHECK_MARKER (marker);
m = XMARKER (marker);
- /* If position is nil or a marker that points nowhere,
- make this marker point nowhere. */
- if (NILP (pos)
- || (MARKERP (pos) && !XMARKER (pos)->buffer))
+ /* Set MARKER to point nowhere if BUFFER is dead, or
+ POSITION is nil or a marker points to nowhere. */
+ if (NILP (position)
+ || (MARKERP (position) && !XMARKER (position)->buffer)
+ || !b)
+ unchain_marker (m);
+
+ /* Optimize the special case where we are copying the position of
+ an existing marker, and MARKER is already in the same buffer. */
+ else if (MARKERP (position) && b == XMARKER (position)->buffer
+ && b == m->buffer)
{
- unchain_marker (m);
- return marker;
+ m->bytepos = XMARKER (position)->bytepos;
+ m->charpos = XMARKER (position)->charpos;
}
- if (NILP (buffer))
- b = current_buffer;
else
{
- CHECK_BUFFER (buffer);
- b = XBUFFER (buffer);
- /* If buffer is dead, set marker to point nowhere. */
- if (EQ (BVAR (b, name), Qnil))
- {
- unchain_marker (m);
- return marker;
- }
+ register ptrdiff_t charpos, bytepos;
+
+ CHECK_NUMBER_COERCE_MARKER (position);
+ charpos = clip_to_bounds (restricted ? BUF_BEGV (b) : BUF_BEG (b),
+ XINT (position),
+ restricted ? BUF_ZV (b) : BUF_Z (b));
+ bytepos = buf_charpos_to_bytepos (b, charpos);
+ attach_marker (m, b, charpos, bytepos);
}
+ return marker;
+}
- /* Optimize the special case where we are copying the position
- of an existing marker, and MARKER is already in the same buffer. */
- if (MARKERP (pos) && b == XMARKER (pos)->buffer
- && b == m->buffer)
- {
- m->bytepos = XMARKER (pos)->bytepos;
- m->charpos = XMARKER (pos)->charpos;
- return marker;
- }
+DEFUN ("set-marker", Fset_marker, Sset_marker, 2, 3, 0,
+ doc: /* Position MARKER before character number POSITION in BUFFER,
+which defaults to the current buffer. If POSITION is nil,
+makes marker point nowhere so it no longer slows down
+editing in any buffer. Returns MARKER. */)
+ (Lisp_Object marker, Lisp_Object position, Lisp_Object buffer)
+{
+ return set_marker_internal (marker, position, buffer, 0);
+}
- CHECK_NUMBER_COERCE_MARKER (pos);
- charpos = clip_to_bounds (BUF_BEGV (b), XINT (pos), BUF_ZV (b));
- bytepos = buf_charpos_to_bytepos (b, charpos);
+/* Like the above, but won't let the position be outside the visible part. */
- attach_marker (m, b, charpos, bytepos);
- return marker;
+Lisp_Object
+set_marker_restricted (Lisp_Object marker, Lisp_Object position,
+ Lisp_Object buffer)
+{
+ return set_marker_internal (marker, position, buffer, 1);
}
-
+
/* Set the position of MARKER, specifying both the
character position and the corresponding byte position. */
Lisp_Object
-set_marker_both (Lisp_Object marker, Lisp_Object buffer, ptrdiff_t charpos, ptrdiff_t bytepos)
+set_marker_both (Lisp_Object marker, Lisp_Object buffer,
+ ptrdiff_t charpos, ptrdiff_t bytepos)
{
- register struct buffer *b;
register struct Lisp_Marker *m;
+ register struct buffer *b = live_buffer (buffer);
CHECK_MARKER (marker);
m = XMARKER (marker);
- if (NILP (buffer))
- b = current_buffer;
+ if (b)
+ attach_marker (m, b, charpos, bytepos);
else
- {
- CHECK_BUFFER (buffer);
- b = XBUFFER (buffer);
- /* If buffer is dead, set marker to point nowhere. */
- if (EQ (BVAR (b, name), Qnil))
- {
- unchain_marker (m);
- return marker;
- }
- }
-
- /* In a single-byte buffer, the two positions must be equal. */
- if (BUF_Z (b) == BUF_Z_BYTE (b)
- && charpos != bytepos)
- abort ();
-
- attach_marker (m, b, charpos, bytepos);
+ unchain_marker (m);
return marker;
}
-/* This version of set_marker_both won't let the position
- be outside the visible part. */
+/* Like the above, but won't let the position be outside the visible part. */
Lisp_Object
-set_marker_restricted_both (Lisp_Object marker, Lisp_Object buffer, ptrdiff_t charpos, ptrdiff_t bytepos)
+set_marker_restricted_both (Lisp_Object marker, Lisp_Object buffer,
+ ptrdiff_t charpos, ptrdiff_t bytepos)
{
- register struct buffer *b;
register struct Lisp_Marker *m;
+ register struct buffer *b = live_buffer (buffer);
CHECK_MARKER (marker);
m = XMARKER (marker);
- if (NILP (buffer))
- b = current_buffer;
- else
+ if (b)
{
- CHECK_BUFFER (buffer);
- b = XBUFFER (buffer);
- /* If buffer is dead, set marker to point nowhere. */
- if (EQ (BVAR (b, name), Qnil))
- {
- unchain_marker (m);
- return marker;
- }
+ attach_marker
+ (m, b,
+ clip_to_bounds (BUF_BEGV (b), charpos, BUF_ZV (b)),
+ clip_to_bounds (BUF_BEGV_BYTE (b), bytepos, BUF_ZV_BYTE (b)));
}
-
- charpos = clip_to_bounds (BUF_BEGV (b), charpos, BUF_ZV (b));
- bytepos = clip_to_bounds (BUF_BEGV_BYTE (b), bytepos, BUF_ZV_BYTE (b));
-
- /* In a single-byte buffer, the two positions must be equal. */
- if (BUF_Z (b) == BUF_Z_BYTE (b)
- && charpos != bytepos)
- abort ();
-
- attach_marker (m, b, charpos, bytepos);
+ else
+ unchain_marker (m);
return marker;
}
-
+
/* Remove MARKER from the chain of whatever buffer it is in,
leaving it points to nowhere. This is called during garbage
collection, so we must be careful to ignore and preserve