Example #1
0
static void on_config_file_clicked(G_GNUC_UNUSED GtkWidget *widget, gpointer user_data)
{
	const gchar *file_name = user_data;
	GeanyFiletype *ft = NULL;

	if (strstr(file_name, G_DIR_SEPARATOR_S "filetypes."))
		ft = filetypes_index(GEANY_FILETYPES_CONF);

	if (g_file_test(file_name, G_FILE_TEST_EXISTS))
		document_open_file(file_name, FALSE, ft, NULL);
	else
	{
		gchar *utf8_filename = utils_get_utf8_from_locale(file_name);
		gchar *base_name = g_path_get_basename(file_name);
		gchar *global_file = g_build_filename(geany->app->datadir, base_name, NULL);
		gchar *global_content = NULL;

		/* if the requested file doesn't exist in the user's config dir, try loading the file
		 * from the global data directory and use its contents for the newly created file */
		if (g_file_test(global_file, G_FILE_TEST_EXISTS))
			g_file_get_contents(global_file, &global_content, NULL, NULL);

		document_new_file(utf8_filename, ft, global_content);

		g_free(utf8_filename);
		g_free(base_name);
		g_free(global_file);
		g_free(global_content);
	}
}
Example #2
0
static PyObject*
Document_new_file(PyObject *self, PyObject *args, PyObject *kwargs)
{
	gchar *filename = NULL, *initial_text = NULL;
	Filetype *filetype = NULL;
	PyObject *py_ft = NULL;
	GeanyDocument *doc;
	GeanyFiletype *ft = NULL;
	static gchar *kwlist[] = { "filename", "filetype", "initial_text", NULL };

	if (PyArg_ParseTupleAndKeywords(args, kwargs, "|zOz", kwlist,
		&filename, &py_ft, &initial_text))
	{
		if (py_ft != NULL  && py_ft != Py_None)
		{
			filetype = (Filetype *) py_ft;
			if (filetype->ft != NULL)
				ft = filetype->ft;
		}
		doc = document_new_file(filename, ft, initial_text);
		if (DOC_VALID(doc))
			return (PyObject *) Document_create_new_from_geany_document(doc);
	}
	Py_RETURN_NONE;
}
Example #3
0
/* name should be in UTF-8, and can have a path. */
static void
show_output(const gchar * std_output, const gchar * name, const gchar * force_encoding,
	    gint filetype_new_file)
{
	gint page;
	GtkNotebook *book;
	GeanyDocument *doc, *cur_doc;

	if (std_output)
	{
		cur_doc = document_get_current();
		doc = document_find_by_filename(name);
		if (doc == NULL)
		{
			doc = document_new_file(name,
						   filetypes[filetype_new_file],
						   std_output);
		}
		else
		{
			sci_set_text(doc->editor->sci, std_output);
			book = GTK_NOTEBOOK(geany->main_widgets->notebook);
			page = gtk_notebook_page_num(book, GTK_WIDGET(doc->editor->sci));
			gtk_notebook_set_current_page(book, page);
		}
		document_set_text_changed(doc, FALSE);
		document_set_encoding(doc, (force_encoding ? force_encoding : "UTF-8"));
		navqueue_goto_line(cur_doc, document_get_current(), 1);
	}
	else
	{
		ui_set_statusbar(FALSE, _("Could not parse the output of command"));
	}
}
Example #4
0
/* ---------------------------------------------------------------------
 * Callback when the menu item is clicked.
 * ---------------------------------------------------------------------
 */
static void
menu_item_activate(guint key_id)
{
	GtkWidget* dialog;
	GtkWidget* dialog_new = NULL;
	GtkWidget* dialog_entry;
	GtkTreeModel* completion_list;
	GeanyDocument* current_doc = document_get_current();
	gchar *chosen_path;
	const gchar *chosen_file;
	gint response;

	log_func();

	if(current_doc == NULL || current_doc->file_name == NULL || current_doc->file_name[0] == '\0')
		return;
		
	/* Build current directory listing */
	directory_ref = g_path_get_dirname(current_doc->file_name);
	completion_list = build_file_list(directory_ref, "");

	/* Create the user dialog and get response */
	dialog_entry = create_dialog(&dialog, completion_list);
	response = gtk_dialog_run(GTK_DIALOG(dialog));

	/* Filename */
	chosen_file = gtk_entry_get_text(GTK_ENTRY(dialog_entry));
	/* Path + Filename */
	chosen_path = g_build_filename(directory_ref, chosen_file, NULL);

	if ( response == GTK_RESPONSE_ACCEPT )
	{
		log_debug("Trying to open: %s", chosen_path);
		if ( ! g_file_test(chosen_path, G_FILE_TEST_EXISTS) )
		{
			log_debug("File not found.");

			dialog_new = gtk_message_dialog_new(GTK_WINDOW(geany_data->main_widgets->window),
													GTK_DIALOG_MODAL,
													GTK_MESSAGE_QUESTION,
													GTK_BUTTONS_OK_CANCEL,
													_("%s not found, create it?"), chosen_file);
			gtk_window_set_title(GTK_WINDOW(dialog_new), "Geany");
			if(gtk_dialog_run(GTK_DIALOG(dialog_new)) == GTK_RESPONSE_OK)
			{
				document_new_file(chosen_path, current_doc->file_type, NULL);
				document_set_text_changed(document_get_current(), TRUE);
			}
			gtk_widget_destroy(dialog_new);
		}
		else
			document_open_file(chosen_path, FALSE, NULL, NULL);
	}

	/* Freeing memory */
	gtk_widget_destroy(dialog);
	g_free(directory_ref);
	g_object_unref (completion_list);
}
Example #5
0
gboolean geany_dbus_application_new_document (GeanyDBusApplication* self) {
	gboolean result = FALSE;
	gboolean _tmp0_ = FALSE;
	g_return_val_if_fail (self != NULL, FALSE);
	if (document_new_file (NULL, NULL, NULL) != NULL) {
		_tmp0_ = TRUE;
	} else {
		_tmp0_ = FALSE;
	}
	result = _tmp0_;
	return result;
}
Example #6
0
/**
 * \brief the function updates the current document
 */
static gint run_filter( ScintillaObject *sci )
{
    int r , ret = 0 ;
    gchar *result = NULL ;

    gms_command = gms_get_str_command(gms_hnd);
    r = system( gms_command ) ;
    if ( r != 0 )
    {
        GtkWidget *dlg ;
        result = read_result_file( gms_get_error_filename(gms_hnd) ) ;

        dlg = gtk_message_dialog_new( GTK_WINDOW(geany->main_widgets->window),
                        GTK_DIALOG_DESTROY_WITH_PARENT,
                        GTK_MESSAGE_ERROR,
                        GTK_BUTTONS_CLOSE,
                        "%s", result);

        gtk_dialog_run(GTK_DIALOG(dlg));
        gtk_widget_destroy(GTK_WIDGET(dlg)) ;
		ret = -1 ;
    }
    else
    {
        result = read_result_file( gms_get_out_filename(gms_hnd) ) ;

        if ( gms_get_output_mode( gms_hnd) == OUT_CURRENT_DOC )
        {
            if ( gms_get_input_mode( gms_hnd) != IN_SELECTION )
                select_entirely_doc(  sci  ) ;

            update_doc( sci, result ) ;
        }
        else
        {
            document_new_file( NULL, NULL, result ) ;
        }
    }
    GMS_G_FREE( result ) ;

	return ret ;
}
/* ---------------------------------------------------------------------
 *  Callback when the menu item is clicked.
 * ---------------------------------------------------------------------
 */
static void
menu_item_activate(guint key_id)
{
	GeanyDocument* current_doc = document_get_current();
	GeanyDocument* new_doc = NULL;
	guint nb_documents = geany->documents_array->len;

	gchar* extension = NULL;	/* e.g. : "hpp" */

	GSList* p_extensions_to_test = NULL;	/* e.g. : ["cpp", "cxx", ...] */

	GSList* filenames_to_test = NULL;	/* e.g. : ["f.cpp", "f.cxx", ...] */

	GSList* iter_lang = NULL;
	GSList* iter_ext = NULL;
	GSList* iter_filename = NULL;
	gint i=0;

	gchar* dirname = NULL;
	gchar* basename = NULL;
	gchar* basename_no_extension = NULL;

	gchar* p_str = NULL;	/* Local variables, used as temporary buffers */
	gchar* p_str2 = NULL;

	log_func();
	log_debug("current_doc->file_name == %s", current_doc->file_name);
	log_debug("geany->documents_array->len == %d", geany->documents_array->len);

	if(current_doc != NULL && current_doc->file_name != NULL && current_doc->file_name[0] != '\0')
	{
		/* Get the basename, e.g. : "/home/me/file.cpp" -> "file.cpp" */
		basename = g_path_get_basename(current_doc->file_name);

		if(g_utf8_strlen(basename, -1) < 2)
			goto free_mem;

		log_debug("basename == %s", basename);

		/* Get the extension , e.g. : "cpp" */
		extension = get_extension(basename);

		if(extension == NULL || g_utf8_strlen(extension, -1) == 0)
			goto free_mem;

		log_debug("extension == %s", extension);

		/* Get the basename without any extension */
		basename_no_extension = copy_and_remove_extension(basename);
		if(basename_no_extension == NULL || g_utf8_strlen(basename_no_extension, -1) == 0)
			goto free_mem;

		/* Identify the language and whether the file is a header or an implementation. */
		/* For each recognized language : */
		for(iter_lang = languages ; iter_lang != NULL ; iter_lang = iter_lang->next)
		{
			Language* lang = (Language*)(iter_lang->data);

			/* Test the headers : */
			if(g_slist_find_custom(lang->head_extensions, extension, (GCompareFunc)(&compare_strings)) != NULL)
			{
				p_extensions_to_test = lang->impl_extensions;
				break;
			}

			/* Test the implementations : */
			else if(g_slist_find_custom(lang->impl_extensions, extension, (GCompareFunc)(&compare_strings)) != NULL)
			{
				p_extensions_to_test = lang->head_extensions;
				break;
			}
		}

		if(p_extensions_to_test == NULL)
			goto free_mem;

#ifdef CODE_NAVIGATION_DEBUG
		log_debug("extension known !");
		log_debug("p_extensions_to_test : ");
		g_slist_foreach(p_extensions_to_test, (GFunc)(&log_debug), NULL);
#endif

		/* Build a list of filenames to test : */
		filenames_to_test = NULL;
		for(iter_ext = p_extensions_to_test ; iter_ext != NULL ; iter_ext = iter_ext->next)
		{
			p_str = g_strdup_printf("%s.%s", basename_no_extension, (const gchar*)(iter_ext->data));
			filenames_to_test = g_slist_prepend(filenames_to_test, p_str);
		}

		filenames_to_test = g_slist_reverse(filenames_to_test);

#ifdef CODE_NAVIGATION_DEBUG
		log_debug("filenames to test :");
		g_slist_foreach(filenames_to_test, (GFunc)(&log_debug), NULL);
#endif

		/* First : look for a corresponding file in the opened files.
		 * If found, open it. */
		for(i=0 ; i < nb_documents ; i++)
		{
			new_doc = document_index(i);

			for(iter_filename = filenames_to_test ; iter_filename != NULL ; iter_filename = iter_filename->next)
			{
				p_str = g_path_get_basename(new_doc->file_name);

				log_debug("comparing \"%s\" and \"%s\"", (const gchar*)(iter_filename->data), p_str);
				if(utils_str_equal((const gchar*)(iter_filename->data), p_str))
				{
					log_debug("FOUND !");
					g_free(p_str);

					p_str = g_locale_from_utf8(new_doc->file_name, -1, NULL, NULL, NULL);
					document_open_file(p_str, FALSE, NULL, NULL);
					g_free(p_str);
					goto free_mem;
				}
				g_free(p_str);
			}
		}

		/* Second : if not found, look for a corresponding file in the same directory.
		 * If found, open it.
		 */
		/* -> compute dirname */
		dirname = g_path_get_dirname(current_doc->real_path);
		if(dirname == NULL)
			goto free_mem;

		log_debug("dirname == \"%s\"", dirname);

		/* -> try all the extensions we should test */
		for(iter_ext = p_extensions_to_test ; iter_ext != NULL ; iter_ext = iter_ext->next)
		{
			p_str = g_strdup_printf(	"%s" G_DIR_SEPARATOR_S "%s.%s",
										dirname, basename_no_extension, (const gchar*)(iter_ext->data));

			p_str2 = g_locale_from_utf8(p_str, -1, NULL, NULL, NULL);
			g_free(p_str);

			log_debug("trying to open the file \"%s\"\n", p_str2);

			/* Try without read-only and in read-only mode */
			if(	document_open_file(p_str2, FALSE, NULL, NULL) != NULL ||
				document_open_file(p_str2, TRUE, NULL, NULL) != NULL)
			{
				g_free(p_str2);
				goto free_mem;
			}
			g_free(p_str2);
		}

		/* Third : if not found, ask the user if he wants to create it or not. */
		{
			p_str = g_strdup_printf("%s.%s", basename_no_extension, (const gchar*)(p_extensions_to_test->data));

			GtkWidget* dialog = gtk_message_dialog_new(	GTK_WINDOW(geany_data->main_widgets->window),
														GTK_DIALOG_MODAL,
														GTK_MESSAGE_QUESTION,
														GTK_BUTTONS_OK_CANCEL,
														_("%s not found, create it?"), p_str);
			gtk_window_set_title(GTK_WINDOW(dialog), "Geany");
			if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
			{
				p_str2 = g_strdup_printf(	"%s" G_DIR_SEPARATOR_S "%s", dirname, p_str);

				document_new_file(p_str2, current_doc->file_type, NULL);
				document_set_text_changed(document_get_current(), TRUE);

				g_free(p_str2);
			}

			log_debug("DESTROY");

			gtk_widget_destroy(dialog);

			g_free(p_str);
		}

		/* Free the memory */
free_mem:
		g_slist_foreach(filenames_to_test, (GFunc)(&g_free), NULL);
		g_free(dirname);
		g_free(basename_no_extension);
		g_free(extension);
		g_free(basename);
	}
}
Example #8
0
void on_new1_activate(GtkMenuItem *menuitem, gpointer user_data)
{
	document_new_file(NULL, NULL, NULL);
}
Example #9
0
/* handler that opens the current filetype's configuration file */
static void
open_current_filetype_conf_handler (GtkWidget  *widget,
                                    gpointer    data)
{
  GeanyDocument *doc;
  
  (void)widget;
  (void)data;
  
  doc = document_get_current ();
  if (DOC_VALID (doc)) {
    gchar  *path_read;
    gchar  *path_write;
    GError *err = NULL;
    
    path_write = ggd_file_type_manager_get_conf_path (doc->file_type->id,
                                                      GGD_PERM_W | GGD_PERM_NOCREAT,
                                                      &err);
    if (! path_write) {
      msgwin_status_add (_("Failed to find configuration file "
                           "for file type \"%s\": %s"),
                         doc->file_type->name, err->message);
      g_error_free (err);
    } else {
      gchar *text = NULL;
      gchar *path_write_u8;
      
      path_read = ggd_file_type_manager_get_conf_path (doc->file_type->id,
                                                       GGD_PERM_R, &err);
      if (! path_read) {
        text = g_strdup (_(
          "# Configuration for this file type doesn't exist yet.\n"
          "# To create it, just write it in this file and save it. For the description\n"
          "# of the syntax of this file, please refer to the manual.\n"
        ));
      } else {
        gchar  *content = NULL;
        gsize   length;
        
        if (! g_file_get_contents (path_read, &content, &length, &err)) {
          gchar *display_path_read;
          
          display_path_read = g_filename_display_name (path_read);
          g_warning (_("Failed to load file \"%s\": %s"),
                     display_path_read, err->message);
          g_free (display_path_read);
          g_error_free (err);
        } else {
          text = encodings_convert_to_utf8 (content, length, NULL);
          g_free (content);
        }
        g_free (path_read);
      }
      path_write_u8 = utils_get_utf8_from_locale (path_write);
      /* It's no Ruby, but it is the closest one I've found. It has:
       *  - # comments
       *  - multi-line double-quoted strings
       */
      document_new_file (path_write_u8, filetypes[GEANY_FILETYPES_RUBY], text);
      g_free (path_write_u8);
      g_free (text);
      g_free (path_write);
    }
  }
}