From ce4a32fe52ef09d8f5ff1dd22c001110902b60a2 Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdelibs@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- kate/part/kateluaindentscript.cpp | 528 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 528 insertions(+) create mode 100644 kate/part/kateluaindentscript.cpp (limited to 'kate/part/kateluaindentscript.cpp') diff --git a/kate/part/kateluaindentscript.cpp b/kate/part/kateluaindentscript.cpp new file mode 100644 index 000000000..a8872c0e8 --- /dev/null +++ b/kate/part/kateluaindentscript.cpp @@ -0,0 +1,528 @@ +/* This file is part of the KDE libraries + Copyright (C) 2005 Joseph Wenninger + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2 as published by the Free Software Foundation. + + 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 "config.h" +#ifdef HAVE_LUA + +#include "kateluaindentscript.h" +#include "katedocument.h" +#include "kateview.h" + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +extern "C" { +#include +#include +} + +#define ONCHAR 1 +#define ONNEWLINE 2 +#define ONCHARSTR "kateonchar" +#define ONNEWLINESTR "kateonnewline" + +#define katelua_registerFunc(n,f,t) \ + (lua_pushstring(m_interpreter, n), \ + lua_pushcfunction(m_interpreter, f), \ + lua_settable(m_interpreter, t)) + +#define katelua_registerNumConst(n,v,t) \ + (lua_pushstring(m_interpreter, n), \ + lua_pushnumber(m_interpreter, v), \ + lua_settable(m_interpreter, t)) + +//BEGIN temporary, try to use registry later +static KateDocument *katelua_doc; +static Kate::View *katelua_view; +//END + + + +//BEGIN STATIC BINDING FUNCTIONS +typedef struct KATELUA_FUNCTIONS { + char *name; + lua_CFunction func; +} KATELUA_FUNCTIONS; + +static int katelua_katedebug(lua_State *L) { + int n=lua_gettop(L); + for (int i=1;i<=n;i++) { + if (lua_isnil(L,i)) kdDebug()<<"NIL VALUE"<textLine((uint)lua_tonumber(L,1)).utf8().data()); + return 1; +} + +static int katelua_document_removeText(lua_State *L) { + if (lua_gettop(L)!=4) { + lua_pushstring(L,i18n("document.removeText:Four parameters needed (start line, start col,end line, end col)").utf8().data()); + lua_error(L); + } + if ((!lua_isnumber(L,1)) || (!lua_isnumber(L,2)) ||(!lua_isnumber(L,3)) || (!lua_isnumber(L,4))) { + lua_pushstring(L,i18n("document.removeText:Four parameters needed (start line, start col,end line, end col) (4x number)").utf8().data()); + lua_error(L); + } + lua_pushboolean(L,katelua_doc->removeText((uint)lua_tonumber(L,1),(uint)lua_tonumber(L,2),(uint)lua_tonumber(L,3),(uint)lua_tonumber(L,4))); + return 1; +} + +static int katelua_document_insertText(lua_State *L) { + if (lua_gettop(L)!=3) { + lua_pushstring(L,i18n("document.insertText:Three parameters needed (line,col,text)").utf8().data()); + lua_error(L); + } + if ((!lua_isnumber(L,1)) || (!lua_isnumber(L,2)) ||(!lua_isstring(L,3)) ) { + lua_pushstring(L,i18n("document.removeText:Three parameters needed (line,col,text) (number,number,string)").utf8().data()); + lua_error(L); + } + lua_pushboolean(L,katelua_doc->insertText((uint)lua_tonumber(L,1),(uint)lua_tonumber(L,2),QString::fromUtf8(lua_tostring(L,3)))); + return 1; +} + +static int katelua_view_cursorline(lua_State *L) { + lua_pushnumber(L,katelua_view->cursorLine()); + return 1; +} +static int katelua_view_cursorcolumn(lua_State *L) { + lua_pushnumber(L,katelua_view->cursorColumn()); + return 1; +} +static int katelua_view_cursorposition(lua_State *L) { + lua_pushnumber(L,katelua_view->cursorLine()); + lua_pushnumber(L,katelua_view->cursorColumn()); + return 2; + +} +static int katelua_view_setcursorpositionreal(lua_State *L) { + return 0; +} + +static const struct KATELUA_FUNCTIONS katelua_documenttable[4]= { +{"textLine",katelua_document_textline}, +{"removeText",katelua_document_removeText}, +{"insertText",katelua_document_insertText}, +{0,0} +}; + +static const struct KATELUA_FUNCTIONS katelua_viewtable[5]= { +{"cursorLine",katelua_view_cursorline}, +{"cursorColumn",katelua_view_cursorcolumn}, +{"cursorPosition",katelua_view_cursorposition}, +{"setCursorPositionReal",katelua_view_setcursorpositionreal}, +{0,0} +}; + +static void kateregistertable(lua_State* m_interpreter,const KATELUA_FUNCTIONS funcs[],char * tablename) { + lua_newtable(m_interpreter); + int table=lua_gettop(m_interpreter); + for (uint i=0;funcs[i].name!=0;i++) + { + katelua_registerFunc(funcs[i].name,funcs[i].func,table); + } + + lua_pushstring(m_interpreter,tablename); + lua_pushvalue(m_interpreter,table); + lua_settable(m_interpreter,LUA_GLOBALSINDEX); + lua_pop(m_interpreter,1); + +} + +//END STATIC BINDING FUNCTIONS + + +//BEGIN KateLUAIndentScriptImpl +KateLUAIndentScriptImpl::KateLUAIndentScriptImpl(const QString& internalName, + const QString &filePath, const QString &niceName, + const QString ©right, double version): + KateIndentScriptImplAbstract(internalName,filePath,niceName,copyright,version),m_interpreter(0)/*,m_indenter(0)*/ +{ +} + + +KateLUAIndentScriptImpl::~KateLUAIndentScriptImpl() +{ + deleteInterpreter(); +} + +void KateLUAIndentScriptImpl::decRef() +{ + KateIndentScriptImplAbstract::decRef(); + if (refCount()==0) + { + deleteInterpreter(); + } +} + +void KateLUAIndentScriptImpl::deleteInterpreter() +{ + if (m_interpreter) + { + lua_close(m_interpreter); + m_interpreter=0; + } +} + +bool KateLUAIndentScriptImpl::setupInterpreter(QString &errorMsg) +{ + if (m_interpreter) return true; + m_interpreter=lua_open(); + + if (!m_interpreter) + { + errorMsg=i18n("LUA interpreter could not be initialized"); + return false; + } + luaopen_base(m_interpreter); + luaopen_string( m_interpreter ); + luaopen_table( m_interpreter ); + luaopen_math( m_interpreter ); + luaopen_io( m_interpreter ); + luaopen_debug( m_interpreter ); + + + /*indenter callback setup table*/ + lua_newtable(m_interpreter); + int indentertable=lua_gettop(m_interpreter); + katelua_registerFunc("register",katelua_indenter_register,indentertable); + katelua_registerNumConst("OnChar",ONCHAR,indentertable); + katelua_registerNumConst("OnNewline",ONNEWLINE,indentertable); + lua_pushstring(m_interpreter,"indenter"); + lua_pushvalue(m_interpreter,indentertable); + lua_settable(m_interpreter,LUA_GLOBALSINDEX); + lua_pop(m_interpreter,1); + + /*debug*/ + katelua_registerFunc("katedebug",katelua_katedebug,LUA_GLOBALSINDEX); + + /*document interface*/ + kateregistertable(m_interpreter,katelua_documenttable,"document"); + /*view interface*/ + kateregistertable(m_interpreter,katelua_viewtable,"view"); + + /*open script*/ + lua_pushstring(m_interpreter,"dofile"); + lua_gettable(m_interpreter,LUA_GLOBALSINDEX); + QCString fn=QFile::encodeName(filePath()); + lua_pushstring(m_interpreter,fn.data()); + int execresult=lua_pcall(m_interpreter,1,1,0); + if (execresult==0) { + kdDebug()<<"Lua script has been loaded successfully. Lua interpreter version:"<doc(); + katelua_view=view; + int oldtop=lua_gettop(m_interpreter); + lua_pushstring(m_interpreter,ONCHARSTR); + lua_gettable(m_interpreter,LUA_REGISTRYINDEX); + bool result=true; + if (!lua_isnil(m_interpreter,lua_gettop(m_interpreter))) + { + lua_pushstring(m_interpreter,QString(c).utf8().data()); + if (lua_pcall(m_interpreter,1,0,0)!=0) + { + errorMsg=i18n("Lua indenting script had errors: %1").arg(lua_tostring(m_interpreter,lua_gettop(m_interpreter))); + kdDebug()<doc(); + katelua_view=view; + int oldtop=lua_gettop(m_interpreter); + lua_pushstring(m_interpreter,ONNEWLINESTR); + lua_gettable(m_interpreter,LUA_REGISTRYINDEX); + bool result=true; + if (!lua_isnil(m_interpreter,lua_gettop(m_interpreter))) + { + if (lua_pcall(m_interpreter,0,0,0)!=0) + { + errorMsg=i18n("Lua indenting script had errors: %1").arg(lua_tostring(m_interpreter,lua_gettop(m_interpreter))); + kdDebug()< config.readNumEntry ("CachedVersion")) + { + config.writeEntry ("CachedVersion", config.readNumEntry ("Version")); + force = true; + } +#endif + + // Let's get a list of all the .js files + QStringList list = KGlobal::dirs()->findAllResources("data","katepart/scripts/indent/*.lua",false,true); + + // Let's iterate through the list and build the Mode List + for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it ) + { + // Each file has a group ed: + QString Group="Cache "+ *it; + + // Let's go to this group + config.setGroup(Group); + + // stat the file + struct stat sbuf; + memset (&sbuf, 0, sizeof(sbuf)); + stat(QFile::encodeName(*it), &sbuf); + kdDebug()<<"Lua script file:"<<(*it)<0) tmpblockdata=value; + currentState=COPYRIGHT; + } else kdDebug(13050)<<"ignoring key"<