/* 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; } }
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); }