From d4fdfa76097dd3b07acab28eac1f54da283a47ad Mon Sep 17 00:00:00 2001
From: Sergios - Anestis Kefalidis <sergioskefalidis@gmail.com>
Date: Thu, 21 Oct 2021 22:52:51 +0300
Subject: [PATCH] Select All: WIP

---
 src/vte.cc | 53 ++++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 42 insertions(+), 11 deletions(-)

diff --git a/src/vte.cc b/src/vte.cc
index 742e73b8..cf0bd81c 100644
--- a/src/vte.cc
+++ b/src/vte.cc
@@ -6087,8 +6087,6 @@ Terminal::get_text(vte::grid::row_t start_row,
 
 	if (attributes)
 		g_array_set_size (attributes, 0);
-
-	string = g_string_new(NULL);
 	memset(&attr, 0, sizeof(attr));
 
         if (start_col < 0)
@@ -6109,7 +6107,13 @@ Terminal::get_text(vte::grid::row_t start_row,
 
         vte::grid::column_t lcol = block ? 0 : start_col;
         vte::grid::row_t row;
+        size_t size = 0;
+        GList *string_list = NULL;
+        GList *attributes_list = NULL;
         for (row = start_row; row < end_row + 1; row++, lcol = 0) {
+                GArray *atrributes_temp = g_array_new (FALSE, TRUE, sizeof (gconstpointer));
+                string = g_string_new(NULL);
+
 		VteRowData const* row_data = find_row_data(row);
                 gsize last_empty, last_nonempty;
                 vte::grid::column_t last_emptycol, last_nonemptycol;
@@ -6174,9 +6178,9 @@ Terminal::get_text(vte::grid::row_t start_row,
 
 					/* If we added text to the string, record its
 					 * attributes, one per byte. */
-					if (attributes) {
-						vte_g_array_fill(attributes,
-								&attr, string->len);
+					if (atrributes_temp) {
+						vte_g_array_fill(atrributes_temp, 
+                                                                &attr, string->len);
 					}
 				}
 
@@ -6184,6 +6188,8 @@ Terminal::get_text(vte::grid::row_t start_row,
 			}
 		}
 
+                size = size + string->len;
+
                 /* Empty cells of nondefault background color can appear anywhere in a line,
                  * not just at the end, e.g. between "foo" and "bar" here:
                  *   echo -e '\e[46mfoo\e[K\e[7Gbar\e[m'
@@ -6204,9 +6210,11 @@ Terminal::get_text(vte::grid::row_t start_row,
                                 }
                         }
                         if (pcell == NULL) {
+                                size -= string->len - last_nonempty;
                                 g_string_truncate(string, last_nonempty);
-                                if (attributes)
-                                        g_array_set_size(attributes, string->len);
+                                if (atrributes_temp) {
+                                        g_array_set_size(atrributes_temp, string->len);
+                                }
                                 attr.column = last_nonemptycol;
                         }
                 }
@@ -6218,6 +6226,7 @@ Terminal::get_text(vte::grid::row_t start_row,
 		/* Add a newline in block mode. */
 		if (block) {
 			string = g_string_append_c(string, '\n');
+                        size++;
 		}
 		/* Else, if the last visible column on this line was in range and
 		 * not soft-wrapped, append a newline. */
@@ -6226,15 +6235,36 @@ Terminal::get_text(vte::grid::row_t start_row,
 			/* XXX need to clear row->soft_wrap on deletion! */
                         if (!m_screen->row_data->is_soft_wrapped(row)) {
 				string = g_string_append_c(string, '\n');
+                                size++;
 			}
 		}
 
 		/* Make sure that the attributes array is as long as the string. */
-		if (attributes) {
-			vte_g_array_fill (attributes, &attr, string->len);
+		if (atrributes_temp) {
+			vte_g_array_fill (atrributes_temp, &attr, string->len);
 		}
+
+                string_list = g_list_prepend (string_list, string->str);
+                attributes_list = g_list_prepend (attributes_list, atrributes_temp);
+                g_string_free (string, FALSE);
 	}
 
+        string = g_string_new_len (NULL, size);
+        string_list = g_list_reverse (string_list);
+        for (GList *lp = string_list; lp != NULL; lp = lp->next) {
+                g_string_append (string, (gchar*) lp->data);
+                g_free ((gchar*) lp->data);
+        }
+        g_list_free (string_list);
+
+        attributes = g_array_sized_new (FALSE, TRUE, sizeof (gconstpointer), size);
+        attributes_list = g_list_reverse (attributes_list);
+        for (GList *lp = attributes_list; lp != NULL; lp = lp->next) {
+                g_array_append_vals (attributes, ((GArray*) lp->data)->data, ((GArray*) lp->data)->len);
+                g_array_free ((GArray*) lp->data, TRUE);
+        }
+        g_list_free (attributes_list);
+
 	/* Sanity check. */
         if (attributes != nullptr)
                 g_assert_cmpuint(string->len, ==, attributes->len);
@@ -6642,8 +6672,9 @@ Terminal::select_all()
 
 	m_selecting_had_delta = TRUE;
 
-        m_selection_resolved.set({m_screen->insert_delta, 0},
-                                 {_vte_ring_next(m_screen->row_data), 0});
+        m_selection_resolved.set ({ _vte_ring_delta (m_screen->row_data), 0 },
+                                  { _vte_ring_next  (m_screen->row_data), 0 });
+
 
 	_vte_debug_print(VTE_DEBUG_SELECTION, "Selecting *all* text.\n");
 
-- 
2.33.0

