View Issue Details

IDProjectCategoryView StatusLast Update
0001644ScribusScripterpublic2005-02-08 12:55
ReporterringercAssigned Tocbradney  
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionfixed 
Platformx86 LinuxOSFedora CoreOS Version3
Product Version1.2.2cvs 
Fixed in Version1.2.2cvs 
Summary0001644: PATCH: Split MenuTest out of scriptplugin.cpp and rename
DescriptionThe attached patch against 1.2.2cvs scripter breaks the MenuTest class out of scriptplugin.{cpp,h} and puts it in its own files. In the process it renames it and pushes most of the initplug functionality into its constructor.

This cleans up scriptplugin.cpp considerably, and lets other classes in the scripter access the functionality in ScripterCore (was MenuTest).

This patch is currently only for 1.2.2cvs. A version for 1.3 that also includes the "load script from file" patch (already in 1.2.2) and some related changes will be along later, in a separate bug.
TagsNo tags attached.
Patch

Relationships

child of 0003813 acknowledged Metabug: Scripter 

Activities

2005-02-07 14:27

 

scripter_menutest_refactor.diff (56,300 bytes)   
diff -r -u --new-file scriptplugin.orig/cmdvar.h scriptplugin/cmdvar.h
--- scriptplugin.orig/cmdvar.h	2005-02-02 22:12:03.000000000 +0800
+++ scriptplugin/cmdvar.h	2005-02-07 23:12:36.631845480 +0800
@@ -22,6 +22,9 @@
 #define FRAME_POLYLINE 7
 #define FRAME_PATHTEXT 8
 
+// Foward declarations
+class ScripterCore;
+
 /* Static global Variables */
 // the QApplication instance passed to the plugin setup
 extern ScribusApp* Carrier;
@@ -42,5 +45,19 @@
 /*! Raised when a normal script tries to run code reserved for extension scripts, or when extension scripts are disabled */
 extern PyObject* AccessDeniedError;
 
+// The following globals are used by the script console
+// to handle input and output.
+extern QString RetString;
+extern QString InValue;
+extern int RetVal;
+
+/// A global pointer to the class containing most of the scripter's core workings
+extern ScripterCore* scripterCore;
+
+/// initscribus, the module init function, must be defined here because the
+/// scriptercore needs to call it.
+
+extern "C" void initscribus(ScribusApp *pl);
+
 #endif
 
diff -r -u --new-file scriptplugin.orig/CVS/Entries scriptplugin/CVS/Entries
--- scriptplugin.orig/CVS/Entries	2005-02-07 23:15:33.460963368 +0800
+++ scriptplugin/CVS/Entries	2005-02-07 23:17:53.870617856 +0800
@@ -1,57 +1,57 @@
 /.cvsignore/1.1/Wed Dec 29 20:28:14 2004//
-/Makefile.am/1.14/Thu Feb  3 18:35:42 2005//
-/cmdcolor.cpp/1.13/Mon Dec 20 13:10:38 2004//
-/cmdcolor.h/1.11/Mon Dec 20 13:10:38 2004//
-/cmddialog.cpp/1.23/Sun Jan  2 19:20:54 2005//
-/cmddialog.h/1.12/Mon Dec 20 13:10:38 2004//
-/cmddoc.cpp/1.19/Mon Dec 20 13:10:38 2004//
-/cmddoc.h/1.12/Mon Dec 20 13:10:38 2004//
-/cmdgetprop.cpp/1.17/Thu Jan 20 16:16:02 2005//
-/cmdgetprop.h/1.8/Mon Dec 20 13:10:38 2004//
-/cmdmani.cpp/1.19/Fri Feb  4 19:21:15 2005//
-/cmdmani.h/1.11/Wed Feb  2 15:13:05 2005//
-/cmdmisc.cpp/1.23/Wed Jan  5 14:27:34 2005//
-/cmdmisc.h/1.11/Mon Dec 20 13:10:38 2004//
-/cmdobj.cpp/1.20/Mon Dec 20 13:10:38 2004//
-/cmdobj.h/1.12/Mon Dec 20 13:10:38 2004//
-/cmdpage.cpp/1.22/Mon Dec 20 13:10:38 2004//
-/cmdpage.h/1.13/Mon Dec 20 13:10:38 2004//
-/cmdsetprop.cpp/1.16/Mon Dec 20 13:10:38 2004//
-/cmdsetprop.h/1.7/Mon Dec 20 13:10:38 2004//
-/cmdtext.cpp/1.30/Thu Jan 20 16:16:02 2005//
-/cmdtext.h/1.12/Wed Feb  2 14:12:03 2005//
-/cmdutil.cpp/1.11/Wed Feb  2 14:12:03 2005//
-/cmdutil.h/1.8/Fri Dec  3 15:02:55 2004//
-/cmdvar.h/1.13/Wed Feb  2 14:12:03 2005//
-/conswin.cpp/1.8/Thu Dec 23 12:15:20 2004//
-/conswin.h/1.3/Wed Apr 14 20:55:41 2004//
-/editmacrodialog.ui/1.1/Wed Feb  2 14:12:03 2005//
-/editmacrodialog.ui.h/1.1/Wed Feb  2 14:12:03 2005//
-/extmacro.cpp/1.1/Wed Feb  2 14:12:03 2005//
-/extmacro.h/1.1/Wed Feb  2 14:12:03 2005//
-/guiapp.cpp/1.12/Mon Dec 20 13:10:38 2004//
-/guiapp.h/1.8/Mon Dec 20 13:10:38 2004//
-/macro.cpp/1.1/Wed Feb  2 14:12:03 2005//
-/macro.h/1.1/Wed Feb  2 14:12:03 2005//
-/macromanager.cpp/1.1/Wed Feb  2 14:12:03 2005//
-/macromanager.h/1.1/Wed Feb  2 14:12:03 2005//
-/managemacrosdialog.ui/1.1/Wed Feb  2 14:12:03 2005//
-/managemacrosdialog.ui.h/1.1/Wed Feb  2 14:12:03 2005//
-/objimageexport.cpp/1.1/Thu Feb  3 18:35:42 2005//
-/objimageexport.h/1.1/Thu Feb  3 18:35:42 2005//
-/objpdffile.cpp/1.5/Fri Dec  3 15:02:55 2004//
-/objpdffile.h/1.2/Fri Dec  3 15:02:55 2004//
-/objprinter.cpp/1.6/Fri Dec  3 15:02:55 2004//
-/objprinter.h/1.2/Fri Dec  3 15:02:55 2004//
-/pconsole.cpp/1.3/Wed Apr 14 20:55:41 2004//
-/pconsole.h/1.1/Mon Oct 20 22:07:06 2003//
-/scripterprefs.cpp/1.2/Fri Feb  4 19:21:15 2005//
-/scripterprefs.h/1.1/Wed Feb  2 14:12:03 2005//
-/scripterprefs.ui/1.1/Wed Feb  2 14:12:03 2005//
-/scripterprefs.ui.h/1.1/Wed Feb  2 14:12:03 2005//
-/scriptplugin.cpp/1.62/Thu Feb  3 20:51:19 2005//
-/scriptplugin.h/1.12/Wed Feb  2 14:12:03 2005//
-/valuedialog.cpp/1.4/Tue Nov 23 17:30:18 2004//
-/valuedialog.h/1.1/Tue Sep 28 22:42:25 2004//
 D/samples////
 D/scripts////
+/Makefile.am/1.14/Mon Feb  7 15:12:24 2005//
+/cmdvar.h/1.13/Mon Feb  7 15:12:24 2005//
+/macromanager.cpp/1.1/Mon Feb  7 15:12:24 2005//
+/scriptplugin.cpp/1.62/Mon Feb  7 15:12:25 2005//
+/scriptplugin.h/1.12/Mon Feb  7 15:12:25 2005//
+/cmdcolor.cpp/1.13/Mon Feb  7 15:12:36 2005//
+/cmdcolor.h/1.11/Mon Feb  7 15:12:36 2005//
+/cmddialog.cpp/1.23/Mon Feb  7 15:12:36 2005//
+/cmddialog.h/1.12/Mon Feb  7 15:12:36 2005//
+/cmddoc.cpp/1.19/Mon Feb  7 15:12:36 2005//
+/cmddoc.h/1.12/Mon Feb  7 15:12:36 2005//
+/cmdgetprop.cpp/1.17/Mon Feb  7 15:12:36 2005//
+/cmdgetprop.h/1.8/Mon Feb  7 15:12:36 2005//
+/cmdmani.cpp/1.19/Mon Feb  7 15:12:36 2005//
+/cmdmani.h/1.11/Mon Feb  7 15:12:36 2005//
+/cmdmisc.cpp/1.23/Mon Feb  7 15:12:36 2005//
+/cmdmisc.h/1.11/Mon Feb  7 15:12:36 2005//
+/cmdobj.cpp/1.20/Mon Feb  7 15:12:36 2005//
+/cmdobj.h/1.12/Mon Feb  7 15:12:36 2005//
+/cmdpage.cpp/1.22/Mon Feb  7 15:12:36 2005//
+/cmdpage.h/1.13/Mon Feb  7 15:12:36 2005//
+/cmdsetprop.cpp/1.16/Mon Feb  7 15:12:36 2005//
+/cmdsetprop.h/1.7/Mon Feb  7 15:12:36 2005//
+/cmdtext.cpp/1.30/Mon Feb  7 15:12:36 2005//
+/cmdtext.h/1.12/Mon Feb  7 15:12:36 2005//
+/cmdutil.cpp/1.11/Mon Feb  7 15:12:36 2005//
+/cmdutil.h/1.8/Mon Feb  7 15:12:36 2005//
+/conswin.cpp/1.8/Mon Feb  7 15:12:36 2005//
+/conswin.h/1.3/Mon Feb  7 15:12:36 2005//
+/editmacrodialog.ui/1.1/Mon Feb  7 15:12:36 2005//
+/editmacrodialog.ui.h/1.1/Mon Feb  7 15:12:36 2005//
+/extmacro.cpp/1.1/Mon Feb  7 15:12:36 2005//
+/extmacro.h/1.1/Mon Feb  7 15:12:36 2005//
+/guiapp.cpp/1.12/Mon Feb  7 15:12:36 2005//
+/guiapp.h/1.8/Mon Feb  7 15:12:36 2005//
+/macro.cpp/1.1/Mon Feb  7 15:12:36 2005//
+/macro.h/1.1/Mon Feb  7 15:12:36 2005//
+/macromanager.h/1.1/Mon Feb  7 15:12:36 2005//
+/managemacrosdialog.ui/1.1/Mon Feb  7 15:12:36 2005//
+/managemacrosdialog.ui.h/1.1/Mon Feb  7 15:12:36 2005//
+/objimageexport.cpp/1.1/Mon Feb  7 15:12:36 2005//
+/objimageexport.h/1.1/Mon Feb  7 15:12:36 2005//
+/objpdffile.cpp/1.5/Mon Feb  7 15:12:36 2005//
+/objpdffile.h/1.2/Mon Feb  7 15:12:36 2005//
+/objprinter.cpp/1.6/Mon Feb  7 15:12:36 2005//
+/objprinter.h/1.2/Mon Feb  7 15:12:36 2005//
+/pconsole.cpp/1.3/Mon Feb  7 15:12:36 2005//
+/pconsole.h/1.1/Mon Feb  7 15:12:36 2005//
+/scripterprefs.cpp/1.2/Mon Feb  7 15:17:53 2005//
+/scripterprefs.h/1.1/Mon Feb  7 15:17:53 2005//
+/scripterprefs.ui/1.1/Mon Feb  7 15:12:36 2005//
+/scripterprefs.ui.h/1.1/Mon Feb  7 15:12:36 2005//
+/valuedialog.cpp/1.4/Mon Feb  7 15:12:36 2005//
+/valuedialog.h/1.1/Mon Feb  7 15:12:36 2005//
diff -r -u --new-file scriptplugin.orig/macromanager.cpp scriptplugin/macromanager.cpp
--- scriptplugin.orig/macromanager.cpp	2005-02-02 22:12:03.000000000 +0800
+++ scriptplugin/macromanager.cpp	2005-02-07 23:12:36.657841528 +0800
@@ -3,6 +3,7 @@
 #include "macro.h"
 #include "editmacrodialog.h"
 #include "managemacrosdialog.h"
+#include "scriptercore.h"
 
 #include "qinputdialog.h"
 #include "qmessagebox.h"
@@ -412,16 +413,10 @@
 // and not intended for user editing. While this should so
 // more checking, currently it just runs the script in the
 // main namespace.
-void MacroManager::importMacros(QString /*filePath*/)
+void MacroManager::importMacros(QString filePath)
 {
-	// FIXME: this can't work until MenuTest is factored
-	// out of scriptplugin.cpp and into a separate class.
-	QMessageBox::warning(Carrier, tr("Scribus - Macro Manager"),
-			"<qt>Sorry, the macro importer doesn't currently work. "
-			"You can import saved macro files using Script->Load Extension Script... "
-			"and as start-up scripts.</qt>",
-			QMessageBox::Ok|QMessageBox::Default|QMessageBox::Escape,
-			QMessageBox::NoButton);
+	// Use `load script from file' to execute the macro file
+	scripterCore->slotRunScriptFile(filePath, true);
 }
 
 
diff -r -u --new-file scriptplugin.orig/Makefile.am scriptplugin/Makefile.am
--- scriptplugin.orig/Makefile.am	2005-02-04 02:35:42.000000000 +0800
+++ scriptplugin/Makefile.am	2005-02-07 23:12:36.659841224 +0800
@@ -8,9 +8,9 @@
 
 libscriptplugin_la_LDFLAGS = -version-info 0:0:0
 libscriptplugin_la_METASOURCES = AUTO
-libscriptplugin_la_SOURCES = valuedialog.cpp cmdutil.cpp cmddialog.cpp cmddoc.cpp cmdpage.cpp cmdobj.cpp cmdgetprop.cpp cmdsetprop.cpp cmdtext.cpp cmdmani.cpp cmdcolor.cpp cmdmisc.cpp objprinter.cpp objpdffile.cpp extmacro.cpp guiapp.cpp conswin.cpp pconsole.cpp scripterprefs.ui managemacrosdialog.ui editmacrodialog.ui macro.cpp macromanager.cpp scriptplugin.cpp objimageexport.cpp
+libscriptplugin_la_SOURCES = valuedialog.cpp cmdutil.cpp cmddialog.cpp cmddoc.cpp cmdpage.cpp cmdobj.cpp cmdgetprop.cpp cmdsetprop.cpp cmdtext.cpp cmdmani.cpp cmdcolor.cpp cmdmisc.cpp objprinter.cpp objpdffile.cpp extmacro.cpp guiapp.cpp conswin.cpp pconsole.cpp scripterprefs.ui managemacrosdialog.ui editmacrodialog.ui macro.cpp macromanager.cpp scriptplugin.cpp objimageexport.cpp scriptercore.cpp
 
-EXTRA_DIST = valuedialog.h cmdvar.h cmdutil.cpp cmdutil.h cmddialog.cpp cmddialog.h cmddoc.cpp cmddoc.h cmdpage.cpp cmdpage.h cmdobj.cpp cmdobj.h cmdgetprop.cpp cmdgetprop.h cmdsetprop.cpp cmdsetprop.h cmdtext.cpp cmdtext.h cmdmani.cpp cmdmani.h cmdcolor.cpp cmdcolor.h cmdmisc.cpp cmdmisc.h objprinter.cpp objprinter.h objpdffile.cpp objpdffile.h extmacro.cpp extmacro.h guiapp.cpp guiapp.h scriptplugin.cpp scriptplugin.h pconsole.cpp pconsole.h conswin.cpp conswin.h scripterprefs.ui scripterprefs.ui.h managemacrosdialog.ui managemacrosdialog.ui.h editmacrodialog.ui editmacrodialog.ui.h macromanager.cpp macromanager.h macro.cpp macro.h objimageexport.cpp objimageexport.h
+EXTRA_DIST = valuedialog.h cmdvar.h cmdutil.cpp cmdutil.h cmddialog.cpp cmddialog.h cmddoc.cpp cmddoc.h cmdpage.cpp cmdpage.h cmdobj.cpp cmdobj.h cmdgetprop.cpp cmdgetprop.h cmdsetprop.cpp cmdsetprop.h cmdtext.cpp cmdtext.h cmdmani.cpp cmdmani.h cmdcolor.cpp cmdcolor.h cmdmisc.cpp cmdmisc.h objprinter.cpp objprinter.h objpdffile.cpp objpdffile.h extmacro.cpp extmacro.h guiapp.cpp guiapp.h scriptplugin.cpp scriptplugin.h pconsole.cpp pconsole.h conswin.cpp conswin.h scripterprefs.ui scripterprefs.ui.h managemacrosdialog.ui managemacrosdialog.ui.h editmacrodialog.ui editmacrodialog.ui.h macromanager.cpp macromanager.h macro.cpp macro.h objimageexport.cpp objimageexport.h scriptercore.cpp scriptercore.h
 
 KDE_OPTIONS = qtonly
 if COMPILE_DEBUG
diff -r -u --new-file scriptplugin.orig/scriptercore.cpp scriptplugin/scriptercore.cpp
--- scriptplugin.orig/scriptercore.cpp	1970-01-01 08:00:00.000000000 +0800
+++ scriptplugin/scriptercore.cpp	2005-02-07 23:12:36.663840616 +0800
@@ -0,0 +1,568 @@
+// Scripter headers
+#include "cmdvar.h"
+#include "scriptercore.h"
+#include "pconsole.h"
+#include "conswin.h"
+#include "macromanager.h"
+#include "scripterprefs.h"
+
+// Scribus headers
+#include "helpbrowser.h"
+#include "customfdialog.h"
+#include "mpalette.h"
+
+// Qt headers
+#include "qwidget.h"
+#include "qpopupmenu.h"
+#include "qdir.h"
+#include "qfileinfo.h"
+#include "qmessagebox.h"
+#include "qtextcodec.h"
+#include "qdom.h"
+#include "qtextstream.h"
+
+#include <cstdlib>
+
+#include "scribus.h"
+
+ScripterCore::ScripterCore(QWidget* parent)
+{
+	this->setName("scripterCore");
+	men = new QPopupMenu();
+	this->rmen = new QPopupMenu();
+	this->smen = new QPopupMenu();
+	if (!men | !this->rmen | !this->smen)
+	{
+		qDebug("ScripterCore::ScripterCore(): Couldn't build menus");
+		return;
+	}
+	this->SavedRecentScripts.clear();
+	// init prefs variables
+	this->enableExtPython = false;
+	this->importAllNames = true;
+	this->legacyAliases = true;
+	this->startupScriptEnable = false;
+	this->useDummyStdin = true;
+	this->startupScript = QString();
+	// now load the prefs
+	this->ReadPlugPrefs();
+	QString pfad = SCRIPTSDIR;
+	QString pfad2;
+	pfad2 = QDir::convertSeparators(pfad);
+	QDir ds(pfad2, "*.py", QDir::Name, QDir::Files | QDir::NoSymLinks);
+	if ((ds.exists()) && (ds.count() != 0))
+	{
+		for (uint dc = 0; dc < ds.count(); ++dc)
+		{
+			QFileInfo fs(ds[dc]);
+			this->smen->insertItem(fs.baseName(true));
+		}
+	}
+	this->RecentScripts.clear();
+	if (this->SavedRecentScripts.count() != 0)
+	{
+		uint max = QMIN(Carrier->Prefs.RecentDCount, this->SavedRecentScripts.count());
+		for (uint m = 0; m < max; ++m)
+		{
+			QFileInfo fd(this->SavedRecentScripts[m]);
+			if (fd.exists())
+			{
+				this->RecentScripts.append(this->SavedRecentScripts[m]);
+				this->rmen->insertItem(this->SavedRecentScripts[m]);
+			}
+		}
+	}
+	int id = -1;
+	this->pcon = new PConsole(parent);
+	if (!this->pcon)
+	{
+		qDebug("ScripterCore::ScripterCore(): Unable to construct script console");
+		return;
+	}
+	this->smenid = men->insertItem(tr("&Scribus Scripts"), this->smen);
+	id = men->insertItem(tr("&Execute Script..."), this, SLOT(slotTest()));
+	men->setWhatsThis(id, tr("Run a Python script from a file.","scripter"));
+	id = men->insertItem(tr("&Load Extension Script..."), this, SLOT(loadScript()));
+	men->setWhatsThis(id, tr("Load a Python script as an extension. "
+	                  "Used for loading macros and for advanced Python scripts that "
+	                  "extend the Scribus user interface.","scripter"));
+	this->rmenid = men->insertItem(tr("&Recent Scripts"), this->rmen);
+	men->insertSeparator();
+	id = this->cons = men->insertItem(tr("Show &Console"), this, SLOT(slotInteractiveScript()));
+	men->setWhatsThis(id, tr("Display an interactive Python console where you can write and "
+	                                  "run Python programs that use the Scripter tools.","scripter"));
+	this->about = men->insertItem(tr("&About Script..."), this, SLOT(aboutScript()));
+	Carrier->menuBar()->insertItem(tr("S&cript"), men, -1, Carrier->menuBar()->count() - 2);
+	connect(this->pcon->OutWin, SIGNAL(returnPressed()), this, SLOT(slotExecute()));
+	connect(this->pcon, SIGNAL(Schliessen()), this, SLOT(slotInteractiveScript()));
+	connect(this->rmen, SIGNAL(activated(int)), this, SLOT(RecentScript(int)));
+	connect(this->smen, SIGNAL(activated(int)), this, SLOT(StdScript(int)));
+	men->insertSeparator();
+	id = men->insertItem(tr("Scripter &Settings","script menu"), this, SLOT(preferencesDialog()));
+}
+
+void ScripterCore::runStartupScript()
+{
+	// and run the start-up script, if any
+	if (this->enableExtPython && this->startupScriptEnable)
+	{
+		if (QFile::exists(this->startupScript))
+		{
+			// run the script in the main interpreter. The user will be informed
+			// with a dialog if something has gone wrong.
+			this->slotRunScriptFile(this->startupScript, true);
+		}
+		else
+			qDebug("Startup script enabled, but couln't find script %s.", (const char*)(this->startupScript.utf8()) );
+	}
+}
+
+ScripterCore::~ScripterCore()
+{
+	SavePlugPrefs();
+}
+
+void ScripterCore::FinishScriptRun()
+{
+	if (Carrier->HaveDoc)
+	{
+		Carrier->Mpal->SetDoc(Carrier->doc);
+		Carrier->Mpal->updateCList();
+		Carrier->Mpal->Spal->SetFormats(Carrier->doc);
+		Carrier->Mpal->SetLineFormats(Carrier->doc);
+		Carrier->Mpal->Cpal->SetColors(Carrier->doc->PageColors);
+		Carrier->Lpal->setLayers(&Carrier->doc->Layers, &Carrier->doc->ActiveLayer);
+		Carrier->Tpal->BuildTree(Carrier->view);
+		Carrier->Sepal->SetView(Carrier->view);
+		Carrier->Sepal->Rebuild();
+		if (Carrier->doc->ActPage->SelItem.count() != 0)
+			Carrier->HaveNewSel(Carrier->doc->ActPage->SelItem.at(0)->PType);
+		else
+			Carrier->HaveNewSel(-1);
+		Carrier->view->DrawNew();
+	}
+}
+
+void ScripterCore::slotTest()
+{
+	QString fileName;
+	QString CurDirP = QDir::currentDirPath();
+	QString scriptDir = Carrier->Prefs.ScriptDir;
+	if (scriptDir == "")
+		scriptDir = CurDirP;
+	CustomFDialog diaf((QWidget*)parent(), scriptDir, tr("Open"), tr("Python Scripts (*.py);; All Files (*)"));
+	if (diaf.exec())
+	{
+		fileName = diaf.selectedFile();
+		slotRunScriptFile(fileName);
+		rmen->clear();
+		if (RecentScripts.findIndex(fileName) == -1)
+			RecentScripts.prepend(fileName);
+		else
+		{
+			RecentScripts.remove(fileName);
+			RecentScripts.prepend(fileName);
+		}
+		uint max = QMIN(Carrier->Prefs.RecentDCount, RecentScripts.count());
+		for (uint m = 0; m < max; m++)
+		{
+			rmen->insertItem(RecentScripts[m]);
+		}
+	}
+	QDir::setCurrent(CurDirP);
+	FinishScriptRun();
+}
+
+void ScripterCore::StdScript(int id)
+{
+	QString pfad = SCRIPTSDIR;
+	QString pfad2;
+	pfad2 = QDir::convertSeparators(pfad);
+	QString fn = pfad2+smen->text(id)+".py";
+	QFileInfo fd(fn);
+	if (!fd.exists())
+		return;
+	slotRunScriptFile(fn);
+	FinishScriptRun();
+}
+
+void ScripterCore::RecentScript(int id)
+{
+	QString fn = rmen->text(id);
+	QFileInfo fd(fn);
+	if (!fd.exists())
+	{
+		RecentScripts.remove(fn);
+		rmen->clear();
+		uint max = QMIN(Carrier->Prefs.RecentDCount, RecentScripts.count());
+		for (uint m = 0; m < max; m++)
+		{
+			rmen->insertItem(RecentScripts[m]);
+		}
+		return;
+	}
+	slotRunScriptFile(fn);
+	FinishScriptRun();
+}
+
+void ScripterCore::slotRunScriptFile(QString fileName, bool inMainInterpreter)
+{
+	char* comm[1];
+	PyThreadState *stateo, *state;
+	QFileInfo fi(fileName);
+	QCString na = fi.fileName().latin1();
+	// Set up a sub-interpreter if needed:
+	if (!inMainInterpreter)
+	{
+		Carrier->ScriptRunning = true;
+		qApp->setOverrideCursor(QCursor(waitCursor), false);
+		// Actually make the sub interpreter
+		// FIXME: This calls abort() in a Python debug build. We're doing something wrong.
+		stateo = PyEval_SaveThread();
+		state = Py_NewInterpreter();
+		// chdir to the dir the script is in
+		QDir::setCurrent(fi.dirPath(true));
+		// init the 'scribus' module in the sub-interpreter
+		initscribus(Carrier);
+	}
+	// make sure sys.argv[0] is the path to the script
+	comm[0] = na.data();
+	PySys_SetArgv(1, comm);
+	// call python script
+	PyObject* m = PyImport_AddModule((char*)"__main__");
+	if (m == NULL)
+		qDebug("Failed to get __main__ - aborting script");
+	else
+	{
+		// FIXME: If filename contains chars outside 7bit ascii, might be problems
+		PyObject* globals = PyModule_GetDict(m);
+		// Build the Python code to run the script
+		QString cm = QString("from __future__ import division\n");
+		cm        += QString("import sys\n");
+		cm        += QString("import cStringIO\n");
+		/* Implementation of the help() in pydoc.py reads some OS variables
+		 * for output settings. I use ugly hack to stop freezing calling help()
+		 * in script. pv. */
+		cm += QString("import os\nos.environ['PAGER'] = '/bin/false'\n"); // HACK
+		cm        += QString("sys.path[0] = \"%1\"\n").arg(fi.dirPath(true));
+		// Replace sys.stdin with a dummy StringIO that always returns
+		// "" for read
+		if (useDummyStdin)
+			cm    += QString("sys.stdin = cStringIO.StringIO()\n");
+		cm        += QString("try:\n");
+		cm        += QString("    execfile(\"%1\")\n").arg(fileName);
+		cm        += QString("except SystemExit:\n");
+		cm        += QString("    pass\n");
+		// Capture the text of any other exception that's raised by the interpreter
+		// into a StringIO buffer for later extraction.
+		cm        += QString("except:\n");
+		cm        += QString("    import traceback\n");
+		cm        += QString("    import scribus\n");                  // we stash our working vars here
+		cm        += QString("    scribus._f=cStringIO.StringIO()\n");
+		cm        += QString("    traceback.print_exc(file=scribus._f)\n");
+		cm        += QString("    _errorMsg = scribus._f.getvalue()\n");
+		cm        += QString("    del(scribus._f)\n");
+		// We re-raise the exception so the return value of PyRun_StringFlags reflects
+		// the fact that an exception has ocurred.
+		cm        += QString("    raise\n");
+		// FIXME: if cmd contains chars outside 7bit ascii, might be problems
+		QCString cmd = cm.latin1();
+		// Now run the script in the interpreter's global scope. It'll run in a
+		// sub-interpreter if we created and switched to one earlier, otherwise
+		// it'll run in the main interpreter.
+		PyObject* result = PyRun_String(cmd.data(), Py_file_input, globals, globals);
+		// NULL is returned if an exception is set. We don't care about any
+		// other return value (most likely None anyway) and can ignore it.
+		if (result == NULL)
+		{
+			// We've already saved the exception text, so clear the exception
+			PyErr_Clear();
+			PyObject* errorMsgPyStr = PyMapping_GetItemString(globals, (char*)"_errorMsg");
+			if (errorMsgPyStr == NULL)
+			{
+				// It's rather unlikely that this will ever be reached - to get here
+				// we'd have to fail to retrive the string we just created.
+				qDebug("Error retrieving error message content after script exception!");
+				qDebug("Exception was:");
+				PyErr_Print();
+			}
+			else
+			{
+				QString errorMsg = PyString_AsString(errorMsgPyStr);
+				// Display a dialog to the user with the exception
+				QClipboard *cp = QApplication::clipboard();
+				cp->setText(errorMsg);
+				QMessageBox::warning(Carrier,
+									tr("Script error"),
+									tr("If you are running an official script report it at <a href=\"http://bugs.scribus.net\">bugs.scribus.net</a> please.")
+									+ "<pre>" +errorMsg + "</pre>"
+									+ tr("This message is in your clipboard too. Use Ctrl+V to paste it into bug tracker."));
+			}
+		} // end if result == NULL
+		// Because 'result' may be NULL, not a PyObject*, we must call PyXDECREF not Py_DECREF
+		Py_XDECREF(result);
+	} // end if m == NULL
+	if (!inMainInterpreter)
+	{
+		Py_EndInterpreter(state);
+		PyEval_RestoreThread(stateo);
+		Carrier->ScriptRunning = false;
+		qApp->restoreOverrideCursor();
+	}
+}
+
+QString ScripterCore::slotRunScript(QString Script)
+{
+	qApp->setOverrideCursor(QCursor(waitCursor), false);
+	char* comm[1];
+	QString cm;
+	InValue = Script;
+	QString CurDir = QDir::currentDirPath();
+	if(PyThreadState_Get() != NULL)
+	{
+		initscribus(Carrier);
+		if (RetVal == 0)
+		{
+			// FIXME: if CurDir contains chars outside 7bit ascii, might be problems
+			cm =  "import sys\n";
+			cm += "sys.path[0] = \""+CurDir+"\"\n";
+			cm += "import cStringIO\n";
+			cm += "import scribus\n";
+			// Only import all names from 'scribus' to the global namespace if the user wants us to.
+			// We still need to pull in a few special names used by Scribus though.
+			if (importAllNames)
+				cm += "from scribus import *\n";
+			if (useDummyStdin)
+				cm += "sys.stdin = cStringIO.StringIO()\n";
+			cm += "scribus._bu = cStringIO.StringIO()\n";
+			cm += "sys.stdout = scribus._bu\n";
+			cm += "sys.stderr = scribus._bu\n";
+			cm += "import code\n";
+			cm += "scribus._ia = code.InteractiveConsole(globals())\n";
+		}
+		cm += "scribus._sc = scribus._getval()\n";
+		cm += "scribus._rv = scribus._ia.push(scribus._sc)\n";
+		cm += "if scribus._rv == 1:\n";
+		cm += "    scribus._re = \"...\"\n";
+		cm += "else:\n";
+		cm += "    scribus._re = scribus._bu.getvalue()\n";
+		cm += "scribus._retval(scribus._re, scribus._rv)\n";
+	}
+	// FIXME: if cmd contains chars outside 7bit ascii, might be problems
+	QCString cmd = cm.latin1();
+	comm[0] = (char*)"scribus";
+	PySys_SetArgv(1, comm);
+	PyRun_SimpleString(cmd.data());
+	if (RetVal == 0)
+	{
+		RetString += ">>>";
+		pcon->OutWin->Prompt = ">>>";
+	}
+	else
+		pcon->OutWin->Prompt = "...";
+	qApp->restoreOverrideCursor();
+	return RetString;
+}
+
+void ScripterCore::loadScript()
+{
+	if (!this->enableExtPython)
+	{
+		QMessageBox::information(Carrier, tr("Scribus - Script Plugin"),
+			tr("The 'Load Script' function of the script plugin is currently disabled.\n"
+			   "If you just want to run a normal script, you probably want to use\n"
+			   "'Execute Script...' instead.\n\n"
+			   "If you do actually want to load a Python extension script or macro, you\n"
+			   "need to go into the Scripter Settings in the Script menu and enable\n"
+			   "scripter extensions there.\n\n"
+			   "Please read the documentation on extension scripts first.\n"));
+		return;
+	}
+	QString fileName;
+	QString scriptDir = Carrier->Prefs.ScriptDir;
+	if (scriptDir == "")
+		scriptDir = QDir::currentDirPath();
+	CustomFDialog diaf((QWidget*)parent(), scriptDir, tr("Open"), tr("Python Scripts (*.py);; All Files (*)"));
+	if (diaf.exec())
+	{
+		fileName = diaf.selectedFile();
+		// Run the script in the main interpreter, not a sub-interpreter
+		slotRunScriptFile(fileName, true);
+	}
+}
+
+void ScripterCore::slotInteractiveScript()
+{
+	if (pcon->isVisible())
+	{
+		men->changeItem(cons, tr("Show &Console"));
+		pcon->hide();
+	}
+	else
+	{
+		men->changeItem(cons, tr("Hide &Console"));
+		pcon->show();
+	}
+}
+
+void ScripterCore::slotExecute()
+{
+	pcon->OutWin->append(slotRunScript(pcon->OutWin->LastComm));
+	pcon->OutWin->moveCursor(QTextEdit::MoveEnd, false);
+	pcon->OutWin->scrollToBottom();
+	pcon->OutWin->ensureCursorVisible();
+	FinishScriptRun();
+}
+
+void ScripterCore::ReadPlugPrefs()
+{
+	QDomDocument docu("scriptrc");
+	QString ho = QDir::homeDirPath();
+	QFile f(QDir::convertSeparators(ho+"/.scribus/scripter.rc"));
+	if(!f.open(IO_ReadOnly))
+		return;
+	if(!docu.setContent(&f))
+	{
+		f.close();
+		return;
+	}
+	f.close();
+	QDomElement elem=docu.documentElement();
+	if (elem.tagName() != "SCRIPTRC")
+		return;
+	QDomNode DOC=elem.firstChild();
+	while(!DOC.isNull())
+	{
+		QDomElement dc=DOC.toElement();
+		if (dc.tagName()=="RECENT")
+			SavedRecentScripts.append(dc.attribute("NAME"));
+		// Check to see if the 'load script' menu item should be enabled
+		else if (dc.tagName() == "EXTPYTHON")
+			enableExtPython = dc.attribute("ENABLE") == "TRUE";
+		// and check whether we should do a 'from scribus import *' in the main interpreter;
+		else if (dc.tagName() == "IMPORTNAMES")
+			importAllNames = dc.attribute("ENABLE") == "TRUE";
+		// Should we import the old-style names?
+		else if (dc.tagName() == "LEGACYALIASES")
+			legacyAliases = dc.attribute("ENABLE") == "TRUE";
+		// Should we run a startup script? If so, where to load it from?
+		else if (dc.tagName() == "STARTUPSCRIPT")
+		{
+			startupScriptEnable = dc.attribute("ENABLE") == "TRUE";
+			startupScript = dc.attribute("FILE");
+		}
+		// replace stdin with a dummy object that always returns "" ?
+		else if (dc.tagName() == "USEDUMMYSTDIN")
+			useDummyStdin = dc.attribute("ENABLE") == "TRUE";
+		DOC=DOC.nextSibling();
+	}
+}
+
+void ScripterCore::SavePlugPrefs()
+{
+	QDomDocument docu("scriptrc");
+	QString st="<SCRIPTRC></SCRIPTRC>";
+	docu.setContent(st);
+	QDomElement elem=docu.documentElement();
+	for (uint rd=0; rd < this->RecentScripts.count(); ++rd)
+	{
+		QDomElement rde=docu.createElement("RECENT");
+		rde.setAttribute("NAME",this->RecentScripts[rd]);
+		elem.appendChild(rde);
+	}
+
+	// save the "load script" flag, from import flag, and aliases flag
+	QDomElement extPythonItem = docu.createElement("EXTPYTHON");
+	extPythonItem.setAttribute("ENABLE", enableExtPython ? "TRUE" : "FALSE");
+	elem.appendChild(extPythonItem);
+
+	QDomElement allNamesItem = docu.createElement("IMPORTNAMES");
+	allNamesItem.setAttribute("ENABLE", importAllNames ? "TRUE" : "FALSE");
+	elem.appendChild(allNamesItem);
+
+	QDomElement legacyAliasesItem = docu.createElement("LEGACYALIASES");
+	legacyAliasesItem.setAttribute("ENABLE", legacyAliases ? "TRUE" : "FALSE");
+	elem.appendChild(legacyAliasesItem);
+
+	QDomElement dummyStdinItem = docu.createElement("USEDUMMYSTDIN");
+	dummyStdinItem.setAttribute("ENABLE", useDummyStdin ? "TRUE" : "FALSE");
+	elem.appendChild(dummyStdinItem);
+
+	// save the startup script path and flag
+	QDomElement startupScriptItem = docu.createElement("STARTUPSCRIPT");
+	startupScriptItem.setAttribute("ENABLE", startupScriptEnable ? "TRUE" : "FALSE");
+	startupScriptItem.setAttribute("FILE", startupScript);
+	elem.appendChild(startupScriptItem);
+
+	// then write out the prefs file
+	QString ho = QDir::homeDirPath();
+	QFile f(QDir::convertSeparators(ho+"/.scribus/scripter.rc"));
+	if(!f.open(IO_WriteOnly))
+		return;
+	QTextStream s(&f);
+	s<<docu.toCString();
+	f.close();
+}
+
+/* 11/1/2004 pv - Show docstring of the script to the user.
+ * I don't know how to get docstring via e.g. pydoc because of
+ * it needs to run script => error cannot find scribus module
+ */
+void ScripterCore::aboutScript()
+{
+	QString fname = Carrier->CFileDialog(".", "about", "Scripts (*.py)", "", 0, 0, 0, 0);
+	QFileInfo fi = QFileInfo(fname);
+	QString html = QDir::convertSeparators(QDir::homeDirPath()+"/.scribus/aboutScript.html");
+	QFile input(fname);
+	if(!input.open(IO_ReadOnly))
+		return;
+	QFile output(html);
+	if(!output.open(IO_WriteOnly))
+		return;
+	QTextStream intputstream(&input);
+	QTextStream outputstream(&output);
+	QString content = intputstream.read();
+	QString docstring = content.section("\"\"\"", 1, 1);
+	if (docstring != "")
+	{
+		outputstream << "<h1>Documentation for: " << fi.fileName() << "</h1><p>";
+		outputstream << docstring.replace("\n\n", "<p>");
+	}
+	else
+	{
+		outputstream << "<pre>" << endl;
+		outputstream << "<p><b>Script "<< fi.fileName() << " doesn't contain any docstring!</b></p>" << content;
+		outputstream << "</pre>" << endl;
+	}
+	output.close();
+	input.close();
+	HelpBrowser *dia = new HelpBrowser(0, tr("About Script") + " " + fi.fileName(), "en", "", html);
+	dia->show();
+}
+
+/* 2005-01-02 CR
+ * Display a preferences dialog to let the user configure
+ * scripter settings such as enabling Python extension scripts
+ * and setting a start-up script. The prefs dialog is defined in
+ * scripterprefs.ui .
+ */
+void ScripterCore::preferencesDialog()
+{
+	ScripterPreferences* prefDia = new ScripterPreferences(Carrier, "scripterPreferences");
+	prefDia->extPythonChk->setChecked(this->enableExtPython);
+	prefDia->startupScriptGroup->setChecked(this->startupScriptEnable);
+	prefDia->startupScriptPath->setText(this->startupScript);
+	prefDia->importNamesChk->setChecked(this->importAllNames);
+	prefDia->legacyAliasesChk->setChecked(this->legacyAliases);
+	prefDia->useFakeStdinChk->setChecked(this->useDummyStdin);
+	if (prefDia->exec())
+	{
+		this->enableExtPython = prefDia->extPythonChk->isChecked();
+		this->startupScriptEnable = prefDia->startupScriptGroup->isChecked();
+		this->startupScript = prefDia->startupScriptPath->text();
+		this->importAllNames = prefDia->importNamesChk->isChecked();
+		this->legacyAliases = prefDia->legacyAliasesChk->isChecked();
+		this->useDummyStdin = prefDia->useFakeStdinChk->isChecked();
+	}
+	delete prefDia;
+}
diff -r -u --new-file scriptplugin.orig/scriptercore.h scriptplugin/scriptercore.h
--- scriptplugin.orig/scriptercore.h	1970-01-01 08:00:00.000000000 +0800
+++ scriptplugin/scriptercore.h	2005-02-07 23:12:36.663840616 +0800
@@ -0,0 +1,60 @@
+#ifndef SCRIPTER_CORE_H
+#define SCRIPTER_CORE_H
+
+#include "cmdvar.h"
+#include "qobject.h"
+#include "qstring.h"
+#include "qstringlist.h"
+
+class PConsole;
+class ScribusApp;
+class QPopupMenu;
+
+class ScripterCore : public QObject
+{
+    Q_OBJECT
+
+public:
+		ScripterCore(QWidget* parent);
+		~ScripterCore();
+		void ReadPlugPrefs();
+		void SavePlugPrefs();
+		void FinishScriptRun();
+		PConsole *pcon;
+		QPopupMenu* rmen;
+		QPopupMenu* smen;
+		int rmenid;
+		int smenid;
+		int cons;
+		int about;
+		QStringList SavedRecentScripts;
+		QStringList RecentScripts;
+		/// pref: Enable the "load script" menu item?
+		bool enableExtPython;
+		/// pref: Run 'from scribus import *' in the main interpreter at startup?
+		bool importAllNames;
+		/// pref: Make aliases of renamed functions for compatibility?
+		bool legacyAliases;
+		/// pref: Replace stdin with dummy cStringIO?
+		bool useDummyStdin;
+		/// pref: Run a script in the main interpreter at startup?
+		bool startupScriptEnable;
+		/// pref: Which script to run at startup, if enabled
+		QString startupScript;
+		// The script plugin's menu
+		QPopupMenu* men;
+public slots:
+		void slotTest();
+		void loadScript();
+		void StdScript(int id);
+		void RecentScript(int id);
+		void slotRunScriptFile(QString fileName, bool inMainInterpreter = false);
+		QString slotRunScript(QString Script);
+		void slotInteractiveScript();
+		void slotExecute();
+		void aboutScript();
+		void preferencesDialog();
+		void runStartupScript();
+};
+
+#endif
diff -r -u --new-file scriptplugin.orig/scriptplugin.cpp scriptplugin/scriptplugin.cpp
--- scriptplugin.orig/scriptplugin.cpp	2005-02-04 04:51:19.000000000 +0800
+++ scriptplugin/scriptplugin.cpp	2005-02-07 23:12:36.687836968 +0800
@@ -15,8 +15,7 @@
  *                                                                         *
  ***************************************************************************/
 
-// include cmdvar.h first, as it pulls in <Python.h>
-#include "cmdvar.h"
+#include "scriptplugin.h"
 
 #include "cmddialog.h"
 #include "cmddoc.h"
@@ -28,8 +27,6 @@
 #include "cmdmani.h"
 #include "cmdcolor.h"
 #include "cmdmisc.h"
-#include "scriptplugin.h"
-#include "scriptplugin.moc"
 #include "cmdutil.h"
 #include "objprinter.h"
 #include "objpdffile.h"
@@ -42,6 +39,7 @@
 #include "customfdialog.h"
 #include "helpbrowser.h"
 #include "mpalette.h"
+#include "scriptercore.h"
 
 #ifdef _MSC_VER
  #if (_MSC_VER >= 1200)
@@ -66,6 +64,14 @@
 PyObject* NameExistsError;
 PyObject* AccessDeniedError;
 
+// Script console globals declared `extern' in cmdvar.h
+QString RetString;
+QString InValue;
+int RetVal;
+
+// Scripter core instance
+ScripterCore* scripterCore;
+
 QString Name()
 {
 	return QObject::tr("S&cripter Manual...");
@@ -95,93 +101,24 @@
 	}
 	RetVal = 0;
 	// Init the class that manages the scripter and its gui
-	Tes = new MenuTest(d);
-	men = new QPopupMenu();
-	Tes->rmen = new QPopupMenu();
-	Tes->smen = new QPopupMenu();
-	Tes->SavedRecentScripts.clear();
-	// init prefs variables
-	Tes->enableExtPython = false;
-	Tes->importAllNames = true;
-	Tes->legacyAliases = true;
-	Tes->startupScriptEnable = false;
-	Tes->useDummyStdin = true;
-	Tes->startupScript = QString();
-	// now load the prefs
-	Tes->ReadPlugPrefs();
-	QString pfad = SCRIPTSDIR;
-	QString pfad2;
-	pfad2 = QDir::convertSeparators(pfad);
-	QDir ds(pfad2, "*.py", QDir::Name, QDir::Files | QDir::NoSymLinks);
-	if ((ds.exists()) && (ds.count() != 0))
-	{
-		for (uint dc = 0; dc < ds.count(); ++dc)
-		{
-			QFileInfo fs(ds[dc]);
-			Tes->smen->insertItem(fs.baseName(true));
-		}
-	}
-	Tes->RecentScripts.clear();
-	if (Tes->SavedRecentScripts.count() != 0)
-	{
-		uint max = QMIN(Carrier->Prefs.RecentDCount, Tes->SavedRecentScripts.count());
-		for (uint m = 0; m < max; ++m)
-		{
-			QFileInfo fd(Tes->SavedRecentScripts[m]);
-			if (fd.exists())
-			{
-				Tes->RecentScripts.append(Tes->SavedRecentScripts[m]);
-				Tes->rmen->insertItem(Tes->SavedRecentScripts[m]);
-			}
-		}
-	}
-	int id = -1;
-	Tes->pcon = new PConsole(d);
-	Tes->smenid = men->insertItem(QObject::tr("&Scribus Scripts"), Tes->smen);
-	id = men->insertItem(QObject::tr("&Execute Script..."), Tes, SLOT(slotTest()));
-	men->setWhatsThis(id, QObject::tr("Run a Python script from a file.","scripter"));
-	id = men->insertItem(QObject::tr("&Load Extension Script..."), Tes, SLOT(loadScript()));
-	men->setWhatsThis(id, QObject::tr("Load a Python script as an extension. "
-	                  "Used for loading macros and for advanced Python scripts that "
-	                  "extend the Scribus user interface.","scripter"));
-	Tes->rmenid = men->insertItem(QObject::tr("&Recent Scripts"), Tes->rmen);
-	men->insertSeparator();
-	id = Tes->cons = men->insertItem(QObject::tr("Show &Console"), Tes, SLOT(slotInteractiveScript()));
-	men->setWhatsThis(id, QObject::tr("Display an interactive Python console where you can write and "
-	                                  "run Python programs that use the Scripter tools.","scripter"));
-	Tes->about = men->insertItem(QObject::tr("&About Script..."), Tes, SLOT(aboutScript()));
-	plug->menuBar()->insertItem(QObject::tr("S&cript"), men, -1, plug->menuBar()->count() - 2);
-	QObject::connect(Tes->pcon->OutWin, SIGNAL(returnPressed()), Tes, SLOT(slotExecute()));
-	QObject::connect(Tes->pcon, SIGNAL(Schliessen()), Tes, SLOT(slotInteractiveScript()));
-	QObject::connect(Tes->rmen, SIGNAL(activated(int)), Tes, SLOT(RecentScript(int)));
-	QObject::connect(Tes->smen, SIGNAL(activated(int)), Tes, SLOT(StdScript(int)));
-	men->insertSeparator();
-	id = men->insertItem(QObject::tr("Scripter &Settings","script menu"), Tes, SLOT(preferencesDialog()));
-	// Now init the scribus module in the main interpreter
+	scripterCore = new ScripterCore(d);
+	if (!scripterCore)
+		qDebug("scripterCore failed to initialize");
+	// Set up the `scribus' module in the main interpreter
 	initscribus(Carrier);
 	// If Python extensions are enabled, load the macro manager.
-	if (Tes->enableExtPython)
+	if (scripterCore->enableExtPython)
 		MacroManager::instance();
 	// and run the start-up script, if any
-	if (Tes->enableExtPython && Tes->startupScriptEnable)
-	{
-		if (QFile::exists(Tes->startupScript))
-		{
-			// run the script in the main interpreter. The user will be informed
-			// with a dialog if something has gone wrong.
-			Tes->slotRunScriptFile(Tes->startupScript, true);
-		}
-		else
-			qDebug("Startup script enabled, but couln't find script %s.", (const char*)(Tes->startupScript.utf8()) );
-	}
+	scripterCore->runStartupScript();
 }
 
 void CleanUpPlug()
 {
 	MacroManager::deleteInstance();
+	if (scripterCore)
+		delete(scripterCore);
 	Py_Finalize();
-	Tes->SavePlugPrefs();
-	delete(Tes);
 }
 
 void Run(QWidget *d, ScribusApp *plug)
@@ -193,452 +130,6 @@
 	dia->show();
 }
 
-
-void MenuTest::FinishScriptRun()
-{
-	if (Carrier->HaveDoc)
-	{
-		Carrier->Mpal->SetDoc(Carrier->doc);
-		Carrier->Mpal->updateCList();
-		Carrier->Mpal->Spal->SetFormats(Carrier->doc);
-		Carrier->Mpal->SetLineFormats(Carrier->doc);
-		Carrier->Mpal->Cpal->SetColors(Carrier->doc->PageColors);
-		Carrier->Lpal->setLayers(&Carrier->doc->Layers, &Carrier->doc->ActiveLayer);
-		Carrier->Tpal->BuildTree(Carrier->view);
-		Carrier->Sepal->SetView(Carrier->view);
-		Carrier->Sepal->Rebuild();
-		if (Carrier->doc->ActPage->SelItem.count() != 0)
-			Carrier->HaveNewSel(Carrier->doc->ActPage->SelItem.at(0)->PType);
-		else
-			Carrier->HaveNewSel(-1);
-		Carrier->view->DrawNew();
-	}
-}
-
-void MenuTest::slotTest()
-{
-	QString fileName;
-	QString CurDirP = QDir::currentDirPath();
-	QString scriptDir = Carrier->Prefs.ScriptDir;
-	if (scriptDir == "")
-		scriptDir = CurDirP;
-	CustomFDialog diaf((QWidget*)parent(), scriptDir, QObject::tr("Open"), QObject::tr("Python Scripts (*.py);; All Files (*)"));
-	if (diaf.exec())
-	{
-		fileName = diaf.selectedFile();
-		slotRunScriptFile(fileName);
-		rmen->clear();
-		if (RecentScripts.findIndex(fileName) == -1)
-			RecentScripts.prepend(fileName);
-		else
-		{
-			RecentScripts.remove(fileName);
-			RecentScripts.prepend(fileName);
-		}
-		uint max = QMIN(Carrier->Prefs.RecentDCount, RecentScripts.count());
-		for (uint m = 0; m < max; m++)
-		{
-			rmen->insertItem(RecentScripts[m]);
-		}
-	}
-	QDir::setCurrent(CurDirP);
-	FinishScriptRun();
-}
-
-void MenuTest::StdScript(int id)
-{
-	QString pfad = SCRIPTSDIR;
-	QString pfad2;
-	pfad2 = QDir::convertSeparators(pfad);
-	QString fn = pfad2+smen->text(id)+".py";
-	QFileInfo fd(fn);
-	if (!fd.exists())
-		return;
-	slotRunScriptFile(fn);
-	FinishScriptRun();
-}
-
-void MenuTest::RecentScript(int id)
-{
-	QString fn = rmen->text(id);
-	QFileInfo fd(fn);
-	if (!fd.exists())
-	{
-		RecentScripts.remove(fn);
-		rmen->clear();
-		uint max = QMIN(Carrier->Prefs.RecentDCount, RecentScripts.count());
-		for (uint m = 0; m < max; m++)
-		{
-			rmen->insertItem(RecentScripts[m]);
-		}
-		return;
-	}
-	slotRunScriptFile(fn);
-	FinishScriptRun();
-}
-
-void MenuTest::slotRunScriptFile(QString fileName, bool inMainInterpreter)
-{
-	char* comm[1];
-	PyThreadState *stateo, *state;
-	QFileInfo fi(fileName);
-	QCString na = fi.fileName().latin1();
-	// Set up a sub-interpreter if needed:
-	if (!inMainInterpreter)
-	{
-		Carrier->ScriptRunning = true;
-		qApp->setOverrideCursor(QCursor(waitCursor), false);
-		// Actually make the sub interpreter
-		// FIXME: This calls abort() in a Python debug build. We're doing something wrong.
-		stateo = PyEval_SaveThread();
-		state = Py_NewInterpreter();
-		// chdir to the dir the script is in
-		QDir::setCurrent(fi.dirPath(true));
-		// init the 'scribus' module in the sub-interpreter
-		initscribus(Carrier);
-	}
-	// make sure sys.argv[0] is the path to the script
-	comm[0] = na.data();
-	PySys_SetArgv(1, comm);
-	// call python script
-	PyObject* m = PyImport_AddModule((char*)"__main__");
-	if (m == NULL)
-		qDebug("Failed to get __main__ - aborting script");
-	else
-	{
-		// FIXME: If filename contains chars outside 7bit ascii, might be problems
-		PyObject* globals = PyModule_GetDict(m);
-		// Build the Python code to run the script
-		QString cm = QString("from __future__ import division\n");
-		cm        += QString("import sys\n");
-		cm        += QString("import cStringIO\n");
-		/* Implementation of the help() in pydoc.py reads some OS variables
-		 * for output settings. I use ugly hack to stop freezing calling help()
-		 * in script. pv. */
-		cm += QString("import os\nos.environ['PAGER'] = '/bin/false'\n"); // HACK
-		cm        += QString("sys.path[0] = \"%1\"\n").arg(fi.dirPath(true));
-		// Replace sys.stdin with a dummy StringIO that always returns
-		// "" for read
-		if (useDummyStdin)
-			cm    += QString("sys.stdin = cStringIO.StringIO()\n");
-		cm        += QString("try:\n");
-		cm        += QString("    execfile(\"%1\")\n").arg(fileName);
-		cm        += QString("except SystemExit:\n");
-		cm        += QString("    pass\n");
-		// Capture the text of any other exception that's raised by the interpreter
-		// into a StringIO buffer for later extraction.
-		cm        += QString("except:\n");
-		cm        += QString("    import traceback\n");
-		cm        += QString("    import scribus\n");                  // we stash our working vars here
-		cm        += QString("    scribus._f=cStringIO.StringIO()\n");
-		cm        += QString("    traceback.print_exc(file=scribus._f)\n");
-		cm        += QString("    _errorMsg = scribus._f.getvalue()\n");
-		cm        += QString("    del(scribus._f)\n");
-		// We re-raise the exception so the return value of PyRun_StringFlags reflects
-		// the fact that an exception has ocurred.
-		cm        += QString("    raise\n");
-		// FIXME: if cmd contains chars outside 7bit ascii, might be problems
-		QCString cmd = cm.latin1();
-		// Now run the script in the interpreter's global scope. It'll run in a
-		// sub-interpreter if we created and switched to one earlier, otherwise
-		// it'll run in the main interpreter.
-		PyObject* result = PyRun_String(cmd.data(), Py_file_input, globals, globals);
-		// NULL is returned if an exception is set. We don't care about any
-		// other return value (most likely None anyway) and can ignore it.
-		if (result == NULL)
-		{
-			// We've already saved the exception text, so clear the exception
-			PyErr_Clear();
-			PyObject* errorMsgPyStr = PyMapping_GetItemString(globals, (char*)"_errorMsg");
-			if (errorMsgPyStr == NULL)
-			{
-				// It's rather unlikely that this will ever be reached - to get here
-				// we'd have to fail to retrive the string we just created.
-				qDebug("Error retrieving error message content after script exception!");
-				qDebug("Exception was:");
-				PyErr_Print();
-			}
-			else
-			{
-				QString errorMsg = PyString_AsString(errorMsgPyStr);
-				// Display a dialog to the user with the exception
-				QClipboard *cp = QApplication::clipboard();
-				cp->setText(errorMsg);
-				QMessageBox::warning(Carrier,
-									tr("Script error"),
-									tr("If you are running an official script report it at <a href=\"http://bugs.scribus.net\">bugs.scribus.net</a> please.")
-									+ "<pre>" +errorMsg + "</pre>"
-									+ tr("This message is in your clipboard too. Use Ctrl+V to paste it into bug tracker."));
-			}
-		} // end if result == NULL
-		// Because 'result' may be NULL, not a PyObject*, we must call PyXDECREF not Py_DECREF
-		Py_XDECREF(result);
-	} // end if m == NULL
-	if (!inMainInterpreter)
-	{
-		Py_EndInterpreter(state);
-		PyEval_RestoreThread(stateo);
-		Carrier->ScriptRunning = false;
-		qApp->restoreOverrideCursor();
-	}
-}
-
-QString MenuTest::slotRunScript(QString Script)
-{
-	qApp->setOverrideCursor(QCursor(waitCursor), false);
-	char* comm[1];
-	QString cm;
-	InValue = Script;
-	QString CurDir = QDir::currentDirPath();
-	if(PyThreadState_Get() != NULL)
-	{
-		initscribus(Carrier);
-		if (RetVal == 0)
-		{
-			// FIXME: if CurDir contains chars outside 7bit ascii, might be problems
-			cm =  "import sys\n";
-			cm += "sys.path[0] = \""+CurDir+"\"\n";
-			cm += "import cStringIO\n";
-			cm += "import scribus\n";
-			// Only import all names from 'scribus' to the global namespace if the user wants us to.
-			// We still need to pull in a few special names used by Scribus though.
-			if (importAllNames)
-				cm += "from scribus import *\n";
-			if (useDummyStdin)
-				cm += "sys.stdin = cStringIO.StringIO()\n";
-			cm += "scribus._bu = cStringIO.StringIO()\n";
-			cm += "sys.stdout = scribus._bu\n";
-			cm += "sys.stderr = scribus._bu\n";
-			cm += "import code\n";
-			cm += "scribus._ia = code.InteractiveConsole(globals())\n";
-		}
-		cm += "scribus._sc = scribus._getval()\n";
-		cm += "scribus._rv = scribus._ia.push(scribus._sc)\n";
-		cm += "if scribus._rv == 1:\n";
-		cm += "    scribus._re = \"...\"\n";
-		cm += "else:\n";
-		cm += "    scribus._re = scribus._bu.getvalue()\n";
-		cm += "scribus._retval(scribus._re, scribus._rv)\n";
-	}
-	// FIXME: if cmd contains chars outside 7bit ascii, might be problems
-	QCString cmd = cm.latin1();
-	comm[0] = (char*)"scribus";
-	PySys_SetArgv(1, comm);
-	PyRun_SimpleString(cmd.data());
-	if (RetVal == 0)
-	{
-		RetString += ">>>";
-		pcon->OutWin->Prompt = ">>>";
-	}
-	else
-		pcon->OutWin->Prompt = "...";
-	qApp->restoreOverrideCursor();
-	return RetString;
-}
-
-void MenuTest::loadScript()
-{
-	if (!this->enableExtPython)
-	{
-		QMessageBox::information(Carrier, tr("Scribus - Script Plugin"),
-			tr("The 'Load Script' function of the script plugin is currently disabled.\n"
-			   "If you just want to run a normal script, you probably want to use\n"
-			   "'Execute Script...' instead.\n\n"
-			   "If you do actually want to load a Python extension script or macro, you\n"
-			   "need to go into the Scripter Settings in the Script menu and enable\n"
-			   "scripter extensions there.\n\n"
-			   "Please read the documentation on extension scripts first.\n"));
-		return;
-	}
-	QString fileName;
-	QString scriptDir = Carrier->Prefs.ScriptDir;
-	if (scriptDir == "")
-		scriptDir = QDir::currentDirPath();
-	CustomFDialog diaf((QWidget*)parent(), scriptDir, QObject::tr("Open"), QObject::tr("Python Scripts (*.py);; All Files (*)"));
-	if (diaf.exec())
-	{
-		fileName = diaf.selectedFile();
-		// Run the script in the main interpreter, not a sub-interpreter
-		slotRunScriptFile(fileName, true);
-	}
-}
-
-void MenuTest::slotInteractiveScript()
-{
-	if (pcon->isVisible())
-	{
-		men->changeItem(cons, tr("Show &Console"));
-		pcon->hide();
-	}
-	else
-	{
-		men->changeItem(cons, tr("Hide &Console"));
-		pcon->show();
-	}
-}
-
-void MenuTest::slotExecute()
-{
-	pcon->OutWin->append(slotRunScript(pcon->OutWin->LastComm));
-	pcon->OutWin->moveCursor(QTextEdit::MoveEnd, false);
-	pcon->OutWin->scrollToBottom();
-	pcon->OutWin->ensureCursorVisible();
-	FinishScriptRun();
-}
-
-void MenuTest::ReadPlugPrefs()
-{
-	QDomDocument docu("scriptrc");
-	QString ho = QDir::homeDirPath();
-	QFile f(QDir::convertSeparators(ho+"/.scribus/scripter.rc"));
-	if(!f.open(IO_ReadOnly))
-		return;
-	if(!docu.setContent(&f))
-	{
-		f.close();
-		return;
-	}
-	f.close();
-	QDomElement elem=docu.documentElement();
-	if (elem.tagName() != "SCRIPTRC")
-		return;
-	QDomNode DOC=elem.firstChild();
-	while(!DOC.isNull())
-	{
-		QDomElement dc=DOC.toElement();
-		if (dc.tagName()=="RECENT")
-			SavedRecentScripts.append(dc.attribute("NAME"));
-		// Check to see if the 'load script' menu item should be enabled
-		else if (dc.tagName() == "EXTPYTHON")
-			enableExtPython = dc.attribute("ENABLE") == "TRUE";
-		// and check whether we should do a 'from scribus import *' in the main interpreter;
-		else if (dc.tagName() == "IMPORTNAMES")
-			importAllNames = dc.attribute("ENABLE") == "TRUE";
-		// Should we import the old-style names?
-		else if (dc.tagName() == "LEGACYALIASES")
-			legacyAliases = dc.attribute("ENABLE") == "TRUE";
-		// Should we run a startup script? If so, where to load it from?
-		else if (dc.tagName() == "STARTUPSCRIPT")
-		{
-			startupScriptEnable = dc.attribute("ENABLE") == "TRUE";
-			startupScript = dc.attribute("FILE");
-		}
-		// replace stdin with a dummy object that always returns "" ?
-		else if (dc.tagName() == "USEDUMMYSTDIN")
-			useDummyStdin = dc.attribute("ENABLE") == "TRUE";
-		DOC=DOC.nextSibling();
-	}
-}
-
-void MenuTest::SavePlugPrefs()
-{
-	QDomDocument docu("scriptrc");
-	QString st="<SCRIPTRC></SCRIPTRC>";
-	docu.setContent(st);
-	QDomElement elem=docu.documentElement();
-	for (uint rd=0; rd < Tes->RecentScripts.count(); ++rd)
-	{
-		QDomElement rde=docu.createElement("RECENT");
-		rde.setAttribute("NAME",Tes->RecentScripts[rd]);
-		elem.appendChild(rde);
-	}
-
-	// save the "load script" flag, from import flag, and aliases flag
-	QDomElement extPythonItem = docu.createElement("EXTPYTHON");
-	extPythonItem.setAttribute("ENABLE", enableExtPython ? "TRUE" : "FALSE");
-	elem.appendChild(extPythonItem);
-
-	QDomElement allNamesItem = docu.createElement("IMPORTNAMES");
-	allNamesItem.setAttribute("ENABLE", importAllNames ? "TRUE" : "FALSE");
-	elem.appendChild(allNamesItem);
-
-	QDomElement legacyAliasesItem = docu.createElement("LEGACYALIASES");
-	legacyAliasesItem.setAttribute("ENABLE", legacyAliases ? "TRUE" : "FALSE");
-	elem.appendChild(legacyAliasesItem);
-
-	QDomElement dummyStdinItem = docu.createElement("USEDUMMYSTDIN");
-	dummyStdinItem.setAttribute("ENABLE", useDummyStdin ? "TRUE" : "FALSE");
-	elem.appendChild(dummyStdinItem);
-
-	// save the startup script path and flag
-	QDomElement startupScriptItem = docu.createElement("STARTUPSCRIPT");
-	startupScriptItem.setAttribute("ENABLE", startupScriptEnable ? "TRUE" : "FALSE");
-	startupScriptItem.setAttribute("FILE", startupScript);
-	elem.appendChild(startupScriptItem);
-
-	// then write out the prefs file
-	QString ho = QDir::homeDirPath();
-	QFile f(QDir::convertSeparators(ho+"/.scribus/scripter.rc"));
-	if(!f.open(IO_WriteOnly))
-		return;
-	QTextStream s(&f);
-	s<<docu.toCString();
-	f.close();
-}
-
-/* 11/1/2004 pv - Show docstring of the script to the user.
- * I don't know how to get docstring via e.g. pydoc because of
- * it needs to run script => error cannot find scribus module
- */
-void MenuTest::aboutScript()
-{
-	QString fname = Carrier->CFileDialog(".", "about", "Scripts (*.py)", "", 0, 0, 0, 0);
-	QFileInfo fi = QFileInfo(fname);
-	QString html = QDir::convertSeparators(QDir::homeDirPath()+"/.scribus/aboutScript.html");
-	QFile input(fname);
-	if(!input.open(IO_ReadOnly))
-		return;
-	QFile output(html);
-	if(!output.open(IO_WriteOnly))
-		return;
-	QTextStream intputstream(&input);
-	QTextStream outputstream(&output);
-	QString content = intputstream.read();
-	QString docstring = content.section("\"\"\"", 1, 1);
-	if (docstring != "")
-	{
-		outputstream << "<h1>Documentation for: " << fi.fileName() << "</h1><p>";
-		outputstream << docstring.replace("\n\n", "<p>");
-	}
-	else
-	{
-		outputstream << "<pre>" << endl;
-		outputstream << "<p><b>Script "<< fi.fileName() << " doesn't contain any docstring!</b></p>" << content;
-		outputstream << "</pre>" << endl;
-	}
-	output.close();
-	input.close();
-	HelpBrowser *dia = new HelpBrowser(0, QObject::tr("About Script") + " " + fi.fileName(), "en", "", html);
-	dia->show();
-}
-
-/* 2005-01-02 CR
- * Display a preferences dialog to let the user configure
- * scripter settings such as enabling Python extension scripts
- * and setting a start-up script. The prefs dialog is defined in
- * scripterprefs.ui .
- */
-void MenuTest::preferencesDialog()
-{
-	ScripterPreferences* prefDia = new ScripterPreferences(Carrier, "scripterPreferences");
-	prefDia->extPythonChk->setChecked(this->enableExtPython);
-	prefDia->startupScriptGroup->setChecked(this->startupScriptEnable);
-	prefDia->startupScriptPath->setText(this->startupScript);
-	prefDia->importNamesChk->setChecked(this->importAllNames);
-	prefDia->legacyAliasesChk->setChecked(this->legacyAliases);
-	prefDia->useFakeStdinChk->setChecked(this->useDummyStdin);
-	if (prefDia->exec())
-	{
-		this->enableExtPython = prefDia->extPythonChk->isChecked();
-		this->startupScriptEnable = prefDia->startupScriptGroup->isChecked();
-		this->startupScript = prefDia->startupScriptPath->text();
-		this->importAllNames = prefDia->importNamesChk->isChecked();
-		this->legacyAliases = prefDia->legacyAliasesChk->isChecked();
-		this->useDummyStdin = prefDia->useFakeStdinChk->isChecked();
-	}
-	delete prefDia;
-}
-
 // This function builds a Python wrapper function called newName around the
 // python function called oldName. The wrapper function prints a warning, then
 // calls oldName with all passed arguments and returns the result from oldName.
@@ -905,6 +396,11 @@
 
 void initscribus(ScribusApp *pl)
 {
+	if (!scripterCore)
+	{
+		qDebug("initscribus called but no scripterCore");
+		return;
+	}
 	PyObject *m, *d;
 	PyImport_AddModule((char*)"scribus");
 	PyType_Ready(&Printer_Type);
@@ -1097,7 +593,7 @@
 	}
 	PyDict_SetItemString(d, const_cast<char*>("warnings"), warningsModule);
 	// Now actually add the aliases
-	if (Tes->legacyAliases)
+	if (scripterCore->legacyAliases)
 	{
 		deprecatedFunctionAlias(d, const_cast<char*>("changeColor"), const_cast<char*>("ChangeColor"));
 		deprecatedFunctionAlias(d, const_cast<char*>("closeDoc"), const_cast<char*>("CloseDoc"));
@@ -1246,7 +742,7 @@
 	// end function aliases
 	// legacy constants - alas, we can't print warnings when these
 	// are used.
-	if (Tes->legacyAliases)
+	if (scripterCore->legacyAliases)
 	{
 		constantAlias(d, "UNIT_POINTS", "Points");
 		constantAlias(d, "UNIT_MILLIMETERS", "Millimeters");
diff -r -u --new-file scriptplugin.orig/scriptplugin.h scriptplugin/scriptplugin.h
--- scriptplugin.orig/scriptplugin.h	2005-02-02 22:12:03.000000000 +0800
+++ scriptplugin/scriptplugin.h	2005-02-07 23:12:36.688836816 +0800
@@ -1,9 +1,10 @@
 #ifndef SCRIPTPLUG_H
 #define SCRIPTPLUG_H
 
-#include "scribus.h"
-#include "pconsole.h"
+#include "cmdvar.h"
 
+class ScribusApp;
+class QWidget;
 
 /** Calls the Plugin with the main Application window as parent
   * and the main Application Class as parameter */
@@ -27,62 +28,8 @@
 /** Possible CleanUpOperations when closing the Plugin */
 extern "C" void CleanUpPlug();
 
-extern "C" void initscribus(ScribusApp *pl);
-
 /** Some useful Subroutines */
 static PyObject *scribus_retval(PyObject *self, PyObject* args);
 static PyObject *scribus_getval(PyObject *self);
-QString RetString;
-QString InValue;
-int RetVal;
-
-static QPopupMenu* men;
-
-class MenuTest : public QObject
-{
-    Q_OBJECT
-
-public:
-	MenuTest(QWidget* /*parent*/) {};
-    ~MenuTest() {};
-		void ReadPlugPrefs();
-		void SavePlugPrefs();
-		void FinishScriptRun();
-		PConsole *pcon;
-		QPopupMenu* rmen;
-		QPopupMenu* smen;
-		int rmenid;
-		int smenid;
-		int cons;
-		int about;
-		QStringList SavedRecentScripts;
-		QStringList RecentScripts;
-		/// pref: Enable the "load script" menu item?
-		bool enableExtPython;
-		/// pref: Run 'from scribus import *' in the main interpreter at startup?
-		bool importAllNames;
-		/// pref: Make aliases of renamed functions for compatibility?
-		bool legacyAliases;
-		/// pref: Replace stdin with dummy cStringIO?
-		bool useDummyStdin;
-		/// pref: Run a script in the main interpreter at startup?
-		bool startupScriptEnable;
-		/// pref: Which script to run at startup, if enabled
-		QString startupScript;
-
-public slots:
-		void slotTest();
-		void loadScript();
-		void StdScript(int id);
-		void RecentScript(int id);
-		void slotRunScriptFile(QString fileName, bool inMainInterpreter = false);
-		QString slotRunScript(QString Script);
-		void slotInteractiveScript();
-		void slotExecute();
-		void aboutScript();
-		void preferencesDialog();
-};
-
-static MenuTest* Tes;
 
-#endif // CMSPLUG_H
+#endif
scripter_menutest_refactor.diff (56,300 bytes)   

ringerc

2005-02-07 14:28

reporter   ~0003642

Last edited: 2005-02-07 14:28

This applies with 'patch -p0' in scribus/plugins, and the diff includes the "new" files (scriptercore.cpp and scriptercore.h), which still need to be 'cvs add'ed.

Issue History

Date Modified Username Field Change
2005-02-07 14:27 ringerc New Issue
2005-02-07 14:27 ringerc File Added: scripter_menutest_refactor.diff
2005-02-07 14:28 ringerc Note Added: 0003642
2005-02-07 14:28 ringerc Note Edited: 0003642
2005-02-08 12:51 cbradney Status new => assigned
2005-02-08 12:51 cbradney Assigned To => cbradney
2005-02-08 12:55 cbradney Status assigned => resolved
2005-02-08 12:55 cbradney Fixed in Version => 1.2.2cvs
2005-02-08 12:55 cbradney Resolution open => fixed
2005-02-08 12:55 cbradney Status resolved => closed
2006-05-13 21:48 christoph_s Relationship added child of 0003813