Kate
katescripthelpers.cpp
Go to the documentation of this file.
00001 // This file is part of the KDE libraries 00002 // Copyright (C) 2010 Dominik Haumann <dhaumann kde org> 00003 // 00004 // This library is free software; you can redistribute it and/or 00005 // modify it under the terms of the GNU Library General Public 00006 // License as published by the Free Software Foundation; either 00007 // version 2 of the License, or (at your option) version 3. 00008 // 00009 // This library is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 // Library General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU Library General Public License 00015 // along with this library; see the file COPYING.LIB. If not, write to 00016 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 // Boston, MA 02110-1301, USA. 00018 00019 #include "katescript.h" 00020 #include "katescriptdocument.h" 00021 #include "katescriptview.h" 00022 #include "kateview.h" 00023 #include "katedocument.h" 00024 00025 #include <iostream> 00026 00027 #include <QScriptEngine> 00028 #include <QScriptValue> 00029 #include <QScriptContext> 00030 00031 #include <kdebug.h> 00032 #include <klocale.h> 00033 #include <klocalizedstring.h> 00034 00035 namespace Kate { 00036 namespace Script { 00037 00038 QScriptValue debug(QScriptContext *context, QScriptEngine *engine) 00039 { 00040 QStringList message; 00041 for(int i = 0; i < context->argumentCount(); ++i) { 00042 message << context->argument(i).toString(); 00043 } 00044 // debug in blue to distance from other debug output if necessary 00045 std::cerr << "\033[34m" << qPrintable(message.join(" ")) << "\033[0m\n"; 00046 return engine->nullValue(); 00047 } 00048 00049 //BEGIN code adapted from kdelibs/kross/modules/translation.cpp 00051 KLocalizedString substituteArguments( const KLocalizedString &kls, const QVariantList &arguments, int max = 99 ) 00052 { 00053 KLocalizedString ls = kls; 00054 int cnt = qMin( arguments.count(), max ); // QString supports max 99 00055 for ( int i = 0; i < cnt; ++i ) { 00056 QVariant arg = arguments[i]; 00057 switch ( arg.type() ) { 00058 case QVariant::Int: ls = ls.subs(arg.toInt()); break; 00059 case QVariant::UInt: ls = ls.subs(arg.toUInt()); break; 00060 case QVariant::LongLong: ls = ls.subs(arg.toLongLong()); break; 00061 case QVariant::ULongLong: ls = ls.subs(arg.toULongLong()); break; 00062 case QVariant::Double: ls = ls.subs(arg.toDouble()); break; 00063 default: ls = ls.subs(arg.toString()); break; 00064 } 00065 } 00066 return ls; 00067 } 00068 00070 QScriptValue i18n( QScriptContext *context, QScriptEngine *engine ) 00071 { 00072 Q_UNUSED(engine) 00073 QString text; 00074 QVariantList args; 00075 const int argCount = context->argumentCount(); 00076 00077 if (argCount == 0) { 00078 kWarning(13050) << "wrong usage of i18n:" << context->backtrace().join("\n\t"); 00079 } 00080 00081 if (argCount > 0) { 00082 text = context->argument(0).toString(); 00083 } 00084 00085 for (int i = 1; i < argCount; ++i) { 00086 args << context->argument(i).toVariant(); 00087 } 00088 00089 KLocalizedString ls = ki18n(text.toUtf8()); 00090 return substituteArguments( ls, args ).toString(); 00091 } 00092 00094 QScriptValue i18nc( QScriptContext *context, QScriptEngine *engine ) 00095 { 00096 Q_UNUSED(engine) 00097 QString text; 00098 QString textContext; 00099 QVariantList args; 00100 const int argCount = context->argumentCount(); 00101 00102 if (argCount < 2) { 00103 kWarning(13050) << "wrong usage of i18nc:" << context->backtrace().join("\n\t"); 00104 } 00105 00106 if (argCount > 0) { 00107 textContext = context->argument(0).toString(); 00108 } 00109 00110 if (argCount > 1) { 00111 text = context->argument(1).toString(); 00112 } 00113 00114 for (int i = 2; i < argCount; ++i) { 00115 args << context->argument(i).toVariant(); 00116 } 00117 00118 KLocalizedString ls = ki18nc(textContext.toUtf8(), text.toUtf8()); 00119 return substituteArguments( ls, args ).toString(); 00120 } 00121 00123 QScriptValue i18np( QScriptContext *context, QScriptEngine *engine ) 00124 { 00125 Q_UNUSED(engine) 00126 QString trSingular; 00127 QString trPlural; 00128 int number; 00129 QVariantList args; 00130 const int argCount = context->argumentCount(); 00131 00132 if (argCount < 3) { 00133 kWarning(13050) << "wrong usage of i18np:" << context->backtrace().join("\n\t"); 00134 } 00135 00136 if (argCount > 0) { 00137 trSingular = context->argument(0).toString(); 00138 } 00139 00140 if (argCount > 1) { 00141 trPlural = context->argument(1).toString(); 00142 } 00143 00144 if (argCount > 2) { 00145 number = context->argument(2).toInt32(); 00146 } 00147 00148 for (int i = 3; i < argCount; ++i) { 00149 args << context->argument(i).toVariant(); 00150 } 00151 00152 KLocalizedString ls = ki18np(trSingular.toUtf8(), trPlural.toUtf8()).subs(number); 00153 return substituteArguments( ls, args, 98 ).toString(); 00154 } 00155 00157 QScriptValue i18ncp( QScriptContext *context, QScriptEngine *engine ) 00158 { 00159 Q_UNUSED(engine) 00160 QString trContext; 00161 QString trSingular; 00162 QString trPlural; 00163 int number; 00164 QVariantList args; 00165 const int argCount = context->argumentCount(); 00166 00167 if (argCount < 4) { 00168 kWarning(13050) << "wrong usage of i18ncp:" << context->backtrace().join("\n\t"); 00169 } 00170 00171 if (argCount > 0) { 00172 trContext = context->argument(0).toString(); 00173 } 00174 00175 if (argCount > 1) { 00176 trSingular = context->argument(1).toString(); 00177 } 00178 00179 if (argCount > 2) { 00180 trPlural = context->argument(2).toString(); 00181 } 00182 00183 if (argCount > 3) { 00184 number = context->argument(3).toInt32(); 00185 } 00186 00187 for (int i = 4; i < argCount; ++i) { 00188 args << context->argument(i).toVariant(); 00189 } 00190 00191 KLocalizedString ls = ki18ncp(trContext.toUtf8(), trSingular.toUtf8(), trPlural.toUtf8()).subs( number ); 00192 return substituteArguments( ls, args, 98 ).toString(); 00193 } 00194 //END code adapted from kdelibs/kross/modules/translation.cpp 00195 00196 } 00197 } 00198 00199 // kate: space-indent on; indent-width 2; replace-tabs on;
KDE 4.6 API Reference