diff options
Diffstat (limited to 'kjsembed/kscript')
-rw-r--r-- | kjsembed/kscript/Makefile.am | 17 | ||||
-rw-r--r-- | kjsembed/kscript/javascript.cpp | 111 | ||||
-rw-r--r-- | kjsembed/kscript/javascript.desktop | 7 | ||||
-rw-r--r-- | kjsembed/kscript/javascript.h | 60 | ||||
-rw-r--r-- | kjsembed/kscript/swaptabs.desktop | 5 | ||||
-rw-r--r-- | kjsembed/kscript/swaptabs.js | 75 | ||||
-rw-r--r-- | kjsembed/kscript/swaptabs.ui | 172 |
7 files changed, 447 insertions, 0 deletions
diff --git a/kjsembed/kscript/Makefile.am b/kjsembed/kscript/Makefile.am new file mode 100644 index 00000000..2e3a6915 --- /dev/null +++ b/kjsembed/kscript/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) -no-undefined -avoid-version +libjavascript_la_LIBADD = -lkscript ../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/kscript/javascript.cpp b/kjsembed/kscript/javascript.cpp new file mode 100644 index 00000000..31976981 --- /dev/null +++ b/kjsembed/kscript/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 QStringList &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() +{ +} + +QString JavaScript::script() const +{ + return m_scriptLoc; +} + +void JavaScript::setScript( const QString &scriptFile ) +{ + m_scriptLoc = scriptFile; +} + +void JavaScript::setScript( const QString &scriptFile, const QString &method ) +{ + m_scriptLoc = scriptFile; + m_method = method; +} + +void JavaScript::run(QObject *context, const QVariant &arg) +{ + QVariant 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 QString &msg ) +{ + ScriptClientInterface->output(msg); +} + +void JavaScript::writeWarning( const QString &msg ) +{ + ScriptClientInterface->warning(msg); +} + +void JavaScript::writeError( const QString &msg ) +{ + ScriptClientInterface->error(msg); +} + +void JavaScript::setProgress( int percent ) +{ + ScriptClientInterface->progress(percent); +} + +QString JavaScript::appID() const +{ + return kapp->dcopClient()->appId(); +} + +void JavaScript::kill() +{ + // not sure what to do here.... +} + +#include "javascript.moc" diff --git a/kjsembed/kscript/javascript.desktop b/kjsembed/kscript/javascript.desktop new file mode 100644 index 00000000..0055e68a --- /dev/null +++ b/kjsembed/kscript/javascript.desktop @@ -0,0 +1,7 @@ +[Desktop Entry] +Name=KJSEmbed Script Runner +Type=Service +X-KDE-Library=libjavascript +X-KDE-Script-Runner=JavaScript/kjs +ServiceTypes=KScriptRunner/KScriptRunner +Comment=Javascript scripts from inside the application. diff --git a/kjsembed/kscript/javascript.h b/kjsembed/kscript/javascript.h new file mode 100644 index 00000000..7b5143a7 --- /dev/null +++ b/kjsembed/kscript/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 <qvariant.h> +#include <qobject.h> + +namespace KJSEmbed { + class KJSEmbedPart; +} + +//using namespace KScriptInterface; +class JavaScript : public KScriptInterface +{ + Q_OBJECT + Q_PROPERTY( QString appID READ appID ) + +public: + JavaScript(KScriptClientInterface *parent, const char *name, const QStringList &args); + virtual ~JavaScript(); + QString script() const; + void setScript( const QString &scriptFile ); + void setScript( const QString &scriptLibFile, const QString &method ); + void run(QObject *context = 0, const QVariant &arg = 0); + void kill(); + + QString appID() const; + +public slots: + void writeLine( const QString &msg ); + void writeWarning( const QString &msg ); + void writeError( const QString &msg ); + void setProgress( int percent ); + +private: + KScriptClientInterface *ScriptClientInterface; + KJSEmbed::KJSEmbedPart *m_jsembed; + QString m_method; + QString m_scriptLoc; +}; + +#endif diff --git a/kjsembed/kscript/swaptabs.desktop b/kjsembed/kscript/swaptabs.desktop new file mode 100644 index 00000000..7de4dcfa --- /dev/null +++ b/kjsembed/kscript/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-KDE-ScriptName=swaptabs.js diff --git a/kjsembed/kscript/swaptabs.js b/kjsembed/kscript/swaptabs.js new file mode 100644 index 00000000..6adbc2c3 --- /dev/null +++ b/kjsembed/kscript/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,QString)", startLine, startCol, destText); + } + else + { + alert("You must first select text."); + return false; + } + } + else + client.call(appID, "EditInterface#" + documentIndex, "setText(QString)", 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/kscript/swaptabs.ui b/kjsembed/kscript/swaptabs.ui new file mode 100644 index 00000000..8cb243f8 --- /dev/null +++ b/kjsembed/kscript/swaptabs.ui @@ -0,0 +1,172 @@ +<!DOCTYPE UI><UI version="3.3" stdsetdef="1"> +<class>MyDialog</class> +<widget class="QDialog"> + <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 & Spaces</string> + </property> + <property name="sizeGripEnabled"> + <bool>true</bool> + </property> + <vbox> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <widget class="QLayoutWidget"> + <property name="name"> + <cstring>layout2</cstring> + </property> + <hbox> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <widget class="QLabel"> + <property name="name"> + <cstring>textLabel1</cstring> + </property> + <property name="text"> + <string>Number of spaces for a tab:</string> + </property> + </widget> + <widget class="QSpinBox"> + <property name="name"> + <cstring>count</cstring> + </property> + <property name="value"> + <number>4</number> + </property> + </widget> + </hbox> + </widget> + <widget class="QButtonGroup"> + <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="QRadioButton"> + <property name="name"> + <cstring>radioButton1</cstring> + </property> + <property name="text"> + <string>Tabs for spaces</string> + </property> + </widget> + <widget class="QRadioButton"> + <property name="name"> + <cstring>radioButton2</cstring> + </property> + <property name="text"> + <string>Spaces for tabs</string> + </property> + </widget> + </vbox> + </widget> + <widget class="QCheckBox"> + <property name="name"> + <cstring>selection</cstring> + </property> + <property name="text"> + <string>Substitute only in current selection</string> + </property> + </widget> + <widget class="QLayoutWidget"> + <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="QPushButton"> + <property name="name"> + <cstring>buttonOk</cstring> + </property> + <property name="text"> + <string>&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="QPushButton"> + <property name="name"> + <cstring>buttonCancel</cstring> + </property> + <property name="text"> + <string>&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> |