summaryrefslogtreecommitdiffstats
path: root/kjsembed/tdescript
diff options
context:
space:
mode:
Diffstat (limited to 'kjsembed/tdescript')
-rw-r--r--kjsembed/tdescript/Makefile.am17
-rw-r--r--kjsembed/tdescript/javascript.cpp111
-rw-r--r--kjsembed/tdescript/javascript.desktop7
-rw-r--r--kjsembed/tdescript/javascript.h60
-rw-r--r--kjsembed/tdescript/swaptabs.desktop5
-rw-r--r--kjsembed/tdescript/swaptabs.js75
-rw-r--r--kjsembed/tdescript/swaptabs.ui172
7 files changed, 447 insertions, 0 deletions
diff --git a/kjsembed/tdescript/Makefile.am b/kjsembed/tdescript/Makefile.am
new file mode 100644
index 00000000..adcf52d4
--- /dev/null
+++ b/kjsembed/tdescript/Makefile.am
@@ -0,0 +1,17 @@
+INCLUDES = -I$(srcdir) -I$(top_srcdir) $(all_includes)
+
+kde_module_LTLIBRARIES = libjavascript.la
+
+libjavascript_la_SOURCES = javascript.cpp
+libjavascript_la_LDFLAGS = $(all_libraries) -module $(KDE_PLUGIN) -lDCOP -ltdecore -ltdeui $(LIB_QT) -lkjs -ltdefx -no-undefined -avoid-version
+libjavascript_la_LIBADD = -ltdescript ../libkjsembed.la
+
+
+METASOURCES = AUTO
+
+servicetypedir = $(kde_servicesdir)
+servicetype_DATA = javascript.desktop
+
+katescriptsdir = $(kde_datadir)/kate/scripts
+katescripts_DATA = swaptabs.desktop swaptabs.ui
+katescripts_SCRIPTS = swaptabs.js
diff --git a/kjsembed/tdescript/javascript.cpp b/kjsembed/tdescript/javascript.cpp
new file mode 100644
index 00000000..22de1aef
--- /dev/null
+++ b/kjsembed/tdescript/javascript.cpp
@@ -0,0 +1,111 @@
+/* This file is part of the KDE project
+ Copyright (C) 2003 Ian Reinhart Geiser (geiseri@kde.org)
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public License
+ along with this library; see the file COPYING.LIB. If not, write to
+ the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
+*/
+
+#include "javascript.h"
+#include <kapplication.h>
+#include <dcopclient.h>
+#include <kdebug.h>
+#include <kjsembed/kjsembedpart.h>
+#include <kjsembed/jsconsolewidget.h>
+#include <kjsembed/jsbinding.h>
+
+#include <kgenericfactory.h>
+#include <scriptclientinterface.h>
+//using namespace KScriptInterface;
+typedef KGenericFactory<JavaScript, KScriptClientInterface> JavaScriptFactory;
+K_EXPORT_COMPONENT_FACTORY( libjavascript, JavaScriptFactory( "JavaScript" ) )
+
+JavaScript::JavaScript(KScriptClientInterface *parent, const char *name, const TQStringList &args )
+{
+ ScriptClientInterface = parent;
+ m_jsembed = new KJSEmbed::KJSEmbedPart(this, "kjsembed_part");
+ m_scriptLoc = ""; // arg1?
+ m_method = ""; // arg2?
+ m_jsembed->addObject( this, "KScriptInterface");
+ m_jsembed->addObject( m_jsembed->view(), "console" );
+}
+
+JavaScript::~JavaScript()
+{
+}
+
+TQString JavaScript::script() const
+{
+ return m_scriptLoc;
+}
+
+void JavaScript::setScript( const TQString &scriptFile )
+{
+ m_scriptLoc = scriptFile;
+}
+
+void JavaScript::setScript( const TQString &scriptFile, const TQString &method )
+{
+ m_scriptLoc = scriptFile;
+ m_method = method;
+}
+
+void JavaScript::run(TQObject *context, const TQVariant &arg)
+{
+ TQVariant retVal;
+ if( context ) m_jsembed->addObject(context);
+
+ m_jsembed->runFile(m_scriptLoc, m_jsembed->globalObject() );
+
+ if( !m_method.isEmpty() )
+ {
+ KJS::List args;
+ args.append( KJSEmbed::convertToValue(m_jsembed->globalExec(), arg) );
+ KJS::Value val = m_jsembed->callMethod(m_method, args);
+ retVal = KJSEmbed::convertToVariant(m_jsembed->globalExec(),val);
+ }
+ ScriptClientInterface->done(KScriptClientInterface::ResultSuccess, retVal);
+}
+
+void JavaScript::writeLine( const TQString &msg )
+{
+ ScriptClientInterface->output(msg);
+}
+
+void JavaScript::writeWarning( const TQString &msg )
+{
+ ScriptClientInterface->warning(msg);
+}
+
+void JavaScript::writeError( const TQString &msg )
+{
+ ScriptClientInterface->error(msg);
+}
+
+void JavaScript::setProgress( int percent )
+{
+ ScriptClientInterface->progress(percent);
+}
+
+TQString JavaScript::appID() const
+{
+ return kapp->dcopClient()->appId();
+}
+
+void JavaScript::kill()
+{
+ // not sure what to do here....
+}
+
+#include "javascript.moc"
diff --git a/kjsembed/tdescript/javascript.desktop b/kjsembed/tdescript/javascript.desktop
new file mode 100644
index 00000000..1d612975
--- /dev/null
+++ b/kjsembed/tdescript/javascript.desktop
@@ -0,0 +1,7 @@
+[Desktop Entry]
+Name=KJSEmbed Script Runner
+Type=Service
+X-TDE-Library=libjavascript
+X-TDE-Script-Runner=JavaScript/kjs
+ServiceTypes=KScriptRunner/KScriptRunner
+Comment=Javascript scripts from inside the application.
diff --git a/kjsembed/tdescript/javascript.h b/kjsembed/tdescript/javascript.h
new file mode 100644
index 00000000..9ca87f24
--- /dev/null
+++ b/kjsembed/tdescript/javascript.h
@@ -0,0 +1,60 @@
+/* This file is part of the KDE project
+ Copyright (C) 2003 Ian Reinhart Geiser (geiseri@kde.org)
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public License
+ along with this library; see the file COPYING.LIB. If not, write to
+ the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
+*/
+#ifndef __javascript_h__
+#define __javascript_h__
+
+#include <scriptinterface.h>
+#include <tqvariant.h>
+#include <tqobject.h>
+
+namespace KJSEmbed {
+ class KJSEmbedPart;
+}
+
+//using namespace KScriptInterface;
+class JavaScript : public KScriptInterface
+{
+ Q_OBJECT
+ TQ_PROPERTY( TQString appID READ appID )
+
+public:
+ JavaScript(KScriptClientInterface *parent, const char *name, const TQStringList &args);
+ virtual ~JavaScript();
+ TQString script() const;
+ void setScript( const TQString &scriptFile );
+ void setScript( const TQString &scriptLibFile, const TQString &method );
+ void run(TQObject *context = 0, const TQVariant &arg = 0);
+ void kill();
+
+ TQString appID() const;
+
+public slots:
+ void writeLine( const TQString &msg );
+ void writeWarning( const TQString &msg );
+ void writeError( const TQString &msg );
+ void setProgress( int percent );
+
+private:
+ KScriptClientInterface *ScriptClientInterface;
+ KJSEmbed::KJSEmbedPart *m_jsembed;
+ TQString m_method;
+ TQString m_scriptLoc;
+};
+
+#endif
diff --git a/kjsembed/tdescript/swaptabs.desktop b/kjsembed/tdescript/swaptabs.desktop
new file mode 100644
index 00000000..ebe80ab9
--- /dev/null
+++ b/kjsembed/tdescript/swaptabs.desktop
@@ -0,0 +1,5 @@
+[Desktop Entry]
+Name=Swap tabs and spaces.
+Comment=Swap tabs and space indentation in text.
+Type=JavaScript/kjs
+X-TDE-ScriptName=swaptabs.js
diff --git a/kjsembed/tdescript/swaptabs.js b/kjsembed/tdescript/swaptabs.js
new file mode 100644
index 00000000..20d19248
--- /dev/null
+++ b/kjsembed/tdescript/swaptabs.js
@@ -0,0 +1,75 @@
+#!/usr/bin/env kjscmd
+var appID = "kate";
+StdDirs.addResourceType("swaptabs", StdDirs.kde_default("data") + "/kate/scripts");
+
+var client = new DCOPClient();
+var config = new Config( this, "swaptabsrc" );
+var documentIndex = client.call(appID, "KateDocumentManager", "activeDocumentNumber()");
+var ui = StdDirs.findResource("swaptabs","swaptabs.ui");
+var dlg = Factory.loadui(ui);
+
+// Load prefs
+dlg.count.value = config.readNumEntry("Spaces", 8 );
+dlg.swap.selectedId = config.readNumEntry("Mode", 0 );
+
+if( dlg.exec() == 1 )
+{
+ var spaces = dlg.count.value;
+
+ var sourceText;
+ var destText;
+ if( dlg.selection.checked )
+ sourceText = client.call(appID, "SelectionInterface#" + documentIndex, "selection()");
+ else
+ sourceText = client.call(appID, "EditInterface#" + documentIndex, "text()");
+
+ if( dlg.swap.selectedId == 0 )
+ destText = replaceSpaces( spaces, sourceText );
+ else
+ destText = replaceTabs( spaces, sourceText );
+
+ if( dlg.selection.checked )
+ {
+ if( client.call(appID, "SelectionInterface#" + documentIndex, "hasSelection()") )
+ {
+ var startLine = client.call(appID, "SelectionInterfaceExt#" + documentIndex, "selStartLine()");
+ var startCol = client.call(appID, "SelectionInterfaceExt#" + documentIndex, "selStartCol()");
+ client.call(appID, "SelectionInterface#" + documentIndex, "removeSelectedText()");
+ client.call(appID, "SelectionInterface#" + documentIndex, "clearSelection()");
+ client.call(appID, "EditInterface#" + documentIndex, "insertText(uint,uint,TQString)", startLine, startCol, destText);
+ }
+ else
+ {
+ alert("You must first select text.");
+ return false;
+ }
+ }
+ else
+ client.call(appID, "EditInterface#" + documentIndex, "setText(TQString)", destText );
+
+ // save prefs
+ config.writeNumEntry("Spaces", dlg.count.value );
+ config.writeNumEntry("Mode", dlg.swap.selectedId );
+}
+
+function replaceSpaces( count, text )
+{
+ var regExp = new RegExp("[ ]{"+count+","+count+"}", "g");
+ regExp.mulitline = true;
+
+ returnText = text.replace( regExp, "\t");
+ return returnText;
+}
+
+function replaceTabs( count, text )
+{
+
+ var regExp = new RegExp("[\t]","g");
+ regExp.mulitline = true;
+
+ var spaces = "";
+ for( var idx = 0; idx < count; ++idx)
+ spaces += " ";
+ returnText = text.replace( regExp, spaces);
+ return returnText;
+}
diff --git a/kjsembed/tdescript/swaptabs.ui b/kjsembed/tdescript/swaptabs.ui
new file mode 100644
index 00000000..f7f5e973
--- /dev/null
+++ b/kjsembed/tdescript/swaptabs.ui
@@ -0,0 +1,172 @@
+<!DOCTYPE UI><UI version="3.3" stdsetdef="1">
+<class>MyDialog</class>
+<widget class="TQDialog">
+ <property name="name">
+ <cstring>MyDialog</cstring>
+ </property>
+ <property name="geometry">
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>219</width>
+ <height>176</height>
+ </rect>
+ </property>
+ <property name="caption">
+ <string>Swap Tabs &amp; Spaces</string>
+ </property>
+ <property name="sizeGripEnabled">
+ <bool>true</bool>
+ </property>
+ <vbox>
+ <property name="name">
+ <cstring>unnamed</cstring>
+ </property>
+ <widget class="TQLayoutWidget">
+ <property name="name">
+ <cstring>layout2</cstring>
+ </property>
+ <hbox>
+ <property name="name">
+ <cstring>unnamed</cstring>
+ </property>
+ <widget class="TQLabel">
+ <property name="name">
+ <cstring>textLabel1</cstring>
+ </property>
+ <property name="text">
+ <string>Number of spaces for a tab:</string>
+ </property>
+ </widget>
+ <widget class="TQSpinBox">
+ <property name="name">
+ <cstring>count</cstring>
+ </property>
+ <property name="value">
+ <number>4</number>
+ </property>
+ </widget>
+ </hbox>
+ </widget>
+ <widget class="TQButtonGroup">
+ <property name="name">
+ <cstring>swap</cstring>
+ </property>
+ <property name="title">
+ <string>Swap</string>
+ </property>
+ <property name="selectedId" stdset="0">
+ <number>0</number>
+ </property>
+ <vbox>
+ <property name="name">
+ <cstring>unnamed</cstring>
+ </property>
+ <widget class="TQRadioButton">
+ <property name="name">
+ <cstring>radioButton1</cstring>
+ </property>
+ <property name="text">
+ <string>Tabs for spaces</string>
+ </property>
+ </widget>
+ <widget class="TQRadioButton">
+ <property name="name">
+ <cstring>radioButton2</cstring>
+ </property>
+ <property name="text">
+ <string>Spaces for tabs</string>
+ </property>
+ </widget>
+ </vbox>
+ </widget>
+ <widget class="TQCheckBox">
+ <property name="name">
+ <cstring>selection</cstring>
+ </property>
+ <property name="text">
+ <string>Substitute only in current selection</string>
+ </property>
+ </widget>
+ <widget class="TQLayoutWidget">
+ <property name="name">
+ <cstring>Layout1</cstring>
+ </property>
+ <hbox>
+ <property name="name">
+ <cstring>unnamed</cstring>
+ </property>
+ <property name="margin">
+ <number>0</number>
+ </property>
+ <property name="spacing">
+ <number>6</number>
+ </property>
+ <spacer>
+ <property name="name">
+ <cstring>Horizontal Spacing2</cstring>
+ </property>
+ <property name="orientation">
+ <enum>Horizontal</enum>
+ </property>
+ <property name="sizeType">
+ <enum>Expanding</enum>
+ </property>
+ <property name="sizeHint">
+ <size>
+ <width>20</width>
+ <height>20</height>
+ </size>
+ </property>
+ </spacer>
+ <widget class="TQPushButton">
+ <property name="name">
+ <cstring>buttonOk</cstring>
+ </property>
+ <property name="text">
+ <string>&amp;OK</string>
+ </property>
+ <property name="accel">
+ <string></string>
+ </property>
+ <property name="autoDefault">
+ <bool>true</bool>
+ </property>
+ <property name="default">
+ <bool>true</bool>
+ </property>
+ </widget>
+ <widget class="TQPushButton">
+ <property name="name">
+ <cstring>buttonCancel</cstring>
+ </property>
+ <property name="text">
+ <string>&amp;Cancel</string>
+ </property>
+ <property name="accel">
+ <string></string>
+ </property>
+ <property name="autoDefault">
+ <bool>true</bool>
+ </property>
+ </widget>
+ </hbox>
+ </widget>
+ </vbox>
+</widget>
+<connections>
+ <connection>
+ <sender>buttonOk</sender>
+ <signal>clicked()</signal>
+ <receiver>MyDialog</receiver>
+ <slot>accept()</slot>
+ </connection>
+ <connection>
+ <sender>buttonCancel</sender>
+ <signal>clicked()</signal>
+ <receiver>MyDialog</receiver>
+ <slot>reject()</slot>
+ </connection>
+</connections>
+<layoutdefaults spacing="6" margin="11"/>
+</UI>