(text-mode): Contruct paragraph-start so
[bpt/emacs.git] / src / indent.c
index 9ac2dd7..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;
        }
@@ -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;
@@ -1414,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
@@ -1475,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;
@@ -1512,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,
@@ -1569,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;
@@ -1597,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));
 
@@ -1646,19 +1611,6 @@ vmotion (from, vtarget, w)
 
   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 ()))
@@ -1683,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,
                                                       text_prop_object),
-                        TEXT_PROP_MEANS_INVISIBLE (propval))
-#endif
-                    ))
+                        TEXT_PROP_MEANS_INVISIBLE (propval))))
            XSETFASTINT (prevline,
                         find_next_newline_no_quit (XFASTINT (prevline) - 1,
                                                    -1));
@@ -1746,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,
                                                   text_prop_object),
-                    TEXT_PROP_MEANS_INVISIBLE (propval))
-#endif
-            ))
+                    TEXT_PROP_MEANS_INVISIBLE (propval))))
        XSETFASTINT (prevline,
                     find_next_newline_no_quit (XFASTINT (prevline) - 1,
                                                -1));
@@ -1808,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))
@@ -1816,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.  */