Beispiel #1
0
/* Get or Set the entire text of the currently active Geany document */
static gint glspi_text(lua_State* L)
{

	GeanyDocument *doc = document_get_current();

	if (!doc) { return 0; }
	if (0 == lua_gettop(L)) { /* Called with no args, GET the current text */
		gint len = sci_get_length(doc->editor->sci);
		gchar *txt = NULL;
		if (len>0) {
			txt = g_malloc0((guint)len+2);
			sci_get_text(doc->editor->sci, len+1, txt);
			lua_pushstring(L, (const gchar *) txt);
			g_free(txt);
		} else {
			lua_pushstring(L, "");
		}
		return 1;
	} else { /* Called with one arg, SET the current text */
		const gchar*txt;
		if (!lua_isstring(L, 1)) {
			return FAIL_STRING_ARG(1);
		}
		txt = lua_tostring(L, 1);
		sci_set_text(doc->editor->sci, txt);
		return 0;
	}
}
Beispiel #2
0
static void setup_range(DocInfo *dinfo, GtkPrintContext *ctx)
{
	dinfo->fr.hdc = dinfo->fr.hdcTarget = gtk_print_context_get_cairo_context(ctx);

	dinfo->fr.rcPage.left   = 0;
	dinfo->fr.rcPage.top    = 0;
	dinfo->fr.rcPage.right  = gtk_print_context_get_width(ctx);
	dinfo->fr.rcPage.bottom = gtk_print_context_get_height(ctx);

	dinfo->fr.rc.left   = dinfo->fr.rcPage.left;
	dinfo->fr.rc.top    = dinfo->fr.rcPage.top;
	dinfo->fr.rc.right  = dinfo->fr.rcPage.right;
	dinfo->fr.rc.bottom = dinfo->fr.rcPage.bottom;
#if GTK_CHECK_VERSION(2, 20, 0)
	{
		gdouble m_top, m_left, m_right, m_bottom;
		if (gtk_print_context_get_hard_margins(ctx, &m_top, &m_bottom, &m_left, &m_right))
		{
			dinfo->fr.rc.left   += m_left;
			dinfo->fr.rc.top    += m_top;
			dinfo->fr.rc.right  -= m_right;
			dinfo->fr.rc.bottom -= m_bottom;
		}
	}
#endif
	if (printing_prefs.print_page_header)
		dinfo->fr.rc.top += dinfo->line_height * 3; /* header height */
	if (printing_prefs.print_page_numbers)
		dinfo->fr.rc.bottom -= dinfo->line_height * 1; /* footer height */

	dinfo->fr.chrg.cpMin = 0;
	dinfo->fr.chrg.cpMax = sci_get_length(dinfo->sci);
}
static PyObject *
Scintilla_get_contents_range(Scintilla *self, PyObject *args, PyObject *kwargs)
{
	gint start = -1, end = -1;
	gchar *text;
	PyObject *py_text;
	static gchar *kwlist[] = { "start", "end", NULL };

	SCI_RET_IF_FAIL(self);

	if (PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist, &start, &end))
	{
		if (start == -1)
			start = 0;
		if (end == -1)
			end = sci_get_length(self->sci) + 1;
		text = sci_get_contents_range(self->sci, start, end);
		if (text == NULL)
			Py_RETURN_NONE;
		py_text = PyString_FromString(text);
		g_free(text);
		return py_text;
	}

	Py_RETURN_NONE;
}
Beispiel #4
0
/**
 * \brief the function select entirely the document
 */
static void select_entirely_doc( ScintillaObject *sci  )
{
    gint            size_buf = sci_get_length(sci);

    sci_set_selection_start( sci , 0 ) ;
    sci_set_selection_end( sci , size_buf ) ;
}
static PyObject *
Scintilla_get_length(Scintilla *self)
{
	gint len;
	SCI_RET_IF_FAIL(self);
	len = sci_get_length(self->sci);
	return Py_BuildValue("i", len);
}
Beispiel #6
0
static gchar *
get_current_word()
{
	gchar *txt;
	GeanyDocument *doc;

	gint pos;
	gint cstart, cend;
	gchar c;
	gint text_len;

	doc = document_get_current();
	g_return_val_if_fail(doc != NULL && doc->file_name != NULL, NULL);

	text_len = sci_get_selected_text_length(doc->editor->sci);
	if (text_len > 1)
	{
		txt = g_malloc(text_len + 1);
		sci_get_selected_text(doc->editor->sci, txt);
		return txt;
	}

	pos = sci_get_current_position(doc->editor->sci);
	if (pos > 0)
		pos--;

	cstart = pos;
	c = sci_get_char_at(doc->editor->sci, cstart);

	if (!word_check_left(c))
		return NULL;

	while (word_check_left(c))
	{
		cstart--;
		if (cstart >= 0)
			c = sci_get_char_at(doc->editor->sci, cstart);
		else
			break;
	}
	cstart++;

	cend = pos;
	c = sci_get_char_at(doc->editor->sci, cend);
	while (word_check_right(c) && cend < sci_get_length(doc->editor->sci))
	{
		cend++;
		c = sci_get_char_at(doc->editor->sci, cend);
	}

	if (cstart == cend)
		return NULL;
	txt = g_malloc0(cend - cstart + 1);

	sci_get_text_range(doc->editor->sci, cstart, cend, txt);
	return txt;
}
Beispiel #7
0
/** Allocates and fills a buffer with text from the start of the document.
 * @param sci Scintilla widget.
 * @param buffer_len Buffer length to allocate, including the terminating
 * null char, e.g. sci_get_length() + 1. Alternatively use @c -1 to get all
 * text (since Geany 1.23).
 * @return A copy of the text. Should be freed when no longer needed.
 *
 * @since 1.23 (0.17)
 */
gchar *sci_get_contents(ScintillaObject *sci, gint buffer_len)
{
	gchar *text;

	if (buffer_len < 0)
		buffer_len = sci_get_length(sci) + 1;

	text = g_malloc(buffer_len);
	SSM(sci, SCI_GETTEXT, (uptr_t) buffer_len, (sptr_t) text);
	return text;
}
Beispiel #8
0
static char* sci_text_get_range(ScintillaObject* sci, unsigned int i, int n) {
  unsigned int length = (unsigned int) sci_get_length(sci);
  if (i > length) {
    i = length;
  }
  if (n < 0) {
    n = length;
  }
  if (i + n > length) {
    n = length - i;
  }
  return sci_get_contents_range(sci, i, i + n);
}
static void send_complete(GeanyEditor* editor, int flag)
{
	if (completion_framework == NULL) {
		return;
	}
	if (!is_completion_file_now()) {
		return;
	}
	int pos = get_completion_position();
	if (pos == 0) {
		return;
	}  // nothing to complete

	int line = sci_get_line_from_position(editor->sci, pos);
	int ls_pos = sci_get_position_from_line(editor->sci, line);
	int byte_line_len = pos - ls_pos;
	if (byte_line_len < 0) {
		return;
	}

	char* content = sci_get_contents(editor->sci, sci_get_length(editor->sci) + 1 + 1);
	content[sci_get_length(editor->sci)] = ' ';  // replace null -> virtual space for clang
	content[sci_get_length(editor->sci)] = '\0';

	// TODO clang's col is byte? character?
	completion_framework->complete_async(editor->document->file_name, content, line + 1,
	                                     byte_line_len + 1);

	edit_tracker.valid = true;
	edit_tracker.start_pos = pos;
	edit_tracker.text.clear();

	if (pos != sci_get_current_position(editor->sci)) {
		int len = sci_get_current_position(editor->sci) - pos;
		edit_tracker.text.append(content + pos, len);
	}

	g_free(content);
}
Beispiel #10
0
/* Returns the line number of the previous marker that matches marker_mask, or -1.
 * marker_mask is a bitor of 1 << marker_index. (See MarkerHandleSet::MarkValue()).
 * Note: If there is a marker on the line, it returns the same line. */
gint sci_marker_previous(ScintillaObject *sci, gint line, gint marker_mask, gboolean wrap)
{
	gint marker_line;

	marker_line = (gint) SSM(sci, SCI_MARKERPREVIOUS, (uptr_t) line, marker_mask);
	if (wrap && marker_line == -1)
	{
		gint len = sci_get_length(sci);
		gint last_line = sci_get_line_from_position(sci, len - 1);

		marker_line = (gint) SSM(sci, SCI_MARKERPREVIOUS, (uptr_t) last_line, marker_mask);
	}
	return marker_line;
}
Beispiel #11
0
static gchar *
current_word(void)
{
	GeanyDocument *doc;

	gint pos;
	gint cstart, cend;
	gchar c;

	doc = document_get_current();
	g_return_val_if_fail(doc != NULL && doc->file_name != NULL, NULL);

	if (sci_has_selection(doc->editor->sci))
		return sci_get_selection_contents(doc->editor->sci);

	pos = sci_get_current_position(doc->editor->sci);
	if (pos > 0)
		pos--;

	cstart = pos;
	c = sci_get_char_at(doc->editor->sci, cstart);

	if (!word_check_left(c))
		return NULL;

	while (word_check_left(c))
	{
		cstart--;
		if (cstart >= 0)
			c = sci_get_char_at(doc->editor->sci, cstart);
		else
			break;
	}
	cstart++;

	cend = pos;
	c = sci_get_char_at(doc->editor->sci, cend);
	while (word_check_right(c) && cend < sci_get_length(doc->editor->sci))
	{
		cend++;
		c = sci_get_char_at(doc->editor->sci, cend);
	}

	if (cstart == cend)
		return NULL;

	return sci_get_contents_range(doc->editor->sci, cstart, cend);
}
Beispiel #12
0
static void sci_text_set_range(ScintillaObject* sci, unsigned int i, int n, const char* src) {
  unsigned int length = sci_get_length(sci);
  if (i > length) {
    i = length;
  }
  if (n < 0) {
    n = length;
  }
  if (i + n > length) {
    n = length - i;
  }
  if (n) {
    // TODO: Restore current position in sci.
    sci_replace_text(sci, i, i+n, src);
  } else {
    // Use insert because it handles current position properly.
    sci_insert_text(sci, i, src);
  }
}
Beispiel #13
0
static void menu_addword_item_activate_cb(GtkMenuItem *menuitem, gpointer gdata)
{
    gint startword, endword, i, doc_len;
    ScintillaObject *sci;
    GString *str;
    gboolean ignore = GPOINTER_TO_INT(gdata);

    if (clickinfo.doc == NULL || clickinfo.word == NULL || clickinfo.pos == -1)
        return;

    /* if we ignore the word, we add it to the current session, to ignore it
     * also for further checks*/
    if (ignore)
        sc_speller_add_word_to_session(clickinfo.word);
    /* if we do not ignore the word, we add the word to the personal dictionary */
    else
        sc_speller_add_word(clickinfo.word);

    /* Remove all indicators on the added/ignored word */
    sci = clickinfo.doc->editor->sci;
    str = g_string_sized_new(256);
    doc_len = sci_get_length(sci);
    for (i = 0; i < doc_len; i++)
    {
        startword = scintilla_send_message(sci, SCI_INDICATORSTART, 0, i);
        if (startword >= 0)
        {
            endword = scintilla_send_message(sci, SCI_INDICATOREND, 0, startword);
            if (startword == endword)
                continue;

            if (str->len < (guint)(endword - startword + 1))
                str = g_string_set_size(str, endword - startword + 1);
            sci_get_text_range(sci, startword, endword, str->str);

            if (strcmp(str->str, clickinfo.word) == 0)
                sci_indicator_clear(sci, startword, endword - startword);

            i = endword;
        }
    }
    g_string_free(str, TRUE);
}
Beispiel #14
0
static PyObject *
ZenEditor_get_content(ZenEditor *self, PyObject *args)
{
	PyObject *result;
	gchar *text;
	ScintillaObject *sci;

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

	text = sci_get_contents(sci, sci_get_length(sci) + 1);
	py_return_none_if_null(text);

	result = PyString_FromString(text);
	g_free(text);
	py_return_none_if_null(result);

	return result;
}
Beispiel #15
0
static gchar *get_paste_text(GeanyDocument *doc, gsize *text_len)
{
    gsize len;
    gchar *paste_text;

    if (sci_has_selection(doc->editor->sci))
    {
        len = sci_get_selected_text_length(doc->editor->sci) + 1;
        paste_text = sci_get_selection_contents(doc->editor->sci);
    }
    else
    {
        len = sci_get_length(doc->editor->sci) + 1;
        paste_text = sci_get_contents(doc->editor->sci, len);
    }

    if (text_len)
        *text_len = len;

    return paste_text;
}
static PyObject *
Scintilla_get_contents(Scintilla *self, PyObject *args, PyObject *kwargs)
{
	gint len = -1;
	gchar *text;
	PyObject *py_text;
	static gchar *kwlist[] = { "len", NULL };

	SCI_RET_IF_FAIL(self);

	if (PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &len))
	{
		if (len == -1)
			len = sci_get_length(self->sci) + 1;
		text = sci_get_contents(self->sci, len);
		if (text == NULL)
			Py_RETURN_NONE;
		py_text = PyString_FromString(text);
		g_free(text);
		return py_text;
	}

	Py_RETURN_NONE;
}
Beispiel #17
0
static void draw_page(GtkPrintOperation *operation, GtkPrintContext *context,
					  gint page_nr, gpointer user_data)
{
	DocInfo *dinfo = user_data;
	cairo_t *cr;
	gdouble width, height;

	g_return_if_fail(dinfo != NULL);
	g_return_if_fail((guint)page_nr < dinfo->pages->len);

	if (dinfo->pages->len > 0)
	{
		gdouble fraction = (page_nr + 1) / (gdouble) dinfo->pages->len;
		gchar *text = g_strdup_printf(_("Page %d of %d"), page_nr + 1, dinfo->pages->len);
		gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(main_widgets.progressbar), fraction);
		gtk_progress_bar_set_text(GTK_PROGRESS_BAR(main_widgets.progressbar), text);
		g_free(text);
	}

	cr = gtk_print_context_get_cairo_context(context);
	width = gtk_print_context_get_width(context);
	height = gtk_print_context_get_height(context);

	if (printing_prefs.print_page_header)
		add_page_header(dinfo, cr, width, page_nr);

	dinfo->fr.chrg.cpMin = g_array_index(dinfo->pages, gint, page_nr);
	if ((guint)page_nr + 1 < dinfo->pages->len)
		dinfo->fr.chrg.cpMax = g_array_index(dinfo->pages, gint, page_nr + 1) - 1;
	else /* it's the last page, print 'til the end */
		dinfo->fr.chrg.cpMax = sci_get_length(dinfo->sci);

	scintilla_send_message(dinfo->sci, SCI_FORMATRANGE, TRUE, (sptr_t) &dinfo->fr);

	/* reset color */
	cairo_set_source_rgb(cr, 0, 0, 0);

	if (printing_prefs.print_line_numbers)
	{	/* print a thin line between the line number margin and the data */
		gint y1 = 0, y2 = height;

		if (printing_prefs.print_page_header)
			y1 += (dinfo->line_height * 3) - 2;	/* "- 2": to connect the line number line to
												 * the page header frame */

		if (printing_prefs.print_page_numbers)
			y2 -= (dinfo->line_height * 2) - 2;

		cairo_set_line_width(cr, 0.3);
		cairo_move_to(cr, dinfo->margin_width, y1);
		cairo_line_to(cr, dinfo->margin_width, y2);
		cairo_stroke(cr);
	}

	if (printing_prefs.print_page_numbers)
	{
		gchar *line = g_strdup_printf("<small>- %d -</small>", page_nr + 1);
		pango_layout_set_markup(dinfo->layout, line, -1);
		pango_layout_set_alignment(dinfo->layout, PANGO_ALIGN_CENTER);
		cairo_move_to(cr, 0, height - dinfo->line_height);
		pango_cairo_show_layout(cr, dinfo->layout);
		g_free(line);
	}
}
Beispiel #18
0
static int sci_text_is_eof(ScintillaObject* sci, unsigned int i) {
  return i >= sci_get_length(sci);
}
Beispiel #19
0
void xml_format(GtkMenuItem* menuitem, gpointer gdata)
{
    /* retrieves the current document */
    GeanyDocument* doc = document_get_current();
    GeanyEditor* editor;
    ScintillaObject* sco;
    int length;
    char* buffer;
    xmlDoc* parsedDocument;
    int result;
    int xOffset;
    GeanyFiletype* fileType;
    
    g_return_if_fail(doc != NULL);

    editor = doc->editor;
    sco = editor->sci;

    /* default printing options */
    if (prettyPrintingOptions == NULL) { prettyPrintingOptions = createDefaultPrettyPrintingOptions(); }

    /* prepare the buffer that will contain the text
     * from the scintilla object */
    length = sci_get_length(sco)+1;
    buffer = (char*)malloc(length*sizeof(char));
    if (buffer == NULL) { exit(-1); } /* malloc error */

    /* retrieves the text */
    sci_get_text(sco, length, buffer);

    /* checks if the data is an XML format */
    parsedDocument = xmlParseDoc((unsigned char*)buffer);

    /* this is not a valid xml => exit with an error message */
    if(parsedDocument == NULL)
    {
        dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("Unable to parse the content as XML."));
        return;
    }

    /* free all */
    xmlFreeDoc(parsedDocument);

    /* process pretty-printing */
    result = processXMLPrettyPrinting(&buffer, &length, prettyPrintingOptions);
    if (result != PRETTY_PRINTING_SUCCESS)
    {
        dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("Unable to process PrettyPrinting on the specified XML because some features are not supported.\n\nSee Help > Debug messages for more details..."));
        return;
    }

    /* updates the document */
    sci_set_text(sco, buffer);

    /* set the line */
    xOffset = scintilla_send_message(sco, SCI_GETXOFFSET, 0, 0);
    scintilla_send_message(sco, SCI_LINESCROLL, -xOffset, 0); /* TODO update with the right function-call for geany-0.19 */

    /* sets the type */
    fileType = filetypes_index(GEANY_FILETYPES_XML);
    document_set_filetype(doc, fileType);
}
Beispiel #20
0
static char sci_text_get_char(ScintillaObject* sci, unsigned int i) {
  return (i >= sci_get_length(sci)) ? 0 : sci_get_char_at(sci, i);
}
Beispiel #21
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);
}