1
/* This file is part of the KDE libraries and the Kate part.
3
* Copyright (C) 2009 Dominik Haumann <dhaumann kde org>
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Library General Public
7
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Library General Public License for more details.
15
* You should have received a copy of the GNU Library General Public License
16
* along with this library; see the file COPYING.LIB. If not, write to
17
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
* Boston, MA 02110-1301, USA.
21
#include "katecommandlinescript.h"
23
#include <QScriptValue>
24
#include <QScriptEngine>
28
#include "katedocument.h"
33
KateCommandLineScript::KateCommandLineScript(const QString &url, const KateCommandLineScriptHeader &header)
35
, m_commandHeader(header)
37
KateCmd::self()->registerCommand (this);
40
KateCommandLineScript::~KateCommandLineScript()
42
KateCmd::self()->unregisterCommand (this);
45
const KateCommandLineScriptHeader& KateCommandLineScript::commandHeader()
47
return m_commandHeader;
51
bool KateCommandLineScript::callFunction(const QString& cmd, const QStringList args, QString &errorMessage)
54
QScriptValue command = function(cmd);
55
if(!command.isValid()) {
56
errorMessage = i18n("Function '%1' not found in script: %2", cmd, url());
60
// add the arguments that we are going to pass to the function
61
QScriptValueList arguments;
62
foreach (const QString& arg, args) {
63
arguments << QScriptValue(m_engine, arg);
66
QScriptValue result = command.call(QScriptValue(), arguments);
67
// error during the calling?
68
if(m_engine->hasUncaughtException()) {
69
errorMessage = backtrace(result, i18n("Error calling %1", cmd));
76
ScriptActionInfo KateCommandLineScript::actionInfo(const QString& cmd)
79
QScriptValue actionFunc = function("action");
80
if(!actionFunc.isValid()) {
81
kDebug() << i18n("Function 'action' not found in script: %1", url());
82
return ScriptActionInfo();
85
// add the arguments that we are going to pass to the function
86
QScriptValueList arguments;
89
QScriptValue result = actionFunc.call(QScriptValue(), arguments);
90
// error during the calling?
91
if(m_engine->hasUncaughtException()) {
92
displayBacktrace(result, i18n("Error calling action(%1)", cmd));
93
return ScriptActionInfo();
96
ScriptActionInfo info;
98
info.setText(result.property("text").toString());
99
info.setIcon(result.property("icon").toString());
100
info.setCategory(result.property("category").toString());
101
info.setInteractive(result.property("interactive").toBool());
102
info.setShortcut(result.property("shortcut").toString());
107
const QStringList& KateCommandLineScript::cmds()
109
return m_commandHeader.functions();
112
bool KateCommandLineScript::exec(KTextEditor::View *view, const QString &cmd, QString &msg)
114
KShell::Errors errorCode;
115
QStringList args(KShell::splitArgs(cmd, KShell::NoOptions, &errorCode));
117
if (errorCode != KShell::NoError) {
118
msg = i18n("Bad quoting in call: %1. Please escape single quotes with a backslash.", cmd);
122
QString _cmd(args.first());
126
msg = i18n("Could not access view");
130
if (setView(qobject_cast<KateView*>(view))) {
131
// setView fails if the script cannot be loaded
132
// balance edit stack in any case!
133
qobject_cast<KateView*>(view)->doc()->pushEditState();
134
bool success = callFunction(_cmd, args, msg);
135
qobject_cast<KateView*>(view)->doc()->popEditState();
143
bool KateCommandLineScript::exec(KTextEditor::View *view, const QString &cmd, QString &msg,
144
const KTextEditor::Range &range)
146
view->setSelection(range);
147
return exec( view, cmd, msg );
150
bool KateCommandLineScript::supportsRange(const QString &)
155
bool KateCommandLineScript::help(KTextEditor::View* view, const QString& cmd, QString &msg)
157
if (!setView(qobject_cast<KateView*>(view))) {
158
// setView fails, if the script cannot be loaded
163
QScriptValue helpFunction = function("help");
164
if(!helpFunction.isValid()) {
168
// add the arguments that we are going to pass to the function
169
QScriptValueList arguments;
170
arguments << QScriptValue(m_engine, cmd);
172
QScriptValue result = helpFunction.call(QScriptValue(), arguments);
174
// error during the calling?
175
if(m_engine->hasUncaughtException()) {
176
msg = backtrace(result, i18n("Error calling 'help %1'", cmd));
180
if (result.isUndefined() || !result.isString()) {
181
kDebug(13050) << i18n("No help specified for command '%1' in script %2", cmd, url());
184
msg = result.toString();
186
return !msg.isEmpty();
189
// kate: space-indent on; indent-width 2; replace-tabs on;