Avoid weird behavior with large horizontal scrolls.
[bpt/emacs.git] / src / window.c
index 4f3d40c..a436965 100644 (file)
@@ -51,6 +51,11 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include "nsterm.h"
 #endif
 
+/* Horizontal scrolling has problems with large scroll amounts.
+   It's too slow with long lines, and even with small lines the
+   display can be messed up.  Impose a reasonable maximum.  */
+enum { HSCROLL_MAX = 100000 };
+
 Lisp_Object Qwindowp, Qwindow_live_p;
 static Lisp_Object Qwindow_configuration_p, Qrecord_window_buffer;
 static Lisp_Object Qwindow_deletable_p, Qdelete_window, Qdisplay_buffer;
@@ -670,27 +675,35 @@ WINDOW must be a live window and defaults to the selected one.  */)
   return make_number (decode_window (window)->hscroll);
 }
 
+/* Set W's horizontal scroll amount to HSCROLL clipped to a reasonable
+   range, returning the new amount as a fixnum.  */
+static Lisp_Object
+set_window_hscroll (struct window *w, EMACS_INT hscroll)
+{
+  int new_hscroll = clip_to_bounds (0, hscroll, HSCROLL_MAX);
+
+  /* Prevent redisplay shortcuts when changing the hscroll.  */
+  if (w->hscroll != new_hscroll)
+    XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
+
+  w->hscroll = new_hscroll;
+  return make_number (new_hscroll);
+}
+
 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
        doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
 If WINDOW is nil, the selected window is used.
-Return NCOL.  NCOL should be zero or positive.
+Clip the number to a reasonable value if out of range.
+Return the new number.  NCOL should be zero or positive.
 
 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
 window so that the location of point moves off-window.  */)
   (Lisp_Object window, Lisp_Object ncol)
 {
   struct window *w = decode_window (window);
-  ptrdiff_t hscroll;
 
   CHECK_NUMBER (ncol);
-  hscroll = clip_to_bounds (0, XINT (ncol), PTRDIFF_MAX);
-
-  /* Prevent redisplay shortcuts when changing the hscroll.  */
-  if (w->hscroll != hscroll)
-    XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
-
-  w->hscroll = hscroll;
-  return ncol;
+  return set_window_hscroll (w, XINT (ncol));
 }
 
 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
@@ -2873,13 +2886,6 @@ adjust_window_margins (struct window *w)
   return 1;
 }
 \f
-static Lisp_Object Fset_window_margins (Lisp_Object, Lisp_Object, Lisp_Object);
-static Lisp_Object Fset_window_fringes (Lisp_Object, Lisp_Object, Lisp_Object,
-                                       Lisp_Object);
-static Lisp_Object Fset_window_scroll_bars (Lisp_Object, Lisp_Object,
-                                           Lisp_Object, Lisp_Object);
-static Lisp_Object Fset_window_vscroll (Lisp_Object, Lisp_Object, Lisp_Object);
-
 /* The following three routines are needed for running a window's
    configuration change hook.  */
 static void
@@ -3244,7 +3250,7 @@ make_parent_window (Lisp_Object window, int horflag)
 
   o = XWINDOW (window);
   p = allocate_window ();
-  memcpy ((char *) p + sizeof (struct vectorlike_header), 
+  memcpy ((char *) p + sizeof (struct vectorlike_header),
          (char *) o + sizeof (struct vectorlike_header),
          sizeof (Lisp_Object) * VECSIZE (struct window));
   XSETWINDOW (parent, p);
@@ -4868,17 +4874,11 @@ will not scroll a window to a column less than the value returned
 by this function.  This happens in an interactive call.  */)
   (register Lisp_Object arg, Lisp_Object set_minimum)
 {
-  Lisp_Object result;
-  ptrdiff_t hscroll;
   struct window *w = XWINDOW (selected_window);
-
-  if (NILP (arg))
-    XSETFASTINT (arg, window_body_cols (w) - 2);
-  else
-    arg = Fprefix_numeric_value (arg);
-
-  hscroll = w->hscroll + XINT (arg);
-  result = Fset_window_hscroll (selected_window, make_number (hscroll));
+  EMACS_INT requested_arg = (NILP (arg)
+                            ? window_body_cols (w) - 2
+                            : XINT (Fprefix_numeric_value (arg)));
+  Lisp_Object result = set_window_hscroll (w, w->hscroll + requested_arg);
 
   if (!NILP (set_minimum))
     w->min_hscroll = w->hscroll;
@@ -4897,17 +4897,11 @@ will not scroll a window to a column less than the value returned
 by this function.  This happens in an interactive call.  */)
   (register Lisp_Object arg, Lisp_Object set_minimum)
 {
-  Lisp_Object result;
-  ptrdiff_t hscroll;
   struct window *w = XWINDOW (selected_window);
-
-  if (NILP (arg))
-    XSETFASTINT (arg, window_body_cols (w) - 2);
-  else
-    arg = Fprefix_numeric_value (arg);
-
-  hscroll = w->hscroll - XINT (arg);
-  result = Fset_window_hscroll (selected_window, make_number (hscroll));
+  EMACS_INT requested_arg = (NILP (arg)
+                            ? window_body_cols (w) - 2
+                            : XINT (Fprefix_numeric_value (arg)));
+  Lisp_Object result = set_window_hscroll (w, w->hscroll - requested_arg);
 
   if (!NILP (set_minimum))
     w->min_hscroll = w->hscroll;
@@ -5541,7 +5535,7 @@ the return value is nil.  Otherwise the value is t.  */)
          w->right_margin_cols = p->right_margin_cols;
          w->left_fringe_width = p->left_fringe_width;
          w->right_fringe_width = p->right_fringe_width;
-         w->fringes_outside_margins = p->fringes_outside_margins;
+         w->fringes_outside_margins = !NILP (p->fringes_outside_margins);
          w->scroll_bar_width = p->scroll_bar_width;
          w->vertical_scroll_bar_type = p->vertical_scroll_bar_type;
          w->dedicated = p->dedicated;
@@ -5857,7 +5851,7 @@ save_window_save (Lisp_Object window, struct Lisp_Vector *vector, int i)
       p->right_margin_cols = w->right_margin_cols;
       p->left_fringe_width = w->left_fringe_width;
       p->right_fringe_width = w->right_fringe_width;
-      p->fringes_outside_margins = w->fringes_outside_margins;
+      p->fringes_outside_margins = w->fringes_outside_margins ? Qt : Qnil;
       p->scroll_bar_width = w->scroll_bar_width;
       p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
       p->dedicated = w->dedicated;
@@ -6094,6 +6088,7 @@ display marginal areas and the text area.  */)
   (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width, Lisp_Object outside_margins)
 {
   struct window *w = decode_window (window);
+  int outside = !NILP (outside_margins);
 
   if (!NILP (left_width))
     CHECK_NATNUM (left_width);
@@ -6104,11 +6099,11 @@ display marginal areas and the text area.  */)
   if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
       && (!EQ (w->left_fringe_width, left_width)
          || !EQ (w->right_fringe_width, right_width)
-         || !EQ (w->fringes_outside_margins, outside_margins)))
+         || w->fringes_outside_margins != outside))
     {
       w->left_fringe_width = left_width;
       w->right_fringe_width = right_width;
-      w->fringes_outside_margins = outside_margins;
+      w->fringes_outside_margins = outside;
 
       adjust_window_margins (w);
 
@@ -6161,7 +6156,7 @@ Fourth parameter HORIZONTAL-TYPE is currently unused.  */)
 
   if (!NILP (width))
     {
-      CHECK_RANGED_INTEGER (0, width, INT_MAX);
+      CHECK_RANGED_INTEGER (width, 0, INT_MAX);
 
       if (XINT (width) == 0)
        vertical_type = Qnil;