From 0b4d6d30be2822df7d6b086bbab32b8ff419ed5d Mon Sep 17 00:00:00 2001 From: Dmitry Antipov Date: Wed, 15 Oct 2014 17:37:10 +0400 Subject: [PATCH] Avoid unwanted point motion in Fline_beginning_position. * lisp.h (scan_newline_from_point): Add prototype. * search.c (scan_newline_from_point): New function, refactored from... * cmds.c (Fforward_line): ...adjusted user. * editfns.c (Fline_beginning_position): Use scan_newline_from_point and simplify the former since the latter doesn't move point. --- src/ChangeLog | 9 +++++++++ src/cmds.c | 7 +------ src/editfns.c | 15 +++------------ src/lisp.h | 1 + src/search.c | 18 ++++++++++++++++++ 5 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index c1b7c6cc8c3..6823f6d3127 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,12 @@ +2014-10-15 Dmitry Antipov + + Avoid unwanted point motion in Fline_beginning_position. + * lisp.h (scan_newline_from_point): Add prototype. + * search.c (scan_newline_from_point): New function, refactored from... + * cmds.c (Fforward_line): ...adjusted user. + * editfns.c (Fline_beginning_position): Use scan_newline_from_point + and simplify the former since the latter doesn't move point. + 2014-10-14 Dmitry Antipov Cleanup terminal handling code. diff --git a/src/cmds.c b/src/cmds.c index 20234638778..9a05218b77b 100644 --- a/src/cmds.c +++ b/src/cmds.c @@ -131,12 +131,7 @@ successfully moved (for the return value). */) count = XINT (n); } - if (count <= 0) - pos = find_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, count - 1, - &shortage, &pos_byte, 1); - else - pos = find_newline (PT, PT_BYTE, ZV, ZV_BYTE, count, - &shortage, &pos_byte, 1); + shortage = scan_newline_from_point (count, &pos, &pos_byte); SET_PT_BOTH (pos, pos_byte); diff --git a/src/editfns.c b/src/editfns.c index be1062dbbc5..376d8e3a0ea 100644 --- a/src/editfns.c +++ b/src/editfns.c @@ -787,26 +787,17 @@ boundaries, bind `inhibit-field-text-motion' to t. This function does not move point. */) (Lisp_Object n) { - ptrdiff_t orig, orig_byte, end; - ptrdiff_t count = SPECPDL_INDEX (); - specbind (Qinhibit_point_motion_hooks, Qt); + ptrdiff_t charpos, bytepos; if (NILP (n)) XSETFASTINT (n, 1); else CHECK_NUMBER (n); - orig = PT; - orig_byte = PT_BYTE; - Fforward_line (make_number (XINT (n) - 1)); - end = PT; - - SET_PT_BOTH (orig, orig_byte); - - unbind_to (count, Qnil); + scan_newline_from_point (XINT (n) - 1, &charpos, &bytepos); /* Return END constrained to the current input field. */ - return Fconstrain_to_field (make_number (end), make_number (orig), + return Fconstrain_to_field (make_number (charpos), make_number (PT), XINT (n) != 1 ? Qt : Qnil, Qt, Qnil); } diff --git a/src/lisp.h b/src/lisp.h index 89f29ea268b..d8809fd10d7 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -4066,6 +4066,7 @@ extern ptrdiff_t find_newline (ptrdiff_t, ptrdiff_t, ptrdiff_t, ptrdiff_t, ptrdiff_t, ptrdiff_t *, ptrdiff_t *, bool); extern ptrdiff_t scan_newline (ptrdiff_t, ptrdiff_t, ptrdiff_t, ptrdiff_t, ptrdiff_t, bool); +extern ptrdiff_t scan_newline_from_point (ptrdiff_t, ptrdiff_t *, ptrdiff_t *); extern ptrdiff_t find_newline_no_quit (ptrdiff_t, ptrdiff_t, ptrdiff_t, ptrdiff_t *); extern ptrdiff_t find_before_next_newline (ptrdiff_t, ptrdiff_t, diff --git a/src/search.c b/src/search.c index 9eed390244f..c6ae9d7e922 100644 --- a/src/search.c +++ b/src/search.c @@ -985,6 +985,24 @@ scan_newline (ptrdiff_t start, ptrdiff_t start_byte, return shortage; } +/* Like above, but always scan from point and report the + resulting position in *CHARPOS and *BYTEPOS. */ + +ptrdiff_t +scan_newline_from_point (ptrdiff_t count, ptrdiff_t *charpos, + ptrdiff_t *bytepos) +{ + ptrdiff_t shortage; + + if (count <= 0) + *charpos = find_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, count - 1, + &shortage, bytepos, 1); + else + *charpos = find_newline (PT, PT_BYTE, ZV, ZV_BYTE, count, + &shortage, bytepos, 1); + return shortage; +} + /* Like find_newline, but doesn't allow QUITting and doesn't return SHORTAGE. */ ptrdiff_t -- 2.39.5