[latexila] LaTeX menu: implement util function in liblatexila
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [latexila] LaTeX menu: implement util function in liblatexila
- Date: Tue, 24 Oct 2017 18:44:54 +0000 (UTC)
commit e6991beb6fb7c9b7b7fc32a1d9bf913a06970144
Author: Sébastien Wilmet <swilmet gnome org>
Date: Tue Oct 24 20:26:58 2017 +0200
LaTeX menu: implement util function in liblatexila
docs/reference/latexila-sections.txt | 2 +-
src/latex_menu.vala | 69 ++----------------------
src/liblatexila/latexila-latex-menu.c | 96 ++++++++++++++++++++++++++++++++-
src/liblatexila/latexila-latex-menu.h | 5 ++-
4 files changed, 104 insertions(+), 68 deletions(-)
---
diff --git a/docs/reference/latexila-sections.txt b/docs/reference/latexila-sections.txt
index 114cbe2..a47c738 100644
--- a/docs/reference/latexila-sections.txt
+++ b/docs/reference/latexila-sections.txt
@@ -132,7 +132,7 @@ latexila_build_view_get_type
<SECTION>
<FILE>latex-menu</FILE>
-latexila_latex_menu_do_something
+latexila_latex_menu_insert_text
</SECTION>
<SECTION>
diff --git a/src/latex_menu.vala b/src/latex_menu.vala
index c45e137..9620d3a 100644
--- a/src/latex_menu.vala
+++ b/src/latex_menu.vala
@@ -473,72 +473,11 @@ public class LatexMenu : Gtk.ActionGroup
private void text_buffer_insert (string text_before, string text_after,
string? text_if_no_selection = null)
{
- return_if_fail (main_window.active_tab != null);
- Document active_document = main_window.active_document;
-
- // we don't use the insert and selection_bound marks because we don't
- // know the order. With gtk_text_buffer_get_selection_bounds, we are certain
- // that "start" points to the start of the selection, where we must insert
- // "text_before".
-
- TextIter start, end;
- bool text_selected = active_document.get_selection_bounds (out start, out end);
-
- // take into account the current indentation
- string? text_before2 = null;
- string? text_after2 = null;
-
- if (text_before.contains ("\n") || text_after.contains ("\n"))
- {
- string current_indent = Tepl.iter_get_line_indentation (start);
-
- if (current_indent != "")
- {
- text_before2 = text_before.replace ("\n", @"\n$current_indent");
- text_after2 = text_after.replace ("\n", @"\n$current_indent");
- }
- }
-
- active_document.begin_user_action ();
-
- // insert around the selected text
- // move the cursor to the end
- if (text_selected)
- {
- TextMark mark_end = active_document.create_mark (null, end, false);
- active_document.insert (ref start, text_before2 ?? text_before, -1);
- active_document.get_iter_at_mark (out end, mark_end);
- active_document.insert (ref end, text_after2 ?? text_after, -1);
-
- active_document.get_iter_at_mark (out end, mark_end);
- active_document.delete_mark (mark_end);
- active_document.place_cursor (end);
- }
-
- // no selection
- else if (text_if_no_selection != null)
- active_document.insert_at_cursor (text_if_no_selection, -1);
-
- // no selection
- // move the cursor between the 2 texts inserted
- else
- {
- active_document.insert_at_cursor (text_before2 ?? text_before, -1);
-
- TextIter between;
- active_document.get_iter_at_mark (out between, active_document.get_insert ());
- TextMark mark = active_document.create_mark (null, between, true);
-
- active_document.insert_at_cursor (text_after2 ?? text_after, -1);
-
- active_document.get_iter_at_mark (out between, mark);
- active_document.delete_mark (mark);
- active_document.place_cursor (between);
- }
-
- active_document.end_user_action ();
+ Tepl.ApplicationWindow tepl_window =
+ Tepl.ApplicationWindow.get_from_gtk_application_window (main_window);
- main_window.active_view.grab_focus ();
+ Latexila.latex_menu_insert_text (tepl_window, text_before, text_after,
+ text_if_no_selection);
}
private string get_indentation ()
diff --git a/src/liblatexila/latexila-latex-menu.c b/src/liblatexila/latexila-latex-menu.c
index b0b853b..c6bd605 100644
--- a/src/liblatexila/latexila-latex-menu.c
+++ b/src/liblatexila/latexila-latex-menu.c
@@ -24,8 +24,102 @@
*/
#include "latexila-latex-menu.h"
+#include "latexila-utils.h"
+/* Temporarily public, will be made private when all GActions for the LaTeX and
+ * Math menus are implemented.
+ */
void
-latexila_latex_menu_do_something (void)
+latexila_latex_menu_insert_text (TeplApplicationWindow *tepl_window,
+ const gchar *text_before,
+ const gchar *text_after,
+ const gchar *text_if_no_selection)
{
+ TeplView *view;
+ GtkTextBuffer *buffer;
+ GtkTextIter selection_start;
+ GtkTextIter selection_end;
+ gboolean has_selection;
+ gchar *text_before_with_indent;
+ gchar *text_after_with_indent;
+
+ g_return_if_fail (TEPL_IS_APPLICATION_WINDOW (tepl_window));
+ g_return_if_fail (text_before != NULL);
+ g_return_if_fail (text_after != NULL);
+
+ view = tepl_tab_group_get_active_view (TEPL_TAB_GROUP (tepl_window));
+ g_return_if_fail (view != NULL);
+
+ buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
+
+ has_selection = gtk_text_buffer_get_selection_bounds (buffer,
+ &selection_start,
+ &selection_end);
+
+ /* Take into account the current indentation. */
+ {
+ gchar *current_indent;
+ gchar *newline_replacement;
+
+ current_indent = tepl_iter_get_line_indentation (&selection_start);
+ newline_replacement = g_strdup_printf ("\n%s", current_indent);
+
+ text_before_with_indent = latexila_utils_str_replace (text_before, "\n", newline_replacement);
+ text_after_with_indent = latexila_utils_str_replace (text_after, "\n", newline_replacement);
+
+ g_free (current_indent);
+ g_free (newline_replacement);
+ }
+
+ gtk_text_buffer_begin_user_action (buffer);
+
+ /* Insert around the selected text.
+ * Move the cursor to the end.
+ */
+ if (has_selection)
+ {
+ GtkTextMark *end_mark;
+ GtkTextIter end_iter;
+
+ end_mark = gtk_text_buffer_create_mark (buffer, NULL, &selection_end, FALSE);
+
+ gtk_text_buffer_insert (buffer, &selection_start, text_before_with_indent, -1);
+
+ gtk_text_buffer_get_iter_at_mark (buffer, &end_iter, end_mark);
+ gtk_text_buffer_delete_mark (buffer, end_mark);
+
+ gtk_text_buffer_insert (buffer, &end_iter, text_after_with_indent, -1);
+ gtk_text_buffer_place_cursor (buffer, &end_iter);
+ }
+ /* No selection */
+ else if (text_if_no_selection != NULL)
+ {
+ gtk_text_buffer_insert_at_cursor (buffer, text_if_no_selection, -1);
+ }
+ /* No selection, move the cursor between the two inserted chunks. */
+ else
+ {
+ GtkTextIter between_iter;
+ GtkTextMark *between_mark;
+
+ gtk_text_buffer_insert_at_cursor (buffer, text_before_with_indent, -1);
+
+ gtk_text_buffer_get_iter_at_mark (buffer,
+ &between_iter,
+ gtk_text_buffer_get_insert (buffer));
+ between_mark = gtk_text_buffer_create_mark (buffer, NULL, &between_iter, TRUE);
+
+ gtk_text_buffer_insert_at_cursor (buffer, text_after_with_indent, -1);
+
+ gtk_text_buffer_get_iter_at_mark (buffer, &between_iter, between_mark);
+ gtk_text_buffer_delete_mark (buffer, between_mark);
+ gtk_text_buffer_place_cursor (buffer, &between_iter);
+ }
+
+ gtk_text_buffer_end_user_action (buffer);
+
+ gtk_widget_grab_focus (GTK_WIDGET (view));
+
+ g_free (text_before_with_indent);
+ g_free (text_after_with_indent);
}
diff --git a/src/liblatexila/latexila-latex-menu.h b/src/liblatexila/latexila-latex-menu.h
index 628d311..e6d971d 100644
--- a/src/liblatexila/latexila-latex-menu.h
+++ b/src/liblatexila/latexila-latex-menu.h
@@ -24,7 +24,10 @@
G_BEGIN_DECLS
-void latexila_latex_menu_do_something (void);
+void latexila_latex_menu_insert_text (TeplApplicationWindow *tepl_window,
+ const gchar *text_before,
+ const gchar *text_after,
+ const gchar *text_if_no_selection);
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]