[gnome-latex: 145/205] Indent/unindent lines



commit 679a6fd7d71dcdda55be6cecac2c071dc5ac675f
Author: Sébastien Wilmet <sebastien wilmet gmail com>
Date:   Sat Dec 19 17:56:13 2009 +0100

    Indent/unindent lines

 TODO            |   2 -
 src/callbacks.c | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++------
 src/callbacks.h |   2 +
 src/ui.c        |   6 ++-
 src/ui.xml      |   3 ++
 5 files changed, 134 insertions(+), 18 deletions(-)
---
diff --git a/TODO b/TODO
index 06a8018..5c673cd 100644
--- a/TODO
+++ b/TODO
@@ -2,8 +2,6 @@ TODO LaTeXila
 
 [-] BibTeX support
 
-[-] indent/unindent lines
-
 [-] documentation
 
 [-] replace the notebook in the side pane by a combo box with a close button
diff --git a/src/callbacks.c b/src/callbacks.c
index 196cf20..b15abfe 100644
--- a/src/callbacks.c
+++ b/src/callbacks.c
@@ -54,6 +54,7 @@ static gboolean find_next_match (const gchar *what, GtkSourceSearchFlags flags,
 static void free_latexila (void);
 static void delete_auxiliaries_files (const gchar *filename);
 static void set_entry_background (GtkWidget *entry, gboolean error);
+static void insert_text_at_beginning_of_selected_lines (gchar *text);
 
 static gboolean save_list_opened_docs = FALSE;
 
@@ -694,6 +695,15 @@ cb_dvi_to_ps (void)
 
 void
 cb_tools_comment (void)
+{
+       if (latexila.active_doc == NULL)
+               return;
+
+       insert_text_at_beginning_of_selected_lines ("% ");
+}
+
+void
+cb_tools_uncomment (void)
 {
        if (latexila.active_doc == NULL)
                return;
@@ -705,18 +715,75 @@ cb_tools_comment (void)
        gint start_line = gtk_text_iter_get_line (&start);
        gint end_line = gtk_text_iter_get_line (&end);
 
+       gint nb_lines = gtk_text_buffer_get_line_count (buffer);
+
        gtk_text_buffer_begin_user_action (buffer);
+
        for (gint i = start_line ; i <= end_line ; i++)
        {
-               GtkTextIter iter;
-               gtk_text_buffer_get_iter_at_line (buffer, &iter, i);
-               gtk_text_buffer_insert (buffer, &iter, "% ", -1);
+               /* get the text of the line */
+               gtk_text_buffer_get_iter_at_line (buffer, &start, i);
+
+               // if last line
+               if (i == nb_lines - 1)
+                       gtk_text_buffer_get_end_iter (buffer, &end);
+               else
+                       gtk_text_buffer_get_iter_at_line (buffer, &end, i + 1);
+
+               gchar *line = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
+
+               /* find the first '%' character */
+               gint j = 0;
+               gint start_delete = -1;
+               gint stop_delete = -1;
+               while (line[j] != '\0')
+               {
+                       if (line[j] == '%')
+                       {
+                               start_delete = j;
+                               stop_delete = j + 1;
+                               if (line[j + 1] == ' ')
+                                       stop_delete++;
+                               break;
+                       }
+
+                       else if (line[j] != ' ' && line[j] != '\t')
+                               break;
+
+                       j++;
+               }
+
+               g_free (line);
+
+               if (start_delete == -1)
+                       continue;
+
+               gtk_text_buffer_get_iter_at_line_offset (buffer, &start, i, start_delete);
+               gtk_text_buffer_get_iter_at_line_offset (buffer, &end, i, stop_delete);
+               gtk_text_buffer_delete (buffer, &start, &end);
        }
+
        gtk_text_buffer_end_user_action (buffer);
 }
 
 void
-cb_tools_uncomment (void)
+cb_tools_indent (void)
+{
+       if (latexila.active_doc == NULL)
+               return;
+
+       if (latexila.prefs.spaces_instead_of_tabs)
+       {
+               gchar *spaces = g_strnfill (latexila.prefs.tab_width, ' ');
+               insert_text_at_beginning_of_selected_lines (spaces);
+               g_free (spaces);
+       }
+       else
+               insert_text_at_beginning_of_selected_lines ("\t");
+}
+
+void
+cb_tools_unindent (void)
 {
        if (latexila.active_doc == NULL)
                return;
@@ -728,35 +795,54 @@ cb_tools_uncomment (void)
        gint start_line = gtk_text_iter_get_line (&start);
        gint end_line = gtk_text_iter_get_line (&end);
 
+       gint nb_lines = gtk_text_buffer_get_line_count (buffer);
+
        gtk_text_buffer_begin_user_action (buffer);
 
        for (gint i = start_line ; i <= end_line ; i++)
        {
-               // get the text of the line
+               /* get the text of the line */
                gtk_text_buffer_get_iter_at_line (buffer, &start, i);
-               gtk_text_buffer_get_iter_at_line (buffer, &end, i + 1);
-               gchar *text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
 
-               // find the first '%' character
+               // if last line
+               if (i == nb_lines - 1)
+                       gtk_text_buffer_get_end_iter (buffer, &end);
+               else
+                       gtk_text_buffer_get_iter_at_line (buffer, &end, i + 1);
+
+               gchar *line = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
+
+               /* find the characters to delete */
                gint j = 0;
                gint start_delete = -1;
                gint stop_delete = -1;
-               while (text[j] != '\0')
+               gint nb_spaces = 0;
+               while (line[j] != '\0')
                {
-                       if (text[j] == '%')
+                       if (line[j] == ' ')
+                       {
+                               if (nb_spaces == 0)
+                                       start_delete = j;
+                               nb_spaces++;
+                               if (nb_spaces % latexila.prefs.tab_width == 0)
+                                       nb_spaces = 0;
+                       }
+                       else if (line[j] == '\t')
                        {
                                start_delete = j;
-                               stop_delete = j + 1;
-                               if (text[j + 1] == ' ')
-                                       stop_delete++;
-                               break;
+                               nb_spaces = 0;
                        }
-                       else if (text[j] != ' ' && text[j] != '\t')
+                       else
+                       {
+                               stop_delete = j;
                                break;
+                       }
 
                        j++;
                }
 
+               g_free (line);
+
                if (start_delete == -1)
                        continue;
 
@@ -1670,3 +1756,26 @@ set_entry_background (GtkWidget *entry, gboolean error)
                gtk_widget_modify_text (entry, GTK_STATE_NORMAL, NULL);
        }
 }
+
+static void
+insert_text_at_beginning_of_selected_lines (gchar *text)
+{
+       if (latexila.active_doc == NULL)
+               return;
+
+       GtkTextIter start, end;
+       GtkTextBuffer *buffer = GTK_TEXT_BUFFER (latexila.active_doc->source_buffer);
+       gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
+       
+       gint start_line = gtk_text_iter_get_line (&start);
+       gint end_line = gtk_text_iter_get_line (&end);
+
+       gtk_text_buffer_begin_user_action (buffer);
+       for (gint i = start_line ; i <= end_line ; i++)
+       {
+               GtkTextIter iter;
+               gtk_text_buffer_get_iter_at_line (buffer, &iter, i);
+               gtk_text_buffer_insert (buffer, &iter, text, -1);
+       }
+       gtk_text_buffer_end_user_action (buffer);
+}
diff --git a/src/callbacks.h b/src/callbacks.h
index d06a404..dffcac0 100644
--- a/src/callbacks.h
+++ b/src/callbacks.h
@@ -58,6 +58,8 @@ void cb_dvi_to_pdf (void);
 void cb_dvi_to_ps (void);
 void cb_tools_comment (void);
 void cb_tools_uncomment (void);
+void cb_tools_indent (void);
+void cb_tools_unindent (void);
 void cb_documents_save_all (void);
 void cb_documents_close_all (void);
 void cb_documents_previous (void);
diff --git a/src/ui.c b/src/ui.c
index a4fc1fe..890055b 100644
--- a/src/ui.c
+++ b/src/ui.c
@@ -120,7 +120,7 @@ static GtkActionEntry entries[] = {
                N_("Search for text"), G_CALLBACK (cb_find)},
        {"SearchReplace", GTK_STOCK_FIND_AND_REPLACE, N_("Replace..."), "<Control>H",
                N_("Search for and replace text"), G_CALLBACK (cb_replace)},
-       {"SearchGoToLine", GTK_STOCK_JUMP_TO, N_("Go to Line..."), "<Control>I",
+       {"SearchGoToLine", GTK_STOCK_JUMP_TO, N_("Go to Line..."), "<Control>G",
                N_("Go to a specific line"), G_CALLBACK (cb_go_to_line)},
        
        {"Build", NULL, N_("Build"), NULL, NULL, NULL},
@@ -146,6 +146,10 @@ static GtkActionEntry entries[] = {
        {"ToolsUncomment", NULL, N_("Uncomment"), "<Shift><Control>D",
                N_("Uncomment the selected lines (remove the character \"%\")"),
                G_CALLBACK (cb_tools_uncomment)},
+       {"ToolsIndent", GTK_STOCK_INDENT, N_("Indent"), "<Control>I",
+               N_("Indent the selected lines"), G_CALLBACK (cb_tools_indent)},
+       {"ToolsUnindent", GTK_STOCK_UNINDENT, N_("Unindent"), "<Shift><Control>I",
+               N_("Unindent the selected lines"), G_CALLBACK (cb_tools_unindent)},
 
        {"Documents", NULL, N_("Documents"), NULL, NULL, NULL},
        {"DocumentsSaveAll", GTK_STOCK_SAVE, N_("Save All"), "<Shift><Control>L",
diff --git a/src/ui.xml b/src/ui.xml
index f4ac7be..dbb5536 100644
--- a/src/ui.xml
+++ b/src/ui.xml
@@ -168,6 +168,9 @@ In the code, GtkUIManager is used to construct them.
     <menu action="Tools">
       <menuitem action="ToolsComment" />
       <menuitem action="ToolsUncomment" />
+      <separator />
+      <menuitem action="ToolsIndent" />
+      <menuitem action="ToolsUnindent" />
     </menu>
 
     <menu action="Documents">


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]