Better smart wrapping.
[claws.git] / src / gtkstext.c
index c6605ea90a5c11256f8353d38e8d597e6fd51543..6e75dce3aa924407e7f4f945c6840801f9477e8b 100644 (file)
@@ -999,6 +999,16 @@ gtk_stext_thaw (GtkSText *text)
   draw_cursor (text, FALSE);
 }
 
+void
+gtk_stext_compact_buffer (GtkSText    *text)
+{
+  GtkEditable *editable = GTK_EDITABLE (text);
+
+  g_return_if_fail (text != NULL);
+  g_return_if_fail (GTK_IS_STEXT (text));
+  move_gap (text, gtk_stext_get_length(text));
+}
+
 void
 gtk_stext_insert (GtkSText    *text,
                 GdkFont    *font,
@@ -2189,8 +2199,17 @@ gtk_stext_key_press (GtkWidget   *widget,
       switch (event->keyval)
        {
        case GDK_Home:
-         if (event->state & GDK_CONTROL_MASK)
-           move_cursor_buffer_ver (text, -1);
+         if (event->state & GDK_CONTROL_MASK) {
+               if (text->wrap_rmargin == 0) {
+                       /* SYLPHEED: old behaviour */
+                       move_cursor_buffer_ver (text, -1);
+               }
+               else {
+                       /* SYLPHEED: contrived, but "trusty" */
+                       move_cursor_buffer_ver(text, -1);
+                       move_cursor_to_display_row_start(text);
+               }
+         }     
          else {
                if (text->wrap_rmargin > 0) {
                        /* SYLPHEED: line start */
@@ -2202,8 +2221,17 @@ gtk_stext_key_press (GtkWidget   *widget,
          }     
          break;
        case GDK_End:
-         if (event->state & GDK_CONTROL_MASK)
-           move_cursor_buffer_ver (text, +1);
+         if (event->state & GDK_CONTROL_MASK) {
+               /* SYLPHEED: a little bit contrived... */
+               if (text->wrap_rmargin == 0) {
+                       /* old behaviour */
+                       move_cursor_buffer_ver (text, +1);
+               }
+               else {
+                       move_cursor_buffer_ver(text, +1);
+                       move_cursor_to_display_row_end(text);
+               }
+         }             
          else {
                if (text->wrap_rmargin > 0) {
                        /* SYLPHEED: line end */
@@ -2231,7 +2259,7 @@ gtk_stext_key_press (GtkWidget   *widget,
                break;
        case GDK_Down:      
                move_cursor_to_display_row_down(text);
-//             move_cursor_ver (text, +1); 
+/*             move_cursor_ver (text, +1);  */
                break;
        case GDK_Left:
          if (event->state & GDK_CONTROL_MASK)
@@ -4219,9 +4247,9 @@ static void move_cursor_to_display_row_start(GtkSText *text)
 }
 
 /* dumb */
-static gboolean range_intersect(gint x1, gint x2, gint y1, gint y2)
+static gboolean range_intersect(guint x1, guint x2, guint y1, guint y2)
 {
-       gint tmp;
+       guint tmp;
        if (x1 > x2) { tmp = x1; x1 = x2; x2 = tmp; }
        if (y1 > y2) { tmp = y1; y1 = y2; y1 = tmp; }
        if (y1 < x1) { tmp = x1; x1 = y1; y1 = tmp; tmp = x2; x2 = y2; y2 = tmp; }
@@ -4261,28 +4289,26 @@ static void move_cursor_to_display_row_up(GtkSText *text)
        int                       new_index;
        int                   col;
        GtkSPropertyMark  mark;
-       
+
        mark = find_this_line_start_mark(text, text->cursor_mark.index, &text->cursor_mark);
 
        /* top of buffer */
        if (mark.index == 0) {
-               XDEBUG ( ("%s(%d) top of buffer", __FILE__, __LINE__) );        
-               return;
+               XDEBUG ( ("%s(%d) top of buffer", __FILE__, __LINE__) );
        }
 
-       /* we need to previous DISPLAY row not the previous BUFFER line, so we go one line back,
-        * and iterate over the lines until we have a LineParams that matches the original */
-
+       /* we need previous DISPLAY row not the previous BUFFER line, so we go to start
+        * of paragraph, and iterate over the lines until we have a LineParams that matches 
+        * the original */
        lp  = CACHE_DATA(text->current_line);
        col = (text->cursor_mark.index - lp.start.index);
        data.start = lp.start.index;
        data.end   = lp.end.index;
 
        /* get the previous line */
-       if (mark.index - 1 > 0) {
+       if (mark.index != 0) {
                decrement_mark(&mark);
-               XDEBUG( ("%s(%d) mark decrement", __FILE__, __LINE__) );
-               if (mark.index - 1 > 0) {
+               if (mark.index != 0) {
                        GtkSPropertyMark smark = mark;
                        XDEBUG( ("%s(%d) finding line start mark", __FILE__, __LINE__) );
                        mark = find_this_line_start_mark(text, smark.index -1,  &smark);
@@ -4316,6 +4342,19 @@ typedef struct {
        LineParams   lp;
 } fdrf; 
 
+#if defined(AHX_DEBUG)
+static void print_line(GtkSText *text, guint start, guint end)
+{
+       gchar *buf = alloca(2048), *walk = buf;
+
+       memset(buf, 0, 2048);
+       for (; start <= end; start++) {
+               *walk++ = GTK_STEXT_INDEX(text, start);
+       }               
+       XDEBUG( ("%s", buf) );  
+}
+#endif
+
 static gint forward_display_row_fetcher(GtkSText *text,
                                                                                LineParams *lp,
                                                                                fdrf       *data)
@@ -4325,6 +4364,7 @@ static gint forward_display_row_fetcher(GtkSText *text,
                                __FILE__, __LINE__, data->start, data->end, lp->start.index, lp->end.index) );
                data->lp = *lp;
                data->completed = TRUE;
+               print_line(text, lp->start.index, lp->end.index);
                return TRUE;
        }
        else if (range_intersect(data->start, data->end, lp->start.index, lp->end.index)) {
@@ -4367,6 +4407,7 @@ static void move_cursor_to_display_row_down       (GtkSText *text)
                        new_index = data.lp.end.index;
                }
                /* and move the cursor */
+               XDEBUG( ("%s(%d) - FW set pos %d", __FILE__, __LINE__, new_index) );
                gtk_stext_set_position_X(GTK_EDITABLE(text), new_index);                
        }
 }