(text-mode): Contruct paragraph-start so
[bpt/emacs.git] / src / indent.c
index 33313a4..af5b689 100644 (file)
@@ -203,7 +203,7 @@ width_run_cache_on_off ()
    characters immediately following, then *NEXT_BOUNDARY_P
    will equal the return value.  */
 
-static int
+int
 skip_invisible (pos, next_boundary_p, to, window)
      int pos;
      int *next_boundary_p;
@@ -268,67 +268,28 @@ skip_invisible (pos, next_boundary_p, to, window)
 \f
 /* Set variables WIDTH and BYTES for a multibyte sequence starting at P.
 
-   C is *P which should satisfy `BASE_LEADING_CODE_P (c)'.
-
    DP is a display table or NULL.
 
    This macro is used in current_column_1, Fmove_to_column, and
    compute_motion.  */
 
-#define MULTIBYTE_BYTES_WIDTH(p, c, dp)                                        \
-  do {                                                                 \
-    unsigned char *pend = p + 1;                                       \
-                                                                       \
-    wide_column = 0;                                                   \
-    while (! CHAR_HEAD_P (*pend)) pend++;                              \
-                                                                       \
-    if (c == LEADING_CODE_COMPOSITION)                                 \
-      {                                                                        \
-       int id = str_cmpchar_id (p, pend - p);                          \
-       int ch = MAKE_COMPOSITE_CHAR (id);                              \
-                                                                       \
-       if (id >= 0)                                                    \
-         {                                                             \
-           bytes = cmpchar_table[id]->len;                             \
-           if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, ch)))         \
-             width = XVECTOR (DISP_CHAR_VECTOR (dp, ch))->size;        \
-           else                                                        \
-             width = cmpchar_table[id]->width;                         \
-           if (width > 1)                                              \
-             wide_column = width;                                      \
-         }                                                             \
-       else                                                            \
-         {                                                             \
-           bytes = 1;                                                  \
-           width = 4;                                                  \
-         }                                                             \
-      }                                                                        \
-    else                                                               \
-      {                                                                        \
-       bytes = BYTES_BY_CHAR_HEAD (c);                                 \
-       if (bytes >= 2 && bytes <= pend - p)                            \
-         {                                                             \
-           int ch;                                                     \
-                                                                       \
-           if (dp && (ch = STRING_CHAR (p, bytes),                     \
-                      VECTORP (DISP_CHAR_VECTOR (dp, ch))))            \
-             width = XVECTOR (DISP_CHAR_VECTOR (dp, ch))->size;        \
-           else                                                        \
-             width = WIDTH_BY_CHAR_HEAD (c);                           \
-           if (width > 1)                                              \
-             wide_column = width;                                      \
-         }                                                             \
-       else                                                            \
-         {                                                             \
-           bytes = 1;                                                  \
-           width = 4;                                                  \
-         }                                                             \
-      }                                                                        \
-    if (p + bytes < pend)                                              \
-      {                                                                        \
-       width += 4 * (pend - (p + bytes));                              \
-       bytes = pend - p;                                               \
-      }                                                                        \
+#define MULTIBYTE_BYTES_WIDTH(p, dp)                                     \
+  do {                                                                   \
+    int c;                                                               \
+                                                                         \
+    wide_column = 0;                                                     \
+    c = STRING_CHAR_AND_LENGTH (p, MAX_LENGTH_OF_MULTI_BYTE_FORM, bytes); \
+    if (BYTES_BY_CHAR_HEAD (*p) != bytes)                                \
+      width = bytes * 4;                                                 \
+    else                                                                 \
+      {                                                                          \
+       if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))                \
+         width = XVECTOR (DISP_CHAR_VECTOR (dp, c))->size;               \
+       else                                                              \
+         width = WIDTH_BY_CHAR_HEAD (*p);                                \
+       if (width > 1)                                                    \
+         wide_column = width;                                            \
+      }                                                                          \
   } while (0)
 
 DEFUN ("current-column", Fcurrent_column, Scurrent_column, 0, 0, 0,
@@ -532,7 +493,7 @@ current_column_1 ()
 
          scan_byte--;
          ptr = BYTE_POS_ADDR (scan_byte);
-         MULTIBYTE_BYTES_WIDTH (ptr, c, dp);
+         MULTIBYTE_BYTES_WIDTH (ptr, dp);
          scan_byte += bytes;
          col += width;
        }
@@ -792,16 +753,16 @@ int
 indented_beyond_p (pos, pos_byte, column)
      int pos, pos_byte, column;
 {
-  Lisp_Object val;
+  int val;
   int opoint = PT, opoint_byte = PT_BYTE;
 
   SET_PT_BOTH (pos, pos_byte);
   while (PT > BEGV && FETCH_BYTE (PT_BYTE) == '\n')
     scan_newline (PT - 1, PT_BYTE - 1, BEGV, BEGV_BYTE, -1, 0);
 
-  XSETFASTINT (val, position_indentation (PT_BYTE));
+  val = position_indentation (PT_BYTE);
   SET_PT_BOTH (opoint, opoint_byte);
-  return val;
+  return val >= column;
 }
 \f
 DEFUN ("move-to-column", Fmove_to_column, Smove_to_column, 1, 2, "p",
@@ -814,9 +775,10 @@ and horizontal scrolling has no effect.\n\
 \n\
 If specified column is within a character, point goes after that character.\n\
 If it's past end of line, point goes to end of line.\n\n\
-A non-nil second (optional) argument FORCE means, if the line\n\
-is too short to reach column COLUMN then add spaces/tabs to get there,\n\
-and if COLUMN is in the middle of a tab character, change it to spaces.\n\
+A non-nil second (optional) argument FORCE means,\n\
+if COLUMN is in the middle of a tab character, change it to spaces.\n\
+In addition, if FORCE is t, and the line is too short\n\
+to reach column COLUMN, add spaces/tabs to get there.\n\
 \n\
 The return value is the current column.")
   (column, force)
@@ -914,7 +876,7 @@ The return value is the current column.")
 
          pos_byte--;
          ptr = BYTE_POS_ADDR (pos_byte);
-         MULTIBYTE_BYTES_WIDTH (ptr, c, dp);
+         MULTIBYTE_BYTES_WIDTH (ptr, dp);
          pos_byte += bytes;
          col += width;
        }
@@ -942,7 +904,7 @@ The return value is the current column.")
     }
 
   /* If line ends prematurely, add space to the end.  */
-  if (col < goal && !NILP (force))
+  if (col < goal && EQ (force, Qt))
     Findent_to (make_number (col = goal), Qnil);
 
   last_known_column = col;
@@ -1061,8 +1023,9 @@ compute_motion (from, fromvpos, fromhpos, did_motion, to, tovpos, tohpos, width,
   Lisp_Object window;
 
   int multibyte = !NILP (current_buffer->enable_multibyte_characters);
-  int wide_column_end_hpos = 0;        /* Horizontal position at the end of
-                                  last wide-column character.  */
+  /* If previous char scanned was a wide character,
+     this is the column where it ended.  Otherwise, this is 0.  */
+  int wide_column_end_hpos = 0;
   int prev_pos;                        /* Previous buffer position.  */
   int prev_pos_byte;           /* Previous buffer position.  */
   int contin_hpos;             /* HPOS of last column of continued line.  */
@@ -1094,6 +1057,26 @@ compute_motion (from, fromvpos, fromhpos, did_motion, to, tovpos, tohpos, width,
          int pos_here = pos;
          int newpos;
 
+         /* Don't skip invisible if we are already at the margin.  */
+         if (vpos > tovpos || vpos == tovpos && hpos >= tohpos)
+           {
+             if (contin_hpos && prev_hpos == 0
+                 && hpos > tohpos
+                 && (contin_hpos == width || wide_column_end_hpos > width))
+               { /* Line breaks because we can't put the character at the
+                    previous line any more.  It is not the multi-column
+                    character continued in middle.  Go back to previous
+                    buffer position, screen position, and set tab offset
+                    to previous value.  It's the beginning of the
+                    line.  */
+                 pos = prev_pos;
+                 pos_byte = prev_pos_byte;
+                 hpos = prev_hpos;
+                 tab_offset = prev_tab_offset;
+               }
+             break;
+           }
+
          /* If the caller says that the screen position came from an earlier
             call to compute_motion, then we've already accounted for the
             overlay strings at point.  This is only true the first time
@@ -1121,7 +1104,10 @@ compute_motion (from, fromvpos, fromhpos, did_motion, to, tovpos, tohpos, width,
          newpos = skip_invisible (pos, &next_boundary, to, window);
 
          if (newpos >= to)
-           goto after_loop;
+           {
+             pos = min (to, newpos);
+             goto after_loop;
+           }
 
          if (newpos != pos_here)
            {
@@ -1256,8 +1242,8 @@ compute_motion (from, fromvpos, fromhpos, did_motion, to, tovpos, tohpos, width,
       if (vpos > tovpos || vpos == tovpos && hpos >= tohpos)
        {
          if (contin_hpos && prev_hpos == 0
-             && ((hpos > tohpos && contin_hpos == width)
-                 || (wide_column_end_hpos > width)))
+             && hpos > tohpos
+             && (contin_hpos == width || wide_column_end_hpos > width))
            { /* Line breaks because we can't put the character at the
                 previous line any more.  It is not the multi-column
                 character continued in middle.  Go back to previous
@@ -1390,7 +1376,9 @@ compute_motion (from, fromvpos, fromhpos, did_motion, to, tovpos, tohpos, width,
                      /* Skip any number of invisible lines all at once */
                      do
                        {
-                         pos = find_before_next_newline (pos, to, 1) + 1;
+                         pos = find_before_next_newline (pos, to, 1);
+                         if (pos < to)
+                           pos++;
                          pos_byte = CHAR_TO_BYTE (pos);
                        }
                      while (pos < to
@@ -1451,7 +1439,7 @@ compute_motion (from, fromvpos, fromhpos, did_motion, to, tovpos, tohpos, width,
 
              pos_byte--;       /* rewind POS_BYTE */
              ptr = BYTE_POS_ADDR (pos_byte);
-             MULTIBYTE_BYTES_WIDTH (ptr, c, dp);
+             MULTIBYTE_BYTES_WIDTH (ptr, dp);
              pos_byte += bytes;
              if (wide_column)
                wide_column_end_hpos = hpos + wide_column;
@@ -1488,6 +1476,7 @@ compute_motion (from, fromvpos, fromhpos, did_motion, to, tovpos, tohpos, width,
   return &val_compute_motion;
 }
 
+
 #if 0 /* The doc string is too long for some compilers,
         but make-docfile can find it in this comment.  */
 DEFUN ("compute-motion", Ffoo, Sfoo, 7, 7, 0,
@@ -1545,20 +1534,20 @@ DEFUN ("compute-motion", Fcompute_motion, Scompute_motion, 7, 7, 0,
 
   CHECK_NUMBER_COERCE_MARKER (from, 0);
   CHECK_CONS (frompos, 0);
-  CHECK_NUMBER (XCONS (frompos)->car, 0);
-  CHECK_NUMBER (XCONS (frompos)->cdr, 0);
+  CHECK_NUMBER (XCAR (frompos), 0);
+  CHECK_NUMBER (XCDR (frompos), 0);
   CHECK_NUMBER_COERCE_MARKER (to, 0);
   CHECK_CONS (topos, 0);
-  CHECK_NUMBER (XCONS (topos)->car, 0);
-  CHECK_NUMBER (XCONS (topos)->cdr, 0);
+  CHECK_NUMBER (XCAR (topos), 0);
+  CHECK_NUMBER (XCDR (topos), 0);
   CHECK_NUMBER (width, 0);
   if (!NILP (offsets))
     {
       CHECK_CONS (offsets, 0);
-      CHECK_NUMBER (XCONS (offsets)->car, 0);
-      CHECK_NUMBER (XCONS (offsets)->cdr, 0);
-      hscroll = XINT (XCONS (offsets)->car);
-      tab_offset = XINT (XCONS (offsets)->cdr);
+      CHECK_NUMBER (XCAR (offsets), 0);
+      CHECK_NUMBER (XCDR (offsets), 0);
+      hscroll = XINT (XCAR (offsets));
+      tab_offset = XINT (XCDR (offsets));
     }
   else
     hscroll = tab_offset = 0;
@@ -1573,10 +1562,10 @@ DEFUN ("compute-motion", Fcompute_motion, Scompute_motion, 7, 7, 0,
   if (XINT (to) < BEGV || XINT (to) > ZV)
     args_out_of_range_3 (to, make_number (BEGV), make_number (ZV));
 
-  pos = compute_motion (XINT (from), XINT (XCONS (frompos)->cdr),
-                       XINT (XCONS (frompos)->car), 0,
-                       XINT (to), XINT (XCONS (topos)->cdr),
-                       XINT (XCONS (topos)->car),
+  pos = compute_motion (XINT (from), XINT (XCDR (frompos)),
+                       XINT (XCAR (frompos)), 0,
+                       XINT (to), XINT (XCDR (topos)),
+                       XINT (XCAR (topos)),
                        XINT (width), hscroll, tab_offset,
                        XWINDOW (window));
 
@@ -1617,21 +1606,17 @@ vmotion (from, vtarget, w)
   Lisp_Object window;
   int start_hpos = 0;
   int did_motion;
+  /* This is the object we use for fetching character properties.  */
+  Lisp_Object text_prop_object;
 
   XSETWINDOW (window, w);
 
-  /* The omission of the clause
-         && marker_position (w->start) == BEG
-     here is deliberate; I think we want to measure from the prompt
-     position even if the minibuffer window has scrolled.  */
-  if (EQ (window, minibuf_window))
-    {
-      if (minibuf_prompt_width == 0 && STRINGP (minibuf_prompt))
-       minibuf_prompt_width
-         = string_display_width (minibuf_prompt, Qnil, Qnil);
-
-      start_hpos = minibuf_prompt_width;
-    }
+  /* If the window contains this buffer, use it for getting text properties.
+     Otherwise use the current buffer as arg for doing that.  */
+  if (EQ (w->buffer, Fcurrent_buffer ()))
+    text_prop_object = window;
+  else
+    text_prop_object = Fcurrent_buffer ();
 
   if (vpos >= vtarget)
     {
@@ -1650,14 +1635,11 @@ vmotion (from, vtarget, w)
                      && indented_beyond_p (XFASTINT (prevline),
                                            CHAR_TO_BYTE (XFASTINT (prevline)),
                                            selective))
-#ifdef USE_TEXT_PROPERTIES
                     /* watch out for newlines with `invisible' property */
                     || (propval = Fget_char_property (prevline,
                                                       Qinvisible,
-                                                      window),
-                        TEXT_PROP_MEANS_INVISIBLE (propval))
-#endif
-                    ))
+                                                      text_prop_object),
+                        TEXT_PROP_MEANS_INVISIBLE (propval))))
            XSETFASTINT (prevline,
                         find_next_newline_no_quit (XFASTINT (prevline) - 1,
                                                    -1));
@@ -1713,13 +1695,10 @@ vmotion (from, vtarget, w)
                  && indented_beyond_p (XFASTINT (prevline),
                                        CHAR_TO_BYTE (XFASTINT (prevline)),
                                        selective))
-#ifdef USE_TEXT_PROPERTIES
                 /* watch out for newlines with `invisible' property */
                 || (propval = Fget_char_property (prevline, Qinvisible,
-                                                  window),
-                    TEXT_PROP_MEANS_INVISIBLE (propval))
-#endif
-            ))
+                                                  text_prop_object),
+                    TEXT_PROP_MEANS_INVISIBLE (propval))))
        XSETFASTINT (prevline,
                     find_next_newline_no_quit (XFASTINT (prevline) - 1,
                                                -1));
@@ -1775,7 +1754,10 @@ whether or not it is currently displayed in some window.")
   (lines, window)
      Lisp_Object lines, window;
 {
-  struct position pos;
+  struct it it;
+  struct text_pos pt;
+  struct buffer *old, *b;
+  struct window *w;
 
   CHECK_NUMBER (lines, 0);
   if (! NILP (window))
@@ -1783,11 +1765,28 @@ whether or not it is currently displayed in some window.")
   else
     window = selected_window;
 
-  pos = *vmotion (PT, (int) XINT (lines), XWINDOW (window));
-
-  SET_PT (pos.bufpos);
-  return make_number (pos.vpos);
+  w = XWINDOW (window);
+  b = XBUFFER (w->buffer);
+  if (b != current_buffer)
+    {
+      old = current_buffer;
+      set_buffer_internal_1 (b);
+    }
+  else
+    old = NULL;
+      
+  SET_TEXT_POS (pt, PT, PT_BYTE);
+  start_display (&it, w, pt);
+  move_it_by_lines (&it, XINT (lines), 0);
+  SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
+
+  if (old)
+    set_buffer_internal_1 (old);
+  
+  return make_number (it.vpos);
 }
+
+
 \f
 /* file's initialization.  */