From cbaa17f76b231aaea2494b1b976cbdf43c622768 Mon Sep 17 00:00:00 2001 From: Ian Griffiths <6thimage@gmail.com> Date: Thu, 14 May 2015 22:12:13 +0100 Subject: [PATCH] Added ability to create file from stdin Running 'echo "hi"|mousepad' will place 'hi' into the contents of an unsaved document. This functionality does not affect the opening of files, with 'echo "hi"|mousepad 1.txt' opening 2 documents, 1.txt as normal and an unsaved document with 'hi' as its contents. Signed-off-by: Ian Griffiths <6thimage@gmail.com> --- mousepad/mousepad-application.c | 7 ++++++- mousepad/mousepad-file.c | 44 +++++++++++++++++++++++++++++++++++++++++ mousepad/mousepad-file.h | 4 ++++ mousepad/mousepad-window.c | 40 +++++++++++++++++++++++++++++++++++++ mousepad/mousepad-window.h | 3 +++ 5 files changed, 97 insertions(+), 1 deletion(-) diff --git a/mousepad/mousepad-application.c b/mousepad/mousepad-application.c index b2e09aa..2c5fb9b 100644 --- a/mousepad/mousepad-application.c +++ b/mousepad/mousepad-application.c @@ -278,6 +278,7 @@ mousepad_application_new_window_with_files (MousepadApplication *application, { GtkWidget *window; gboolean succeed = FALSE; + gboolean stdin_succeed = FALSE; MousepadDocument *document; g_return_if_fail (MOUSEPAD_IS_APPLICATION (application)); @@ -293,8 +294,12 @@ mousepad_application_new_window_with_files (MousepadApplication *application, if (working_directory && filenames && g_strv_length (filenames)) succeed = mousepad_window_open_files (MOUSEPAD_WINDOW (window), working_directory, filenames); + /* try to load stdin */ + if (!isatty (STDIN_FILENO)) + stdin_succeed = mousepad_window_open_stream (MOUSEPAD_WINDOW (window), STDIN_FILENO); + /* open an empty document */ - if (succeed == FALSE) + if ((succeed == FALSE) && (stdin_succeed == FALSE)) { /* create a new document */ document = mousepad_document_new (); diff --git a/mousepad/mousepad-file.c b/mousepad/mousepad-file.c index b44acec..e3d29f8 100644 --- a/mousepad/mousepad-file.c +++ b/mousepad/mousepad-file.c @@ -692,6 +692,50 @@ mousepad_file_open (MousepadFile *file, } +gint +mousepad_file_open_stream (MousepadFile *file, + gint stream, + GError **error) +{ + GtkTextIter start_iter; + gchar read_buffer[4092], *text; + gint read_length; + gsize text_size; + + g_return_val_if_fail (MOUSEPAD_IS_FILE (file), FALSE); + g_return_val_if_fail (GTK_IS_TEXT_BUFFER (file->buffer), FALSE); + g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + + gtk_text_buffer_get_start_iter (file->buffer, &start_iter); + + while (1) + { + /* read from standard in in 4k blocks */ + read_length = read (stream, read_buffer, 4092); + + /* check for read errors */ + if (read_length < 0) + return FALSE; + + /* if read length is zero, we have reached the end */ + if (read_length == 0) + break; + + /* convert the text */ + text = g_locale_to_utf8 (read_buffer, read_length, NULL, &text_size, error); + + /* add it to the buffer */ + gtk_text_buffer_insert (file->buffer, &start_iter, text, text_size); + + g_free (text); + } + + /* do not reset the modified flag of the buffer */ + + return TRUE; +} + + gboolean mousepad_file_save (MousepadFile *file, diff --git a/mousepad/mousepad-file.h b/mousepad/mousepad-file.h index 134a772..eb53487 100644 --- a/mousepad/mousepad-file.h +++ b/mousepad/mousepad-file.h @@ -86,6 +86,10 @@ gint mousepad_file_open (MousepadFile const gchar *template_filename, GError **error); +gint mousepad_file_open_stream (MousepadFile *file, + gint stream, + GError **error); + gboolean mousepad_file_save (MousepadFile *file, GError **error); diff --git a/mousepad/mousepad-window.c b/mousepad/mousepad-window.c index f1acc2b..dc048aa 100644 --- a/mousepad/mousepad-window.c +++ b/mousepad/mousepad-window.c @@ -1619,6 +1619,46 @@ mousepad_window_open_files (MousepadWindow *window, +gboolean +mousepad_window_open_stream (MousepadWindow *window, + const gint stream) +{ + MousepadDocument *document; + GError *error = NULL; + gint result; + + g_return_val_if_fail (MOUSEPAD_IS_WINDOW (window), FALSE); + + /* new document */ + document = mousepad_document_new (); + + /* make sure it's not a floating object */ + g_object_ref_sink (G_OBJECT (document)); + + /* set the passed encoding */ + mousepad_file_set_encoding (document->file, MOUSEPAD_ENCODING_UTF_8); + + /* lock the undo manager */ + gtk_source_buffer_begin_not_undoable_action (GTK_SOURCE_BUFFER (document->buffer)); + + /* read the content into the buffer */ + result = mousepad_file_open_stream (document->file, STDIN_FILENO, &error); + + /* release the lock */ + gtk_source_buffer_end_not_undoable_action (GTK_SOURCE_BUFFER (document->buffer)); + + if (result == TRUE) + /* add the document to the window */ + mousepad_window_add (window, document); + else + /* release the document */ + g_object_unref (G_OBJECT (document)); + + return (result == TRUE); +} + + + void mousepad_window_add (MousepadWindow *window, MousepadDocument *document) diff --git a/mousepad/mousepad-window.h b/mousepad/mousepad-window.h index 9cef2ec..1326174 100644 --- a/mousepad/mousepad-window.h +++ b/mousepad/mousepad-window.h @@ -54,6 +54,9 @@ gboolean mousepad_window_open_files (MousepadWindow *window, const gchar *working_directory, gchar **filenames); +gboolean mousepad_window_open_stream (MousepadWindow *window, + const gint stream); + void mousepad_window_show_preferences (MousepadWindow *window); G_END_DECLS -- 2.1.4