コード例 #1
0
ファイル: anjuta-modeline.c プロジェクト: VujinovM/anjuta
/**
 * anjuta_apply_modeline:
 * @editor: #IAnjutaEditor object
 *
 * Check the editor buffer to find a mode line and update the indentation
 * settings if found.
 * 
 * The mode line is special line used by the text editor to define settings for
 * the current file, typically indentation. Anjuta currently recognize two kinds
 * of mode line:
 *
 * Emacs mode line, on the first or the second line if the first one is a
 * shebang (#!) with the following format:
 * -*- key1: value1; key2: value2 -*-
 *
 * Vim mode line, one the first 5 or the last 5 lines with the following format:
 * vim:set key1=value1 key2=value2
 * 
 * Returns: %TRUE if a mode line has been found and applied.
 */
gboolean
anjuta_apply_modeline (IAnjutaEditor *editor)
{
	IndentationParams params = {CHECK_NEXT,0,0,0};
	gint line;
	gchar *content = NULL;
	
	g_return_val_if_fail (IANJUTA_IS_EDITOR (editor), FALSE);

	/* Check the first lines */
	for (line = 1; params.settings == CHECK_NEXT; line++)
	{
		g_free (content);
		content = get_editor_line (editor, line);
		if (content == NULL) return FALSE;

		params.settings = 0;
		if (parse_vim_modeline (&params, content, line)) break;
		if (parse_emacs_modeline (&params, content, line)) break;
	}

	/* Check the last lines */
	if (params.settings == 0) params.settings = CHECK_NEXT;
	for (line = -1;params.settings == CHECK_NEXT; line--)
	{
		g_free (content);
		content = get_editor_line (editor, line);
		if (content == NULL) return FALSE;

		params.settings = 0;
		if (parse_vim_modeline (&params, content, line)) break;
		if (parse_emacs_modeline (&params, content, line)) break;
	}
	g_free (content);

	/* Set indentation settings */
	return set_indentation (editor, &params);
}
コード例 #2
0
/* Scan a line for vi(m)/emacs/kate modelines.
 * Line numbers are counted starting at one.
 */
static void
parse_modeline (gchar           *s,
		gint             line_number,
		gint             line_count,
		ModelineOptions *options)
{
	gchar prev;

	/* look for the beginning of a modeline */
	for (prev = ' '; (s != NULL) && (*s != '\0'); prev = *(s++))
	{
		if (!g_ascii_isspace (prev))
			continue;

		if ((line_number <= 3 || line_number > line_count - 3) &&
		    (strncmp (s, "ex:", 3) == 0 ||
		     strncmp (s, "vi:", 3) == 0 ||
		     strncmp (s, "vim:", 4) == 0))
		{
			gedit_debug_message (DEBUG_PLUGINS, "Vim modeline on line %d", line_number);

		    	while (*s != ':') s++;
		    	s = parse_vim_modeline (s + 1, options);
		}
		else if (line_number <= 2 && strncmp (s, "-*-", 3) == 0)
		{
			gedit_debug_message (DEBUG_PLUGINS, "Emacs modeline on line %d", line_number);

			s = parse_emacs_modeline (s + 3, options);
		}
		else if ((line_number <= 10 || line_number > line_count - 10) &&
			 strncmp (s, "kate:", 5) == 0)
		{
			gedit_debug_message (DEBUG_PLUGINS, "Kate modeline on line %d", line_number);

			s = parse_kate_modeline (s + 5, options);
		}
	}
}