~ubuntu-branches/ubuntu/vivid/kate/vivid-updates

« back to all changes in this revision

Viewing changes to part/script/katecommandlinescript.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2014-12-04 16:49:41 UTC
  • mfrom: (1.6.6)
  • Revision ID: package-import@ubuntu.com-20141204164941-l3qbvsly83hhlw2v
Tags: 4:14.11.97-0ubuntu1
* New upstream release
* Update build-deps and use pkg-kde v3 for Qt 5 build
* kate-data now kate5-data for co-installability

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  This file is part of the KDE libraries and the Kate part.
2
 
 *
3
 
 *  Copyright (C) 2009 Dominik Haumann <dhaumann kde org>
4
 
 *
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.
9
 
 *
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.
14
 
 *
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.
19
 
 */
20
 
 
21
 
#include "katecommandlinescript.h"
22
 
 
23
 
#include <QScriptValue>
24
 
#include <QScriptEngine>
25
 
 
26
 
#include <klocale.h>
27
 
 
28
 
#include "katedocument.h"
29
 
#include "kateview.h"
30
 
#include "katecmd.h"
31
 
#include "kshell.h"
32
 
 
33
 
KateCommandLineScript::KateCommandLineScript(const QString &url, const KateCommandLineScriptHeader &header)
34
 
  : KateScript(url)
35
 
  , m_commandHeader(header)
36
 
{
37
 
  KateCmd::self()->registerCommand (this);
38
 
}
39
 
 
40
 
KateCommandLineScript::~KateCommandLineScript()
41
 
{
42
 
  KateCmd::self()->unregisterCommand (this);
43
 
}
44
 
 
45
 
const KateCommandLineScriptHeader& KateCommandLineScript::commandHeader()
46
 
{
47
 
  return m_commandHeader;
48
 
}
49
 
 
50
 
 
51
 
bool KateCommandLineScript::callFunction(const QString& cmd, const QStringList args, QString &errorMessage)
52
 
{
53
 
  clearExceptions();
54
 
  QScriptValue command = function(cmd);
55
 
  if(!command.isValid()) {
56
 
    errorMessage = i18n("Function '%1' not found in script: %2", cmd, url());
57
 
    return false;
58
 
  }
59
 
 
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);
64
 
  }
65
 
 
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));
70
 
    return false;
71
 
  }
72
 
 
73
 
  return true;
74
 
}
75
 
 
76
 
ScriptActionInfo KateCommandLineScript::actionInfo(const QString& cmd)
77
 
{
78
 
  clearExceptions();
79
 
  QScriptValue actionFunc = function("action");
80
 
  if(!actionFunc.isValid()) {
81
 
    kDebug() << i18n("Function 'action' not found in script: %1", url());
82
 
    return ScriptActionInfo();
83
 
  }
84
 
 
85
 
  // add the arguments that we are going to pass to the function
86
 
  QScriptValueList arguments;
87
 
  arguments << cmd;
88
 
 
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();
94
 
  }
95
 
 
96
 
  ScriptActionInfo info;
97
 
  info.setCommand(cmd);
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());
103
 
 
104
 
  return info;
105
 
}
106
 
 
107
 
const QStringList& KateCommandLineScript::cmds()
108
 
{
109
 
  return m_commandHeader.functions();
110
 
}
111
 
 
112
 
bool KateCommandLineScript::exec(KTextEditor::View *view, const QString &cmd, QString &msg)
113
 
{
114
 
  KShell::Errors errorCode;
115
 
  QStringList args(KShell::splitArgs(cmd, KShell::NoOptions, &errorCode));
116
 
 
117
 
  if (errorCode != KShell::NoError) {
118
 
    msg = i18n("Bad quoting in call: %1. Please escape single quotes with a backslash.", cmd);
119
 
    return false;
120
 
  }
121
 
 
122
 
  QString _cmd(args.first());
123
 
  args.removeFirst();
124
 
 
125
 
  if (!view) {
126
 
    msg = i18n("Could not access view");
127
 
    return false;
128
 
  }
129
 
 
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();
136
 
    return success;
137
 
  }
138
 
 
139
 
  return false;
140
 
}
141
 
 
142
 
 
143
 
bool KateCommandLineScript::exec(KTextEditor::View *view, const QString &cmd, QString &msg,
144
 
                                 const KTextEditor::Range &range)
145
 
{
146
 
  view->setSelection(range);
147
 
  return exec( view, cmd, msg );
148
 
}
149
 
 
150
 
bool KateCommandLineScript::supportsRange(const QString &)
151
 
{
152
 
  return true;
153
 
}
154
 
 
155
 
bool KateCommandLineScript::help(KTextEditor::View* view, const QString& cmd, QString &msg)
156
 
{
157
 
  if (!setView(qobject_cast<KateView*>(view))) {
158
 
    // setView fails, if the script cannot be loaded
159
 
    return false;
160
 
  }
161
 
 
162
 
  clearExceptions();
163
 
  QScriptValue helpFunction = function("help");
164
 
  if(!helpFunction.isValid()) {
165
 
    return false;
166
 
  }
167
 
 
168
 
  // add the arguments that we are going to pass to the function
169
 
  QScriptValueList arguments;
170
 
  arguments << QScriptValue(m_engine, cmd);
171
 
 
172
 
  QScriptValue result = helpFunction.call(QScriptValue(), arguments);
173
 
 
174
 
  // error during the calling?
175
 
  if(m_engine->hasUncaughtException()) {
176
 
    msg = backtrace(result, i18n("Error calling 'help %1'", cmd));
177
 
    return false;
178
 
  }
179
 
 
180
 
  if (result.isUndefined() || !result.isString()) {
181
 
    kDebug(13050) << i18n("No help specified for command '%1' in script %2", cmd, url());
182
 
    return false;
183
 
  }
184
 
  msg = result.toString();
185
 
 
186
 
  return !msg.isEmpty();
187
 
}
188
 
 
189
 
// kate: space-indent on; indent-width 2; replace-tabs on;