From: Dmitry Antipov Date: Fri, 6 Jul 2012 08:53:15 +0000 (+0400) Subject: Avoid code repetition in marker-related functions. X-Git-Tag: emacs-24.2.90~1199^2~181 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=4e57b342c5713d900b93ffde70cf7bb9150ca8fd;p=emacs.git Avoid code repetition in marker-related functions. * marker.c (attach_marker): New function. (Fset_marker, set_marker_restricted, set_marker_both) (set_marker_restricted_both): Use it. (Fset_marker, set_marker_restricted, Fbuffer_has_markers_at): Consistently rename charno to charpos. (marker_position): Add eassert. (marker_byte_position): Convert to eassert. --- diff --git a/src/ChangeLog b/src/ChangeLog index 72355fc682f..5e28882e6b3 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,7 +1,19 @@ 2012-07-06 Dmitry Antipov + Avoid code repetition in marker-related functions. + * marker.c (attach_marker): New function. + (Fset_marker, set_marker_restricted, set_marker_both) + (set_marker_restricted_both): Use it. + (Fset_marker, set_marker_restricted, Fbuffer_has_markers_at): + Consistently rename charno to charpos. + (marker_position): Add eassert. + (marker_byte_position): Convert to eassert. + +2012-07-06 Dmitry Antipov + + Simplify list operations in unchain_overlay and unchain_marker. * buffer.c (unchain_overlay): Simplify. Add comment. - * marker.c (unchain_marker): Simplify. Fix comments. + * marker.c (unchain_marker): Simplify. Fix comments. 2012-07-06 Dmitry Antipov diff --git a/src/marker.c b/src/marker.c index 0a1dd41b498..47a3e85e582 100644 --- a/src/marker.c +++ b/src/marker.c @@ -432,7 +432,28 @@ Returns nil if MARKER points nowhere. */) return Qnil; } - + +/* Change M so it points to B at CHARPOS and BYTEPOS. */ + +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); + + m->charpos = charpos; + m->bytepos = bytepos; + + if (m->buffer != b) + { + unchain_marker (m); + m->buffer = b; + m->next = BUF_MARKERS (b); + BUF_MARKERS (b) = m; + } +} + 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. @@ -441,7 +462,7 @@ Then it no longer slows down editing in any buffer. Returns MARKER. */) (Lisp_Object marker, Lisp_Object position, Lisp_Object buffer) { - register ptrdiff_t charno; + register ptrdiff_t charpos; register ptrdiff_t bytepos; register struct buffer *b; register struct Lisp_Marker *m; @@ -483,24 +504,10 @@ Returns MARKER. */) } CHECK_NUMBER_COERCE_MARKER (position); - charno = clip_to_bounds (BUF_BEG (b), XINT (position), BUF_Z (b)); - bytepos = buf_charpos_to_bytepos (b, charno); - - /* Every character is at least one byte. */ - if (charno > bytepos) - abort (); - - m->bytepos = bytepos; - m->charpos = charno; - - if (m->buffer != b) - { - unchain_marker (m); - m->buffer = b; - m->next = BUF_MARKERS (b); - BUF_MARKERS (b) = m; - } + 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; } @@ -510,7 +517,7 @@ Returns MARKER. */) Lisp_Object set_marker_restricted (Lisp_Object marker, Lisp_Object pos, Lisp_Object buffer) { - register ptrdiff_t charno; + register ptrdiff_t charpos; register ptrdiff_t bytepos; register struct buffer *b; register struct Lisp_Marker *m; @@ -552,24 +559,10 @@ set_marker_restricted (Lisp_Object marker, Lisp_Object pos, Lisp_Object buffer) } CHECK_NUMBER_COERCE_MARKER (pos); - charno = clip_to_bounds (BUF_BEGV (b), XINT (pos), BUF_ZV (b)); - bytepos = buf_charpos_to_bytepos (b, charno); - - /* Every character is at least one byte. */ - if (charno > bytepos) - abort (); - - m->bytepos = bytepos; - m->charpos = charno; - - if (m->buffer != b) - { - unchain_marker (m); - m->buffer = b; - m->next = BUF_MARKERS (b); - BUF_MARKERS (b) = m; - } + charpos = clip_to_bounds (BUF_BEGV (b), XINT (pos), BUF_ZV (b)); + bytepos = buf_charpos_to_bytepos (b, charpos); + attach_marker (m, b, charpos, bytepos); return marker; } @@ -603,21 +596,8 @@ set_marker_both (Lisp_Object marker, Lisp_Object buffer, ptrdiff_t charpos, ptrd if (BUF_Z (b) == BUF_Z_BYTE (b) && charpos != bytepos) abort (); - /* Every character is at least one byte. */ - if (charpos > bytepos) - abort (); - - m->bytepos = bytepos; - m->charpos = charpos; - - if (m->buffer != b) - { - unchain_marker (m); - m->buffer = b; - m->next = BUF_MARKERS (b); - BUF_MARKERS (b) = m; - } + attach_marker (m, b, charpos, bytepos); return marker; } @@ -654,21 +634,8 @@ set_marker_restricted_both (Lisp_Object marker, Lisp_Object buffer, ptrdiff_t ch if (BUF_Z (b) == BUF_Z_BYTE (b) && charpos != bytepos) abort (); - /* Every character is at least one byte. */ - if (charpos > bytepos) - abort (); - - m->bytepos = bytepos; - m->charpos = charpos; - - if (m->buffer != b) - { - unchain_marker (m); - m->buffer = b; - m->next = BUF_MARKERS (b); - BUF_MARKERS (b) = m; - } + attach_marker (m, b, charpos, bytepos); return marker; } @@ -726,6 +693,8 @@ marker_position (Lisp_Object marker) if (!buf) error ("Marker does not point anywhere"); + eassert (BUF_BEG (buf) <= m->charpos && m->charpos <= BUF_Z (buf)); + return m->charpos; } @@ -736,15 +705,13 @@ marker_byte_position (Lisp_Object marker) { register struct Lisp_Marker *m = XMARKER (marker); register struct buffer *buf = m->buffer; - register ptrdiff_t i = m->bytepos; if (!buf) error ("Marker does not point anywhere"); - if (i < BUF_BEG_BYTE (buf) || i > BUF_Z_BYTE (buf)) - abort (); + eassert (BUF_BEG_BYTE (buf) <= m->bytepos && m->bytepos <= BUF_Z_BYTE (buf)); - return i; + return m->bytepos; } DEFUN ("copy-marker", Fcopy_marker, Scopy_marker, 0, 2, 0, @@ -797,12 +764,12 @@ DEFUN ("buffer-has-markers-at", Fbuffer_has_markers_at, Sbuffer_has_markers_at, (Lisp_Object position) { register struct Lisp_Marker *tail; - register ptrdiff_t charno; + register ptrdiff_t charpos; - charno = clip_to_bounds (BEG, XINT (position), Z); + charpos = clip_to_bounds (BEG, XINT (position), Z); for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next) - if (tail->charpos == charno) + if (tail->charpos == charpos) return Qt; return Qnil;