示例#1
0
文件: main.c 项目: chaos8/l3afpad
static void parse_args(gint argc, gchar **argv, FileInfo *fi)
{
	EncArray *encarray;
	gint i;
	GError *error = NULL;

	GOptionContext *context;
	gchar *opt_codeset = NULL;
	gint opt_tab_width = 0;
	gboolean opt_jump = 0;
	gboolean opt_version = FALSE;
	GOptionEntry entries[] =
	{
		{ "codeset", 0, 0, G_OPTION_ARG_STRING, &opt_codeset, "Set codeset to open file", "CODESET" },
		{ "tab-width", 0, 0, G_OPTION_ARG_INT, &opt_tab_width, "Set tab width", "WIDTH" },
		{ "jump", 0, 0, G_OPTION_ARG_INT, &opt_jump, "Jump to specified line", "LINENUM" },
		{ "version", 0, 0, G_OPTION_ARG_NONE, &opt_version, "Show version number", NULL },
		{ NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL }
	};

	context = g_option_context_new("[filename]");
	g_option_context_add_main_entries(context, entries, PACKAGE);
	g_option_context_add_group(context, gtk_get_option_group(TRUE));
	g_option_context_set_ignore_unknown_options(context, FALSE);
	g_option_context_parse(context, &argc, &argv, &error);
	g_option_context_free(context);

	if (error) {
		g_print("%s: %s\n", PACKAGE, error->message);
		g_error_free(error);
		exit(-1);
	}
	if (opt_version) {
		g_print("%s\n", PACKAGE_STRING);
		exit(0);
	}
	if (opt_codeset) {
		g_convert("TEST", -1, "UTF-8", opt_codeset, NULL, NULL, &error);
		if (error) {
			g_error_free(error);
			error = NULL;
		} else {
			g_free(fi->charset);
			fi->charset = g_strdup(opt_codeset);
		}
	}
	if (opt_tab_width)
		indent_set_default_tab_width(opt_tab_width);
	if (opt_jump)
		jump_linenum = opt_jump;

	if (fi->charset
		&& (g_ascii_strcasecmp(fi->charset, get_default_charset()) != 0)
		&& (g_ascii_strcasecmp(fi->charset, "UTF-8") != 0)) {
		encarray = get_encoding_items(get_encoding_code());
		for (i = 0; i < ENCODING_MAX_ITEM_NUM; i++)
			if (encarray->item[i])
				if (g_ascii_strcasecmp(fi->charset, encarray->item[i]) == 0)
					break;
		if (i == ENCODING_MAX_ITEM_NUM)
			fi->charset_flag = TRUE;
	}

	if (argc >= 2)
		fi->filename = parse_file_uri(argv[1]);
}
示例#2
0
static void parse_args(gint argc, gchar **argv, FileInfo *fi)
{
	EncArray *encarray;
	gint i;
	GError *error = NULL;
	
#if GLIB_CHECK_VERSION(2, 6, 0)
	GOptionContext *context;
	gchar *opt_codeset = NULL;
	gint opt_tab_width = 0;
	gboolean opt_jump = 0;
	gboolean opt_version = FALSE;
	GOptionEntry entries[] = 
	{
		{ "codeset", 0, 0, G_OPTION_ARG_STRING, &opt_codeset, "Set codeset to open file", "CODESET" },
		{ "tab-width", 0, 0, G_OPTION_ARG_INT, &opt_tab_width, "Set tab width", "WIDTH" },
		{ "jump", 0, 0, G_OPTION_ARG_INT, &opt_jump, "Jump to specified line", "LINENUM" },
		{ "version", 0, 0, G_OPTION_ARG_NONE, &opt_version, "Show version number", NULL },
		{ NULL }
	};
	//Structure defined in glib for entries and commandline option parser
	//4th argument is an enum defined in glib
	context = g_option_context_new("[Filename]");		//Creates new context for option parsing
	g_option_context_add_main_entries(context, entries, PACKAGE);		//PACKAGE == Translation Domain ??
	g_option_context_add_group(context, gtk_get_option_group(TRUE));
	g_option_context_set_ignore_unknown_options(context, FALSE);		//sets error when unknown command
	g_option_context_parse(context, &argc, &argv, &error);			//passes the arguments
	g_option_context_free(context);						//free for gcontext
	
	if (error) {
		g_print("%s: %s\n", PACKAGE, error->message);			//things to do for each option
		g_error_free(error);
		exit(-1);
	}
	if (opt_version) {
		g_print("%s\n", PACKAGE_STRING);
		exit(0);
	}
	if (opt_codeset) {
		g_convert("TEST", -1, "UTF-8", opt_codeset, NULL, NULL, &error);
		if (error) {
			g_error_free(error);
			error = NULL;
		} else {
			g_free(fi->charset);
			fi->charset = g_strdup(opt_codeset);
		}
	}
	if (opt_tab_width)
		indent_set_default_tab_width(opt_tab_width);
	if (opt_jump)
		jump_linenum = opt_jump;
	
#else									//code for older version of glib
	gint c;								//getopt also parses the command line argument
	
	do {
		c = getopt_long(argc, argv, "", longopts, NULL);
		switch (c) {
		case 0:
			if (optarg) {
				g_convert("TEST", -1, "UTF-8", optarg, NULL, NULL, &error);
				if (error) {
					g_error_free(error);
					error = NULL;
				} else {
					g_free(fi->charset);
					fi->charset = g_strdup(optarg);
				}
			}
			break;
		case 't':
			if (optarg)
				indent_set_default_tab_width(atoi(optarg));
			break;
		case 'j':
			if (optarg)
				jump_linenum = atoi(optarg);
			break;
		case 'v':
			g_print("%s\n", PACKAGE_STRING);
			exit(0);
		case '?':
			print_usage();
			exit(0);
		}
	} while (c != -1);
#endif
	
	if (fi->charset 							//Find fileinfo encarray data structure
		&& (g_strcasecmp(fi->charset, get_default_charset()) != 0)
		&& (g_strcasecmp(fi->charset, "UTF-8") != 0)) {
		encarray = get_encoding_items(get_encoding_code());
		for (i = 0; i < ENCODING_MAX_ITEM_NUM; i++)
			if (encarray->item[i])
				if (g_strcasecmp(fi->charset, encarray->item[i]) == 0)
					break;
		if (i == ENCODING_MAX_ITEM_NUM)
			fi->charset_flag = TRUE;
	}	
#if GLIB_CHECK_VERSION(2, 6, 0)
	if (argc >= 2)
		fi->filename = parse_file_uri(argv[1]);
#else
	if (optind < argc)
		fi->filename = parse_file_uri(argv[optind]);
#endif
}
示例#3
0
文件: main.c 项目: City-busz/l3afpad
gint main(gint argc, gchar **argv)
{
	Conf *conf;
	gchar *stdin_data = NULL;

	bindtextdomain(PACKAGE, LOCALEDIR);
	bind_textdomain_codeset(PACKAGE, "UTF-8");
	textdomain(PACKAGE);

	pub = g_malloc(sizeof(PublicData));
	pub->fi = g_malloc(sizeof(FileInfo));
	pub->fi->filename     = NULL;
	pub->fi->charset      = NULL;
	pub->fi->charset_flag = FALSE;
	pub->fi->lineend      = LF;

	parse_args(argc, argv, pub->fi);

#if !ENABLE_XINPUT2
	gdk_disable_multidevice();
#endif

	gtk_init(&argc, &argv);
	g_set_application_name(PACKAGE_NAME);

	selection_primary = gtk_clipboard_get(GDK_SELECTION_PRIMARY);

	pub->mw = create_main_window();

	conf = g_malloc(sizeof(Conf));
	conf->width       = 600;
	conf->height      = 400;
	conf->fontname    = g_strdup("Monospace 12");
	conf->wordwrap    = FALSE;
	conf->linenumbers = FALSE;
	conf->autoindent  = FALSE;
	conf->tabwidth    = get_current_tab_width();

	load_config_file(conf);

	gtk_window_set_default_size(
		GTK_WINDOW(pub->mw->window), conf->width, conf->height);
	set_text_font_by_name(pub->mw->view, conf->fontname);

	gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(
		gtk_ui_manager_get_widget(pub->mw->menubar, "/M/Options/WordWrap")),
		conf->wordwrap);
	gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(
		gtk_ui_manager_get_widget(pub->mw->menubar, "/M/Options/LineNumbers")),
		conf->linenumbers);
	indent_set_default_tab_width(conf->tabwidth);
	indent_refresh_tab_width(pub->mw->view);
	gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(
		gtk_ui_manager_get_widget(pub->mw->menubar, "/M/Options/AutoIndent")),
		conf->autoindent);

	gtk_widget_show_all(pub->mw->window);
	g_free(conf->fontname);
	g_free(conf);

#if ENABLE_EMACS
	check_emacs_key_theme(GTK_WINDOW(pub->mw->window), pub->mw->menubar);
#endif

	hlight_init(pub->mw->buffer);
	undo_init(pub->mw->view,
		gtk_ui_manager_get_widget(pub->mw->menubar, "/M/Edit/Undo"),
		gtk_ui_manager_get_widget(pub->mw->menubar, "/M/Edit/Redo"));
//	hlight_init(pub->mw->buffer);
	dnd_init(pub->mw->view);

	if (pub->fi->filename)
		file_open_real(pub->mw->view, pub->fi);
#ifdef G_OS_UNIX
	else
		stdin_data = gedit_utils_get_stdin();
#endif
	if (stdin_data) {
		gchar *str;
		GtkTextIter iter;

		str = g_convert(stdin_data, -1, "UTF-8",
			get_default_charset(), NULL, NULL, NULL);
		g_free(stdin_data);

//		gtk_text_buffer_set_text(buffer, "", 0);
		gtk_text_buffer_get_start_iter(pub->mw->buffer, &iter);
		gtk_text_buffer_insert(pub->mw->buffer, &iter, str, strlen(str));
		gtk_text_buffer_get_start_iter(pub->mw->buffer, &iter);
		gtk_text_buffer_place_cursor(pub->mw->buffer, &iter);
		gtk_text_buffer_set_modified(pub->mw->buffer, FALSE);
		gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(pub->mw->view), &iter, 0, FALSE, 0, 0);
		g_free(str);
	}

	if (jump_linenum) {
		GtkTextIter iter;

		gtk_text_buffer_get_iter_at_line(pub->mw->buffer, &iter, jump_linenum - 1);
		gtk_text_buffer_place_cursor(pub->mw->buffer, &iter);
//		gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(textview), &iter, 0.1, FALSE, 0.5, 0.5);
		scroll_to_cursor(pub->mw->buffer, 0.25);
	}

	set_main_window_title();
	indent_refresh_tab_width(pub->mw->view);
//	hlight_apply_all(pub->mw->buffer);

	gtk_main();

	return 0;
}