Exemplo n.º 1
0
/* Remove Whitespace Lines */
gint
rmwhspln(ScintillaObject *sci, gint line_num, gint end_line_num)
{
	gint indent;                       /* indent position */
	gint changed = 0;                  /* number of lines removed */

	while(line_num <= end_line_num)    /* loop through lines */
	{
		indent = scintilla_send_message(sci,
									SCI_GETLINEINDENTPOSITION,
									line_num, 0);

		/* check if the posn of indentation is also the end of line posn */
		if(indent -
		   sci_get_position_from_line(sci, line_num) ==
		   sci_get_line_end_position (sci, line_num) -
		   sci_get_position_from_line(sci, line_num))
		{
			scintilla_send_message(sci,
					   SCI_DELETERANGE,
					   sci_get_position_from_line(sci, line_num),
					   sci_get_line_length(sci, line_num));

			line_num--;
			end_line_num--;
			changed++;
		}
		line_num++;

	}

	/* return the number of lines deleted */
	return -changed;
}
Exemplo n.º 2
0
static void
reindent (GeanyDocument  *doc,
          GrindIndenter  *indenter)
{
  if (DOC_VALID (doc)) {
    int               start;
    int               end;
    ScintillaObject  *sci = doc->editor->sci;
    
    if (sci_has_selection (sci)) {
      start = sci_get_line_from_position (sci, sci_get_selection_start (sci));
      end = sci_get_line_from_position (sci, sci_get_selection_end (sci));
    } else {
      start = 0;
      end = sci_get_line_count (sci);
    }
    
    if (start != end) {
      g_debug ("Using indenter \"%s\" by \"%s\"",
               grind_indenter_get_name (indenter),
               grind_indenter_get_author (indenter));
      
      sci_start_undo_action (sci);
      if (grind_indenter_indent (indenter, doc,
                                 sci_get_position_from_line (sci, start),
                                 sci_get_line_end_position (sci, end))) {
        msgwin_status_add ("Reindented \"%s\"", DOC_FILENAME (doc));
      }
      sci_end_undo_action (sci);
    }
  }
}
Exemplo n.º 3
0
/* Remove Empty Lines */
gint
rmemtyln(ScintillaObject *sci, gint line_num, gint end_line_num)
{
	gint  changed = 0;     /* number of lines removed */

	while(line_num <= end_line_num)    /* loop through lines */
	{
		/* check if the first posn of the line is also the end of line posn */
		if(sci_get_position_from_line(sci, line_num) ==
		   sci_get_line_end_position (sci, line_num))
		{
			scintilla_send_message(sci,
					   SCI_DELETERANGE,
					   sci_get_position_from_line(sci, line_num),
					   sci_get_line_length(sci, line_num));

			line_num--;
			end_line_num--;
			changed++;
		}
		line_num++;
	}

	/* return the number of lines deleted */
	return -changed;
}
Exemplo n.º 4
0
static gint
get_line_end(ScintillaObject *sci, gint line)
{
	gint end;
	gchar ch;
	end = sci_get_line_end_position(sci, line);
	ch = sci_get_char_at(sci, end - 1);
	while(ch == ' ' || ch == '\t')
	{
		end--;
		ch = sci_get_char_at(sci, end - 1);
	}
	return end;
}
Exemplo n.º 5
0
static gint
get_end_pos(ScintillaObject *sci, gint line)
{
	gint end;
	gchar ch;
	end = sci_get_line_end_position(sci, line);
	ch = char_at(sci, end - 1);
	/* ignore spaces and "}" */
	while (isspace_no_newline(ch) || '}' == ch)
	{
		end--;
		ch = char_at(sci, end - 1);
	}
	return end;
}
Exemplo n.º 6
0
static void
define_format_newline(ScintillaObject *sci)
{
	gint    end_pos;
	gint    line;

	line = sci_get_current_line(sci);
	if(!inside_define(sci, line, TRUE))
		return;
	line--;
	end_pos = sci_get_line_end_position(sci, line);
	sci_insert_text(sci, end_pos,  "\\");
	line += 2;
	dprintf("Added line: %d\n", line);
	g_array_append_val(lines_stack, line);
}
Exemplo n.º 7
0
static void findMatchingClosingTag(ScintillaObject *sci, gchar *tagName, gint closingBracket)
{
    gint pos;
    gint linesInDocument = sci_get_line_count(sci);
    gint endOfDocument = sci_get_position_from_line(sci, linesInDocument);
    gint openingTagsCount = 1;
    gint closingTagsCount = 0;

    for(pos=closingBracket; pos<endOfDocument; pos++)
    {
        /* are we inside tag? */
        gint lineNumber = sci_get_line_from_position(sci, pos);
        gint lineEnd = sci_get_line_end_position(sci, lineNumber);
        gint matchingOpeningBracket = findBracket(sci, pos, lineEnd, '<', '\0', TRUE);
        gint matchingClosingBracket = findBracket(sci, pos, lineEnd, '>', '\0', TRUE);

        if(-1 != matchingOpeningBracket && -1 != matchingClosingBracket
            && (matchingClosingBracket > matchingOpeningBracket))
        {
            /* we are inside of some tag. Let us check what tag*/
            gboolean isMatchingTagOpening = is_tag_opening(sci, matchingOpeningBracket);
            gchar *matchingTagName = get_tag_name(sci, matchingOpeningBracket,
                                                  matchingClosingBracket,
                                                  isMatchingTagOpening);
            if(matchingTagName && strcmp(tagName, matchingTagName) == 0)
            {
                if(TRUE == isMatchingTagOpening)
                    openingTagsCount++;
                else
                    closingTagsCount++;
            }
            pos = matchingClosingBracket;
            g_free(matchingTagName);
        }

        if(openingTagsCount == closingTagsCount)
        {
            /* matching tag is found */
            highlightedBrackets[2] = matchingOpeningBracket;
            highlightedBrackets[3] = matchingClosingBracket;
            highlight_matching_pair(sci);
            return;
        }
    }
    highlight_tag(sci, highlightedBrackets[0], highlightedBrackets[1],
                  NONMATCHING_PAIR_COLOR);
}
Exemplo n.º 8
0
static gboolean
improve_indent(
	ScintillaObject *sci,
	GeanyEditor     *editor,
	gint             pos)
{
	gint ch, ch_next;
	gint line;
	gint indent, indent_width;
	gint end_pos;
	if (!ac_info->improved_cbracket_indent)
		return AC_CONTINUE_ACTION;
	ch = char_at(sci, pos - 1);
	if (ch != '{')
		return AC_CONTINUE_ACTION;
	/* if curly bracket completion is enabled - just make indents
	 * but ensure that second "}" exists. If disabled - make indent
	 * and complete second curly bracket */
	ch_next = char_at(sci, pos);
	if (ac_info->cbracket && ch_next != '}')
		return AC_CONTINUE_ACTION;
	line = sci_get_line_from_position(sci, pos);
	indent = sci_get_line_indentation(sci, line);
	indent_width = editor_get_indent_prefs(editor)->width;
	sci_start_undo_action(sci);
	if (ac_info->cbracket)
		SSM(sci, SCI_ADDTEXT, 2, (sptr_t)"\n\n");
	else
		SSM(sci, SCI_ADDTEXT, 3, (sptr_t)"\n\n}");
	if (ac_info->whitesmiths_style)
	{
		sci_set_line_indentation(sci, line,     indent);
		sci_set_line_indentation(sci, line + 1, indent);
		sci_set_line_indentation(sci, line + 2, indent);
	}
	else
	{
		sci_set_line_indentation(sci, line + 1, indent + indent_width);
		sci_set_line_indentation(sci, line + 2, indent);
	}
	/* move to the end of added line */
	end_pos = sci_get_line_end_position(sci, line + 1);
	sci_set_current_position(sci, end_pos, TRUE);
	sci_end_undo_action(sci);
	/* do not alow internal auto-indenter to do the work */
	return AC_STOP_ACTION;
}
Exemplo n.º 9
0
static PyObject *
ZenEditor_get_current_line_range(ZenEditor *self, PyObject *args)
{
	PyObject *result;
	gint line, line_start, line_end;
	ScintillaObject *sci;

	print_called();
	py_return_none_if_null(sci = ZenEditor_get_scintilla(self));

	line = sci_get_current_line(sci);
	line_start = sci_get_position_from_line(sci, line);
	line_end = sci_get_line_end_position(sci, line);

	result = Py_BuildValue("ii", line_start, line_end);
	return result;
}
Exemplo n.º 10
0
static PyObject *
Scintilla_get_line_end_position(Scintilla *self, PyObject *args, PyObject *kwargs)
{
	gint line = -1, line_end_pos;
	static gchar *kwlist[] = { "line", NULL };

	SCI_RET_IF_FAIL(self);

	if (PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &line))
	{
		if (line == -1)
			line = sci_get_current_line(self->sci);
		line_end_pos = sci_get_line_end_position(self->sci, line);
		return Py_BuildValue("i", line_end_pos);
	}

	Py_RETURN_NONE;
}
Exemplo n.º 11
0
static gboolean
inside_define(ScintillaObject *sci, gint line, gboolean newline)
{
	gint    lexer;
	gint    start_pos;
	gint    end_pos;
	gchar   end_char;

	lexer = sci_get_lexer(sci);
	if(lexer != SCLEX_CPP)
		return FALSE;

	end_pos = get_line_end(sci, line);
	end_char = sci_get_char_at(sci, end_pos - 1);
	if(end_char != '\\')
	{
		dprintf("End char is not \\, exit\n");
		return FALSE;
	}
	if(newline)
		line--;
	do {
		line--;
		end_pos = get_line_end(sci, line);
		end_char = sci_get_char_at(sci, end_pos - 1);
	} while(end_char == '\\' && line >= 0);
	line++;
	dprintf("Expecting define on line %d\n", line + 1);
	start_pos = (gint)SSM(sci, SCI_GETLINEINDENTPOSITION, (uptr_t)line, 0);
	end_pos = sci_get_line_end_position(sci, line);
	if(start_pos == end_pos)
	{
		dprintf("line empty, exit\n");
		return FALSE;
	}
	const gchar *start_line = get_char_range(sci, start_pos, 7);
	g_return_val_if_fail(NULL != start_line, FALSE);
	if(0 != strncmp(start_line, "#define ", strlen("#define ")))
	{
		dprintf("Start line is not \"#define\", exit\n");
		return FALSE;
	}
	return TRUE;
}
Exemplo n.º 12
0
static void
define_format_line(ScintillaObject *sci, gint current_line)
{
	gint    length;
	gint    first_line;
	gint    first_end;
	gint    max = geany_data->editor_prefs->long_line_column;

	if(!inside_define(sci, current_line, FALSE))
		return;

	first_line = current_line;
	first_end = get_line_end(sci, first_line);
	for (first_end--; sci_get_char_at(sci, first_end - 1) == ' '; first_end--) {}
	SSM(sci, SCI_DELETERANGE, first_end, sci_get_line_end_position(sci, first_line) - first_end);
	length = first_end - get_indent_pos(sci, first_line) + sci_get_line_indentation(sci, first_line);
	for(; length < max - 1; length++, first_end++)
		sci_insert_text(sci, first_end, " ");
	sci_insert_text(sci, first_end, "\\");
}
Exemplo n.º 13
0
static void run_tag_highlighter(ScintillaObject *sci)
{
    gint position = sci_get_current_position(sci);
    gint lineNumber = sci_get_current_line(sci);
    gint lineStart = sci_get_position_from_line(sci, lineNumber);
    gint lineEnd = sci_get_line_end_position(sci, lineNumber);
    gint openingBracket = findBracket(sci, position, lineStart, '<', '>', FALSE);
    gint closingBracket = findBracket(sci, position, lineEnd, '>', '<', TRUE);
    int i;

    if(-1 == openingBracket || -1 == closingBracket)
    {
        clear_previous_highlighting(sci, highlightedBrackets[0], highlightedBrackets[1]);
        clear_previous_highlighting(sci, highlightedBrackets[2], highlightedBrackets[3]);
        for(i=0; i<3; i++)
            highlightedBrackets[i] = 0;
        return;
    }

    /* If the cursor jumps from one tag into another, clear
     * previous highlighted tags*/
    if(openingBracket != highlightedBrackets[0] ||
        closingBracket != highlightedBrackets[1])
    {
        clear_previous_highlighting(sci, highlightedBrackets[0], highlightedBrackets[1]);
        clear_previous_highlighting(sci, highlightedBrackets[2], highlightedBrackets[3]);
    }

    /* Don't run search on empty brackets <> */
    if (closingBracket - openingBracket > 1) {
        highlightedBrackets[0] = openingBracket;
        highlightedBrackets[1] = closingBracket;

        findMatchingTag(sci, openingBracket, closingBracket);
    }
}
Exemplo n.º 14
0
static gboolean
editor_notify_cb(GObject *object, GeanyEditor *editor, SCNotification *nt, gpointer data)
{
	gint i = 0, val;
	gint old_position = 0;
	gint old_lposition = 0;
	gint old_line = 0;
	gint pos;
	if(NULL == editor || NULL == editor->sci)
		return FALSE;
	if(nt->nmhdr.code == SCN_CHARADDED)
	{
		if('\n' == nt->ch)
			define_format_newline(editor->sci);
	}
	if(nt->nmhdr.code == SCN_UPDATEUI)
	{
		if(g_array_index(lines_stack, gint, 0))
		{
			/* save current position */
			old_line = sci_get_current_line(editor->sci);
			old_lposition = sci_get_line_end_position(editor->sci, old_line) - sci_get_line_length(editor->sci, old_line);
			old_position = sci_get_current_position(editor->sci);
			sci_start_undo_action(editor->sci);
		}
		while((val = g_array_index(lines_stack, gint, i)))
		{
			i++;
			define_format_line(editor->sci, val - 1);
			dprintf("Removed from stack: %d\n", val);
		}
		if(i > 0)
		{
			sci_end_undo_action(editor->sci);
			g_array_remove_range(lines_stack, 0, i);
			/* restore current position */
			pos = sci_get_line_end_position(editor->sci, old_line) - sci_get_line_length(editor->sci, old_line);
			sci_set_current_position(editor->sci, old_position + pos - old_lposition, FALSE);
		}
	}
	if(nt->nmhdr.code == SCN_MODIFIED)
	{
		if(nt->modificationType & (SC_MOD_INSERTTEXT | SC_MOD_DELETETEXT))
		{
			if(nt->modificationType & (SC_PERFORMED_UNDO | SC_PERFORMED_REDO))
				return FALSE;
			gint line = sci_get_line_from_position(editor->sci, nt->position) + 1;
			if(sci_get_char_at(editor->sci, get_line_end(editor->sci, line - 1) - 1) == '\\')
			{
				gboolean found = FALSE;
				while((val = g_array_index(lines_stack, gint, i)))
				{
					if(val == line)
					{
						found = TRUE;
						break;
					}
					i++;
				}
				if(!found)
				{
					dprintf("Added line: %d\n", line);
					g_array_append_val(lines_stack, line);
				}
			}
		}
	}
	return FALSE;
}
Exemplo n.º 15
0
static void do_format(GeanyDocument *doc, bool entire_doc, bool autof)
{
  GString *formatted;
  ScintillaObject *sci;
  size_t offset = 0, length = 0, sci_len;
  size_t cursor_pos, old_first_line, new_first_line, line_delta;
  const char *sci_buf;
  bool changed = true;
  bool was_changed;

  if (doc == NULL)
    doc = document_get_current();

  if (!DOC_VALID(doc))
  {
    g_warning("Cannot format with no documents open");
    return;
  }
  sci = doc->editor->sci;
  was_changed = doc->changed;

  // FIXME: instead of failing, ask user to save the document once
  if (!doc->real_path)
  {
    g_warning("Cannot format document that's never been saved");
    return;
  }

  if (!entire_doc)
  {
    if (sci_has_selection(sci))
    { // format selection
      offset = sci_get_selection_start(sci);
      length = sci_get_selection_end(sci) - offset;
    }
    else
    { // format current line
      size_t cur_line = sci_get_current_line(sci);
      offset = sci_get_position_from_line(sci, cur_line);
      length = sci_get_line_end_position(sci, cur_line) - offset;
    }
  }
  else
  { // format entire document
    offset = 0;
    length = sci_get_length(sci);
  }

  cursor_pos = sci_get_current_position(sci);
  sci_len = sci_get_length(sci);
  sci_buf =
      (const char *)scintilla_send_message(sci, SCI_GETCHARACTERPOINTER, 0, 0);

  formatted = fmt_clang_format(doc->file_name, sci_buf, sci_len, &cursor_pos,
                               offset, length, false);

  // FIXME: handle better
  if (formatted == NULL)
    return;

  if (!autof)
  {
    changed = (formatted->len != sci_len) ||
              (g_strcmp0(formatted->str, sci_buf) != 0);
  }

  old_first_line = scintilla_send_message(sci, SCI_GETFIRSTVISIBLELINE, 0, 0);

  // Replace document text and move cursor to new position
  scintilla_send_message(sci, SCI_BEGINUNDOACTION, 0, 0);
  scintilla_send_message(sci, SCI_CLEARALL, 0, 0);
  scintilla_send_message(sci, SCI_ADDTEXT, formatted->len,
                         (sptr_t)formatted->str);
  scintilla_send_message(sci, SCI_GOTOPOS, cursor_pos, 0);
  new_first_line = scintilla_send_message(sci, SCI_GETFIRSTVISIBLELINE, 0, 0);
  line_delta = new_first_line - old_first_line;
  scintilla_send_message(sci, SCI_LINESCROLL, 0, -line_delta);
  scintilla_send_message(sci, SCI_ENDUNDOACTION, 0, 0);

  document_set_text_changed(doc, (was_changed || changed));

  g_string_free(formatted, true);
}
Exemplo n.º 16
0
static gboolean
blank_line(ScintillaObject *sci, gint line)
{
	return get_indent(sci, line) ==
		sci_get_line_end_position(sci, line);
}