[gnome-latex: 144/205] Comment/uncomment selected lines



commit 21ed5f368dc383058b857144ec9b7604b2d43a5e
Author: Sébastien Wilmet <sebastien wilmet gmail com>
Date:   Sat Dec 19 00:03:35 2009 +0100

    Comment/uncomment selected lines

 TODO            |  1 -
 src/callbacks.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/callbacks.h |  2 ++
 src/ui.c        |  8 ++++++
 src/ui.xml      |  5 ++++
 5 files changed, 91 insertions(+), 1 deletion(-)
---
diff --git a/TODO b/TODO
index da9d5a4..06a8018 100644
--- a/TODO
+++ b/TODO
@@ -2,7 +2,6 @@ TODO LaTeXila
 
 [-] BibTeX support
 
-[-] comment/uncomment selected lines
 [-] indent/unindent lines
 
 [-] documentation
diff --git a/src/callbacks.c b/src/callbacks.c
index f86cb0f..196cf20 100644
--- a/src/callbacks.c
+++ b/src/callbacks.c
@@ -692,6 +692,82 @@ cb_dvi_to_ps (void)
        convert_document (_("DVI to PS"), ".dvi", latexila.prefs.command_dvips);
 }
 
+void
+cb_tools_comment (void)
+{
+       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, "% ", -1);
+       }
+       gtk_text_buffer_end_user_action (buffer);
+}
+
+void
+cb_tools_uncomment (void)
+{
+       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++)
+       {
+               // 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
+               gint j = 0;
+               gint start_delete = -1;
+               gint stop_delete = -1;
+               while (text[j] != '\0')
+               {
+                       if (text[j] == '%')
+                       {
+                               start_delete = j;
+                               stop_delete = j + 1;
+                               if (text[j + 1] == ' ')
+                                       stop_delete++;
+                               break;
+                       }
+                       else if (text[j] != ' ' && text[j] != '\t')
+                               break;
+
+                       j++;
+               }
+
+               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_documents_save_all (void)
 {
diff --git a/src/callbacks.h b/src/callbacks.h
index 6f50722..d06a404 100644
--- a/src/callbacks.h
+++ b/src/callbacks.h
@@ -56,6 +56,8 @@ void cb_view_pdf (void);
 void cb_view_ps (void);
 void cb_dvi_to_pdf (void);
 void cb_dvi_to_ps (void);
+void cb_tools_comment (void);
+void cb_tools_uncomment (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 62598fe..a4fc1fe 100644
--- a/src/ui.c
+++ b/src/ui.c
@@ -139,6 +139,14 @@ static GtkActionEntry entries[] = {
        {"viewPS", "view_ps", N_("View PS"), NULL,
                N_("View the PostScript file"), G_CALLBACK (cb_view_ps)},
 
+       {"Tools", NULL, N_("Tools"), NULL, NULL, NULL},
+       {"ToolsComment", NULL, N_("Comment"), "<Control>D",
+               N_("Comment the selected lines (add the character \"%\")"),
+               G_CALLBACK (cb_tools_comment)},
+       {"ToolsUncomment", NULL, N_("Uncomment"), "<Shift><Control>D",
+               N_("Uncomment the selected lines (remove the character \"%\")"),
+               G_CALLBACK (cb_tools_uncomment)},
+
        {"Documents", NULL, N_("Documents"), NULL, NULL, NULL},
        {"DocumentsSaveAll", GTK_STOCK_SAVE, N_("Save All"), "<Shift><Control>L",
                N_("Save all open files"), G_CALLBACK (cb_documents_save_all)},
diff --git a/src/ui.xml b/src/ui.xml
index faefd77..f4ac7be 100644
--- a/src/ui.xml
+++ b/src/ui.xml
@@ -165,6 +165,11 @@ In the code, GtkUIManager is used to construct them.
       </menu>
     </menu>
 
+    <menu action="Tools">
+      <menuitem action="ToolsComment" />
+      <menuitem action="ToolsUncomment" />
+    </menu>
+
     <menu action="Documents">
       <menuitem action="DocumentsSaveAll" />
       <menuitem action="DocumentsCloseAll" />


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